From c6a461160b169b8ffc5a7a187675e0787b91c6e8 Mon Sep 17 00:00:00 2001 From: iron3oxide Date: Mon, 13 May 2024 21:01:16 +0200 Subject: [PATCH 1/4] fix: Improve type prefix and suffix extraction Refactored the logic in `get_prefix_and_suffix` function to improve type prefix and suffix extraction. Now using `rsplit` method to accurately extract the prefix and suffix from the input string. Closes #12 --- typeid/typeid.py | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/typeid/typeid.py b/typeid/typeid.py index f0f5da6..111243f 100644 --- a/typeid/typeid.py +++ b/typeid/typeid.py @@ -80,15 +80,12 @@ def from_uuid(suffix: uuid6.UUID, prefix: Optional[str] = None) -> TypeID: def get_prefix_and_suffix(string: str) -> tuple: - parts = string.split("_") - prefix = None + parts = string.rsplit("_", 1) if len(parts) == 1: + prefix = None suffix = parts[0] - elif len(parts) == 2 and parts[0] != "": - suffix = parts[1] - prefix = parts[0] else: - raise InvalidTypeIDStringException(f"Invalid TypeID: {string}") + prefix, suffix = parts return prefix, suffix From db68acea732500ab8bb49ec81298edf281ac0d48 Mon Sep 17 00:00:00 2001 From: iron3oxide Date: Tue, 14 May 2024 14:58:01 +0200 Subject: [PATCH 2/4] refactor: Update exception name in test_construct_type_from_invalid_string in test_typeid.py --- tests/test_typeid.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/test_typeid.py b/tests/test_typeid.py index 2f4dd26..1cf6d70 100644 --- a/tests/test_typeid.py +++ b/tests/test_typeid.py @@ -2,7 +2,7 @@ import uuid6 from typeid import TypeID -from typeid.errors import InvalidTypeIDStringException +from typeid.errors import SuffixValidationException def test_default_suffix() -> None: @@ -87,7 +87,7 @@ def test_construct_type_from_string_with_prefix_standalone() -> None: def test_construct_type_from_invalid_string() -> None: string = "invalid_string_to_typeid" - with pytest.raises(InvalidTypeIDStringException): + with pytest.raises(SuffixValidationException): TypeID.from_string(string) From 76f185b195229d223e9827678bb6237645e7e07f Mon Sep 17 00:00:00 2001 From: iron3oxide Date: Tue, 14 May 2024 14:58:04 +0200 Subject: [PATCH 3/4] feat: Add validation for empty prefix in get_prefix_and_suffix function. --- typeid/typeid.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/typeid/typeid.py b/typeid/typeid.py index 111243f..f0c6851 100644 --- a/typeid/typeid.py +++ b/typeid/typeid.py @@ -84,6 +84,8 @@ def get_prefix_and_suffix(string: str) -> tuple: if len(parts) == 1: prefix = None suffix = parts[0] + elif len(parts) == 2 and parts[0] == "": + raise InvalidTypeIDStringException(f"Invalid TypeID: {string}") else: prefix, suffix = parts From e25ac6400b6b5fe53dd8f29a908d3776fa131aa3 Mon Sep 17 00:00:00 2001 From: iron3oxide Date: Tue, 14 May 2024 15:15:20 +0200 Subject: [PATCH 4/4] feat: Add test for constructing type from string with multi underscore prefix --- tests/test_typeid.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/tests/test_typeid.py b/tests/test_typeid.py index 2f4dd26..a5caedc 100644 --- a/tests/test_typeid.py +++ b/tests/test_typeid.py @@ -84,6 +84,16 @@ def test_construct_type_from_string_with_prefix_standalone() -> None: assert isinstance(typeid.suffix, str) +def test_construct_type_from_string_with_multi_underscore_prefix() -> None: + string = "double_prefix_00041061050r3gg28a1c60t3gf" + + typeid = TypeID.from_string(string) + + assert isinstance(typeid, TypeID) + assert typeid.prefix == "double_prefix" + assert isinstance(typeid.suffix, str) + + def test_construct_type_from_invalid_string() -> None: string = "invalid_string_to_typeid"