import copy import json from pathlib import Path import unittest import subprocess import sys import tempfile import observe class LayoutTests(unittest.TestCase): @classmethod def setUpClass(cls): cls.result = observe.run() def test_matrix(self): cases = self.result["cases"] self.assertEqual(len(cases), 12) self.assertEqual({(x["pageSize"], x["order"], x["repeat"]) for x in cases}, {(s, o, r) for s in observe.PAGE_SIZES for o in observe.ORDERS for r in (1, 2)}) for case in cases: observe.validate(case) def test_repeat_identity(self): cases = self.result["cases"] for first, second in zip(cases[::2], cases[1::2]): self.assertEqual(first["objects"], second["objects"]) self.assertEqual(first["databaseSha256Before"], second["databaseSha256Before"]) def test_oracle_detects_changed_row(self): case = copy.deepcopy(self.result["cases"][0]) case["objects"]["sample"]["rowsSha256"] = "0" * 64 with self.assertRaisesRegex(ValueError, "row oracle"): observe.validate(case) def test_no_boolean_count(self): case = copy.deepcopy(self.result["cases"][0]) case["objects"]["sample"]["rowCount"] = True with self.assertRaisesRegex(ValueError, "row count"): observe.validate(case) def test_detects_changed_file(self): case = copy.deepcopy(self.result["cases"][0]) case["databaseSha256After"] = "0" * 64 with self.assertRaisesRegex(ValueError, "file changed"): observe.validate(case) def test_detects_header_mismatch(self): case = copy.deepcopy(self.result["cases"][0]) case["objects"]["sample"]["pages"][0]["headerType"] = 2 with self.assertRaisesRegex(ValueError, "page type"): observe.validate(case) def test_detects_aggregate_mismatch(self): case = copy.deepcopy(self.result["cases"][0]) case["objects"]["sample_token"]["totals"]["leaf"]["ncell"] += 1 with self.assertRaisesRegex(ValueError, "aggregate mismatch"): observe.validate(case) def test_rejects_empty_pages(self): case = copy.deepcopy(self.result["cases"][0]) case["objects"]["clustered"]["pages"] = [] with self.assertRaisesRegex(ValueError, "missing pages"): observe.validate(case) def test_rejects_missing_object(self): case = copy.deepcopy(self.result["cases"][0]) del case["objects"]["clustered"] with self.assertRaisesRegex(ValueError, "object set"): observe.validate(case) def test_existing_output_preserved(self): with tempfile.TemporaryDirectory(prefix="hesela-layout-cli-") as folder: path = Path(folder) / "keep.json" path.write_bytes(b"existing output") result = subprocess.run([sys.executable, "-B", observe.__file__, "--output", str(path)], capture_output=True, text=True) self.assertEqual(result.returncode, 2) self.assertIn("output already exists", result.stderr) self.assertEqual(path.read_bytes(), b"existing output") def test_published_record_when_present(self): path = Path(__file__).with_name("results.json") if not path.exists(): self.skipTest("Download results.json to validate the published record") if path.exists(): record = json.loads(path.read_text()) self.assertEqual(len(record["cases"]), 12) self.assertEqual({(x["pageSize"], x["order"], x["repeat"]) for x in record["cases"]}, {(s, o, r) for s in observe.PAGE_SIZES for o in observe.ORDERS for r in (1, 2)}) for case in record["cases"]: observe.validate(case) self.assertEqual(record["codeSha256"], observe.digest(Path(observe.__file__).read_bytes())) if __name__ == "__main__": unittest.main()