Changeset 02d288d in flexoentity for tests/test_persistance_integrity.py
- Timestamp:
- 10/23/25 13:27:08 (3 months ago)
- Branches:
- master
- Children:
- 4ceca57
- Parents:
- 6a7dec1
- File:
-
- 1 edited
-
tests/test_persistance_integrity.py (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
-
tests/test_persistance_integrity.py
r6a7dec1 r02d288d 6 6 import pytest 7 7 8 from flexoentity import FlexOID, EntityType,EntityState9 from tests.conftest import DummyEntity8 from flexoentity import EntityState 9 from builder.questions import RadioQuestion, AnswerOption 10 10 11 11 12 12 # ────────────────────────────────────────────────────────────────────────────── 13 13 @pytest.fixture 14 def approved_ entity():15 """ A fully published dummy entityfor persistence tests."""16 e = DummyEntity(17 domain= "AF",18 etype= EntityType.QUESTION,14 def approved_question(domain): 15 """Provide a fully approved and published RadioQuestion for persistence tests.""" 16 q = RadioQuestion( 17 domain=domain, 18 etype=None, # RadioQuestion sets this internally to EntityType.QUESTION 19 19 state=EntityState.DRAFT, 20 seed="What is Ohm’s law?" 20 text="What is Ohm’s law?", 21 options=[ 22 AnswerOption(text="U = R × I", points=1), 23 AnswerOption(text="U = I / R", points=0), 24 AnswerOption(text="R = U × I", points=0), 25 ], 21 26 ) 22 e.approve()23 e.sign()24 e.publish()25 return e27 q.approve() 28 q.sign() 29 q.publish() 30 return q 26 31 27 @pytest.mark.skip(reason="FlexOIDs are regenerated on import; enable once JSON format is stable") 28 def test_json_roundtrip_preserves_integrity(approved_entity): 32 33 @pytest.mark.skip(reason="FlexOIDs regenerated on import; enable once JSON format is stable") 34 def test_json_roundtrip_preserves_integrity(approved_question): 29 35 """ 30 Export to JSON and reload — ensure fingerprints remain valid.36 Export to JSON and reload — ensure fingerprints and signatures remain valid. 31 37 """ 32 json_str = approved_ entity.to_json()33 loaded = approved_entity.__class__.from_json(json_str)38 json_str = approved_question.to_json() 39 loaded = RadioQuestion.from_json(json_str) 34 40 35 41 # Fingerprint and state should match — integrity must pass 36 assert approved_entity.__class__.verify_integrity(loaded)42 assert RadioQuestion.verify_integrity(loaded) 37 43 38 44 # Metadata should be preserved exactly 39 assert approved_entity.flexo_id.signature == loaded.flexo_id.signature 40 assert approved_entity.flexo_id == loaded.flexo_id 41 assert loaded.state == approved_entity.state 45 assert approved_question.signature == loaded.signature 46 assert approved_question.flexo_id == loaded.flexo_id 47 assert loaded.state == approved_question.state 48 42 49 43 50 # ────────────────────────────────────────────────────────────────────────────── 44 51 45 @pytest.mark.skip(reason="FlexOIDs regenerated on import; tampering detection not applicable yet")46 def test_json_tampering_detection(approved_ entity):52 @pytest.mark.skip(reason="FlexOIDs regenerated on import; tampering detection not yet implemented") 53 def test_json_tampering_detection(approved_question): 47 54 """Tampering with content should invalidate fingerprint verification.""" 48 json_str = approved_ entity.to_json()49 tampered _data= json.loads(json_str)50 tampered _data["text_seed"] = "Tampered content injection"51 tampered_json = json.dumps(tampered _data)55 json_str = approved_question.to_json() 56 tampered = json.loads(json_str) 57 tampered["text"] = "Tampered content injection" 58 tampered_json = json.dumps(tampered) 52 59 53 # We use DummyEntity.from_json to reconstruct (FlexoEntity is abstract) 54 loaded = approved_entity.__class__.from_json(tampered_json) 55 assert not approved_entity.__class__.verify_integrity(loaded) 60 loaded = RadioQuestion.from_json(tampered_json) 61 assert not RadioQuestion.verify_integrity(loaded) 56 62 57 63 … … 59 65 60 66 @pytest.mark.skip(reason="FlexOIDs regenerated on import; corruption detection not yet applicable") 61 def test_json_file_corruption(approved_ entity, tmp_path):67 def test_json_file_corruption(approved_question, tmp_path): 62 68 """Simulate file corruption — integrity check must fail.""" 63 file = tmp_path / " entity.json"64 json_str = approved_ entity.to_json()69 file = tmp_path / "question.json" 70 json_str = approved_question.to_json() 65 71 file.write_text(json_str) 66 72 67 # Corrupt the file 73 # Corrupt the file (simulate accidental byte modification) 68 74 corrupted = json_str.replace("Ohm’s", "Omm’s") 69 75 file.write_text(corrupted) 70 76 71 loaded = approved_entity.__class__.from_json(file.read_text())72 assert not approved_entity.__class__.verify_integrity(loaded)77 loaded = RadioQuestion.from_json(file.read_text()) 78 assert not RadioQuestion.verify_integrity(loaded)
Note:
See TracChangeset
for help on using the changeset viewer.
