From 48a98f6b26c28715b0a9b1788df6ab14dd785039 Mon Sep 17 00:00:00 2001 From: matveyvarg Date: Thu, 26 Oct 2023 18:04:00 +0200 Subject: [PATCH 1/4] - Fixed empty attrs_schema error - Generating uid moved to a func --- deker/tools/array.py | 16 +++++++++++++--- deker/types/private/enums.py | 11 ++++++++--- deker/validators.py | 2 +- tests/test_cases/test_uri/test_uri.py | 6 ++++++ .../test_attributes_validator.py | 17 +++++++++++++++++ 5 files changed, 45 insertions(+), 7 deletions(-) diff --git a/deker/tools/array.py b/deker/tools/array.py index 5838d4c..7707e0a 100644 --- a/deker/tools/array.py +++ b/deker/tools/array.py @@ -17,7 +17,7 @@ import uuid from functools import singledispatch -from typing import Any, Dict, List, Tuple, Union +from typing import Any, Dict, List, Tuple, Union, Literal import numpy as np @@ -26,6 +26,7 @@ from psutil import swap_memory, virtual_memory from deker.errors import DekerMemoryError, DekerValidationError +from deker.types.private.enums import ArrayType def calculate_total_cells_in_array(seq: Union[Tuple[int, ...], List[int]]) -> int: @@ -87,6 +88,15 @@ def check_memory(shape: tuple, dtype: type, mem_limit_from_settings: int) -> Non ) +def generate_uid(array_type: ArrayType) -> str: + """Generate uuid5 for given array_type. + + :param array_type: Either array or varray + """ + namespace = uuid.NAMESPACE_X500 if array_type == "array" else uuid.NAMESPACE_OID + return str(uuid.uuid5(namespace, array_type + get_utc().isoformat())) + + def get_id(array: Any) -> str: """Generate unique id by object type and datetime. @@ -108,7 +118,7 @@ def array_id(arr: Array) -> str: # noqa[ARG001] :param arr: Array type """ - return str(uuid.uuid5(uuid.NAMESPACE_X500, "array" + get_utc().isoformat())) + return generate_uid(ArrayType.array) @generate_id.register(VArray) def varray_id(arr: VArray) -> str: # noqa[ARG001] @@ -116,6 +126,6 @@ def varray_id(arr: VArray) -> str: # noqa[ARG001] :param arr: VArray type """ - return str(uuid.uuid5(uuid.NAMESPACE_OID, "varray" + get_utc().isoformat())) + return generate_uid(ArrayType.varray) return generate_id(array) diff --git a/deker/types/private/enums.py b/deker/types/private/enums.py index 492fc53..ed5eb4c 100644 --- a/deker/types/private/enums.py +++ b/deker/types/private/enums.py @@ -61,14 +61,14 @@ def get_name(object: "DTypeEnum") -> str: return f"numpy.{object.name}" -class DimensionType(Enum): +class DimensionType(str, Enum): """Enum of dimensions' types.""" generic = "generic" time = "time" -class LocksExtensions(Enum): +class LocksExtensions(str, Enum): """Extensions for lock files.""" array_lock = ".arrlock" @@ -77,10 +77,15 @@ class LocksExtensions(Enum): varray_lock = ".varraylock" -class LocksTypes(Enum): +class LocksTypes(str, Enum): """Locks enum.""" array_lock = "array creation lock" array_read_lock = "array read lock" collection_lock = "collection creation lock" varray_lock = "varray write lock" + + +class ArrayType(str, Enum): + array = "array" + varray = "varray" diff --git a/deker/validators.py b/deker/validators.py index 3b181c6..411c704 100644 --- a/deker/validators.py +++ b/deker/validators.py @@ -96,7 +96,7 @@ def process_attributes( array_type = "VArray" if isinstance(schema, VArraySchema) else "Array" - attrs_schema = schema.attributes if schema else None + attrs_schema = schema.attributes if schema else [] if primary_attributes is None: primary_attributes = {} diff --git a/tests/test_cases/test_uri/test_uri.py b/tests/test_cases/test_uri/test_uri.py index 6eb27c6..78b4c14 100644 --- a/tests/test_cases/test_uri/test_uri.py +++ b/tests/test_cases/test_uri/test_uri.py @@ -132,6 +132,8 @@ def test_uri_raises_invalid_uri(uri): "http://host:8080/data/collections/", "https://user:pass@host:8080/data/collections/", "http://host:8080/data/collections/", + "https://user:pass@host:8080,host:8000/data/collections/", + "http://host:8080,host:3000/data/collections/", "https://user:pass@host:8080/data/collections/", ], ) @@ -152,6 +154,8 @@ def test_uri_path_concatenation(string): "https://user:pass@host:8080/data/collections/", "http://host:8080/data/collections/", "https://user:pass@host:8080/data/collections/", + "https://user:pass@host:8080,host:8000/data/collections/", + "http://host:8080,host:3000/data/collections/", ], ) def test_uri_path_concatenation_with_assignment_wrong_expectations(string): @@ -171,6 +175,8 @@ def test_uri_path_concatenation_with_assignment_wrong_expectations(string): "https://user:pass@host:8080/data/collections/", "http://host:8080/data/collections/", "https://user:pass@host:8080/data/collections/", + "https://user:pass@host:8080,host:8000/data/collections/", + "http://host:8080,host:3000/data/collections/", ], ) def test_uri_path_correct_concatenation_with_assignment(string): diff --git a/tests/test_cases/test_validators/test_attributes_validator.py b/tests/test_cases/test_validators/test_attributes_validator.py index 5dc3053..6ad7442 100644 --- a/tests/test_cases/test_validators/test_attributes_validator.py +++ b/tests/test_cases/test_validators/test_attributes_validator.py @@ -600,5 +600,22 @@ def test_custom_attributes_schema_attrs_no_attrs_provided( collection.delete() +class TestNoAttributes: + def test_no_attributes(self, client): + """Test that it's possible to create array with empty primary and custom attributes schema.""" + coll_params = ClientParams.ArraySchema.OK.no_vgrid_no_attrs() + try: + collection: Collection = client.create_collection(**coll_params) + except DekerCollectionAlreadyExistsError: + coll = client.get_collection(coll_params["name"]) + coll.delete() + collection: Collection = client.create_collection(**coll_params) + try: + array = collection.create() + assert array + finally: + collection.delete() + + if __name__ == "__main__": pytest.main() From 797bf9fabab657185abe7a93ff5d3cf7ce1586ba Mon Sep 17 00:00:00 2001 From: matveyvarg Date: Thu, 26 Oct 2023 18:04:00 +0200 Subject: [PATCH 2/4] removed unused import --- deker/tools/array.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/deker/tools/array.py b/deker/tools/array.py index 7707e0a..8355268 100644 --- a/deker/tools/array.py +++ b/deker/tools/array.py @@ -17,7 +17,7 @@ import uuid from functools import singledispatch -from typing import Any, Dict, List, Tuple, Union, Literal +from typing import Any, Dict, List, Tuple, Union import numpy as np From 2617e19ca24a3cb64809b82e2517f96c22a2578a Mon Sep 17 00:00:00 2001 From: matveyvarg Date: Fri, 27 Oct 2023 10:09:38 +0200 Subject: [PATCH 3/4] fix comparing --- deker/tools/array.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/deker/tools/array.py b/deker/tools/array.py index 8355268..3fd9c43 100644 --- a/deker/tools/array.py +++ b/deker/tools/array.py @@ -93,7 +93,8 @@ def generate_uid(array_type: ArrayType) -> str: :param array_type: Either array or varray """ - namespace = uuid.NAMESPACE_X500 if array_type == "array" else uuid.NAMESPACE_OID + assert isinstance(array_type, ArrayType), "Invalid argument type. Array type is required" + namespace = uuid.NAMESPACE_X500 if array_type == ArrayType.array else uuid.NAMESPACE_OID return str(uuid.uuid5(namespace, array_type + get_utc().isoformat())) From ddde6d93d82e7cc1e3aabd0a9a422228bda2e83b Mon Sep 17 00:00:00 2001 From: matveyvarg Date: Fri, 27 Oct 2023 10:09:38 +0200 Subject: [PATCH 4/4] fix comparing --- deker/tools/array.py | 6 ++++-- tests/test_cases/test_tools/test_tools.py | 7 +++++++ 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/deker/tools/array.py b/deker/tools/array.py index 3fd9c43..9ef340b 100644 --- a/deker/tools/array.py +++ b/deker/tools/array.py @@ -93,9 +93,11 @@ def generate_uid(array_type: ArrayType) -> str: :param array_type: Either array or varray """ - assert isinstance(array_type, ArrayType), "Invalid argument type. Array type is required" + if not isinstance(array_type, ArrayType): + raise TypeError("Invalid argument type. Array type is required") + namespace = uuid.NAMESPACE_X500 if array_type == ArrayType.array else uuid.NAMESPACE_OID - return str(uuid.uuid5(namespace, array_type + get_utc().isoformat())) + return str(uuid.uuid5(namespace, array_type.value + get_utc().isoformat())) def get_id(array: Any) -> str: diff --git a/tests/test_cases/test_tools/test_tools.py b/tests/test_cases/test_tools/test_tools.py index 0a5a7a0..0df3848 100644 --- a/tests/test_cases/test_tools/test_tools.py +++ b/tests/test_cases/test_tools/test_tools.py @@ -6,6 +6,7 @@ from deker_local_adapters import LocalCollectionAdapter +from deker.tools.array import generate_uid from tests.parameters.collection_params import CollectionParams from deker.collection import Collection @@ -220,5 +221,11 @@ def test_convert_isoformat_attrs_raises(attrs): assert convert_iso_attrs_to_datetime(attrs) +@pytest.mark.parametrize("array_type_arg", (list(), set(), tuple(), dict(), 1, "2", 3.4)) +def test_generate_id_raises(array_type_arg): + with pytest.raises(TypeError): + generate_uid(array_type_arg) + + if __name__ == "__main__": pytest.main()