Skip to content

Commit

Permalink
Computed Values support for PUT `/_api/collection/collection/properti…
Browse files Browse the repository at this point in the history
…es` (#268)

* initial commit

* fix: black

* fix: prevent computedValue from affecting other tests

* typing: Jsons instead of List[Json]

* fix: isort

* update: computedValues formatter
  • Loading branch information
aMahanna authored Aug 16, 2023
1 parent f5b1d50 commit 23de7d3
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 19 deletions.
13 changes: 8 additions & 5 deletions arango/collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -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, Jsons, Params
from arango.utils import get_batches, get_doc_id, is_none_or_int, is_none_or_str


Expand Down Expand Up @@ -306,6 +306,7 @@ def configure(
schema: Optional[Json] = None,
replication_factor: Optional[int] = None,
write_concern: Optional[int] = None,
computed_values: Optional[Jsons] = None,
) -> Result[Json]:
"""Configure collection properties.
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -976,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
Expand All @@ -1001,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]
Expand Down Expand Up @@ -1034,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.
Expand All @@ -1047,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"]
Expand Down
15 changes: 2 additions & 13 deletions arango/formatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -249,20 +249,9 @@ def format_collection(body: Json) -> Json:
result["min_revision"] = body["minRevision"]
if "schema" in body:
result["schema"] = body["schema"]

# 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"]
]
result["computedValues"] = body["computedValues"]

if "internalValidatorType" in body:
result["internal_validator_type"] = body["internalValidatorType"]

Expand Down
33 changes: 32 additions & 1 deletion tests/test_collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,27 @@ 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
col.configure(computed_values=[])

# Test configure properties with bad collection
with assert_raises(CollectionConfigureError) as err:
Expand Down Expand Up @@ -153,6 +170,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,
Expand All @@ -172,6 +200,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

Expand All @@ -181,6 +210,8 @@ 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
col.configure(computed_values=[])

# Test create duplicate collection
with assert_raises(CollectionCreateError) as err:
Expand Down

0 comments on commit 23de7d3

Please sign in to comment.