|
| 1 | +import json |
| 2 | +import os |
| 3 | +import subprocess |
| 4 | +from pathlib import Path |
| 5 | + |
| 6 | + |
| 7 | +def run_generate_test(tmp_path: Path, config_name: str): |
| 8 | + """ |
| 9 | + Run the generate test with the given configuration file and temporary path. |
| 10 | +
|
| 11 | + Args: |
| 12 | + tmp_path: pytest temporary path |
| 13 | + config_name: configuration file name (e.g. "atomic_config.yaml") |
| 14 | +
|
| 15 | + Returns: |
| 16 | + tuple: (run_folder, json_files[0]) |
| 17 | + """ |
| 18 | + repo_root = Path(__file__).resolve().parents[2] |
| 19 | + os.chdir(repo_root) |
| 20 | + |
| 21 | + config_path = repo_root / "graphgen" / "configs" / config_name |
| 22 | + output_dir = tmp_path / "output" |
| 23 | + output_dir.mkdir(parents=True, exist_ok=True) |
| 24 | + |
| 25 | + result = subprocess.run( |
| 26 | + [ |
| 27 | + "python", |
| 28 | + "-m", |
| 29 | + "graphgen.generate", |
| 30 | + "--config_file", |
| 31 | + str(config_path), |
| 32 | + "--output_dir", |
| 33 | + str(output_dir), |
| 34 | + ], |
| 35 | + capture_output=True, |
| 36 | + text=True, |
| 37 | + check=False, |
| 38 | + ) |
| 39 | + assert result.returncode == 0, f"Script failed with error: {result.stderr}" |
| 40 | + |
| 41 | + data_root = output_dir / "data" / "graphgen" |
| 42 | + assert data_root.exists(), f"{data_root} does not exist" |
| 43 | + run_folders = sorted(data_root.iterdir(), key=lambda p: p.name, reverse=True) |
| 44 | + assert run_folders, f"No run folders found in {data_root}" |
| 45 | + run_folder = run_folders[0] |
| 46 | + |
| 47 | + config_saved = run_folder / "config.yaml" |
| 48 | + assert config_saved.exists(), f"{config_saved} not found" |
| 49 | + |
| 50 | + json_files = list(run_folder.glob("*.json")) |
| 51 | + assert json_files, f"No JSON output found in {run_folder}" |
| 52 | + |
| 53 | + log_files = list(run_folder.glob("*.log")) |
| 54 | + assert log_files, "No log file generated" |
| 55 | + |
| 56 | + with open(json_files[0], "r", encoding="utf-8") as f: |
| 57 | + data = json.load(f) |
| 58 | + assert ( |
| 59 | + isinstance(data, list) and len(data) > 0 |
| 60 | + ), "JSON output is empty or not a list" |
| 61 | + |
| 62 | + return run_folder, json_files[0] |
| 63 | + |
0 commit comments