cleanup io handling and Propagation

This commit is contained in:
Benoît Sierro
2023-08-09 11:21:57 +02:00
parent a650169443
commit 7bb15871c3
6 changed files with 177 additions and 76 deletions

View File

@@ -7,7 +7,7 @@ import pytest
from scgenerator.io import DataFile, MemoryIOHandler, ZipFileIOHandler, unique_name
from scgenerator.parameter import Parameters
from scgenerator.spectra import Propagation
from scgenerator.spectra import PARAMS_FN, propagation
PARAMS = dict(
name="PM2000D sech pulse",
@@ -78,13 +78,34 @@ def test_memory():
assert len(io) == 8
def test_reopen(tmp_path: Path):
zpath = tmp_path / "file.zip"
params = Parameters(**PARAMS)
prop = propagation(zpath, params)
prop2 = propagation(zpath)
assert prop.parameters == prop2.parameters
def test_clear(tmp_path: Path):
params = Parameters(**PARAMS)
zpath = tmp_path / "file.zip"
prop = propagation(zpath, params)
assert zpath.exists()
assert zpath.read_bytes() != b""
prop.io.clear()
assert not zpath.exists()
def test_zip_bundle(tmp_path: Path):
params = Parameters(**PARAMS)
io = ZipFileIOHandler(tmp_path / "file.zip")
prop = Propagation(io, params.copy(True))
assert (tmp_path / "file.zip").exists()
assert (tmp_path / "file.zip").read_bytes() != b""
with pytest.raises(FileNotFoundError):
propagation(tmp_path / "file2.zip", params, bundle_data=True)
new_disp_path = Path("./tests/data/PM2000D_2 extrapolated 4 0.npz")
new_aeff_path = Path("./tests/data/PM2000D_A_eff_marcuse.npz")
@@ -92,30 +113,32 @@ def test_zip_bundle(tmp_path: Path):
params.effective_area_file.path = str(new_aeff_path)
params.freeze()
io = ZipFileIOHandler(tmp_path / "file2.zip")
prop2 = Propagation(io, params, True)
prop2 = propagation(tmp_path / "file3.zip", params, bundle_data=True)
assert params.dispersion_file.path == new_disp_path.name
assert params.dispersion_file.prefix == "zip"
assert params.effective_area_file.path == new_aeff_path.name
assert params.effective_area_file.prefix == "zip"
assert prop2.parameters.dispersion_file.path == new_disp_path.name
assert prop2.parameters.dispersion_file.prefix == "zip"
assert prop2.parameters.effective_area_file.path == new_aeff_path.name
assert prop2.parameters.effective_area_file.prefix == "zip"
with ZipFile(tmp_path / "file2.zip", "r") as zfile:
with ZipFile(tmp_path / "file3.zip", "r") as zfile:
with zfile.open(new_aeff_path.name) as file:
assert file.read() == new_aeff_path.read_bytes()
with zfile.open(new_disp_path.name) as file:
assert file.read() == new_disp_path.read_bytes()
with zfile.open(Propagation.PARAMS_FN) as file:
with zfile.open(PARAMS_FN) as file:
df = json.loads(file.read().decode())
assert (
df["dispersion_file"] == params.dispersion_file.prefix + "::" + params.dispersion_file.path
df["dispersion_file"]
== prop2.parameters.dispersion_file.prefix + "::" + Path(params.dispersion_file.path).name
)
assert (
df["effective_area_file"]
== params.effective_area_file.prefix + "::" + params.effective_area_file.path
== prop2.parameters.effective_area_file.prefix
+ "::"
+ Path(params.effective_area_file.path).name
)