"""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?", "squad": "What squad were you assigned?", **{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_squad_ignores_case(self): for typed, coded in (("second", "Second"), (" THIRD ", "Third"), ("Don't know", "Don't know"), (None, "Unknown")): self.assertEqual(code(NEW, company="Alone", squad=typed)["squad"], coded) 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_left_squadmates_unknown_before_the_answer_existed(self): # "> Left them" was only offered from 2026-10-27: earlier or undated squad runs are unknown, not "stayed" self.assertIsNone(code(NEW, company="1 Squadmate", timestamp="2026-10-20 10:00:00")["left_squadmates"]) self.assertIsNone(code(NEW, company="2 Squadmates", timestamp="Data Import")["left_squadmates"]) self.assertIs(code(NEW, company="1 Squadmate", timestamp="2026-10-28 10:00:00")["left_squadmates"], False) self.assertIs(code(NEW, company="Alone", timestamp="2026-10-20 10:00:00")["left_squadmates"], False) def test_letting_the_red_pass(self): # only climbing down quickly puts you above the Red; passing on it and meeting the Greens means you let it pass r = code(OLD, hour="Climbed the tallest tree", enc_Red="Changed my mind", enc_Green="Bonded!") self.assertEqual((r["branch"], r["outcome_color"]), ("Descend quickly: let the Red pass", "Green")) def test_red_and_greens_both_attempted_is_flagged(self): r = code(OLD, hour="Climbed the tallest tree", enc_Red="Incinerated", enc_Green="Incinerated") self.assertIn("both the Red and the two Greens", r["data_flags"]) 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()