|
| 1 | +""" |
| 2 | +Integration tests for scripts/rebuild_sample_data.py. |
| 3 | +
|
| 4 | +These tests actually run the script and verify the artifact it produces, |
| 5 | +rather than just checking whether the file exists. |
| 6 | +""" |
| 7 | + |
| 8 | +from __future__ import annotations |
| 9 | + |
| 10 | +import json |
| 11 | +import subprocess |
| 12 | +import sys |
1 | 13 | from pathlib import Path |
2 | 14 |
|
| 15 | +import pytest |
| 16 | + |
| 17 | +SCRIPT = Path("scripts/rebuild_sample_data.py").resolve() |
| 18 | + |
| 19 | + |
| 20 | +@pytest.mark.integration |
| 21 | +def test_script_exists(): |
| 22 | + assert SCRIPT.exists(), f"Expected script at {SCRIPT}" |
| 23 | + |
| 24 | + |
| 25 | +@pytest.mark.integration |
| 26 | +def test_script_runs_successfully(tmp_path, monkeypatch): |
| 27 | + """Script must exit 0 and write a valid JSON file under the tmp workspace.""" |
| 28 | + monkeypatch.chdir(tmp_path) |
| 29 | + result = subprocess.run( |
| 30 | + [sys.executable, str(SCRIPT)], |
| 31 | + capture_output=True, |
| 32 | + text=True, |
| 33 | + ) |
| 34 | + assert result.returncode == 0, f"Script exited with {result.returncode}:\n{result.stderr}" |
| 35 | + |
| 36 | + |
| 37 | +@pytest.mark.integration |
| 38 | +def test_script_produces_valid_json(tmp_path, monkeypatch): |
| 39 | + """The artifact written by the script must be a non-empty JSON list.""" |
| 40 | + monkeypatch.chdir(tmp_path) |
| 41 | + subprocess.run([sys.executable, str(SCRIPT)], check=True, capture_output=True) |
| 42 | + |
| 43 | + output_file = tmp_path / "data" / "raw" / "github" / "repos_raw.json" |
| 44 | + assert output_file.exists(), f"Expected output at {output_file}" |
| 45 | + |
| 46 | + payload = json.loads(output_file.read_text(encoding="utf-8")) |
| 47 | + assert isinstance(payload, list), "Output must be a JSON list" |
| 48 | + assert len(payload) > 0, "Output list must not be empty" |
| 49 | + |
| 50 | + |
| 51 | +@pytest.mark.integration |
| 52 | +def test_script_output_has_required_fields(tmp_path, monkeypatch): |
| 53 | + """Each entry in the output must carry the fields downstream code relies on.""" |
| 54 | + monkeypatch.chdir(tmp_path) |
| 55 | + subprocess.run([sys.executable, str(SCRIPT)], check=True, capture_output=True) |
| 56 | + |
| 57 | + output_file = tmp_path / "data" / "raw" / "github" / "repos_raw.json" |
| 58 | + payload = json.loads(output_file.read_text(encoding="utf-8")) |
3 | 59 |
|
4 | | -def test_sample_data_file_layout(): |
5 | | - path = Path("scripts/rebuild_sample_data.py") |
6 | | - assert path.exists() |
| 60 | + required_fields = {"name", "html_url", "language", "private", "fork", "archived"} |
| 61 | + for entry in payload: |
| 62 | + missing = required_fields - entry.keys() |
| 63 | + assert not missing, f"Entry missing fields {missing}: {entry}" |
0 commit comments