From c342074478fcbd5840605ebfb9e2e082c690b064 Mon Sep 17 00:00:00 2001 From: Thomas Wilmering Date: Fri, 12 Jan 2024 17:15:12 +0000 Subject: [PATCH] fixing check issues (in progress) --- cmem_plugin_uuid/transform/__init__.py | 35 +++-- cmem_plugin_uuid/utils.py | 31 ++-- tests/test_uuid.py | 200 +++++++++++++++---------- 3 files changed, 159 insertions(+), 107 deletions(-) diff --git a/cmem_plugin_uuid/transform/__init__.py b/cmem_plugin_uuid/transform/__init__.py index a417ae2..ccb6200 100644 --- a/cmem_plugin_uuid/transform/__init__.py +++ b/cmem_plugin_uuid/transform/__init__.py @@ -55,13 +55,14 @@ class UUID1(TransformPlugin): def __init__( self, - node=None, - clock_seq=None, + node: str | None, + clock_seq: str | None, ): self.node = node_to_int(node) if node else None self.clock_seq = clock_seq_to_int(clock_seq) if clock_seq else None def transform(self, inputs: Sequence[Sequence[str]]) -> Sequence[str]: + """Transform""" result = [] if len(inputs) != 0: for collection in inputs: @@ -106,13 +107,14 @@ class UUID3(TransformPlugin): def __init__( self, - namespace=None, - namespace_as_uuid=False, + namespace: str | None, + namespace_as_uuid: bool | None, ): self.namespace = namespace self.namespace_as_uuid = namespace_as_uuid def transform(self, inputs: Sequence[Sequence[str]]) -> Sequence[str]: + """Transform""" result = [] namespace_uuid = get_namespace_uuid( namespace_as_uuid=self.namespace_as_uuid, @@ -142,6 +144,7 @@ class UUID4(TransformPlugin): """UUID4 Transform Plugin""" def transform(self, inputs: Sequence[Sequence[str]]) -> Sequence[str]: + """Transform""" result = [] if len(inputs) != 0: for collection in inputs: @@ -184,13 +187,14 @@ class UUID5(TransformPlugin): def __init__( self, - namespace=None, - namespace_as_uuid=False, + namespace: str | None, + namespace_as_uuid: bool | None, ): self.namespace = namespace self.namespace_as_uuid = namespace_as_uuid def transform(self, inputs: Sequence[Sequence[str]]) -> Sequence[str]: + """Transform""" result = [] namespace_uuid = get_namespace_uuid( namespace_as_uuid=self.namespace_as_uuid, @@ -249,13 +253,14 @@ class UUID6(TransformPlugin): def __init__( self, - node=None, - clock_seq=None, + node: str | None, + clock_seq: str | None, ): self.node = node_to_int(node) if node else None self.clock_seq = clock_seq_to_int(clock_seq) if clock_seq else None def transform(self, inputs: Sequence[Sequence[str]]) -> Sequence[str]: + """Transform""" result = [] if len(inputs) != 0: for collection in inputs: @@ -282,6 +287,7 @@ class UUID1ToUUID6(TransformPlugin): """UUID1 to UUID6 Transform Plugin""" def transform(self, inputs: Sequence[Sequence[str]]) -> Sequence[str]: + """Transform""" result = [] if len(inputs) != 0: for collection in inputs: @@ -312,6 +318,7 @@ class UUID7(TransformPlugin): """UUID7 Transform Plugin""" def transform(self, inputs: Sequence[Sequence[str]]) -> Sequence[str]: + """Transform""" result = [] if len(inputs) != 0: for collection in inputs: @@ -335,6 +342,7 @@ class UUID8(TransformPlugin): """UUID8 Transform Plugin""" def transform(self, inputs: Sequence[Sequence[str]]) -> Sequence[str]: + """Transform""" result = [] if len(inputs) != 0: for collection in inputs: @@ -374,13 +382,12 @@ def transform(self, inputs: Sequence[Sequence[str]]) -> Sequence[str]: class UUIDConvert(TransformPlugin): """Converts UUID representation""" - def __init__(self, from_format=None, to_format=None): + def __init__(self, from_format: str | None, to_format: str | None) -> None: self.from_ = from_format self.to_ = to_format - def uuid_validate(self, test_uuid, uuid_string): - """Warning if UUID string not standard (versions 1 to 8) - """ + def uuid_validate(self, test_uuid: uuid.UUID, uuid_string: str) -> None: + """Warning if UUID string not standard (versions 1 to 8)""" pattern = r"^[0-9a-f]{8}-[0-9a-f]{4}-[1-8][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$" if not re.match(pattern, str(test_uuid)): @@ -389,7 +396,7 @@ def uuid_validate(self, test_uuid, uuid_string): f"the proposed updates" ) - def convert_uuid(self, uuid_string, result=None): + def convert_uuid(self, uuid_string: str) -> str: # noqa: PLR0912 C901 """Convert UUID string""" if self.from_ == "uuid_hex": try: @@ -427,6 +434,7 @@ def convert_uuid(self, uuid_string, result=None): return result def transform(self, inputs: Sequence[Sequence[str]]) -> Sequence[str]: + """Trasnform""" result = [] if len(inputs) != 0: for collection in inputs: @@ -446,6 +454,7 @@ class UUIDVersion(TransformPlugin): """Outputs UUID version number""" def transform(self, inputs: Sequence[Sequence[str]]) -> Sequence[str]: + """Transform""" result = [] if len(inputs) != 0: for collection in inputs: diff --git a/cmem_plugin_uuid/utils.py b/cmem_plugin_uuid/utils.py index 492b75e..31d0b42 100644 --- a/cmem_plugin_uuid/utils.py +++ b/cmem_plugin_uuid/utils.py @@ -49,43 +49,40 @@ uuid_convert_param_out.allow_only_autocompleted_values = True -def node_to_int(node: str): - """Convert a string representation of a node byte array to an integer. - E.g. 01:23:45:67:89:AB -> 1250999896491 - """ +def node_to_int(node: str) -> int: + """Convert a string representation of a node byte array to an integer""" try: byte_string = node.replace(":", "").replace("-", "") byte_array = unhexlify(byte_string) return int.from_bytes(byte_array, byteorder="big", signed=False) - except Exception as exc: + except ValueError as exc: raise ValueError(f"node: {exc} ({node})") from exc -def clock_seq_to_int(clock_seq: str): +def clock_seq_to_int(clock_seq: str) -> int: """Convert a string representation of a clock_seq to an integer.""" try: return int(clock_seq) - except Exception as exc: + except ValueError as exc: raise ValueError(f"clock_seq: {exc} ({clock_seq})") from exc -def namespace_hex(value, uuid_version): - """Return hex string from input value - """ +def namespace_hex(value: str, uuid_version: int) -> str | None: + """Return hex string from input value""" hex_value = None - if uuid_version == 3: + if uuid_version == 3: # noqa: PLR2004 hex_value = md5(value.encode(), usedforsecurity=False).hexdigest() - elif uuid_version == 5: + elif uuid_version == 5: # noqa: PLR2004 hex_value = sha1(value.encode(), usedforsecurity=False).hexdigest()[:32] return hex_value def get_namespace_uuid( - namespace_as_uuid=None, - namespace=None, - uuid_version=None, -): - """Returns namespace UUID""" + namespace_as_uuid: bool | None, + namespace: str | None, + uuid_version: int | None, +) -> uuid.UUID | None: + """Return namespace UUID""" namespace_uuid = None if namespace == "namespace_url": diff --git a/tests/test_uuid.py b/tests/test_uuid.py index e556ea9..5b78487 100644 --- a/tests/test_uuid.py +++ b/tests/test_uuid.py @@ -20,7 +20,8 @@ # Test UUID1 -def test_uuid1_without_input(): +def test_uuid1_without_input() -> None: + """Test UUID1 without input""" result = UUID1( node=None, clock_seq=None, @@ -30,24 +31,26 @@ def test_uuid1_without_input(): assert uuid.UUID(item).version == 1 -def test_uuid1_with_value_input(): +def test_uuid1_with_value_input() -> None: + """Test UUID1 with value input""" input_values = [["input1"], ["input2"]] result = UUID1( node=None, clock_seq=None, ).transform(inputs=input_values) - assert len(result) == 2 + assert len(result) == 2 # noqa: PLR2004 for item in result: assert uuid.UUID(item).version == 1 -def test_uuid1_with_parameter_setting(): +def test_uuid1_with_parameter_setting() -> None: + """Test UUID1 with parameter setting""" input_values = [["input1"], ["input2"]] result = UUID1( node="2001", clock_seq="1234", ).transform(inputs=input_values) - assert len(result) == 2 + assert len(result) == 2 # noqa: PLR2004 for item in result: assert uuid.UUID(item).version == 1 @@ -55,41 +58,46 @@ def test_uuid1_with_parameter_setting(): # Test UUID3 -def test_uuid3_with_namespace_param_as_uuid(): +def test_uuid3_with_namespace_param_as_uuid() -> None: + """Test UUID3 with namespace parameter as UUID""" input_values = ["input1", "input2"] namespace = str(uuid.uuid1()) result = UUID3( namespace=namespace, namespace_as_uuid=True, ).transform(inputs=[[i] for i in input_values]) - assert len(result) == 2 + assert len(result) == 2 # noqa: PLR2004 for i, item in enumerate(result): - assert uuid.UUID(item).version == 3 + assert uuid.UUID(item).version == 3 # noqa: PLR2004 assert item == str(uuid.uuid3(namespace=uuid.UUID(namespace), name=input_values[i])) -def test_uuid3_with_namespace_param_as_string(): +def test_uuid3_with_namespace_param_as_string() -> None: + """Test UUID3 with namespace parameter as string""" input_values = ["input1", "input2"] namespace_str = "test" hex_value = md5(namespace_str.encode(), usedforsecurity=False).hexdigest() namespace = uuid.UUID(hex=hex_value, version=1) result = UUID3( namespace=namespace_str, + namespace_as_uuid=False, ).transform(inputs=[[i] for i in input_values]) - assert len(result) == 2 + assert len(result) == 2 # noqa: PLR2004 for i, item in enumerate(result): - assert uuid.UUID(item).version == 3 + assert uuid.UUID(item).version == 3 # noqa: PLR2004 assert item == str(uuid.uuid3(namespace=namespace, name=input_values[i])) -def test_uuid3_with_namespace_dns(): +def test_uuid3_with_namespace_dns() -> None: + """Test UUID3 with Namespace DNS""" input_values = ["input1", "input2"] result = UUID3( namespace="namespace_dns", + namespace_as_uuid=False, ).transform(inputs=[[i] for i in input_values]) - assert len(result) == 2 + assert len(result) == 2 # noqa: PLR2004 for i, item in enumerate(result): - assert uuid.UUID(item).version == 3 + assert uuid.UUID(item).version == 3 # noqa: PLR2004 assert item == str( uuid.uuid3( namespace=uuid.NAMESPACE_DNS, @@ -98,14 +106,16 @@ def test_uuid3_with_namespace_dns(): ) -def test_uuid3_with_namespace_url(): +def test_uuid3_with_namespace_url() -> None: + """Test UUID3 with Namespace URL""" input_values = ["input1", "input2"] result = UUID3( namespace="namespace_url", + namespace_as_uuid=False, ).transform(inputs=[[i] for i in input_values]) - assert len(result) == 2 + assert len(result) == 2 # noqa: PLR2004 for i, item in enumerate(result): - assert uuid.UUID(item).version == 3 + assert uuid.UUID(item).version == 3 # noqa: PLR2004 assert item == str( uuid.uuid3( namespace=uuid.NAMESPACE_URL, @@ -114,14 +124,16 @@ def test_uuid3_with_namespace_url(): ) -def test_uuid3_with_namespace_oid(): +def test_uuid3_with_namespace_oid() -> None: + """Test UUID3 with Namespace OID""" input_values = ["input1", "input2"] result = UUID3( namespace="namespace_oid", + namespace_as_uuid=False, ).transform(inputs=[[i] for i in input_values]) - assert len(result) == 2 + assert len(result) == 2 # noqa: PLR2004 for i, item in enumerate(result): - assert uuid.UUID(item).version == 3 + assert uuid.UUID(item).version == 3 # noqa: PLR2004 assert item == str( uuid.uuid3( namespace=uuid.NAMESPACE_OID, @@ -130,14 +142,16 @@ def test_uuid3_with_namespace_oid(): ) -def test_uuid3_with_namespace_x500(): +def test_uuid3_with_namespace_x500() -> None: + """Test UUID3 with Namespace X500""" input_values = ["input1", "input2"] result = UUID3( namespace="namespace_x500", + namespace_as_uuid=False, ).transform(inputs=[[i] for i in input_values]) - assert len(result) == 2 + assert len(result) == 2 # noqa: PLR2004 for i, item in enumerate(result): - assert uuid.UUID(item).version == 3 + assert uuid.UUID(item).version == 3 # noqa: PLR2004 assert item == str( uuid.uuid3( namespace=uuid.NAMESPACE_X500, @@ -146,10 +160,12 @@ def test_uuid3_with_namespace_x500(): ) -def test_uuid3_with_empty_namespace(): +def test_uuid3_with_empty_namespace() -> None: + """Test UUID3 with empty""" input_values = [["input1"]] result = UUID3( namespace="empty_value", + namespace_as_uuid=False, ).transform(inputs=input_values) assert len(result) == 1 assert result == [ @@ -165,57 +181,64 @@ def test_uuid3_with_empty_namespace(): # Test UUID4 -def test_uuid4_without_input(): +def test_uuid4_without_input() -> None: + """Test UUID4 without input""" result = UUID4().transform(inputs=[]) for item in result: - assert uuid.UUID(item).version == 4 + assert uuid.UUID(item).version == 4 # noqa: PLR2004 -def test_uuid4_with_input(): +def test_uuid4_with_input() -> None: + """Test UUID4 with input""" result = UUID4().transform(inputs=[["input1"], ["input2"]]) - assert len(result) == 2 + assert len(result) == 2 # noqa: PLR2004 for item in result: - assert uuid.UUID(item).version == 4 + assert uuid.UUID(item).version == 4 # noqa: PLR2004 # Test UUID5 -def test_uuid5_with_namespace_param_as_uuid(): +def test_uuid5_with_namespace_param_as_uuid() -> None: + """Test UUID5 with namespace parameter as UUID""" input_values = ["input1", "input2"] namespace = str(uuid.uuid1()) result = UUID5( namespace=namespace, namespace_as_uuid=True, ).transform(inputs=[[i] for i in input_values]) - assert len(result) == 2 + assert len(result) == 2 # noqa: PLR2004 for i, item in enumerate(result): - assert uuid.UUID(item).version == 5 + assert uuid.UUID(item).version == 5 # noqa: PLR2004 assert item == str(uuid.uuid5(namespace=uuid.UUID(namespace), name=input_values[i])) -def test_uuid5_with_namespace_param_as_string(): +def test_uuid5_with_namespace_param_as_string() -> None: + """Test UUID5 with namespace parameter as string""" input_values = ["input1", "input2"] namespace_str = "test" hex_value = sha1(namespace_str.encode(), usedforsecurity=False).hexdigest()[:32] namespace = uuid.UUID(hex=hex_value, version=1) result = UUID5( namespace=namespace_str, + namespace_as_uuid=False, ).transform(inputs=[[i] for i in input_values]) - assert len(result) == 2 + assert len(result) == 2 # noqa: PLR2004 for i, item in enumerate(result): - assert uuid.UUID(item).version == 5 + assert uuid.UUID(item).version == 5 # noqa: PLR2004 assert item == str(uuid.uuid5(namespace=namespace, name=input_values[i])) -def test_uuid5_with_namespace_dns(): +def test_uuid5_with_namespace_dns() -> None: + """Test UUID5 with Namespace DNS""" input_values = ["input1", "input2"] result = UUID5( namespace="namespace_dns", + namespace_as_uuid=False, ).transform(inputs=[[i] for i in input_values]) - assert len(result) == 2 + assert len(result) == 2 # noqa: PLR2004 for i, item in enumerate(result): - assert uuid.UUID(item).version == 5 + assert uuid.UUID(item).version == 5 # noqa: PLR2004 assert item == str( uuid.uuid5( namespace=uuid.NAMESPACE_DNS, @@ -224,14 +247,16 @@ def test_uuid5_with_namespace_dns(): ) -def test_uuid5_with_namespace_url(): +def test_uuid5_with_namespace_url() -> None: + """Test UUID5 with Namespace URL""" input_values = ["input1", "input2"] result = UUID5( namespace="namespace_url", + namespace_as_uuid=False, ).transform(inputs=[[i] for i in input_values]) - assert len(result) == 2 + assert len(result) == 2 # noqa: PLR2004 for i, item in enumerate(result): - assert uuid.UUID(item).version == 5 + assert uuid.UUID(item).version == 5 # noqa: PLR2004 assert item == str( uuid.uuid5( namespace=uuid.NAMESPACE_URL, @@ -240,14 +265,16 @@ def test_uuid5_with_namespace_url(): ) -def test_uuid5_with_namespace_oid(): +def test_uuid5_with_namespace_oid() -> None: + """Test UUID5 with Namespace OID""" input_values = ["input1", "input2"] result = UUID5( namespace="namespace_oid", + namespace_as_uuid=False, ).transform(inputs=[[i] for i in input_values]) - assert len(result) == 2 + assert len(result) == 2 # noqa: PLR2004 for i, item in enumerate(result): - assert uuid.UUID(item).version == 5 + assert uuid.UUID(item).version == 5 # noqa: PLR2004 assert item == str( uuid.uuid5( namespace=uuid.NAMESPACE_OID, @@ -256,14 +283,16 @@ def test_uuid5_with_namespace_oid(): ) -def test_uuid5_with_namespace_x500(): +def test_uuid5_with_namespace_x500() -> None: + """Test UUID5 with Namespace X500""" input_values = ["input1", "input2"] result = UUID5( namespace="namespace_x500", + namespace_as_uuid=False, ).transform(inputs=[[i] for i in input_values]) - assert len(result) == 2 + assert len(result) == 2 # noqa: PLR2004 for i, item in enumerate(result): - assert uuid.UUID(item).version == 5 + assert uuid.UUID(item).version == 5 # noqa: PLR2004 assert item == str( uuid.uuid5( namespace=uuid.NAMESPACE_X500, @@ -272,10 +301,12 @@ def test_uuid5_with_namespace_x500(): ) -def test_uuid5_with_empty_namespace(): +def test_uuid5_with_empty_namespace() -> None: + """Test UUID5 with empty namespace""" input_values = [["input1"]] result = UUID5( namespace="empty_value", + namespace_as_uuid=False, ).transform(inputs=input_values) assert len(result) == 1 assert result == [ @@ -291,122 +322,136 @@ def test_uuid5_with_empty_namespace(): # Test UUID6 -def test_uuid6_without_input(): +def test_uuid6_without_input() -> None: + """Test UUID6 without input""" result = UUID6( node=None, clock_seq=None, ).transform(inputs=[]) assert len(result) == 1 for item in result: - assert uuid.UUID(item).version == 6 + assert uuid.UUID(item).version == 6 # noqa: PLR2004 -def test_uuid6_with_value_input(): +def test_uuid6_with_value_input() -> None: + """Test UUID6 with value input""" input_values = [["input1"], ["input2"]] result = UUID6( node=None, clock_seq=None, ).transform(inputs=input_values) - assert len(result) == 2 + assert len(result) == 2 # noqa: PLR2004 for item in result: - assert uuid.UUID(item).version == 6 + assert uuid.UUID(item).version == 6 # noqa: PLR2004 -def test_uuid6_with_parameter_setting(): +def test_uuid6_with_parameter_setting() -> None: + """Test UUID6 with parameter setting""" input_values = [["input1"], ["input2"]] result = UUID6( node="2001", clock_seq="1234", ).transform(inputs=input_values) - assert len(result) == 2 + assert len(result) == 2 # noqa: PLR2004 for item in result: - assert uuid.UUID(item).version == 6 + assert uuid.UUID(item).version == 6 # noqa: PLR2004 # Test UUID1ToUUID6 -def test_uuid1_to_uuid6_with_input(): +def test_uuid1_to_uuid6_with_input() -> None: + """Test UUID1 to UUID6 with input""" input_values = [str(uuid.uuid1()), str(uuid.uuid1())] result = UUID1ToUUID6().transform(inputs=[[i] for i in input_values]) - assert len(result) == 2 + assert len(result) == 2 # noqa: PLR2004 for i, item in enumerate(result): - assert uuid.UUID(item).version == 6 + assert uuid.UUID(item).version == 6 # noqa: PLR2004 assert item == str(uuid6.uuid1_to_uuid6(uuid.UUID(input_values[i]))) # Test UUID7 -def test_uuid7_without_input(): +def test_uuid7_without_input() -> None: + """Test UUID7 without input""" result = UUID7().transform(inputs=[]) assert len(result) == 1 for item in result: - assert uuid.UUID(item).version == 7 + assert uuid.UUID(item).version == 7 # noqa: PLR2004 -def test_uuid7_with_input(): +def test_uuid7_with_input() -> None: + """Test UUID7 with input""" result = UUID7().transform(inputs=[["input1"], ["input2"]]) - assert len(result) == 2 + assert len(result) == 2 # noqa: PLR2004 for item in result: - assert uuid.UUID(item).version == 7 + assert uuid.UUID(item).version == 7 # noqa: PLR2004 # Test UUID8 -def test_uuid8_without_input(): +def test_uuid8_without_input() -> None: + """Test UUID8 without input""" result = UUID8().transform(inputs=[]) for item in result: - assert uuid.UUID(item).version == 8 + assert uuid.UUID(item).version == 8 # noqa: PLR2004 -def test_uuid8_with_input(): +def test_uuid8_with_input() -> None: + """Test UUID8 without input""" result = UUID8().transform(inputs=[["input1"], ["input2"]]) - assert len(result) == 2 + assert len(result) == 2 # noqa: PLR2004 for item in result: - assert uuid.UUID(item).version == 8 + assert uuid.UUID(item).version == 8 # noqa: PLR2004 # Test UUIDConvert -def test_uuid_convert_to_uuid(): +def test_uuid_convert_to_uuid() -> None: + """Test UUID Convert to UUID""" test_uuid = uuid.uuid4() input_values = [str(test_uuid)] result = UUIDConvert(from_format="uuid_hex", to_format="uuid").transform(inputs=[input_values]) assert result == [str(test_uuid)] -def test_uuid_convert_to_hex(): +def test_uuid_convert_to_hex() -> None: + """Test UUID Convert to hex""" test_uuid = uuid.uuid4() input_values = [str(test_uuid)] result = UUIDConvert(from_format="uuid_hex", to_format="hex").transform(inputs=[input_values]) assert result == [str(test_uuid.hex)] -def test_uuid_convert_to_int(): +def test_uuid_convert_to_int() -> None: + """Test UUID Convert to int""" test_uuid = uuid.uuid4() input_values = [str(test_uuid)] result = UUIDConvert(from_format="uuid_hex", to_format="int").transform(inputs=[input_values]) assert result == [str(test_uuid.int)] -def test_uuid_convert_to_urn(): +def test_uuid_convert_to_urn() -> None: + """Test UUID Convert to URN""" test_uuid = uuid.uuid4() input_values = [str(test_uuid)] result = UUIDConvert(from_format="uuid_hex", to_format="urn").transform(inputs=[input_values]) assert result == [str(test_uuid.urn)] -def test_uuid_convert_from_int(): +def test_uuid_convert_from_int() -> None: + """Test UUID Convert from int""" test_uuid = uuid.uuid4() input_values = [str(test_uuid.int)] result = UUIDConvert(from_format="int", to_format="uuid").transform(inputs=[input_values]) assert result == [str(test_uuid)] -def test_uuid_convert_from_urn(): +def test_uuid_convert_from_urn() -> None: + """Test UUID Convert from URN""" test_uuid = uuid.uuid4() input_values = [str(test_uuid.urn)] result = UUIDConvert(from_format="urn", to_format="uuid").transform(inputs=[input_values]) @@ -416,7 +461,8 @@ def test_uuid_convert_from_urn(): # Test UUIDVersion -def test_uuid_version(): +def test_uuid_version() -> None: + """Test UUID Version""" input_values = [ str(uuid.uuid1()), str(uuid.uuid3(name="test", namespace=uuid.NAMESPACE_URL)), @@ -428,5 +474,5 @@ def test_uuid_version(): str(uuid6.uuid8()), ] result = UUIDVersion().transform(inputs=[input_values]) - assert len(result) == 8 + assert len(result) == 8 # noqa: PLR2004 assert result == [str(i) for i in [1, 3, 4, 5, 6, 6, 7, 8]]