Core: coding rules as declarative form.yaml + Python package (atlas.coding) (#9)
Step 1 of moving the repo to Python with declarative rules. - core/spec/form.yaml: every question (how it is found in an export), every answer and what it is coded as, missing answers, email redaction, burn order, first-dragon pools. - core/atlas/coding.py reads it and replaces core/python/clean.py (python -m atlas.coding). - tests/core/test_coding.py: 16 rule tests, one hand-written row per rule, checking the rules are right rather than only that JS and Python agree. - analysis/stats/analyze.py takes the colours and missing answers from the spec instead of its own copy; the daily update job codes rows through the package. - pyproject.toml for the package; pyyaml and pytest added to requirements. The coded runs are byte-identical to before (5,988 runs); all parity checks still match. Claude-Session: https://claude.ai/code/session_01UCzTd31GphpMXDhQqWrCMQ Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
432e04c2c7
commit
3536007ade
@@ -0,0 +1,132 @@
|
||||
"""Rule tests for the coding of community form rows (core/atlas/coding.py + core/spec/form.yaml).
|
||||
|
||||
Each test is one small, hand-written row with the answer it must produce. Unlike the parity tests, these check
|
||||
that the rules are right, not just that two implementations agree. Run: python -m pytest tests/core
|
||||
(or: PYTHONPATH=core python -m unittest discover -s tests/core -p 'test_*.py').
|
||||
"""
|
||||
import unittest
|
||||
|
||||
from atlas import coding
|
||||
|
||||
# Question texts as they appear in the exports (shortened, but containing the words form.yaml looks for)
|
||||
OLD = {
|
||||
"timestamp": "Timestamp",
|
||||
"company": "Who accompanied you?",
|
||||
"dir_East": "Initial direction [East / Roar]",
|
||||
"dir_West": "Initial direction [West / Scent]",
|
||||
"dir_Center": "Initial direction [Forest Center]",
|
||||
"cadet": "What was the outcome of your cadet encounter?",
|
||||
"combat": "Combat choice",
|
||||
"post": "Which path did you take after the bully fight?",
|
||||
"hour": "After walking an hour, what did you do?",
|
||||
"goal": "Which colour dragon are you trying to bond?",
|
||||
**{f"enc_{c}": f"Did you attempt to bond? [{c}]" for c in coding.COLORS},
|
||||
}
|
||||
NEW = {
|
||||
"timestamp": "Timestamp",
|
||||
"company": "Who accompanied you?",
|
||||
"first_dir": "What was your first direction?",
|
||||
"first_colour": "What colour was your initial dragon sighting?",
|
||||
"sighting_action": "What action did you take during the first dragon sighting?",
|
||||
"cadet_actions": "What did you do during your cadet encounter?",
|
||||
"cadet": "What was the outcome of your cadet encounter?",
|
||||
"post": "Which path did you take after the bully fight?",
|
||||
"hour": "After walking an hour, what did you do?",
|
||||
"goal_v2": "Why are you doing this run?",
|
||||
"malek": "Would you seek Malek?",
|
||||
**{f"enc_{c}": f"Did you attempt to bond? [{c}]" for c in coding.COLORS},
|
||||
}
|
||||
|
||||
|
||||
def code(form, **answers):
|
||||
"""Code one row given answers by question key."""
|
||||
headers = list(form.values())
|
||||
row = {h: None for h in headers}
|
||||
for k, v in answers.items():
|
||||
row[form[k]] = v
|
||||
C, _ = coding.find_columns(headers)
|
||||
return coding.code_row(row, C)
|
||||
|
||||
|
||||
class Columns(unittest.TestCase):
|
||||
def test_both_form_versions_are_recognised(self):
|
||||
for form in (OLD, NEW):
|
||||
C, missing = coding.find_columns(list(form.values()))
|
||||
self.assertFalse(set(missing) & set(form), missing)
|
||||
for key, header in form.items():
|
||||
self.assertEqual(C[key], header)
|
||||
|
||||
def test_a_missing_required_question_is_reported(self):
|
||||
headers = [h for k, h in OLD.items() if k != "hour"]
|
||||
self.assertIn("hour", coding.find_columns(headers)[1])
|
||||
|
||||
|
||||
class Rows(unittest.TestCase):
|
||||
def test_form_submission_vs_bulk_import(self):
|
||||
self.assertEqual(code(OLD, timestamp="2026-10-03 14:02:11")["source"], "Form")
|
||||
r = code(OLD, timestamp="Data Import")
|
||||
self.assertEqual((r["source"], r["date"]), ("Data Import", None))
|
||||
|
||||
def test_old_form_direction_and_first_dragon(self):
|
||||
r = code(OLD, dir_Center="Brown")
|
||||
self.assertEqual((r["direction"], r["first_sighting"]), ("Center", "Brown"))
|
||||
r = code(OLD, dir_Center="Brown", dir_East="Red")
|
||||
self.assertEqual(r["direction"], "Multiple")
|
||||
self.assertIn("direction multiple", r["data_flags"])
|
||||
|
||||
def test_new_form_direction_and_first_dragon(self):
|
||||
r = code(NEW, first_dir="West / Scent", first_colour="Green", sighting_action="Hold Ground / Stay Still")
|
||||
self.assertEqual((r["direction"], r["first_sighting"], r["sighting_action"]), ("West", "Green", "Hold still"))
|
||||
|
||||
def test_left_squadmates_suffix(self):
|
||||
r = code(NEW, company="1 Squadmate > Left them")
|
||||
self.assertEqual((r["company"], r["left_squadmates"]), ("1 Squadmate", True))
|
||||
|
||||
def test_old_cadet_answer_and_fight_move(self):
|
||||
r = code(OLD, cadet="Bullies > Squadmate's revenge", combat="Bullies > Even the numbers (Rushed the smaller cadet)", post="West")
|
||||
self.assertEqual((r["scream"], r["resolution"], r["combat_first"], r["post_path"]),
|
||||
("Investigate & help", "Squadmate's revenge", "Even the numbers (smaller cadet)", "West"))
|
||||
|
||||
def test_new_cadet_path_plus_outcome(self):
|
||||
r = code(NEW, cadet_actions="Bullies > Assess > Daggers or not", cadet="Left to the Dragons", post="Forest")
|
||||
self.assertEqual((r["combat_first"], r["combat_second"], r["resolution"]),
|
||||
("Assess the situation", "Daggers or not (protect squadmate)", "Left bullies to the dragons"))
|
||||
|
||||
def test_ignoring_the_scream_has_no_fork(self):
|
||||
r = code(OLD, cadet="Injured Cadet > Tourniquet", post="East")
|
||||
self.assertEqual((r["scream"], r["resolution"], r["post_path"]), ("Ignore the scream", "Tourniquet", "(no fork)"))
|
||||
|
||||
def test_death_at_the_first_dragon(self):
|
||||
r = code(OLD, cadet="N/A - You died with initial dragon sighting", hour="Climbed the tallest tree")
|
||||
self.assertEqual((r["outcome"], r["hour"]), ("Died before the hour", None))
|
||||
|
||||
def test_tree_branches(self):
|
||||
self.assertEqual(code(OLD, hour="Climbed the tallest tree", enc_Red="Incinerated")["branch"], "Descend quickly: Red")
|
||||
self.assertEqual(code(OLD, hour="Climbed the tallest tree", enc_Green="Bonded!")["branch"], "Descend slowly: two Greens")
|
||||
self.assertEqual(code(OLD, hour="Climbed the tallest tree", enc_Black="Bonded!")["branch"], "Shadow (rare)")
|
||||
|
||||
def test_valley_branches(self):
|
||||
self.assertEqual(code(OLD, hour="Continued to Valley Centre", enc_Red="Bonded!")["branch"], "Valley Red")
|
||||
r = code(OLD, hour="Continued to Valley Centre", enc_Red="Changed my mind", enc_Blue="Bonded!")
|
||||
self.assertEqual((r["branch"], r["outcome"], r["outcome_color"], r["saw_blue_meadow"]),
|
||||
("Passed the Red: meadow", "Bonded", "Blue", True))
|
||||
|
||||
def test_greens_rescue(self):
|
||||
r = code(OLD, hour="Climbed the tallest tree", enc_Green="Saved by another", enc_Orange="Bonded!")
|
||||
self.assertTrue(r["rescued"])
|
||||
|
||||
def test_last_dragon_met_decides_an_incineration(self):
|
||||
r = code(OLD, hour="Continued to Valley Centre", enc_Red="Incinerated", enc_Orange="Incinerated")
|
||||
self.assertEqual(r["outcome_color"], "Orange")
|
||||
|
||||
def test_unsure_answers_are_missing(self):
|
||||
r = code(NEW, cadet_actions="Unsure")
|
||||
self.assertTrue(coding.is_missing(r["scream"]))
|
||||
self.assertTrue(coding.is_missing(r["resolution"]))
|
||||
|
||||
def test_emails_are_redacted(self):
|
||||
self.assertEqual(coding.redact("write to me at rider@example.com"), "write to me at [email removed]")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
@@ -1,4 +1,4 @@
|
||||
"""Check that the JS engine (core/engine.js) and the Python reference (python/clean.py) code every run identically."""
|
||||
"""Check that the JS engine (core/engine.js) and the Python coding rules (core/atlas/coding.py) code every run identically."""
|
||||
import json, sys
|
||||
import pandas as pd
|
||||
js = pd.DataFrame(json.load(open(sys.argv[1] if len(sys.argv) > 1 else "tests/out/runs_js.json")))
|
||||
|
||||
Reference in New Issue
Block a user