code backup: tests
Browse files
tests/test_B/test_prompts.py
CHANGED
|
@@ -50,3 +50,45 @@ def test_every_na_question_type_builds(question_type):
|
|
| 50 |
def test_every_mca_question_type_builds(question_type):
|
| 51 |
prompt = code_prompts.build_prompt(_CODE, question_type, "q?", ["A. x", "B. y"])
|
| 52 |
assert prompt.startswith(code_prompts.PRE_PROMPT)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
def test_every_mca_question_type_builds(question_type):
|
| 51 |
prompt = code_prompts.build_prompt(_CODE, question_type, "q?", ["A. x", "B. y"])
|
| 52 |
assert prompt.startswith(code_prompts.PRE_PROMPT)
|
| 53 |
+
|
| 54 |
+
|
| 55 |
+
def test_default_arguments_reproduce_the_standard_prompt_byte_for_byte():
|
| 56 |
+
standard = code_prompts.build_prompt(_CODE, "object_counting", "How many chairs?")
|
| 57 |
+
explicit_defaults = code_prompts.build_prompt(
|
| 58 |
+
_CODE, "object_counting", "How many chairs?", serialization="json", context_line=None
|
| 59 |
+
)
|
| 60 |
+
assert standard == explicit_defaults
|
| 61 |
+
|
| 62 |
+
|
| 63 |
+
def test_yaml_serialization_renders_the_identical_dict():
|
| 64 |
+
import yaml
|
| 65 |
+
|
| 66 |
+
prompt = code_prompts.build_prompt(
|
| 67 |
+
_CODE, "object_counting", "How many chairs?", serialization="yaml"
|
| 68 |
+
)
|
| 69 |
+
rendered = prompt.split("\n", 1)[1].rsplit("How many chairs?", 1)[0]
|
| 70 |
+
assert yaml.safe_load(rendered) == _CODE
|
| 71 |
+
|
| 72 |
+
|
| 73 |
+
def test_yaml_arm_context_line_does_not_claim_json():
|
| 74 |
+
prompt = code_prompts.build_prompt(
|
| 75 |
+
_CODE, "object_counting", "How many chairs?", serialization="yaml"
|
| 76 |
+
)
|
| 77 |
+
context = prompt.split("\n", 1)[0]
|
| 78 |
+
assert "JSON" not in context
|
| 79 |
+
assert "YAML" in context
|
| 80 |
+
|
| 81 |
+
|
| 82 |
+
def test_paraphrase_context_line_swaps_only_the_first_line():
|
| 83 |
+
standard = code_prompts.build_prompt(_CODE, "object_counting", "How many chairs?")
|
| 84 |
+
paraphrased = code_prompts.build_prompt(
|
| 85 |
+
_CODE, "object_counting", "How many chairs?",
|
| 86 |
+
context_line=code_prompts.PARAPHRASE_PRE_PROMPT,
|
| 87 |
+
)
|
| 88 |
+
assert standard.split("\n", 1)[1] == paraphrased.split("\n", 1)[1]
|
| 89 |
+
assert standard.split("\n", 1)[0] != paraphrased.split("\n", 1)[0]
|
| 90 |
+
|
| 91 |
+
|
| 92 |
+
def test_unknown_serialization_rejected():
|
| 93 |
+
with pytest.raises(ValueError):
|
| 94 |
+
code_prompts.render_code(_CODE, "xml")
|
tests/test_corruption/test_run.py
CHANGED
|
@@ -121,3 +121,25 @@ def test_make_code_transform_ignores_the_loaded_code():
|
|
| 121 |
hook = corruption_run.make_code_transform("position-jitter", 0.25)
|
| 122 |
out = hook({"not": "used"}, _SCENE, "compact")
|
| 123 |
assert out == corruption_run.corrupted_code(_SCENE, "position-jitter", 0.25, "compact")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 121 |
hook = corruption_run.make_code_transform("position-jitter", 0.25)
|
| 122 |
out = hook({"not": "used"}, _SCENE, "compact")
|
| 123 |
assert out == corruption_run.corrupted_code(_SCENE, "position-jitter", 0.25, "compact")
|
| 124 |
+
|
| 125 |
+
|
| 126 |
+
def test_single_object_info_is_deterministic_and_recomputable():
|
| 127 |
+
first = corruption_run.single_object_info(_SCENE, 0)
|
| 128 |
+
second = corruption_run.single_object_info(_SCENE, 0)
|
| 129 |
+
assert first == second
|
| 130 |
+
assert "class" in first and "instance" in first
|
| 131 |
+
|
| 132 |
+
|
| 133 |
+
def test_single_object_solver_run_writes_perturbation_sidecar(tmp_path):
|
| 134 |
+
corruption_run.run_solver("single-object", 0, scenes=[_SCENE], results_dir=tmp_path)
|
| 135 |
+
sidecar = tmp_path / _SCENE / "_perturbation.json"
|
| 136 |
+
assert sidecar.is_file()
|
| 137 |
+
import json
|
| 138 |
+
|
| 139 |
+
info = json.loads(sidecar.read_text())
|
| 140 |
+
assert info == corruption_run.single_object_info(_SCENE, 0)
|
| 141 |
+
|
| 142 |
+
|
| 143 |
+
def test_non_probe_runs_write_no_sidecar(tmp_path):
|
| 144 |
+
corruption_run.run_solver("position-jitter", 0.0, scenes=[_SCENE], results_dir=tmp_path)
|
| 145 |
+
assert not (tmp_path / _SCENE / "_perturbation.json").exists()
|
tests/test_corruption/test_sample.py
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Tests for corruption/sample.py -- the pre-registered question sampler."""
|
| 2 |
+
|
| 3 |
+
from corruption import sample as corruption_sample
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
def _rows(monkeypatch, rows):
|
| 7 |
+
monkeypatch.setattr(corruption_sample, "load_questions", lambda: list(rows))
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
def test_budget_key_pools_direction_subtypes():
|
| 11 |
+
assert corruption_sample._budget_key("object_rel_direction_easy") == "object_rel_direction"
|
| 12 |
+
assert corruption_sample._budget_key("object_rel_direction_hard") == "object_rel_direction"
|
| 13 |
+
assert corruption_sample._budget_key("object_counting") == "object_counting"
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
def test_draw_sample_is_deterministic_for_the_frozen_seed(monkeypatch):
|
| 17 |
+
rows = [
|
| 18 |
+
{"id": i, "scene_name": f"scene{i % 5}", "question_type": "object_counting"}
|
| 19 |
+
for i in range(50)
|
| 20 |
+
]
|
| 21 |
+
_rows(monkeypatch, rows)
|
| 22 |
+
scenes = {f"scene{i}" for i in range(5)}
|
| 23 |
+
first = corruption_sample.draw_sample(scenes, budgets={"object_counting": 10})
|
| 24 |
+
second = corruption_sample.draw_sample(scenes, budgets={"object_counting": 10})
|
| 25 |
+
assert first == second
|
| 26 |
+
assert len(first) == 10
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
def test_draw_sample_spreads_across_scenes_round_robin(monkeypatch):
|
| 30 |
+
# 5 scenes x 10 questions each; a 5-question budget must touch 5 DISTINCT scenes.
|
| 31 |
+
rows = [
|
| 32 |
+
{"id": scene * 100 + i, "scene_name": f"scene{scene}", "question_type": "object_counting"}
|
| 33 |
+
for scene in range(5)
|
| 34 |
+
for i in range(10)
|
| 35 |
+
]
|
| 36 |
+
_rows(monkeypatch, rows)
|
| 37 |
+
scenes = {f"scene{i}" for i in range(5)}
|
| 38 |
+
sampled = corruption_sample.draw_sample(scenes, budgets={"object_counting": 5})
|
| 39 |
+
assert len({qid // 100 for qid in sampled}) == 5
|
| 40 |
+
|
| 41 |
+
|
| 42 |
+
def test_draw_sample_excludes_unlisted_categories_and_scenes(monkeypatch):
|
| 43 |
+
rows = [
|
| 44 |
+
{"id": 1, "scene_name": "scene_in", "question_type": "object_counting"},
|
| 45 |
+
{"id": 2, "scene_name": "scene_in", "question_type": "obj_appearance_order"},
|
| 46 |
+
{"id": 3, "scene_name": "scene_out", "question_type": "object_counting"},
|
| 47 |
+
]
|
| 48 |
+
_rows(monkeypatch, rows)
|
| 49 |
+
sampled = corruption_sample.draw_sample({"scene_in"}, budgets={"object_counting": 10})
|
| 50 |
+
assert sampled == [1]
|
| 51 |
+
|
| 52 |
+
|
| 53 |
+
def test_draw_sample_budget_caps_the_draw(monkeypatch):
|
| 54 |
+
rows = [
|
| 55 |
+
{"id": i, "scene_name": "scene0", "question_type": "object_counting"}
|
| 56 |
+
for i in range(100)
|
| 57 |
+
]
|
| 58 |
+
_rows(monkeypatch, rows)
|
| 59 |
+
sampled = corruption_sample.draw_sample({"scene0"}, budgets={"object_counting": 7})
|
| 60 |
+
assert len(sampled) == 7
|