|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import importlib.util |
| 4 | +from pathlib import Path |
| 5 | + |
| 6 | +import pytest |
| 7 | + |
| 8 | + |
| 9 | +def load_generate_data_module(): |
| 10 | + module_path = Path(__file__).parents[1] / "generate_data.py" |
| 11 | + spec = importlib.util.spec_from_file_location("generate_data", module_path) |
| 12 | + if spec is None or spec.loader is None: |
| 13 | + raise RuntimeError(f"Unable to load module from {module_path}") |
| 14 | + module = importlib.util.module_from_spec(spec) |
| 15 | + spec.loader.exec_module(module) |
| 16 | + return module |
| 17 | + |
| 18 | + |
| 19 | +def test_load_pipeline_config_valid(tmp_path: Path): |
| 20 | + module = load_generate_data_module() |
| 21 | + config_path = tmp_path / "pipeline.yaml" |
| 22 | + config_path.write_text( |
| 23 | + "\n".join( |
| 24 | + [ |
| 25 | + 'version: "v0"', |
| 26 | + "matrix:", |
| 27 | + " dates:", |
| 28 | + " - start: 2024-01-01", |
| 29 | + " end: 2024-02-01", |
| 30 | + " granularities:", |
| 31 | + " - day", |
| 32 | + " - week", |
| 33 | + "", |
| 34 | + ] |
| 35 | + ) |
| 36 | + ) |
| 37 | + |
| 38 | + time_periods, granularities = module.load_pipeline_config(config_path) |
| 39 | + |
| 40 | + assert time_periods == [("2024-01-01", "2024-02-01")] |
| 41 | + assert granularities == ("day", "week") |
| 42 | + |
| 43 | + |
| 44 | +def test_load_pipeline_config_rejects_wrong_version(tmp_path: Path): |
| 45 | + module = load_generate_data_module() |
| 46 | + config_path = tmp_path / "pipeline.yaml" |
| 47 | + config_path.write_text( |
| 48 | + "\n".join( |
| 49 | + [ |
| 50 | + 'version: "v1"', |
| 51 | + "matrix:", |
| 52 | + " dates:", |
| 53 | + " - start: 2024-01-01", |
| 54 | + " end: 2024-02-01", |
| 55 | + " granularities:", |
| 56 | + " - day", |
| 57 | + "", |
| 58 | + ] |
| 59 | + ) |
| 60 | + ) |
| 61 | + |
| 62 | + with pytest.raises(module.click.ClickException, match="Unsupported pipeline config"): |
| 63 | + module.load_pipeline_config(config_path) |
| 64 | + |
| 65 | + |
| 66 | +def test_load_pipeline_config_rejects_empty_dates(tmp_path: Path): |
| 67 | + module = load_generate_data_module() |
| 68 | + config_path = tmp_path / "pipeline.yaml" |
| 69 | + config_path.write_text( |
| 70 | + "\n".join( |
| 71 | + [ |
| 72 | + 'version: "v0"', |
| 73 | + "matrix:", |
| 74 | + " dates: []", |
| 75 | + " granularities:", |
| 76 | + " - day", |
| 77 | + "", |
| 78 | + ] |
| 79 | + ) |
| 80 | + ) |
| 81 | + |
| 82 | + with pytest.raises(module.click.ClickException, match="matrix must include non-empty"): |
| 83 | + module.load_pipeline_config(config_path) |
| 84 | + |
| 85 | + |
| 86 | +def test_load_pipeline_config_rejects_blank_granularity(tmp_path: Path): |
| 87 | + module = load_generate_data_module() |
| 88 | + config_path = tmp_path / "pipeline.yaml" |
| 89 | + config_path.write_text( |
| 90 | + "\n".join( |
| 91 | + [ |
| 92 | + 'version: "v0"', |
| 93 | + "matrix:", |
| 94 | + " dates:", |
| 95 | + " - start: 2024-01-01", |
| 96 | + " end: 2024-02-01", |
| 97 | + " granularities:", |
| 98 | + " - day", |
| 99 | + ' - ""', |
| 100 | + "", |
| 101 | + ] |
| 102 | + ) |
| 103 | + ) |
| 104 | + |
| 105 | + with pytest.raises(module.click.ClickException, match="matrix must include non-empty"): |
| 106 | + module.load_pipeline_config(config_path) |
| 107 | + |
| 108 | + |
| 109 | +def test_load_pipeline_config_rejects_non_mapping(tmp_path: Path): |
| 110 | + module = load_generate_data_module() |
| 111 | + config_path = tmp_path / "pipeline.yaml" |
| 112 | + config_path.write_text("- not-a-mapping") |
| 113 | + |
| 114 | + with pytest.raises(module.click.ClickException, match="must be a mapping"): |
| 115 | + module.load_pipeline_config(config_path) |
0 commit comments