| 1 | import pytest
|
|---|
| 2 | from flexoentity import Domain, DomainManager, DuplicateDomainError
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 | # ---------------------------------------------------------------
|
|---|
| 6 | # Setup/Teardown (clear DomainManager between tests)
|
|---|
| 7 | # ---------------------------------------------------------------
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 | @pytest.fixture(autouse=True)
|
|---|
| 11 | def clear_domain_manager(sample_domain_manager):
|
|---|
| 12 | sample_domain_manager.clear()
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 | # ---------------------------------------------------------------
|
|---|
| 16 | # Domain creation + registration
|
|---|
| 17 | # ---------------------------------------------------------------
|
|---|
| 18 | def test_domain_registration(sample_domain_manager, sample_domain):
|
|---|
| 19 |
|
|---|
| 20 | sample_domain_manager.add(sample_domain)
|
|---|
| 21 | # Manager must know it now
|
|---|
| 22 | assert sample_domain_manager.get_by_domain_id("PY_ARITHM") is sample_domain
|
|---|
| 23 |
|
|---|
| 24 |
|
|---|
| 25 | # ---------------------------------------------------------------
|
|---|
| 26 | # Uniqueness: registering the same code twice must fail
|
|---|
| 27 | # ---------------------------------------------------------------
|
|---|
| 28 | def test_domain_uniqueness_enforced(sample_domain, sample_domain_manager):
|
|---|
| 29 |
|
|---|
| 30 | sample_domain_manager.add(sample_domain)
|
|---|
| 31 | with pytest.raises(DuplicateDomainError):
|
|---|
| 32 | sample_domain_manager.add(sample_domain)
|
|---|
| 33 |
|
|---|
| 34 |
|
|---|
| 35 | # ---------------------------------------------------------------
|
|---|
| 36 | # Lookup by FlexOID
|
|---|
| 37 | # ---------------------------------------------------------------
|
|---|
| 38 | def test_lookup_by_oid(sample_domain, sample_domain_manager):
|
|---|
| 39 | sample_domain_manager.add(sample_domain)
|
|---|
| 40 | found = sample_domain_manager.get_by_domain_id(sample_domain.domain_id)
|
|---|
| 41 | assert found is sample_domain
|
|---|
| 42 |
|
|---|
| 43 | # ---------------------------------------------------------------
|
|---|
| 44 | # JSON roundtrip should preserve identity and not regenerate FlexOID
|
|---|
| 45 | # ---------------------------------------------------------------
|
|---|
| 46 | def test_domain_json_roundtrip(sample_domain):
|
|---|
| 47 | sample_data = sample_domain.to_dict()
|
|---|
| 48 | loaded = Domain.from_dict(sample_data)
|
|---|
| 49 |
|
|---|
| 50 | # Same exact instance
|
|---|
| 51 | assert loaded == sample_domain
|
|---|
| 52 |
|
|---|
| 53 | # Same FlexOID
|
|---|
| 54 | assert str(loaded.flexo_id) == str(sample_domain.flexo_id)
|
|---|
| 55 |
|
|---|