Skip to content

Commit

Permalink
Added noteq, in and notin to MongoAdpater
Browse files Browse the repository at this point in the history
  • Loading branch information
jmaruland committed Jun 15, 2022
1 parent 0f292d5 commit 844adbe
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 2 deletions.
24 changes: 23 additions & 1 deletion databroker/experimental/server_ext.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
Comparison,
Eq,
FullText,
In,
NotEq,
NotIn,
Operator,
Regex,
)
Expand Down Expand Up @@ -435,13 +438,29 @@ def eq(query, catalog):
return catalog.apply_mongo_query({f"metadata.{query.key}": query.value})


def noteq(query, catalog):
return catalog.apply_mongo_query({f"metadata.{query.key}": {"$ne": query.value}})


def regex(query, catalog):
options = "" if query.case_sensitive else "i"
return catalog.apply_mongo_query(
{f"metadata.{query.key}": {"$regex": query.pattern, "$options": options}}
)


def _in(query, catalog):
if not isinstance(query.value, list):
query.value = [query.value]
return catalog.apply_mongo_query({f"metadata.{query.key}": {"$in": query.value}})


def notin(query, catalog):
if not isinstance(query.value, list):
query.value = [query.value]
return catalog.apply_mongo_query({f"metadata.{query.key}": {"$nin": query.value}})


def full_text_search(query, catalog):
# First if this catalog is backed by mongomock, which does not support $text queries.
# Avoid importing mongomock if it is not already imported.
Expand All @@ -463,5 +482,8 @@ def full_text_search(query, catalog):
MongoAdapter.register_query(Contains, contains)
MongoAdapter.register_query(Comparison, comparison)
MongoAdapter.register_query(Eq, eq)
MongoAdapter.register_query(FullText, full_text_search)
MongoAdapter.register_query(NotEq, noteq)
MongoAdapter.register_query(Regex, regex)
MongoAdapter.register_query(In, _in)
MongoAdapter.register_query(NotIn, notin)
MongoAdapter.register_query(FullText, full_text_search)
27 changes: 26 additions & 1 deletion databroker/tests/test_experimental.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
from tiled.client import from_tree
from tiled.queries import Contains, Comparison, Eq, FullText, Key, Regex
from tiled.queries import (
Contains,
Comparison,
Eq,
FullText,
In,
Key,
NotEq,
NotIn,
Regex,
)

from ..experimental.server_ext import MongoAdapter

Expand Down Expand Up @@ -98,3 +108,18 @@ def test_queries(tmpdir):
numpy.testing.assert_equal(
test5.values()[0].read(), test5.values()[0].metadata["number"] * numpy.ones(10)
)
test6 = client.search(NotEq("letter", "a"))
# The first result should not be "a"
assert test6.values()[0].metadata["letter"] != "a"

test7 = client.search(In("letter", ["a", "b"]))
numpy.testing.assert_equal(
test7.values()[0].read(), test7.values()[0].metadata["number"] * numpy.ones(10)
)
numpy.testing.assert_equal(
test7.values()[1].read(), test7.values()[1].metadata["number"] * numpy.ones(10)
)

test8 = client.search(NotIn("letter", ["a"]))
# The first result should not be "a"
assert test8.values()[0].metadata["letter"] != "a"

0 comments on commit 844adbe

Please sign in to comment.