|
| 1 | +"""A write is judged by what it adds, not by what it inherited. |
| 2 | +
|
| 3 | +Every scenario bundle in the live graph fails shape validation, and about a |
| 4 | +third of those failures are **missing content** -- which sector a study covers, |
| 5 | +who wrote a publication -- that no repair script can infer. Those are exactly |
| 6 | +the fields a person would supply *by patching*. So a rule of "the post-state |
| 7 | +must conform" makes the defect unfixable through the API: you would need a |
| 8 | +patch to add the missing sector, and the patch is refused because the sector is |
| 9 | +missing. |
| 10 | +
|
| 11 | +The promise that matters survives: **nothing invalid is written by this API.** |
| 12 | +What is given up is holding a client responsible for damage that predates it. |
| 13 | +
|
| 14 | +A create is the deliberate exception and is tested here too -- it has no |
| 15 | +pre-state, so everything it produces is new, and a bundle cannot be *created* |
| 16 | +broken. |
| 17 | +
|
| 18 | +SPDX-FileCopyrightText: 2026 Jonas Huber <https://github.com/jh-RLI> © Reiner Lemoine Institut |
| 19 | +SPDX-License-Identifier: AGPL-3.0-or-later |
| 20 | +""" # noqa: 501 |
| 21 | + |
| 22 | +from rdflib import Graph |
| 23 | + |
| 24 | +from factsheet.models import ScenarioBundleAccessControl |
| 25 | +from oekg.bundles import OEO, build_bundle_graph |
| 26 | +from oekg.tests.bundle_fixtures import VALID_PAYLOAD, BundleApiTestCase |
| 27 | + |
| 28 | +# The shape requires at least one technology. A bundle without one is exactly |
| 29 | +# the kind of thing the browser has written for years. |
| 30 | +INHERITED_DEFECT = {k: v for k, v in VALID_PAYLOAD.items() if k != "technologies"} |
| 31 | + |
| 32 | + |
| 33 | +class InheritedViolationTest(BundleApiTestCase): |
| 34 | + def existing_bundle(self, payload=None, uid=None): |
| 35 | + """A bundle put straight into the store, as another writer would.""" |
| 36 | + uid = uid or "11111111-2222-3333-4444-555555555555" |
| 37 | + self.store.insert(build_bundle_graph(uid, payload or INHERITED_DEFECT)) |
| 38 | + ScenarioBundleAccessControl.objects.create(owner_user=self.user, bundle_id=uid) |
| 39 | + return uid |
| 40 | + |
| 41 | + def test_a_bundle_that_already_violates_the_shape_can_be_patched(self): |
| 42 | + # The whole point: a defect the caller did not cause does not stand |
| 43 | + # between them and a change to an unrelated field. |
| 44 | + uid = self.existing_bundle() |
| 45 | + |
| 46 | + response = self.patch(uid, {"label": "Renamed"}, if_match='"0"') |
| 47 | + |
| 48 | + self.assertEqual(response.status_code, 200, response.data) |
| 49 | + self.assertEqual(self.client.get(self.detail_url(uid)).data["label"], "Renamed") |
| 50 | + |
| 51 | + def test_the_inherited_violation_is_still_there_afterwards(self): |
| 52 | + # Accepted, not repaired. The API does not quietly fix what it did not |
| 53 | + # break, and the bundle stays as non-conforming as it was. |
| 54 | + uid = self.existing_bundle() |
| 55 | + |
| 56 | + self.patch(uid, {"label": "Renamed"}, if_match='"0"') |
| 57 | + |
| 58 | + self.assertEqual(self.client.get(self.detail_url(uid)).data["technologies"], []) |
| 59 | + |
| 60 | + def test_a_patch_that_introduces_a_violation_is_still_refused(self): |
| 61 | + uid = self.existing_bundle() |
| 62 | + |
| 63 | + response = self.patch(uid, {"sectors": []}, if_match='"0"') |
| 64 | + |
| 65 | + self.assertEqual(response.status_code, 400, response.data) |
| 66 | + messages = [v["message"] for v in response.data["violations"]] |
| 67 | + self.assertIn("Study target: This should cover at least one sector.", messages) |
| 68 | + |
| 69 | + def test_the_refusal_names_only_what_the_write_added(self): |
| 70 | + # The inherited one must not appear in the list, or a client cannot |
| 71 | + # tell which of them is its own doing. |
| 72 | + uid = self.existing_bundle() |
| 73 | + |
| 74 | + response = self.patch(uid, {"sectors": []}, if_match='"0"') |
| 75 | + |
| 76 | + messages = [v["message"] for v in response.data["violations"]] |
| 77 | + self.assertNotIn( |
| 78 | + "Study target: This should cover at least one technology.", messages |
| 79 | + ) |
| 80 | + |
| 81 | + def test_the_refusal_says_how_many_were_inherited(self): |
| 82 | + # Named rather than hidden: the bundle is not clean, and a client that |
| 83 | + # succeeds should still be able to learn that. |
| 84 | + uid = self.existing_bundle() |
| 85 | + |
| 86 | + response = self.patch(uid, {"sectors": []}, if_match='"0"') |
| 87 | + |
| 88 | + self.assertGreaterEqual(response.data["pre_existing_violations"], 1) |
| 89 | + |
| 90 | + def test_the_refusal_keeps_its_types(self): |
| 91 | + # The framework rewrites every scalar in an error body it renders |
| 92 | + # itself, so `None` would come back as the string "None" and a count as |
| 93 | + # a string. Refusals carry their own response for exactly this reason. |
| 94 | + uid = self.existing_bundle() |
| 95 | + |
| 96 | + response = self.patch(uid, {"sectors": []}, if_match='"0"') |
| 97 | + |
| 98 | + self.assertIsInstance(response.data["pre_existing_violations"], int) |
| 99 | + nullable = [v["path"] for v in response.data["violations"]] |
| 100 | + self.assertTrue( |
| 101 | + any(p is None for p in nullable) |
| 102 | + or all(isinstance(p, str) for p in nullable), |
| 103 | + nullable, |
| 104 | + ) |
| 105 | + for violation in response.data["violations"]: |
| 106 | + self.assertNotEqual(violation["value"], "None", violation) |
| 107 | + |
| 108 | + def test_a_patch_that_repairs_the_bundle_is_accepted(self): |
| 109 | + uid = self.existing_bundle() |
| 110 | + |
| 111 | + response = self.patch( |
| 112 | + uid, {"technologies": [str(OEO.OEO_00000407)]}, if_match='"0"' |
| 113 | + ) |
| 114 | + |
| 115 | + self.assertEqual(response.status_code, 200, response.data) |
| 116 | + self.assertEqual( |
| 117 | + self.client.get(self.detail_url(uid)).data["technologies"], |
| 118 | + [str(OEO.OEO_00000407)], |
| 119 | + ) |
| 120 | + |
| 121 | + def test_a_second_copy_of_an_inherited_violation_is_refused(self): |
| 122 | + # Compared as a multiset, not as a set: a bundle that already misses |
| 123 | + # one required field may not come out missing two. |
| 124 | + uid = self.existing_bundle() |
| 125 | + |
| 126 | + response = self.patch( |
| 127 | + uid, {"sector_divisions": [], "sectors": []}, if_match='"0"' |
| 128 | + ) |
| 129 | + |
| 130 | + self.assertEqual(response.status_code, 400, response.data) |
| 131 | + self.assertEqual(len(response.data["violations"]), 2, response.data) |
| 132 | + |
| 133 | + def test_nothing_is_written_when_a_new_violation_is_refused(self): |
| 134 | + uid = self.existing_bundle() |
| 135 | + |
| 136 | + self.patch(uid, {"sectors": [], "label": "Should not land"}, if_match='"0"') |
| 137 | + |
| 138 | + read = self.client.get(self.detail_url(uid)).data |
| 139 | + self.assertEqual(read["label"], VALID_PAYLOAD["label"]) |
| 140 | + self.assertEqual(read["sectors"], VALID_PAYLOAD["sectors"]) |
| 141 | + |
| 142 | + |
| 143 | +class CreateStillHasToConformTest(BundleApiTestCase): |
| 144 | + """The deliberate exception: a bundle cannot be created broken.""" |
| 145 | + |
| 146 | + def test_a_create_missing_a_required_field_is_refused(self): |
| 147 | + response = self.create(INHERITED_DEFECT) |
| 148 | + |
| 149 | + self.assertEqual(response.status_code, 400, response.data) |
| 150 | + self.assertFalse(self.store.ask("ASK { ?s ?p ?o }")) |
| 151 | + |
| 152 | + def test_a_create_reports_every_violation_it_has(self): |
| 153 | + # No pre-state to subtract, so nothing is forgiven. |
| 154 | + response = self.create( |
| 155 | + {k: v for k, v in INHERITED_DEFECT.items() if k != "sectors"} |
| 156 | + ) |
| 157 | + |
| 158 | + messages = {v["message"] for v in response.data["violations"]} |
| 159 | + self.assertIn("Study target: This should cover at least one sector.", messages) |
| 160 | + self.assertIn( |
| 161 | + "Study target: This should cover at least one technology.", messages |
| 162 | + ) |
| 163 | + |
| 164 | + |
| 165 | +class IntroducedViolationsUnitTest(BundleApiTestCase): |
| 166 | + """The comparison itself.""" |
| 167 | + |
| 168 | + def test_an_unchanged_graph_introduces_nothing(self): |
| 169 | + from oekg.validation import introduced_violations |
| 170 | + |
| 171 | + graph = build_bundle_graph("u1", INHERITED_DEFECT) |
| 172 | + |
| 173 | + new, inherited = introduced_violations(graph, graph) |
| 174 | + |
| 175 | + self.assertEqual(new, []) |
| 176 | + self.assertGreaterEqual(inherited, 1) |
| 177 | + |
| 178 | + def test_an_empty_before_forgives_nothing(self): |
| 179 | + from oekg.validation import introduced_violations |
| 180 | + |
| 181 | + after = build_bundle_graph("u1", INHERITED_DEFECT) |
| 182 | + |
| 183 | + new, inherited = introduced_violations(Graph(), after) |
| 184 | + |
| 185 | + self.assertEqual(inherited, 0) |
| 186 | + self.assertGreaterEqual(len(new), 1) |
0 commit comments