source: flexoentity/tests/test_id_stress.py@ 8a238e2

Last change on this file since 8a238e2 was 8a238e2, checked in by Enrico Schwass <ennoausberlin@…>, 3 months ago

skip some tests due to missing correct serialized entities

  • Property mode set to 100644
File size: 3.3 KB
Line 
1"""
2Stress tests for the Flex-O ID lifecycle.
3Focus: collision avoidance, version ceiling, reproducibility.
4"""
5import pytest
6import random
7
8from flexoentity import FlexOID, EntityType, EntityState
9
10from tests.conftest import DummyEntity
11
12# ──────────────────────────────────────────────────────────────────────────────
13def test_bulk_generation_uniqueness():
14 """Generate 10,000 IDs and assert uniqueness (statistical test)."""
15 domain = "AF"
16 etype = EntityType.QUESTION
17 estate = EntityState.DRAFT
18 seeds = [f"question {i}" for i in range(10_000)]
19
20 ids = [FlexOID.generate(domain, etype, estate, seed) for seed in seeds]
21
22 assert len(ids) == len(set(ids)), "ID collisions detected in bulk generation"
23
24
25def test_disambiguator_trigger():
26 """
27 Generating the same entity twice with same inputs yields identical ID.
28 (No runtime disambiguation; IDs are deterministic by design.)
29 """
30 domain = "AF"
31 etype = EntityType.QUESTION
32 estate = EntityState.DRAFT
33 text = "identical question text"
34 id1 = FlexOID.generate(domain, etype, estate, text)
35 id2 = FlexOID.generate(domain, etype, estate, text)
36 # IDs must be identical, because we now enforce determinism, not randomization
37 assert id1 == id2
38 assert id1.signature == id2.signature
39
40
41def test_id_reproducibility_across_runs():
42 """
43 The same seed on a new process (fresh _seen_hashes)
44 should yield the same base ID (without suffix).
45 """
46 domain = "AF"
47 etype = EntityType.CATALOG
48 estate = EntityState.DRAFT
49 seed = "reproducibility test seed"
50 id1 = FlexOID.generate(domain, etype, estate, seed)
51 # Reset hash cache
52 FlexOID._seen_hashes.clear()
53 id2 = FlexOID.generate(domain, etype, estate, seed)
54 assert id1 == id2
55 assert id1.signature == id2.signature
56
57
58def test_version_ceiling_enforcement():
59 """Simulate approaching @999 to trigger obsolescence guard."""
60 entity = DummyEntity(domain="AF", etype=EntityType.EXAM, state=EntityState.DRAFT, seed="Final Exam 2025")
61 entity.approve()
62 # artificially bump version number to near ceiling
63 entity.flexo_id = FlexOID.from_oid_and_version(entity.flexo_id, 998)
64
65 # 998 → 999 is allowed
66 entity.sign()
67 assert entity.flexo_id.version == 999
68
69 # 999 → 1000 should raise RuntimeError
70 with pytest.raises(RuntimeError):
71 entity.sign()
72
73
74def test_massive_lifecycle_simulation():
75 """
76 Generate 100 random entities, simulate multiple edits and state transitions,
77 ensure all final IDs and fingerprints are unique and valid.
78 """
79 entities = [DummyEntity(domain="AF", etype=EntityType.QUESTION, state=EntityState.DRAFT, seed=f"random question {i}") for i in range(100)]
80
81 for e in entities:
82 # random edit, approval, signing
83 e._seed += " updated"
84 e._update_fingerprint()
85 e.approve()
86 if random.random() > 0.3:
87 e.sign()
88 if random.random() > 0.6:
89 e.publish()
90
91 ids = [e.flexo_id for e in entities]
92 fps = [e.flexo_id.signature for e in entities]
93 assert len(ids) == len(set(ids)), "Duplicate IDs after random lifecycle"
94 assert len(fps) == len(set(fps)), "Duplicate fingerprints after random lifecycle"
Note: See TracBrowser for help on using the repository browser.