tests/test_assertions.py
| 1 | """Tests for Assertion subclasses.""" |
| 2 | |
| 3 | from __future__ import annotations |
| 4 | |
| 5 | from pqc_content_provenance import ( |
| 6 | AIGeneratedAssertion, |
| 7 | TrainingAssertion, |
| 8 | UsageAssertion, |
| 9 | ) |
| 10 | |
| 11 | |
| 12 | def test_ai_generated_assertion_roundtrip() -> None: |
| 13 | a = AIGeneratedAssertion( |
| 14 | model_name="Llama-3-8B-Instruct", |
| 15 | model_version="1.0", |
| 16 | model_did="did:pqaid:abc", |
| 17 | generator_type="text", |
| 18 | human_edited=True, |
| 19 | generation_params={"temperature": 0.7, "top_p": 0.9, "seed": 42}, |
| 20 | ) |
| 21 | d = a.to_dict() |
| 22 | assert d["label"] == "c2pa.ai_generated" |
| 23 | restored = AIGeneratedAssertion.from_dict(d) |
| 24 | assert restored.model_name == a.model_name |
| 25 | assert restored.human_edited is True |
| 26 | assert restored.generation_params == a.generation_params |
| 27 | |
| 28 | |
| 29 | def test_training_assertion_includes_license_list() -> None: |
| 30 | t = TrainingAssertion( |
| 31 | dataset_name="common-crawl-2024", |
| 32 | dataset_root_hash="f" * 64, |
| 33 | pii_filtered=True, |
| 34 | copyright_cleared=True, |
| 35 | licenses=["cc-by-4.0", "apache-2.0", "mit"], |
| 36 | ) |
| 37 | d = t.to_dict() |
| 38 | assert d["label"] == "c2pa.training" |
| 39 | assert d["licenses"] == ["cc-by-4.0", "apache-2.0", "mit"] |
| 40 | restored = TrainingAssertion.from_dict(d) |
| 41 | assert restored.licenses == t.licenses |
| 42 | assert restored.copyright_cleared is True |
| 43 | |
| 44 | |
| 45 | def test_usage_assertion_defaults() -> None: |
| 46 | u = UsageAssertion() |
| 47 | assert u.license == "all-rights-reserved" |
| 48 | assert u.commercial_use is False |
| 49 | assert u.attribution_required is True |
| 50 | assert u.attribution_text == "" |
| 51 | assert u.jurisdictions == [] |
| 52 | assert u.expiry == "" |
| 53 | d = u.to_dict() |
| 54 | assert d["label"] == "c2pa.usage" |
| 55 | |
| 56 | |
| 57 | def test_assertion_hash_changes_with_field() -> None: |
| 58 | a = AIGeneratedAssertion(model_name="Llama-3-8B-Instruct", model_version="1.0") |
| 59 | b = AIGeneratedAssertion(model_name="Llama-3-8B-Instruct", model_version="2.0") |
| 60 | assert a.hash() != b.hash() |
| 61 | # Same data -> same hash |
| 62 | c = AIGeneratedAssertion(model_name="Llama-3-8B-Instruct", model_version="1.0") |
| 63 | assert a.hash() == c.hash() |
| 64 | |
| 65 | |
| 66 | def test_usage_assertion_roundtrip_preserves_jurisdictions() -> None: |
| 67 | u = UsageAssertion( |
| 68 | license="cc-by-nc-4.0", |
| 69 | commercial_use=False, |
| 70 | attribution_required=True, |
| 71 | attribution_text="Generated by Llama-3", |
| 72 | jurisdictions=["US", "EU", "UK"], |
| 73 | expiry="2030-01-01T00:00:00Z", |
| 74 | ) |
| 75 | restored = UsageAssertion.from_dict(u.to_dict()) |
| 76 | assert restored.jurisdictions == ["US", "EU", "UK"] |
| 77 | assert restored.expiry == "2030-01-01T00:00:00Z" |
| 78 | |