Skip to content

Commit

Permalink
feat: enforce unique non-empty prefixes in TypeIDMixin
Browse files Browse the repository at this point in the history
Generated-by: aiautocommit
  • Loading branch information
iloveitaly committed Dec 13, 2024
1 parent c7445c7 commit a167293
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
9 changes: 9 additions & 0 deletions activemodel/mixins/typeid.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,23 @@

from activemodel.types.typeid import TypeIDType

_prefixes = []


def TypeIDMixin(prefix: str):
assert prefix
assert (
prefix not in _prefixes
), f"prefix {prefix} already exists, pick a different one"

class _TypeIDMixin:
id: uuid.UUID = Field(
sa_column=Column(TypeIDType(prefix), primary_key=True),
default_factory=lambda: TypeID(prefix),
)

_prefixes.append(prefix)

return _TypeIDMixin


Expand Down
14 changes: 14 additions & 0 deletions test/typeid_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import pytest
from activemodel.mixins import TypeIDMixin


def test_enforces_unique_prefixes():
TypeIDMixin("hi")

with pytest.raises(AssertionError):
TypeIDMixin("hi")


def test_no_empty_prefixes_test():
with pytest.raises(AssertionError):
TypeIDMixin("")

0 comments on commit a167293

Please sign in to comment.