import copy from fractions import Fraction import json from pathlib import Path import tempfile import unittest from audit import audit, load_input, rounded_tenth DATA = load_input(Path(__file__).with_name("inputs.json")) class AuditTests(unittest.TestCase): def test_all_published_means(self): result = audit(DATA) self.assertEqual([r["rounded_mean"] for r in result["rows"]], ["9.0", "7.4", "3.3", "2.2", "1.7", "1.0"]) def test_independent_integer_sum(self): sums = [806, 669, 296, 194, 152, 89] self.assertEqual([sum(int(v.replace(".", "")) for v in r["scores"]) for r in DATA["score_table"]["rows"]], sums) self.assertEqual([str(Fraction(s, 90)) for s in sums], [r["exact_mean_of_printed_scores"] for r in audit(DATA)["rows"]]) def test_no_lifetime(self): self.assertIsNone(audit(DATA)["lifetime_years"]) self.assertFalse(audit(DATA)["physical_experiment"]) def test_rounding_boundary(self): self.assertEqual(rounded_tenth(Fraction(105, 100)), "1.1") self.assertEqual(rounded_tenth(Fraction(1049, 1000)), "1.0") def reject(self, mutate): changed = copy.deepcopy(DATA) mutate(changed) with self.assertRaises((ValueError, KeyError)): audit(changed) def test_cannot_drop_post_stop_zeros(self): self.reject(lambda d: d["score_table"]["rows"][5]["scores"].pop()) def test_cannot_drop_baseline(self): self.reject(lambda d: d["score_table"].update(includes_baseline=False)) def test_cannot_call_score_error_rate(self): self.reject(lambda d: d["score_table"].update(unit="error_rate")) def test_reject_changed_global_mean(self): self.reject(lambda d: d["score_table"]["rows"][0].update(reported_global="8.0")) def test_reject_mixed_denominators(self): self.reject(lambda d: d["lne_protocol"].update(heat_total_discs=96)) def test_reject_invented_acceleration(self): self.reject(lambda d: d["lne_protocol"].update(acceleration_factor=100)) def test_reject_invented_full_text(self): self.reject(lambda d: d["sources"][1].update(full_text_verified=True)) def test_reject_unknown_citation(self): self.reject(lambda d: d["claims"][0].update(source_ids=["missing"])) def test_reject_duplicate_reference(self): self.reject(lambda d: d["score_table"]["rows"][0].update(reference=1)) def test_cci_categories_remain_distinct(self): self.assertEqual([x["years"] for x in DATA["cci_guidance"]], [[5, 10], [10, 20], [20, 50]]) def test_duplicate_json_keys(self): with tempfile.TemporaryDirectory() as temp: path = Path(temp) / "input.json" path.write_text('{"value":1,"value":2}') with self.assertRaises(ValueError): load_input(path) if __name__ == "__main__": unittest.main()