Files
SP2XR/tests/test_xr_ini_to_yaml.py

40 lines
1.3 KiB
Python

from pathlib import Path
import yaml
from sp2xr.helpers import read_xr_ini_file, export_xr_ini_to_yaml
# Paths for testing
DATA = Path(__file__).parent / "data"
INI_FILE = DATA / "sample_SP2XR.ini"
YAML_FILE = DATA / "sample_SP2XR_config.yaml"
def test_ini_to_yaml_conversion(tmp_path):
"""Test that an .ini file is correctly parsed and exported to YAML."""
# 1. Read parameters from ini
params = read_xr_ini_file(INI_FILE)
assert isinstance(params, dict)
assert "SaveRate" in params, "Missing expected parameter from ini parsing"
# 2. Export to YAML
yaml_path = tmp_path / "out_config.yaml"
export_xr_ini_to_yaml(INI_FILE, yaml_path)
assert yaml_path.exists(), "YAML file was not created"
# 3. Reload YAML and check values
with open(yaml_path, "r") as f:
loaded = yaml.safe_load(f)
assert "SaveRate" in loaded, "SaveRate not found in YAML output"
# 4. Simulate user adding calibration
loaded["calibration"] = {"mass_factor": 1.05, "mass_offset": -0.1}
with open(yaml_path, "w") as f:
yaml.dump(loaded, f, sort_keys=False)
# 5. Reload and verify calibration is preserved
with open(yaml_path, "r") as f:
updated = yaml.safe_load(f)
assert "calibration" in updated, "Calibration section missing after update"
print("✅ INI → YAML conversion test passed successfully")