Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Improve type prefix and suffix extraction #13

Merged
merged 7 commits into from
Jun 16, 2024
14 changes: 12 additions & 2 deletions tests/test_typeid.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -84,10 +84,20 @@ 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"

with pytest.raises(InvalidTypeIDStringException):
with pytest.raises(SuffixValidationException):
TypeID.from_string(string)


Expand Down
11 changes: 5 additions & 6 deletions typeid/typeid.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,15 +80,14 @@ 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)
akhundMurad marked this conversation as resolved.
Show resolved Hide resolved
if len(parts) == 1:
prefix = None
suffix = parts[0]
elif len(parts) == 2 and parts[0] != "":
suffix = parts[1]
prefix = parts[0]
else:
elif len(parts) == 2 and parts[0] == "":
raise InvalidTypeIDStringException(f"Invalid TypeID: {string}")
else:
prefix, suffix = parts
akhundMurad marked this conversation as resolved.
Show resolved Hide resolved

return prefix, suffix

Expand Down
Loading