From d6658de6d19f644b4a69b0663c2e9eb68b1a481b Mon Sep 17 00:00:00 2001 From: 0ssigeno Date: Fri, 26 Aug 2022 17:43:05 +0200 Subject: [PATCH] Changed __text to __search + removed __search if we are not using atlas --- atlasq/queryset/node.py | 9 +++++++++ atlasq/queryset/transform.py | 4 ++-- tests/queryset/test_node.py | 2 +- tests/queryset/test_queryset.py | 2 +- tests/queryset/test_transform.py | 34 ++++++++++++++++++++++++-------- tests/queryset/test_visitor.py | 16 +++++++-------- version.py | 2 +- 7 files changed, 48 insertions(+), 21 deletions(-) diff --git a/atlasq/queryset/node.py b/atlasq/queryset/node.py index 1b8415e..a46c7cc 100644 --- a/atlasq/queryset/node.py +++ b/atlasq/queryset/node.py @@ -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}") diff --git a/atlasq/queryset/transform.py b/atlasq/queryset/transform.py index 10e8342..72d0849 100644 --- a/atlasq/queryset/transform.py +++ b/atlasq/queryset/transform.py @@ -36,7 +36,7 @@ class AtlasTransform: "iregex", "match", "is", - "text", + "search", ] negative_keywords = ["ne", "nin", "not"] exists_keywords = ["exists"] @@ -47,7 +47,7 @@ class AtlasTransform: "icontains", "iwholeword", "wholeword", - "text", + "search", ] size_keywords = ["size"] not_converted = [ diff --git a/tests/queryset/test_node.py b/tests/queryset/test_node.py index f2b94a1..be93681 100644 --- a/tests/queryset/test_node.py +++ b/tests/queryset/test_node.py @@ -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)) diff --git a/tests/queryset/test_queryset.py b/tests/queryset/test_queryset.py index 505d060..e486bde 100644 --- a/tests/queryset/test_queryset.py +++ b/tests/queryset/test_queryset.py @@ -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, [ diff --git a/tests/queryset/test_transform.py b/tests/queryset/test_transform.py index da68c46..2c0842e 100644 --- a/tests/queryset/test_transform.py +++ b/tests/queryset/test_transform.py @@ -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") @@ -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): @@ -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") ) @@ -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") ) @@ -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") ) @@ -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") ) @@ -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") ) @@ -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") ) diff --git a/tests/queryset/test_visitor.py b/tests/queryset/test_visitor.py index d8aab1f..9c60557 100644 --- a/tests/queryset/test_visitor.py +++ b/tests/queryset/test_visitor.py @@ -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 @@ -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")) @@ -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")) diff --git a/version.py b/version.py index 6c5007c..91ee717 100644 --- a/version.py +++ b/version.py @@ -1 +1 @@ -VERSION = "0.2.0" +VERSION = "0.2.1"