From 54058e5aaaef84c9b46ec02e0d720c8f12a31538 Mon Sep 17 00:00:00 2001 From: 0ssigeno Date: Fri, 7 Oct 2022 17:44:28 +0200 Subject: [PATCH 1/3] Added check for empty string --- atlasq/queryset/index.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/atlasq/queryset/index.py b/atlasq/queryset/index.py index 9a7864f..67821af 100644 --- a/atlasq/queryset/index.py +++ b/atlasq/queryset/index.py @@ -73,7 +73,8 @@ def ensure_index_exists( def _set_indexed_fields(self, index_result: Dict, base_field: str = ""): if index_result["type"] == "document": - self._indexed_fields.append(base_field) + if base_field: + self._indexed_fields.append(base_field) if index_result.get("dynamic", False): self._indexed_fields.append(f"{base_field}.*" if base_field else "*") else: From cde985d0306013977f057bd8e251c7522774fe3f Mon Sep 17 00:00:00 2001 From: Simone Berni Date: Mon, 10 Oct 2022 11:17:52 +0200 Subject: [PATCH 2/3] Rewrote _embedded_document() + index mgmt for embedded/documents + removed use_embedded_document on Manager -> Requires index to be ensured (#30) * Rewrote _embedded_document() + index mgmt for embedded/documents * pylint * Removed use_embedded_document variable Fixed tests --- atlasq/queryset/index.py | 54 ++++-- atlasq/queryset/manager.py | 7 +- atlasq/queryset/transform.py | 81 +++++---- atlasq/queryset/visitor.py | 6 +- tests/queryset/test_index.py | 10 +- tests/queryset/test_transform.py | 292 ++++++++++++++++++++++--------- 6 files changed, 301 insertions(+), 149 deletions(-) diff --git a/atlasq/queryset/index.py b/atlasq/queryset/index.py index 67821af..de9bfcb 100644 --- a/atlasq/queryset/index.py +++ b/atlasq/queryset/index.py @@ -1,11 +1,12 @@ import fnmatch +from enum import Enum from logging import getLogger from typing import Dict, List import requests from requests.auth import HTTPDigestAuth -from atlasq.queryset.exceptions import AtlasIndexError +from atlasq.queryset.exceptions import AtlasIndexError, AtlasIndexFieldError logger = getLogger(__name__) @@ -16,14 +17,26 @@ ) +class AtlasIndexType(Enum): + DOCUMENT = "document" + EMBEDDED_DOCUMENT = "embeddedDocuments" + STRING = "string" + INTEGER = "integer" + BOOLEAN = "boolean" + DATE = "date" + + @classmethod + def values(cls) -> List[str]: + return [e.value for e in cls] + + class AtlasIndex: - fields_to_copy = ["ensured", "_indexed_fields", "use_embedded_documents"] + fields_to_copy = ["ensured", "_indexed_fields"] - def __init__(self, index_name: str, use_embedded_documents: bool = True): - self._indexed_fields: List[str] = [] + def __init__(self, index_name: str): + self._indexed_fields: Dict[str, str] = {} self.ensured: bool = False - self.use_embedded_documents: bool = use_embedded_documents self._index: str = index_name def __copy__(self): @@ -72,23 +85,25 @@ def ensure_index_exists( return self.ensured def _set_indexed_fields(self, index_result: Dict, base_field: str = ""): - if index_result["type"] == "document": - if base_field: - self._indexed_fields.append(base_field) - if index_result.get("dynamic", False): - self._indexed_fields.append(f"{base_field}.*" if base_field else "*") - else: + lucene_type = index_result["type"] + if lucene_type in [ + AtlasIndexType.DOCUMENT.value, + AtlasIndexType.EMBEDDED_DOCUMENT.value, + ]: + if not index_result.get("dynamic", False): for field, value in index_result.get("fields", {}).items(): field = f"{base_field}.{field}" if base_field else field self._set_indexed_fields(value, base_field=field) - - else: - assert base_field - self._indexed_fields.append(base_field) + else: + self._indexed_fields[f"{base_field}.*" if base_field else "*"] = "" + if base_field: + if lucene_type not in AtlasIndexType.values(): + logger.warning(f"Lucene type {lucene_type} not configured") + self._indexed_fields[base_field] = lucene_type def _set_indexed_from_mappings(self, index_result: Dict): mappings = index_result["mappings"] - mappings["type"] = "document" + mappings["type"] = AtlasIndexType.DOCUMENT.value self._set_indexed_fields(mappings) logger.debug(self._indexed_fields) @@ -96,3 +111,10 @@ def ensure_keyword_is_indexed(self, keyword: str): if not self.ensured: raise AtlasIndexError("Index not ensured") return any(fnmatch.fnmatch(keyword, field) for field in self._indexed_fields) + + def get_type_from_keyword(self, keyword) -> str: + if not self.ensured: + raise AtlasIndexError("Index not ensured") + if keyword in self._indexed_fields: + return self._indexed_fields[keyword] + raise AtlasIndexFieldError(f"Keyword {keyword} not present in index") diff --git a/atlasq/queryset/manager.py b/atlasq/queryset/manager.py index c790377..b715e9d 100644 --- a/atlasq/queryset/manager.py +++ b/atlasq/queryset/manager.py @@ -22,14 +22,9 @@ def __init__( self, atlas_index: Union[str, None], save_execution_time: bool = False, - use_embedded_documents_indexes: bool = True, ): super().__init__() - self._index = ( - AtlasIndex(atlas_index, use_embedded_documents_indexes) - if atlas_index - else None - ) + self._index = AtlasIndex(atlas_index) if atlas_index else None self._save_execution_time = save_execution_time def __get__(self, instance, owner): diff --git a/atlasq/queryset/transform.py b/atlasq/queryset/transform.py index efdba37..0b27b00 100644 --- a/atlasq/queryset/transform.py +++ b/atlasq/queryset/transform.py @@ -5,7 +5,7 @@ from mongoengine import QuerySet from atlasq.queryset.exceptions import AtlasFieldError, AtlasIndexFieldError -from atlasq.queryset.index import AtlasIndex +from atlasq.queryset.index import AtlasIndex, AtlasIndexType logger = logging.getLogger(__name__) @@ -59,29 +59,40 @@ class AtlasTransform: "match", ] - def __init__(self, atlas_query): + def __init__(self, atlas_query, atlas_index: AtlasIndex): self.atlas_query = atlas_query + self.atlas_index = atlas_index def _regex(self, path: str, value: str): return {"regex": {"query": value, "path": path}} - def _embedded_document(self, path: List[str], operator: Dict): - # recursive - if len(path) > 2: - new_path = path[1:] - new_path[0] = f"{path[0]}.{new_path[0]}" - - return { - "embeddedDocument": { - "path": path[0], - "operator": self._embedded_document(new_path, operator), - } + def _embedded_document(self, path: str, content: Dict): + return { + "embeddedDocument": { + "path": path, + "operator": content, } - # real exit case - if len(path) > 1: - return {"embeddedDocument": {"path": path[0], "operator": operator}} - # we do nothing in case it was not an embedded document - return operator + } + + def _convert_to_embedded_document( + self, path: List[str], operator: Dict, start: str = "" + ): + element = path.pop(0) + partial_path = f"{start}.{element}" if start else element + if not self.atlas_index.ensured: + return operator + if ( + self.atlas_index.get_type_from_keyword(partial_path) + != AtlasIndexType.EMBEDDED_DOCUMENT.value + ): + return operator + + if not path: + return operator + return self._embedded_document( + partial_path, + self._convert_to_embedded_document(path, operator, start=partial_path), + ) def _exists(self, path: str) -> Dict: return {"exists": {"path": path}} @@ -134,15 +145,18 @@ def _size(self, path: str, value: int, operator: str) -> Dict: } } - def _ensure_keyword_is_indexed(self, atlas_index: AtlasIndex, keyword: str) -> None: - if not atlas_index.ensure_keyword_is_indexed(keyword): - raise AtlasIndexFieldError( - f"The keyword {keyword} is not indexed in {atlas_index.index}" - ) + def _ensure_path_is_indexed(self, path: List[str]) -> None: + start = "" + for element in path: + partial_path = f"{start}.{element}" if start else element + + if not self.atlas_index.ensure_keyword_is_indexed(partial_path): + raise AtlasIndexFieldError( + f"The keyword {partial_path} is not indexed in {self.atlas_index.index}" + ) + start = partial_path - def transform( - self, atlas_index: AtlasIndex - ) -> Tuple[List[Dict], List[Dict], List[Dict]]: + def transform(self) -> Tuple[List[Dict], List[Dict], List[Dict]]: other_aggregations = [] affirmative = [] negative = [] @@ -210,15 +224,12 @@ def transform( obj = self._text(path, value) if obj: - if atlas_index.use_embedded_documents: - # we are wrapping the result to an embedded document - obj = self._embedded_document(path.split("."), obj) - - if atlas_index.ensured: - # if we are using the embedded object, in the index is defined only the first level - if atlas_index.use_embedded_documents: - path = path.split(".", maxsplit=1)[0] - self._ensure_keyword_is_indexed(atlas_index, path) + # we are wrapping the result to an embedded document + obj = self._convert_to_embedded_document(path.split("."), obj) + + if self.atlas_index.ensured: + + self._ensure_path_is_indexed(path.split(".")) logger.debug(obj) if to_go == 1: diff --git a/atlasq/queryset/visitor.py b/atlasq/queryset/visitor.py index 9819468..2cf5832 100644 --- a/atlasq/queryset/visitor.py +++ b/atlasq/queryset/visitor.py @@ -87,9 +87,9 @@ def visit_combination(self, combination) -> List[Dict]: def visit_query(self, query) -> List[Dict]: from atlasq.queryset.transform import AtlasTransform - affirmative, negative, aggregations = AtlasTransform(query.query).transform( - self.atlas_index - ) + affirmative, negative, aggregations = AtlasTransform( + query.query, self.atlas_index + ).transform() filters = {} if affirmative: filters.setdefault("compound", {})["filter"] = affirmative diff --git a/tests/queryset/test_index.py b/tests/queryset/test_index.py index 1b0ee86..dd01b8d 100644 --- a/tests/queryset/test_index.py +++ b/tests/queryset/test_index.py @@ -19,7 +19,7 @@ def raise_for_status(self): raise HTTPError(self.status_code) -class TestManager(TestBaseCase): +class TestIndex(TestBaseCase): def test_ensure_keyword_is_indexed(self): index = AtlasIndex("myindex") index._indexed_fields = ["field1", "field2.*"] @@ -61,10 +61,13 @@ def test_set_indexed_from_mappings(self): } ) self.assertCountEqual( - index._indexed_fields, + index._indexed_fields.keys(), [ + "field1", "field1.*", + "field2", "field2.field", + "field2.field4", "field2.field4.field5", "field2.field4.field6", ], @@ -89,7 +92,8 @@ def test_set_indexed_fields(self): } ) self.assertCountEqual( - index._indexed_fields, ["field1", "field2.field3", "field2.field4"] + index._indexed_fields, + ["field1", "field2", "field2.field3", "field2.field4"], ) index._indexed_fields.clear() index._set_indexed_fields( diff --git a/tests/queryset/test_transform.py b/tests/queryset/test_transform.py index 0c78787..62761ab 100644 --- a/tests/queryset/test_transform.py +++ b/tests/queryset/test_transform.py @@ -12,45 +12,155 @@ class TestTransformSteps(TestBaseCase): + def test__embedded_document(self): + index = AtlasIndex("test") + index.ensured = True + index._indexed_fields = {"field": "embeddedDocuments", "field.field2": "string"} + q = AtlasQ(field__field2="aaa") + try: + result = AtlasTransform(q.query, index).transform() + except AtlasIndexFieldError as e: + self.fail(e) + else: + self.assertEqual( + result[0][0], + { + "embeddedDocument": { + "path": "field", + "operator": {"text": {"query": "aaa", "path": "field.field2"}}, + } + }, + ) + + index._indexed_fields = {"field": "document", "field.field2": "string"} + q = AtlasQ(field__field2="aaa") + try: + result = AtlasTransform(q.query, index).transform() + except AtlasIndexFieldError as e: + self.fail(e) + else: + self.assertEqual( + result[0][0], {"text": {"query": "aaa", "path": "field.field2"}} + ) + + index._indexed_fields = { + "field": "document", + "field.field2": "document", + "field.field2.field3": "string", + } + q = AtlasQ(field__field2__field3="aaa") + try: + result = AtlasTransform(q.query, index).transform() + except AtlasIndexFieldError as e: + self.fail(e) + else: + self.assertEqual( + result[0][0], {"text": {"query": "aaa", "path": "field.field2.field3"}} + ) + + index._indexed_fields = { + "field": "document", + "field.field2": "embeddedDocuments", + "field.field2.field3": "string", + } + q = AtlasQ(field__field2__field3="aaa") + try: + result = AtlasTransform(q.query, index).transform() + except AtlasIndexFieldError as e: + self.fail(e) + else: + self.assertEqual( + result[0][0], {"text": {"query": "aaa", "path": "field.field2.field3"}} + ) + + index._indexed_fields = { + "field": "embeddedDocuments", + "field.field2": "embeddedDocuments", + "field.field2.field3": "string", + } + q = AtlasQ(field__field2__field3="aaa") + try: + result = AtlasTransform(q.query, index).transform() + except AtlasIndexFieldError as e: + self.fail(e) + else: + self.assertEqual( + result[0][0], + { + "embeddedDocument": { + "path": "field", + "operator": { + "embeddedDocument": { + "path": "field.field2", + "operator": { + "text": { + "query": "aaa", + "path": "field.field2.field3", + } + }, + } + }, + } + }, + ) + + index._indexed_fields = { + "field": "embeddedDocuments", + "field.field2": "document", + "field.field2.field3": "string", + } + q = AtlasQ(field__field2__field3="aaa") + try: + result = AtlasTransform(q.query, index).transform() + except AtlasIndexFieldError as e: + self.fail(e) + else: + self.assertEqual( + result[0][0], + { + "embeddedDocument": { + "path": "field", + "operator": { + "text": {"query": "aaa", "path": "field.field2.field3"} + }, + } + }, + ) + def test__ensure_keyword(self): index = AtlasIndex("test") - index._indexed_fields = ["field"] + index._indexed_fields = {"field": "string"} q = AtlasQ(field="aaa") try: - AtlasTransform(q.query).transform(index) + AtlasTransform(q.query, index).transform() except AtlasIndexFieldError as e: self.fail(e) q = AtlasQ(field2="aaa") try: - AtlasTransform(q.query).transform(index) + AtlasTransform(q.query, index).transform() except AtlasIndexFieldError as e: self.fail(e) index.ensured = True with self.assertRaises(AtlasIndexFieldError): - AtlasTransform(q.query).transform(index) + AtlasTransform(q.query, index).transform() q = AtlasQ(field__field2="bbb") - try: - AtlasTransform(q.query).transform(index) - except AtlasIndexFieldError as e: - self.fail(e) - index.use_embedded_documents = False with self.assertRaises(AtlasIndexFieldError): - AtlasTransform(q.query).transform(index) + AtlasTransform(q.query, index).transform() - index._indexed_fields.append("field.field2") + index._indexed_fields["field.field2"] = "string" try: - AtlasTransform(q.query).transform(index) + AtlasTransform(q.query, index).transform() except AtlasIndexFieldError as e: self.fail(e) def test__regex(self): q = AtlasQ(f__regex=".*") - t = AtlasTransform(q.query) + t = AtlasTransform(q.query, AtlasIndex("test")) res = t._regex("f", ".*") self.assertIn("regex", res) self.assertIn("query", res["regex"]) @@ -66,9 +176,9 @@ class MyDoc(Document): objects = MyDoc.objects.all().values_list("field") q = AtlasQ(f=objects) - positive, negative, aggregations = AtlasTransform(q.query).transform( - AtlasIndex("test") - ) + positive, negative, aggregations = AtlasTransform( + q.query, AtlasIndex("test") + ).transform() self.assertEqual(positive, [{"text": {"query": ["aaa"], "path": "f"}}]) self.assertEqual(negative, []) @@ -76,7 +186,7 @@ class MyDoc(Document): def test__exists(self): q = AtlasQ(f=3) - t = AtlasTransform(q.query) + t = AtlasTransform(q.query, AtlasIndex("test")) res = t._exists("field") self.assertIn("exists", res) self.assertIn("path", res["exists"]) @@ -84,7 +194,7 @@ def test__exists(self): def test__range_date_valid(self): q = AtlasQ(f=3) - t = AtlasTransform(q.query) + t = AtlasTransform(q.query, AtlasIndex("test")) time = datetime.datetime.now() res = t._range("field", time, "lte") self.assertEqual( @@ -99,32 +209,32 @@ def test__range_date_valid(self): def test__range_integer_valid(self): q = AtlasQ(f=3) - t = AtlasTransform(q.query) + t = AtlasTransform(q.query, AtlasIndex("test")) res = t._range("field", 3, "lte") self.assertEqual(res, {"range": {"path": "field", "lte": 3}}) def test__range_date_invalid(self): q = AtlasQ(f=3) - t = AtlasTransform(q.query) + t = AtlasTransform(q.query, AtlasIndex("test")) time = datetime.datetime.now() with self.assertRaises(AtlasFieldError): t._range("field", time, "wat") def test__range_none(self): q = AtlasQ(f=3) - t = AtlasTransform(q.query) + t = AtlasTransform(q.query, AtlasIndex("test")) with self.assertRaises(AtlasFieldError): t._range("field", None, "lte") def test__range_string(self): q = AtlasQ(f=3) - t = AtlasTransform(q.query) + t = AtlasTransform(q.query, AtlasIndex("test")) with self.assertRaises(AtlasFieldError): t._range("field", "3", "lte") def test__equals(self): q = AtlasQ(f=3) - t = AtlasTransform(q.query) + t = AtlasTransform(q.query, AtlasIndex("test")) res = t._equals("field", "aaa") self.assertIn("equals", res) self.assertIn("path", res["equals"]) @@ -134,7 +244,7 @@ def test__equals(self): def test(self): q = AtlasQ(f=3) - t = AtlasTransform(q.query) + t = AtlasTransform(q.query, AtlasIndex("test")) res = t._text("field", "aaa") self.assertIn("text", res) self.assertIn("path", res["text"]) @@ -144,25 +254,25 @@ def test(self): def test_none(self): q = AtlasQ(f=3) - t = AtlasTransform(q.query) + t = AtlasTransform(q.query, AtlasIndex("test")) with self.assertRaises(AtlasFieldError): t._text("field", None) def test__size_operator_not_supported(self): q = AtlasQ(f=3) - t = AtlasTransform(q.query) + t = AtlasTransform(q.query, AtlasIndex("test")) with self.assertRaises(NotImplementedError): t._size("field", 0, "lte") def test__size__not_zero(self): q = AtlasQ(f=3) - t = AtlasTransform(q.query) + t = AtlasTransform(q.query, AtlasIndex("test")) with self.assertRaises(NotImplementedError): t._size("field", 3, "ne") def test__size_equal_zero(self): q = AtlasQ(f=3) - t = AtlasTransform(q.query) + t = AtlasTransform(q.query, AtlasIndex("test")) res = t._size("field", 0, "eq") self.assertIn("$match", res) self.assertIn("field", res["$match"]) @@ -173,7 +283,7 @@ def test__size_equal_zero(self): def test__size_ne_zero(self): q = AtlasQ(f=3) - t = AtlasTransform(q.query) + t = AtlasTransform(q.query, AtlasIndex("test")) res = t._size("field", 0, "ne") self.assertIn("$match", res) self.assertIn("field", res["$match"]) @@ -187,23 +297,23 @@ class TestAtlasQ(TestBaseCase): def test_size_val(self): q1 = AtlasQ(key__size=1) with self.assertRaises(NotImplementedError): - AtlasTransform(q1.query).transform(AtlasIndex("test")) + AtlasTransform(q1.query, AtlasIndex("test")).transform() def test_size_ne(self): q1 = AtlasQ(key__size__ne=0) with self.assertRaises(NotImplementedError): - AtlasTransform(q1.query).transform(AtlasIndex("test")) + AtlasTransform(q1.query, AtlasIndex("test")).transform() def test_size_gte(self): q1 = AtlasQ(key__not__size__gte=0) with self.assertRaises(NotImplementedError): - AtlasTransform(q1.query).transform(AtlasIndex("test")) + AtlasTransform(q1.query, AtlasIndex("test")).transform() def test_size_negative(self): q1 = AtlasQ(key__not__size=0) - positive, negative, aggregations = AtlasTransform(q1.query).transform( - AtlasIndex("test") - ) + positive, negative, aggregations = AtlasTransform( + q1.query, AtlasIndex("test") + ).transform() self.assertEqual(positive, []) self.assertEqual(negative, []) self.assertEqual( @@ -214,9 +324,9 @@ def test_size_negative(self): def test_size_positive(self): q1 = AtlasQ(key__size=0) - positive, negative, aggregations = AtlasTransform(q1.query).transform( - AtlasIndex("test") - ) + positive, negative, aggregations = AtlasTransform( + q1.query, AtlasIndex("test") + ).transform() self.assertEqual(positive, []) self.assertEqual(negative, []) self.assertEqual( @@ -227,9 +337,9 @@ def test_size_positive(self): def test_atlas_q_not_none(self): q1 = AtlasQ(key__nin=["", None]) - positive, negative, aggregations = AtlasTransform(q1.query).transform( - AtlasIndex("test") - ) + positive, negative, aggregations = AtlasTransform( + q1.query, AtlasIndex("test") + ).transform() self.assertEqual(aggregations, []) self.assertEqual(positive, []) self.assertEqual( @@ -242,9 +352,9 @@ def test_atlas_q_not_none(self): def test_atlas_q_not_exists_3(self): q1 = AtlasQ(key__not__exists=True) - positive, negative, aggregations = AtlasTransform(q1.query).transform( - AtlasIndex("test") - ) + positive, negative, aggregations = AtlasTransform( + q1.query, AtlasIndex("test") + ).transform() self.assertEqual(positive, []) self.assertEqual(negative, [{"exists": {"path": "key"}}]) self.assertEqual( @@ -254,9 +364,9 @@ def test_atlas_q_not_exists_3(self): def test_atlas_q_not_exists_2(self): q1 = AtlasQ(key__not__exists=False) - positive, negative, aggregations = AtlasTransform(q1.query).transform( - AtlasIndex("test") - ) + positive, negative, aggregations = AtlasTransform( + q1.query, AtlasIndex("test") + ).transform() self.assertEqual(positive, [{"exists": {"path": "key"}}]) self.assertEqual(negative, []) self.assertEqual( @@ -267,9 +377,9 @@ def test_atlas_q_not_exists_2(self): def test_atlas_q_not_exists_1(self): q1 = AtlasQ(key__exists=False) - positive, negative, aggregations = AtlasTransform(q1.query).transform( - AtlasIndex("test") - ) + positive, negative, aggregations = AtlasTransform( + q1.query, AtlasIndex("test") + ).transform() self.assertEqual(positive, []) self.assertEqual(negative, [{"exists": {"path": "key"}}]) self.assertEqual( @@ -280,9 +390,9 @@ def test_atlas_q_not_exists_1(self): def test_atlas_q_not_exists_0(self): q1 = AtlasQ(key__exists=True) - positive, negative, aggregations = AtlasTransform(q1.query).transform( - AtlasIndex("test") - ) + positive, negative, aggregations = AtlasTransform( + q1.query, AtlasIndex("test") + ).transform() self.assertEqual(positive, [{"exists": {"path": "key"}}]) self.assertEqual(negative, []) self.assertEqual( @@ -293,9 +403,9 @@ def test_atlas_q_not_exists_0(self): def test_atlas_q_negative_equals(self): q1 = AtlasQ(key__ne=True) - positive, negative, aggregations = AtlasTransform(q1.query).transform( - AtlasIndex("test") - ) + positive, negative, aggregations = AtlasTransform( + q1.query, AtlasIndex("test") + ).transform() self.assertEqual(aggregations, []) self.assertEqual(positive, []) self.assertEqual( @@ -308,9 +418,9 @@ def test_atlas_q_negative_equals(self): def test_atlas_q_equals(self): q1 = AtlasQ(key=True) - positive, negative, aggregations = AtlasTransform(q1.query).transform( - AtlasIndex("test") - ) + positive, negative, aggregations = AtlasTransform( + q1.query, AtlasIndex("test") + ).transform() self.assertEqual([], aggregations) self.assertEqual([], negative) self.assertEqual( @@ -325,9 +435,9 @@ def test_atlas_q_lt(self): date = "2021-01-01T00:00:00.000Z" date = datetime.datetime.strptime(date, "%Y-%m-%dT%H:%M:%S.%fZ") q1 = AtlasQ(last_sent_time__lt=date) - positive, negative, aggregations = AtlasTransform(q1.query).transform( - AtlasIndex("test") - ) + positive, negative, aggregations = AtlasTransform( + q1.query, AtlasIndex("test") + ).transform() self.assertEqual([], aggregations) self.assertEqual([], negative) self.assertEqual( @@ -347,9 +457,9 @@ def test_atlas_q_gte(self): date = "2021-01-01T00:00:00.000Z" date = datetime.datetime.strptime(date, "%Y-%m-%dT%H:%M:%S.%fZ") q1 = AtlasQ(last_sent_time__gte=date) - positive, negative, aggregations = AtlasTransform(q1.query).transform( - AtlasIndex("test") - ) + positive, negative, aggregations = AtlasTransform( + q1.query, AtlasIndex("test") + ).transform() self.assertEqual([], aggregations) self.assertEqual([], negative) self.assertEqual( @@ -367,9 +477,9 @@ def test_atlas_q_gte(self): def test_atlas_q_not_whole_word(self): q1 = AtlasQ(key__not__wholeword="value") - positive, negative, aggregations = AtlasTransform(q1.query).transform( - AtlasIndex("test") - ) + positive, negative, aggregations = AtlasTransform( + q1.query, AtlasIndex("test") + ).transform() self.assertEqual([], aggregations) self.assertEqual( [ @@ -385,9 +495,9 @@ def test_atlas_q_not_whole_word(self): def test_atlas_q_field_start_with_keyword(self): q1 = AtlasQ(key__in=["value"]) - positive, negative, aggregations = AtlasTransform(q1.query).transform( - AtlasIndex("test") - ) + positive, negative, aggregations = AtlasTransform( + q1.query, AtlasIndex("test") + ).transform() self.assertEqual([], aggregations) self.assertEqual([], negative) self.assertEqual( @@ -399,10 +509,15 @@ def test_atlas_q_field_start_with_keyword(self): ) def test_atlas_q_ne_embedded_document(self): + index = AtlasIndex("test") + index._indexed_fields = { + "f": "embeddedDocuments", + "f.g": "embeddedDocuments", + "f.g.h": "string", + } + index.ensured = True q = AtlasQ(f__g__h__ne="test") - positive, negative, aggregations = AtlasTransform(q.query).transform( - AtlasIndex("test") - ) + positive, negative, aggregations = AtlasTransform(q.query, index).transform() self.assertEqual( negative, [ @@ -427,9 +542,14 @@ def test_atlas_q_ne_embedded_document(self): def test_atlas_q_embedded_document(self): q = AtlasQ(f__g__h="test") - positive, negative, aggregations = AtlasTransform(q.query).transform( - AtlasIndex("test") - ) + index = AtlasIndex("test") + index._indexed_fields = { + "f": "embeddedDocuments", + "f.g": "embeddedDocuments", + "f.g.h": "string", + } + index.ensured = True + positive, negative, aggregations = AtlasTransform(q.query, index).transform() self.assertEqual( positive, [ @@ -454,9 +574,9 @@ def test_atlas_q_embedded_document(self): def test_atlas_q_nin(self): q1 = AtlasQ(key__nin=["value", "value2"]) - positive, negative, aggregations = AtlasTransform(q1.query).transform( - AtlasIndex("test") - ) + positive, negative, aggregations = AtlasTransform( + q1.query, AtlasIndex("test") + ).transform() self.assertEqual(aggregations, []) self.assertEqual(positive, []) self.assertEqual( @@ -469,9 +589,9 @@ def test_atlas_q_nin(self): def test_atlas_q_negative(self): q1 = AtlasQ(key__ne="value") - positive, negative, aggregations = AtlasTransform(q1.query).transform( - AtlasIndex("test") - ) + positive, negative, aggregations = AtlasTransform( + q1.query, AtlasIndex("test") + ).transform() self.assertEqual(aggregations, []) self.assertEqual(positive, []) @@ -485,9 +605,9 @@ def test_atlas_q_negative(self): def test_atlas_q(self): q1 = AtlasQ(key="value", key2="value2") - positive, negative, aggregations = AtlasTransform(q1.query).transform( - AtlasIndex("test") - ) + positive, negative, aggregations = AtlasTransform( + q1.query, AtlasIndex("test") + ).transform() self.assertEqual([], aggregations) self.assertEqual([], negative) self.assertEqual( From 2d089aa77d34af21bc460dca9467769ec4c1ee4d Mon Sep 17 00:00:00 2001 From: 0ssigeno Date: Mon, 10 Oct 2022 11:18:16 +0200 Subject: [PATCH 3/3] Bump 0.4.0 --- version.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/version.py b/version.py index ceb32f1..6cc293e 100644 --- a/version.py +++ b/version.py @@ -1 +1 @@ -VERSION = "0.3.9" +VERSION = "0.4.0"