From 7b70f49f3978e3f734ad8d14bd6f99f9ed8ecc8d Mon Sep 17 00:00:00 2001 From: Anthony Mahanna Date: Tue, 8 Aug 2023 12:52:49 -0400 Subject: [PATCH 1/6] initial commit --- arango/collection.py | 3 +++ arango/formatter.py | 15 ++------------- tests/test_collection.py | 31 ++++++++++++++++++++++++++++++- 3 files changed, 35 insertions(+), 14 deletions(-) diff --git a/arango/collection.py b/arango/collection.py index 015e6607..1d6294b9 100644 --- a/arango/collection.py +++ b/arango/collection.py @@ -306,6 +306,7 @@ def configure( schema: Optional[Json] = None, replication_factor: Optional[int] = None, write_concern: Optional[int] = None, + computed_values: Optional[List[Json]] = None ) -> Result[Json]: """Configure collection properties. @@ -341,6 +342,8 @@ def configure( data["replicationFactor"] = replication_factor if write_concern is not None: data["writeConcern"] = write_concern + if computed_values is not None: + data["computedValues"] = computed_values request = Request( method="put", diff --git a/arango/formatter.py b/arango/formatter.py index 7baff6dc..8169e03a 100644 --- a/arango/formatter.py +++ b/arango/formatter.py @@ -249,20 +249,9 @@ def format_collection(body: Json) -> Json: result["min_revision"] = body["minRevision"] if "schema" in body: result["schema"] = body["schema"] + if "computedValues" in body: + result["computedValues"] = body["computedValues"] - # New in 3.10 - if body.get("computedValues") is not None: - result["computedValues"] = [ - { - "name": cv["name"], - "expression": cv["expression"], - "overwrite": cv["overwrite"], - "computedOn": cv["computedOn"], - "keepNull": cv["keepNull"], - "failOnWarning": cv["failOnWarning"], - } - for cv in body["computedValues"] - ] if "internalValidatorType" in body: result["internal_validator_type"] = body["internalValidatorType"] diff --git a/tests/test_collection.py b/tests/test_collection.py index acaf3b63..9084a97b 100644 --- a/tests/test_collection.py +++ b/tests/test_collection.py @@ -49,10 +49,26 @@ def test_collection_misc_methods(col, bad_col, cluster): # Test configure properties prev_sync = properties["sync"] - properties = col.configure(sync=not prev_sync, schema={}) + + computed_values = [ + { + "name": "foo", + "expression": "RETURN 1", + "computeOn": ["insert", "update", "replace"], + "overwrite": True, + "failOnWarning": False, + "keepNull": True, + } + ] + + properties = col.configure( + sync=not prev_sync, schema={}, computed_values=computed_values + ) + assert properties["name"] == col.name assert properties["system"] is False assert properties["sync"] is not prev_sync + assert properties["computedValues"] == computed_values # Test configure properties with bad collection with assert_raises(CollectionConfigureError) as err: @@ -153,6 +169,17 @@ def test_collection_management(db, bad_db, cluster): "type": "json", } + computed_values = [ + { + "name": "foo", + "expression": "RETURN 1", + "computeOn": ["insert", "update", "replace"], + "overwrite": True, + "failOnWarning": False, + "keepNull": True, + } + ] + col = db.create_collection( name=col_name, sync=True, @@ -172,6 +199,7 @@ def test_collection_management(db, bad_db, cluster): smart_join_attribute="test_attr", write_concern=1, schema=schema, + computedValues=computed_values, ) assert db.has_collection(col_name) is True @@ -181,6 +209,7 @@ def test_collection_management(db, bad_db, cluster): assert properties["name"] == col_name assert properties["sync"] is True assert properties["system"] is False + assert properties["computedValues"] == computed_values # Test create duplicate collection with assert_raises(CollectionCreateError) as err: From 44bd495158a851f81e61bde6812f627f22c6be2d Mon Sep 17 00:00:00 2001 From: Anthony Mahanna Date: Tue, 8 Aug 2023 16:41:00 -0400 Subject: [PATCH 2/6] fix: black --- arango/collection.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/arango/collection.py b/arango/collection.py index 1d6294b9..2f79483b 100644 --- a/arango/collection.py +++ b/arango/collection.py @@ -306,7 +306,7 @@ def configure( schema: Optional[Json] = None, replication_factor: Optional[int] = None, write_concern: Optional[int] = None, - computed_values: Optional[List[Json]] = None + computed_values: Optional[List[Json]] = None, ) -> Result[Json]: """Configure collection properties. From 26083c57da75104d2f33f16e4bf62e5749af0c28 Mon Sep 17 00:00:00 2001 From: Anthony Mahanna Date: Tue, 8 Aug 2023 17:04:16 -0400 Subject: [PATCH 3/6] fix: prevent computedValue from affecting other tests --- tests/test_collection.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/test_collection.py b/tests/test_collection.py index 9084a97b..b9d961b2 100644 --- a/tests/test_collection.py +++ b/tests/test_collection.py @@ -69,6 +69,7 @@ def test_collection_misc_methods(col, bad_col, cluster): assert properties["system"] is False assert properties["sync"] is not prev_sync assert properties["computedValues"] == computed_values + col.configure(computed_values=[]) # Test configure properties with bad collection with assert_raises(CollectionConfigureError) as err: @@ -210,6 +211,7 @@ def test_collection_management(db, bad_db, cluster): assert properties["sync"] is True assert properties["system"] is False assert properties["computedValues"] == computed_values + col.configure(computed_values=[]) # Test create duplicate collection with assert_raises(CollectionCreateError) as err: From 5bd3843e532ade6aac35f719858e2816ebe4fbe1 Mon Sep 17 00:00:00 2001 From: Anthony Mahanna Date: Tue, 8 Aug 2023 17:18:15 -0400 Subject: [PATCH 4/6] typing: Jsons instead of List[Json] --- arango/collection.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/arango/collection.py b/arango/collection.py index 2f79483b..0ea0a278 100644 --- a/arango/collection.py +++ b/arango/collection.py @@ -41,7 +41,7 @@ from arango.request import Request from arango.response import Response from arango.result import Result -from arango.typings import Fields, Headers, Json, Params +from arango.typings import Fields, Headers, Json, Params, Jsons from arango.utils import get_batches, get_doc_id, is_none_or_int, is_none_or_str @@ -306,7 +306,7 @@ def configure( schema: Optional[Json] = None, replication_factor: Optional[int] = None, write_concern: Optional[int] = None, - computed_values: Optional[List[Json]] = None, + computed_values: Optional[Jsons] = None, ) -> Result[Json]: """Configure collection properties. @@ -979,7 +979,7 @@ def get_many( self, documents: Sequence[Union[str, Json]], allow_dirty_read: bool = False, - ) -> Result[List[Json]]: + ) -> Result[Jsons]: """Return multiple documents ignoring any missing ones. :param documents: List of document keys, IDs or bodies. Document bodies @@ -1004,7 +1004,7 @@ def get_many( headers={"x-arango-allow-dirty-read": "true"} if allow_dirty_read else None, ) - def response_handler(resp: Response) -> List[Json]: + def response_handler(resp: Response) -> Jsons: if not resp.is_success: raise DocumentGetError(resp, request) return [doc for doc in resp.body if "_id" in doc] @@ -1037,7 +1037,7 @@ def response_handler(resp: Response) -> Json: # Index Management # #################### - def indexes(self) -> Result[List[Json]]: + def indexes(self) -> Result[Jsons]: """Return the collection indexes. :return: Collection indexes. @@ -1050,7 +1050,7 @@ def indexes(self) -> Result[List[Json]]: params={"collection": self.name}, ) - def response_handler(resp: Response) -> List[Json]: + def response_handler(resp: Response) -> Jsons: if not resp.is_success: raise IndexListError(resp, request) result = resp.body["indexes"] From 0d7c42cb99f334ee0a1edb3339cc0426cef42301 Mon Sep 17 00:00:00 2001 From: Anthony Mahanna Date: Tue, 8 Aug 2023 17:19:31 -0400 Subject: [PATCH 5/6] fix: isort --- arango/collection.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/arango/collection.py b/arango/collection.py index 0ea0a278..aad2caf2 100644 --- a/arango/collection.py +++ b/arango/collection.py @@ -41,7 +41,7 @@ from arango.request import Request from arango.response import Response from arango.result import Result -from arango.typings import Fields, Headers, Json, Params, Jsons +from arango.typings import Fields, Headers, Json, Jsons, Params from arango.utils import get_batches, get_doc_id, is_none_or_int, is_none_or_str From 3acd627d760c27609f608ca900ff456a01f1f1f3 Mon Sep 17 00:00:00 2001 From: Anthony Mahanna Date: Wed, 9 Aug 2023 14:19:29 -0400 Subject: [PATCH 6/6] update: computedValues formatter --- arango/formatter.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/arango/formatter.py b/arango/formatter.py index 8169e03a..ea558720 100644 --- a/arango/formatter.py +++ b/arango/formatter.py @@ -249,7 +249,7 @@ def format_collection(body: Json) -> Json: result["min_revision"] = body["minRevision"] if "schema" in body: result["schema"] = body["schema"] - if "computedValues" in body: + if body.get("computedValues") is not None: result["computedValues"] = body["computedValues"] if "internalValidatorType" in body: