diff --git a/tests/test_spec.py b/tests/test_spec.py index 1165d86..ec47320 100644 --- a/tests/test_spec.py +++ b/tests/test_spec.py @@ -1,13 +1,13 @@ import pytest from uuid6 import UUID -from typeid import from_string, from_uuid +from typeid import TypeID def test_invalid_spec(invalid_spec: list) -> None: for spec in invalid_spec: with pytest.raises(Exception): - from_string(spec["typeid"]) + TypeID.from_string(spec["typeid"]) def test_valid_spec(valid_spec: list) -> None: @@ -15,5 +15,5 @@ def test_valid_spec(valid_spec: list) -> None: prefix = spec["prefix"] uuid = UUID(spec["uuid"]) - typeid = from_uuid(prefix=prefix, suffix=uuid) + typeid = TypeID.from_uuid(prefix=prefix, suffix=uuid) assert str(typeid) == spec["typeid"] diff --git a/tests/test_typeid.py b/tests/test_typeid.py index 60adb31..2f4dd26 100644 --- a/tests/test_typeid.py +++ b/tests/test_typeid.py @@ -1,7 +1,7 @@ import pytest -from uuid6 import uuid7 +import uuid6 -from typeid import TypeID, from_string, from_uuid +from typeid import TypeID from typeid.errors import InvalidTypeIDStringException @@ -57,7 +57,7 @@ def test_construct_type_from_string() -> None: def test_construct_type_from_string_standalone() -> None: string = "00041061050r3gg28a1c60t3gf" - typeid = from_string(string) + typeid = TypeID.from_string(string) assert isinstance(typeid, TypeID) assert typeid.prefix == "" @@ -77,7 +77,7 @@ def test_construct_type_from_string_with_prefix() -> None: def test_construct_type_from_string_with_prefix_standalone() -> None: string = "prefix_00041061050r3gg28a1c60t3gf" - typeid = from_string(string) + typeid = TypeID.from_string(string) assert isinstance(typeid, TypeID) assert typeid.prefix == "prefix" @@ -88,13 +88,13 @@ def test_construct_type_from_invalid_string() -> None: string = "invalid_string_to_typeid" with pytest.raises(InvalidTypeIDStringException): - from_string(string) + TypeID.from_string(string) def test_construct_type_from_uuid() -> None: - uuid = uuid7() + uuid = uuid6.uuid7() - typeid = from_uuid(suffix=uuid, prefix="") + typeid = TypeID.from_uuid(suffix=uuid, prefix="") assert isinstance(typeid, TypeID) assert typeid.prefix == "" @@ -102,10 +102,10 @@ def test_construct_type_from_uuid() -> None: def test_construct_type_from_uuid_with_prefix() -> None: - uuid = uuid7() + uuid = uuid6.uuid7() prefix = "prefix" - typeid = from_uuid(prefix=prefix, suffix=uuid) + typeid = TypeID.from_uuid(prefix=prefix, suffix=uuid) assert isinstance(typeid, TypeID) assert typeid.prefix == "prefix" @@ -122,3 +122,13 @@ def test_hash_type_id() -> None: assert hash(typeid_1) == hash(typeid_2) assert hash(typeid_3) != hash(typeid_1) + + +def test_uuid_property() -> None: + uuid = uuid6.uuid7() + + typeid = TypeID.from_uuid(suffix=uuid) + + assert isinstance(typeid.uuid, uuid6.UUID) + assert typeid.uuid.version == uuid.version == 7 + assert typeid.uuid.time == uuid.time diff --git a/typeid/typeid.py b/typeid/typeid.py index 90e6784..f0f5da6 100644 --- a/typeid/typeid.py +++ b/typeid/typeid.py @@ -1,8 +1,7 @@ import warnings from typing import Optional -from uuid import UUID -from uuid6 import uuid7 +import uuid6 from typeid import base32 from typeid.errors import InvalidTypeIDStringException @@ -11,7 +10,7 @@ class TypeID: def __init__(self, prefix: Optional[str] = None, suffix: Optional[str] = None) -> None: - suffix = _convert_uuid_to_b32(uuid7()) if not suffix else suffix + suffix = _convert_uuid_to_b32(uuid6.uuid7()) if not suffix else suffix validate_suffix(suffix=suffix) if prefix: validate_prefix(prefix=prefix) @@ -25,9 +24,9 @@ def from_string(cls, string: str): return cls(suffix=suffix, prefix=prefix) @classmethod - def from_uuid(cls, suffix: UUID, prefix: Optional[str] = None): + def from_uuid(cls, suffix: uuid6.UUID, prefix: Optional[str] = None): suffix_str = _convert_uuid_to_b32(suffix) - return TypeID(suffix=suffix_str, prefix=prefix) + return cls(suffix=suffix_str, prefix=prefix) @property def suffix(self) -> str: @@ -38,7 +37,7 @@ def prefix(self) -> str: return self._prefix @property - def uuid(self) -> UUID: + def uuid(self) -> uuid6.UUID: return _convert_b32_to_uuid(self.suffix) def __str__(self) -> str: @@ -75,14 +74,13 @@ def from_string(string: str) -> TypeID: return TypeID.from_string(string=string) -def from_uuid(suffix: UUID, prefix: Optional[str] = None) -> TypeID: +def from_uuid(suffix: uuid6.UUID, prefix: Optional[str] = None) -> TypeID: warnings.warn("Consider TypeID.from_uuid instead.", DeprecationWarning) return TypeID.from_uuid(suffix=suffix, prefix=prefix) def get_prefix_and_suffix(string: str) -> tuple: parts = string.split("_") - suffix = None prefix = None if len(parts) == 1: suffix = parts[0] @@ -95,9 +93,11 @@ def get_prefix_and_suffix(string: str) -> tuple: return prefix, suffix -def _convert_uuid_to_b32(uuid_instance: UUID) -> str: +def _convert_uuid_to_b32(uuid_instance: uuid6.UUID) -> str: return base32.encode(list(uuid_instance.bytes)) -def _convert_b32_to_uuid(b32: str) -> UUID: - return UUID(bytes=bytes(base32.decode(b32))) +def _convert_b32_to_uuid(b32: str) -> uuid6.UUID: + uuid_bytes = bytes(base32.decode(b32)) + uuid_int = int.from_bytes(uuid_bytes, byteorder="big") + return uuid6.UUID(int=uuid_int, version=7)