Coverage for tests / test_solar_prepare_epw.py: 100%
35 statements
« prev ^ index » next coverage.py v7.13.5, created at 2026-04-01 22:14 +0000
« prev ^ index » next coverage.py v7.13.5, created at 2026-04-01 22:14 +0000
1"""Tests for NIWA EPW archive preparation."""
3import importlib.util
4import json
5import tarfile
6from pathlib import Path
9def load_solar_prepare_epw():
10 """
11 Load the script module directly from its path for test usage.
12 """
13 module_path = (
14 Path(__file__).resolve().parents[1]
15 / "scripts/stage_3_scenarios/electricity/solar_prepare_epw.py"
16 )
17 spec = importlib.util.spec_from_file_location("solar_prepare_epw", module_path)
18 module = importlib.util.module_from_spec(spec)
19 assert spec.loader is not None
20 spec.loader.exec_module(module)
21 return module
24def test_prepare_epw_files_can_extract_from_tar_gz(tmp_path):
25 """
26 Prepared EPW files should be extracted from a committed tar.gz bundle.
27 """
28 module = load_solar_prepare_epw()
29 archive_path = tmp_path / "tmy2_epw.tar.gz"
30 archive_source_dir = tmp_path / "source"
31 archive_source_dir.mkdir()
32 epw_source = archive_source_dir / "TMY_NZ_AK.epw"
33 epw_source.write_text(
34 "\n".join(
35 [
36 "LOCATION,Auckland,Auckland,New Zealand,TMY2 NIWA,0,0,0,0,0",
37 "DESIGN CONDITIONS,0",
38 "TYPICAL/EXTREME PERIODS,0",
39 "GROUND TEMPERATURES,0",
40 "HOLIDAYS/DAYLIGHT SAVING,No,0,0,0",
41 "COMMENTS 1,Test",
42 "COMMENTS 2,Test",
43 "DATA PERIODS,1,1,TMY2 Year,Sunday,1,365",
44 "2007,01,01,01,60",
45 ]
46 ),
47 encoding="utf-8",
48 )
49 with tarfile.open(archive_path, "w:gz") as handle:
50 handle.add(epw_source, arcname="tmy2_epw/TMY_NZ_AK.epw")
52 prepared_dir = tmp_path / "prepared_epw"
53 metadata_dir = tmp_path / "metadata"
54 module.PREFERRED_EPW_DIR = tmp_path / "missing"
55 module.PREFERRED_EPW_ARCHIVE = archive_path
56 module.PREPARED_EPW_DIR = prepared_dir
57 module.PREPARED_EPW_SENTINEL = prepared_dir / ".prepared"
58 module.METADATA_DIR = metadata_dir
60 module.prepare_epw_files()
62 extracted = prepared_dir / "TMY_NZ_AK.epw"
63 assert extracted.exists()
64 assert module.PREPARED_EPW_SENTINEL.exists()
66 summary = json.loads((metadata_dir / "prepare_epw_summary.json").read_text())
67 assert summary["source_type"] == "archive"
68 assert summary["source_path"] == str(archive_path)
69 assert summary["copied_files"] == 1