Skip to content

Commit

Permalink
Merge pull request #10 from attevaltojarvi/main
Browse files Browse the repository at this point in the history
Improvements and fixes
  • Loading branch information
akhundMurad authored Apr 19, 2024
2 parents 511582e + 2762c85 commit 8c702ca
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 23 deletions.
6 changes: 3 additions & 3 deletions tests/test_spec.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
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:
for spec in valid_spec:
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"]
28 changes: 19 additions & 9 deletions tests/test_typeid.py
Original file line number Diff line number Diff line change
@@ -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


Expand Down Expand Up @@ -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 == ""
Expand All @@ -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"
Expand All @@ -88,24 +88,24 @@ 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 == ""
assert isinstance(typeid.suffix, str)


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"
Expand All @@ -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
22 changes: 11 additions & 11 deletions typeid/typeid.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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)
Expand All @@ -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:
Expand All @@ -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:
Expand Down Expand Up @@ -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]
Expand All @@ -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)

0 comments on commit 8c702ca

Please sign in to comment.