sadASDasd / python_lora_dataset_builder /tests /test_runner_regressions.py
PERDYPTO
Upload 45 files
0109502 verified
Raw History Blame Contribute Delete
5.09 kB
from pathlib import Path
from pipeline import runner
class FakeDataset:
def __init__(self, rows):
self._rows = rows
def __iter__(self):
return iter(self._rows)
def _project_root() -> Path:
return Path(__file__).resolve().parents[1]
def _redirect_paths(monkeypatch, tmp_path):
"""Point every write path at a tmp dir instead of the real config/data dirs
that ship in the repo, without touching the real dedupe/coverage state."""
root = _project_root()
real_paths = runner.build_paths(root)
real_paths["output"] = tmp_path / "data"
real_paths["dedupe"] = tmp_path / "data/metadata/exact_hashes.txt"
real_paths["coverage"] = tmp_path / "data/metadata/topic_coverage.json"
real_paths["checkpoint"] = tmp_path / "data/metadata/checkpoint.json"
monkeypatch.setattr(runner, "build_paths", lambda project_root: real_paths)
return real_paths
def _run_with_rows(monkeypatch, tmp_path, rows, **run_kwargs):
monkeypatch.setenv("ALLOW_MISSING_GITLEAKS", "1")
monkeypatch.setenv("EXECUTOR_MODE", "local")
def fake_stream(name, split="train"):
return FakeDataset(rows)
monkeypatch.setattr(runner, "stream_hf_dataset", fake_stream)
paths = _redirect_paths(monkeypatch, tmp_path)
stats = runner.run(_project_root(), limit=len(rows), dry_run=True, **run_kwargs)
return stats, paths
def test_end_to_end_smoke_dry_run(monkeypatch, tmp_path):
"""A real call into runner.run() (not hand-built dicts) with a mix of
accept-shaped and empty rows, exercising extraction, static checks,
dedupe, secret scan, tier classification, and the hard-check /
dry-run branches all the way through to disk."""
rows = [
{"instruction": "Write a function that adds two numbers.",
"input": "", "output": "```python\ndef add(a, b):\n return a + b\n```"},
{"instruction": "", "input": "", "output": "No code here, just prose."},
]
stats, paths = _run_with_rows(monkeypatch, tmp_path, rows)
assert stats["processed"] == 2
assert stats["reject"] == 1 # the prose-only row
assert stats["manual_review"] == 1 # dry_run manual_review for the valid function
manual_review_file = paths["output"] / "basic_python_syntax" / "manual_review" / "records.jsonl"
assert manual_review_file.exists()
def test_hard_check_branch_does_not_crash_on_real_run(monkeypatch, tmp_path):
"""Regression guard for the historic `NameError: decision/reason is not
defined` crash in the hard-failure branch. Only calling the real run()
end-to-end (not asserting on a hand-built dict, as the old version of
this test did) can actually catch a NameError here: it would surface as
an uncaught exception propagating out of run()."""
rows = [
{"instruction": "x", "input": "", "output": "```python\ndef broken(:\n pass\n```"},
]
stats, paths = _run_with_rows(monkeypatch, tmp_path, rows)
assert stats["processed"] == 1
assert stats["reject"] == 1
rejected_file = paths["output"] / "basic_python_syntax" / "rejected" / "records.jsonl"
assert rejected_file.exists()
assert "syntax_invalid" in rejected_file.read_text()
def test_execution_status_contract_through_real_run(monkeypatch, tmp_path):
"""The execution_status contract (decision.py reads checks['execution_status'])
is exercised through an actual failing-at-runtime snippet, not a hand-built
checks dict."""
rows = [
{"instruction": "x", "input": "", "output": "```python\nraise RuntimeError('boom')\n```"},
]
stats, paths = _run_with_rows(monkeypatch, tmp_path, rows)
assert stats["processed"] == 1
assert stats["reject"] == 1
rejected_file = paths["output"] / "basic_python_syntax" / "rejected" / "records.jsonl"
assert "sandbox_execution_failed" in rejected_file.read_text()
def test_tier2_library_code_is_classified_and_not_auto_included(monkeypatch, tmp_path):
"""Code that imports a single recognised library should be routed to the
per_library tier, tagged with that library, and — since tier 2 has no
execution environment yet — skipped by default (tier not active)."""
rows = [
{"instruction": "x", "input": "", "output": "```python\nimport json\nprint(json.dumps({}))\n```"},
]
stats, paths = _run_with_rows(monkeypatch, tmp_path, rows)
assert stats["processed"] == 1
assert stats["skip"] == 1
skipped_file = paths["output"] / "per_library" / "json" / "skipped" / "records.jsonl"
assert skipped_file.exists()
assert "tier_not_active" in skipped_file.read_text()
def test_tier3_unknown_library_is_classified_as_real_world(monkeypatch, tmp_path):
rows = [
{"instruction": "x", "input": "", "output": "```python\nimport some_totally_unknown_lib\n```"},
]
stats, paths = _run_with_rows(monkeypatch, tmp_path, rows)
assert stats["processed"] == 1
assert stats["skip"] == 1
skipped_file = paths["output"] / "real_world_repository" / "skipped" / "records.jsonl"
assert skipped_file.exists()