Skip to content

Commit

Permalink
Merge pull request #21 from certego/develop
Browse files Browse the repository at this point in the history
0.2.1
  • Loading branch information
0ssigeno authored Aug 26, 2022
2 parents a148f95 + d6658de commit ca3215b
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 21 deletions.
9 changes: 9 additions & 0 deletions atlasq/queryset/node.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,15 @@ def to_query(self, document) -> List[Dict]: # pylint: disable=arguments-differ
if qs is None:
raise ValueError("Document must set `atlas` to an AtlasManager")
if not isinstance(qs, AtlasQuerySet):
new_query = {}
# search is possible to use only on atlas
for key, value in self.query:
key = key.rsplit("__")
if key[-1] == "search":
key = key[:-1]
key = key.join("__")
new_query[key] = value
self.query = new_query
return super().to_query(document)
atlas_index = qs.index
logger.debug(f"to_query {self.__class__.__name__} {document}")
Expand Down
4 changes: 2 additions & 2 deletions atlasq/queryset/transform.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class AtlasTransform:
"iregex",
"match",
"is",
"text",
"search",
]
negative_keywords = ["ne", "nin", "not"]
exists_keywords = ["exists"]
Expand All @@ -47,7 +47,7 @@ class AtlasTransform:
"icontains",
"iwholeword",
"wholeword",
"text",
"search",
]
size_keywords = ["size"]
not_converted = [
Expand Down
2 changes: 1 addition & 1 deletion tests/queryset/test_node.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class MyDocument(Document):

atlas = AtlasManager("test")

q = AtlasQ(name__text="test")
q = AtlasQ(name__search="test")
res = q.to_query(MyDocument)
self.assertIsInstance(res, list)
self.assertEqual(1, len(res))
Expand Down
2 changes: 1 addition & 1 deletion tests/queryset/test_queryset.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ def test_exclude(self):
self.assertEqual(qs._get_projections(), [{"$project": {"name": 0}}])

def test_filter(self):
qs = self.base.filter(name__text="test.com")
qs = self.base.filter(name__search="test.com")
self.assertEqual(
qs._aggrs,
[
Expand Down
34 changes: 26 additions & 8 deletions tests/queryset/test_transform.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ def test__equals(self):
self.assertEqual(res["equals"]["path"], "field")
self.assertEqual(res["equals"]["value"], "aaa")

def test__text(self):
def test__search(self):
q = AtlasQ(f=3)
t = AtlasTransform(q.query)
res = t._text("field", "aaa")
Expand All @@ -130,7 +130,7 @@ def test__text(self):
self.assertEqual(res["text"]["path"], "field")
self.assertEqual(res["text"]["query"], "aaa")

def test__text_none(self):
def test__search_none(self):
q = AtlasQ(f=3)
t = AtlasTransform(q.query)
with self.assertRaises(AtlasFieldError):
Expand Down Expand Up @@ -378,8 +378,26 @@ def test_atlas_q_gte(self):
json.dumps(positive, indent=4, default=str),
)

def test_atlas_q_not_whole_word(self):
q1 = AtlasQ(key__not__wholeword="value")
positive, negative, aggregations = AtlasTransform(q1.query).transform(
AtlasIndex("test")
)
self.assertEqual([], aggregations)
self.assertEqual(
[
{"text": {"path": "key", "query": "value"}},
],
negative,
)
self.assertEqual(
[],
positive,
json.dumps(positive, indent=4),
)

def test_atlas_q_field_start_with_keyword(self):
q1 = AtlasQ(key__internal__in__text=["value"])
q1 = AtlasQ(key__internal__in__search=["value"])
positive, negative, aggregations = AtlasTransform(q1.query).transform(
AtlasIndex("test")
)
Expand All @@ -394,7 +412,7 @@ def test_atlas_q_field_start_with_keyword(self):
)

def test_atlas_q_ne_embedded_document(self):
q1 = AtlasQ(key__internal__key__ne__text="value")
q1 = AtlasQ(key__internal__key__ne__search="value")
positive, negative, aggregations = AtlasTransform(q1.query).transform(
AtlasIndex("test")
)
Expand All @@ -409,7 +427,7 @@ def test_atlas_q_ne_embedded_document(self):
)

def test_atlas_q_embedded_document(self):
q1 = AtlasQ(key__internal__key__text="value")
q1 = AtlasQ(key__internal__key__search="value")
positive, negative, aggregations = AtlasTransform(q1.query).transform(
AtlasIndex("test")
)
Expand All @@ -424,7 +442,7 @@ def test_atlas_q_embedded_document(self):
)

def test_atlas_q_nin(self):
q1 = AtlasQ(key__nin__text=["value", "value2"])
q1 = AtlasQ(key__nin__search=["value", "value2"])
positive, negative, aggregations = AtlasTransform(q1.query).transform(
AtlasIndex("test")
)
Expand All @@ -439,7 +457,7 @@ def test_atlas_q_nin(self):
)

def test_atlas_q_negative(self):
q1 = AtlasQ(key__ne__text="value")
q1 = AtlasQ(key__ne__search="value")
positive, negative, aggregations = AtlasTransform(q1.query).transform(
AtlasIndex("test")
)
Expand All @@ -455,7 +473,7 @@ def test_atlas_q_negative(self):
)

def test_atlas_q(self):
q1 = AtlasQ(key__text="value", key2__text="value2")
q1 = AtlasQ(key__search="value", key2__search="value2")
positive, negative, aggregations = AtlasTransform(q1.query).transform(
AtlasIndex("test")
)
Expand Down
16 changes: 8 additions & 8 deletions tests/queryset/test_visitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,16 @@ class MyDocument(Document):

class TestAtlasQueryCompilerVisitor(TestBaseCase):
def test_visit_combination_and_or(self):
q1 = AtlasQ(key__text="value")
q1 = AtlasQ(key__search="value")

q2 = AtlasQ(key2__text="value2")
q2 = AtlasQ(key2__search="value2")
q3 = q1 | q2

q4 = AtlasQ(key4__text="value4")
q4 = AtlasQ(key4__search="value4")

q5 = q3 & q4

q6 = AtlasQ(key5__text="value5")
q6 = AtlasQ(key5__search="value5")

q7 = q5 | q6

Expand Down Expand Up @@ -99,8 +99,8 @@ def test_visit_combination_and_or(self):
)

def test_combination_and(self):
q1 = AtlasQ(key__text="value", key2="value2")
q2 = AtlasQ(key3="value3", key4__text="value4")
q1 = AtlasQ(key__search="value", key2="value2")
q2 = AtlasQ(key3="value3", key4__search="value4")
q5 = q1 & q2
filters, *aggregations = q5.accept(
AtlasQueryCompilerVisitor(MyDocument, AtlasIndex("test"))
Expand Down Expand Up @@ -128,8 +128,8 @@ def test_combination_and(self):
)

def test_atlas_q_or(self):
q1 = AtlasQ(key__text="value", key2__text="value2")
q2 = AtlasQ(key3__text="value3", key4__text="value4")
q1 = AtlasQ(key__search="value", key2__search="value2")
q2 = AtlasQ(key3__search="value3", key4__search="value4")
q5 = q1 | q2
filters, *aggregations = q5.accept(
AtlasQueryCompilerVisitor(MyDocument, AtlasIndex("test"))
Expand Down
2 changes: 1 addition & 1 deletion version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
VERSION = "0.2.0"
VERSION = "0.2.1"

0 comments on commit ca3215b

Please sign in to comment.