Where the numbers come from
Data
Loading data…
- The data refresh every day from the community sources. Every number on this page is computed in Python (analysis/stats/site_stats.py) from the coded runs and published as data/stats.json; the coded runs themselves are data/runs_clean.csv.
+ The data refresh every day from the community sources. Every number on this page is computed in Python (analysis/stats/site_stats.py) from the coded runs and published as data/stats.json; the coded runs themselves are data/runs_clean.csv. Every column is described in data/dictionary.yaml, and data/croissant.json carries the same description in the Croissant metadata format, with a checksum for every file.
Open by design
Share & reproduce
diff --git a/site/build.js b/site/build.js
index d98b70a..b00271b 100644
--- a/site/build.js
+++ b/site/build.js
@@ -26,6 +26,7 @@ copy("core/solver.js", "solver.js");
// The numbers the page shows (all computed in Python) and the published data
for (const f of ["stats.json", "register.json", "graph.json", "policy.json"]) copy(`data/public/${f}`, `data/${f}`);
copy("data/derived/runs_clean.csv", "data/runs_clean.csv");
+for (const f of ["croissant.json", "dictionary.yaml"]) copy(`data/${f}`, `data/${f}`);
// Source files shown in the page's "view the code" panel
copy("core/atlas/coding.py", "python/coding.py");
copy("core/spec/form.yaml", "python/form.yaml");
diff --git a/tests/core/test_dictionary.py b/tests/core/test_dictionary.py
new file mode 100644
index 0000000..f1c72c3
--- /dev/null
+++ b/tests/core/test_dictionary.py
@@ -0,0 +1,78 @@
+"""The data dictionary describes every published column and value, and data/croissant.json matches it."""
+import csv
+import importlib.util
+import json
+import re
+import unittest
+from pathlib import Path
+
+import yaml
+
+ROOT = Path(__file__).resolve().parents[2]
+spec = importlib.util.spec_from_file_location("publish_metadata", ROOT / "collection" / "publish_metadata.py")
+publish_metadata = importlib.util.module_from_spec(spec)
+spec.loader.exec_module(publish_metadata)
+
+with open(ROOT / "data" / "dictionary.yaml", encoding="utf-8") as f:
+ DICT = yaml.safe_load(f)
+with open(ROOT / "data" / "derived" / "runs_clean.csv", encoding="utf-8", newline="") as f:
+ reader = csv.DictReader(f)
+ HEADER = reader.fieldnames
+ ROWS = list(reader)
+
+CHECKS = {
+ "integer": lambda v: re.fullmatch(r"-?\d+", v),
+ "boolean": lambda v: v in ("True", "False"),
+ "date": lambda v: re.fullmatch(r"\d{4}-\d{2}-\d{2}", v),
+ "text": lambda v: True,
+}
+
+
+class Dictionary(unittest.TestCase):
+ def test_every_column_described_in_order(self):
+ self.assertEqual(HEADER, list(DICT["columns"]))
+
+ def test_every_column_has_type_and_description(self):
+ for name, col in DICT["columns"].items():
+ self.assertIn(col.get("type"), CHECKS, name)
+ self.assertTrue(col.get("description"), name)
+
+ def test_values_have_the_declared_type(self):
+ for name, col in DICT["columns"].items():
+ bad = {r[name] for r in ROWS if r[name] and not CHECKS[col["type"]](r[name])}
+ self.assertFalse(bad, f"{name}: not {col['type']}: {sorted(bad)[:5]}")
+
+ def test_every_value_is_listed(self):
+ for name, col in DICT["columns"].items():
+ if "values" not in col:
+ continue
+ allowed = col["values"]
+ # YAML reads bare Yes/No as booleans: a listed value must be written as text
+ self.assertTrue(all(isinstance(v, str) for v in allowed), f"{name}: quote every value")
+ bad = {r[name] for r in ROWS if r[name] and r[name] not in allowed}
+ self.assertFalse(bad, f"{name}: values not in the dictionary: {sorted(bad)}")
+
+ def test_every_published_file_exists(self):
+ for key, f in DICT["files"].items():
+ self.assertTrue((ROOT / f["path"]).exists(), key)
+
+
+class Croissant(unittest.TestCase):
+ def test_croissant_is_up_to_date(self):
+ expected = publish_metadata.render(publish_metadata.build(DICT))
+ actual = (ROOT / "data" / "croissant.json").read_text(encoding="utf-8")
+ self.assertEqual(actual, expected, "run python3 collection/publish_metadata.py")
+
+ def test_fields_and_checksums(self):
+ doc = json.loads((ROOT / "data" / "croissant.json").read_text(encoding="utf-8"))
+ names = [f["name"] for f in doc["recordSet"][0]["field"]]
+ self.assertEqual(names, HEADER)
+ for obj in doc["distribution"]:
+ path = ROOT / DICT["files"][obj["@id"]]["path"]
+ self.assertEqual(obj["sha256"], publish_metadata.sha256(path), obj["@id"])
+ # the route PDF is not redistributed, so it is not a source of the published data
+ self.assertNotIn("pdf", json.dumps(doc["isBasedOn"]).lower())
+
+
+if __name__ == "__main__":
+ unittest.main()