Changeset ef964d8 in flexoentity for tests/test_persistance_integrity.py


Ignore:
Timestamp:
11/27/25 18:12:23 (7 weeks ago)
Author:
Enrico Schwass <ennoausberlin@…>
Branches:
master
Children:
4e11d58
Parents:
9a50e0b
Message:

new serialization structure adopted and tests fixed

File:
1 edited

Legend:

Unmodified
Added
Removed
  • tests/test_persistance_integrity.py

    r9a50e0b ref964d8  
    66import pytest
    77
    8 from flexoentity import EntityState, EntityType, Domain
     8from flexoentity import Domain
     9
    910
    1011@pytest.fixture
    11 def approved_question():
    12     """Provide a fully approved and published SingleChoiceQuestion for persistence tests."""
    13     q = SingleChoiceQuestion(
    14         domain=Domain(domain="GEN", entity_type=EntityType.DOMAIN, state=EntityState.DRAFT),
    15         entity_type=None,  # SingleChoiceQuestion sets this internally to EntityType.ITEM
    16         state=EntityState.DRAFT,
    17         text="What is Ohm’s law?",
    18         options=[
    19             AnswerOption(id="OP1", text="U = R × I", points=1),
    20             AnswerOption(id="OP2", text="U = I / R", points=0),
    21             AnswerOption(id="OP3", text="R = U × I", points=0),
    22         ],
    23     )
    24     q.approve()
    25     q.sign()
    26     q.publish()
    27     return q
     12def approved_domain(sample_domain):
     13    """Provide a fully approved and published Domain for persistence tests."""
     14    sample_domain.approve()
     15    sample_domain.sign()
     16    sample_domain.publish()
     17    return sample_domain
    2818
    2919
    30 @pytest.mark.skip(reason="FlexOIDs regenerated on import; enable once JSON format is stable")
    31 def test_json_roundtrip_preserves_integrity(approved_question):
     20def test_json_roundtrip_preserves_integrity(approved_domain):
    3221    """
    3322    Export to JSON and reload — ensure fingerprints and signatures remain valid.
    3423    """
    35     json_str = approved_question.to_json()
    36     loaded = SingleChoiceQuestion.from_json(json_str)
     24    json_str = approved_domain.to_json()
     25    loaded = Domain.from_json(json_str)
    3726
    3827    # Fingerprint and state should match — integrity must pass
    39     assert SingleChoiceQuestion.verify_integrity(loaded)
     28    assert Domain.verify_integrity(loaded)
    4029
    4130    # Metadata should be preserved exactly
    42     assert approved_question.fingerprint == loaded.fingerprint
    43     assert approved_question.flexo_id == loaded.flexo_id
    44     assert loaded.state == approved_question.state
     31    assert approved_domain.fingerprint == loaded.fingerprint
     32    assert approved_domain.flexo_id == loaded.flexo_id
     33    assert loaded.state == approved_domain.state
    4534
    46 @pytest.mark.skip(reason="FlexOIDs regenerated on import; tampering detection not yet implemented")
    47 def test_json_tampering_detection(approved_question):
     35
     36def test_json_tampering_detection(approved_domain):
    4837    """Tampering with content should invalidate fingerprint verification."""
    49     json_str = approved_question.to_json()
     38    json_str = approved_domain.to_json()
    5039    tampered = json.loads(json_str)
    51     tampered["text"] = "Tampered content injection"
     40    tampered["content"]["fullname"] = "Tampered content injection"
    5241    tampered_json = json.dumps(tampered)
    5342
    54     loaded = SingleChoiceQuestion.from_json(tampered_json)
    55     assert not SingleChoiceQuestion.verify_integrity(loaded)
     43    loaded = Domain.from_json(tampered_json)
     44    assert not Domain.verify_integrity(loaded)
    5645
    57 @pytest.mark.skip(reason="FlexOIDs regenerated on import; corruption detection not yet applicable")
    58 def test_json_file_corruption(approved_question, tmp_path):
     46
     47def test_json_file_corruption(approved_domain, tmp_path):
    5948    """Simulate file corruption — integrity check must fail."""
    6049    file = tmp_path / "question.json"
    61     json_str = approved_question.to_json()
     50    json_str = approved_domain.to_json()
     51    print("JSON", json_str)
    6252    file.write_text(json_str)
    6353
    6454    # Corrupt the file (simulate accidental byte modification)
    65     corrupted = json_str.replace("Ohm’s", "Omm’s")
     55    corrupted = json_str.replace("ARITHMETIC", "ARITHM")
    6656    file.write_text(corrupted)
    6757
    68     loaded = SingleChoiceQuestion.from_json(file.read_text())
    69     assert not SingleChoiceQuestion.verify_integrity(loaded)
     58    loaded = Domain.from_json(file.read_text())
     59    assert not Domain.verify_integrity(loaded)
Note: See TracChangeset for help on using the changeset viewer.