| 1 | import pytest
|
|---|
| 2 | from uuid import UUID
|
|---|
| 3 | from flexoentity import FlexOID, EntityType
|
|---|
| 4 | from flexoentity.domain import Domain
|
|---|
| 5 | from flexoentity.domain_manager import DomainManager
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 | # ---------------------------------------------------------------
|
|---|
| 9 | # Setup/Teardown (clear DomainManager between tests)
|
|---|
| 10 | # ---------------------------------------------------------------
|
|---|
| 11 | @pytest.fixture(autouse=True)
|
|---|
| 12 | def clear_domain_manager():
|
|---|
| 13 | DomainManager.clear()
|
|---|
| 14 | yield
|
|---|
| 15 | DomainManager.clear()
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 | # ---------------------------------------------------------------
|
|---|
| 19 | # Domain creation + registration
|
|---|
| 20 | # ---------------------------------------------------------------
|
|---|
| 21 | def test_domain_creation_and_registration():
|
|---|
| 22 | d = DomainManager.create(
|
|---|
| 23 | domain_id="PY_ARITHM",
|
|---|
| 24 | fullname="Python Arithmetic",
|
|---|
| 25 | description="Basic arithmetic operations",
|
|---|
| 26 | classification="UNCLASSIFIED",
|
|---|
| 27 | )
|
|---|
| 28 |
|
|---|
| 29 | assert d.domain_id == "PY_ARITHM"
|
|---|
| 30 | assert d.entity_type == EntityType.DOMAIN
|
|---|
| 31 | assert isinstance(d.flexo_id, FlexOID)
|
|---|
| 32 |
|
|---|
| 33 | # Manager must know it now
|
|---|
| 34 | assert DomainManager.get("PY_ARITHM") is d
|
|---|
| 35 |
|
|---|
| 36 |
|
|---|
| 37 | # ---------------------------------------------------------------
|
|---|
| 38 | # Uniqueness: registering the same code twice must fail
|
|---|
| 39 | # ---------------------------------------------------------------
|
|---|
| 40 | def test_domain_uniqueness_enforced():
|
|---|
| 41 | DomainManager.create("AF")
|
|---|
| 42 |
|
|---|
| 43 | with pytest.raises(ValueError):
|
|---|
| 44 | DomainManager.create("AF") # duplicate code rejected
|
|---|
| 45 |
|
|---|
| 46 |
|
|---|
| 47 | # ---------------------------------------------------------------
|
|---|
| 48 | # Lookup by FlexOID
|
|---|
| 49 | # ---------------------------------------------------------------
|
|---|
| 50 | def test_lookup_by_oid():
|
|---|
| 51 | d = DomainManager.create("PY_ARITHM")
|
|---|
| 52 | found = DomainManager.get_by_oid(d.flexo_id)
|
|---|
| 53 | assert found is d
|
|---|
| 54 |
|
|---|
| 55 |
|
|---|
| 56 | # ---------------------------------------------------------------
|
|---|
| 57 | # JSON roundtrip should preserve identity and not regenerate FlexOID
|
|---|
| 58 | # ---------------------------------------------------------------
|
|---|
| 59 | def test_domain_json_roundtrip():
|
|---|
| 60 | orig = DomainManager.create(
|
|---|
| 61 | domain_id="ELEKTRO_BASICS",
|
|---|
| 62 | fullname="Elektronik Grundlagen",
|
|---|
| 63 | )
|
|---|
| 64 |
|
|---|
| 65 | data = orig.to_dict()
|
|---|
| 66 | loaded = Domain.from_dict(data)
|
|---|
| 67 |
|
|---|
| 68 | # Same exact instance
|
|---|
| 69 | assert loaded == orig
|
|---|
| 70 |
|
|---|
| 71 | # Same FlexOID
|
|---|
| 72 | assert str(loaded.flexo_id) == str(orig.flexo_id)
|
|---|
| 73 |
|
|---|
| 74 |
|
|---|
| 75 | # ---------------------------------------------------------------
|
|---|
| 76 | # Loading a domain from dict that was not previously registered
|
|---|
| 77 | # ---------------------------------------------------------------
|
|---|
| 78 | def test_domain_restore_new_from_dict():
|
|---|
| 79 | d = Domain.with_domain_id(
|
|---|
| 80 | domain_id="PY_LOOPS",
|
|---|
| 81 | fullname="Python Loops",
|
|---|
| 82 | classification="UNCLASSIFIED",
|
|---|
| 83 | )
|
|---|
| 84 |
|
|---|
| 85 | serialized = d.to_dict()
|
|---|
| 86 |
|
|---|
| 87 | # Simulate fresh startup — no domains registered
|
|---|
| 88 | DomainManager.clear()
|
|---|
| 89 |
|
|---|
| 90 | restored = Domain.from_dict(serialized)
|
|---|
| 91 | assert restored.domain_id == "PY_LOOPS"
|
|---|
| 92 | assert str(restored.flexo_id) == str(d.flexo_id)
|
|---|
| 93 |
|
|---|
| 94 | # Manager must now contain it
|
|---|
| 95 | # assert DomainManager.get("PY_LOOPS") == restored
|
|---|
| 96 |
|
|---|