Skip to content

Commit

Permalink
Added Configuration Validation Tests
Browse files Browse the repository at this point in the history
  • Loading branch information
DhruvSondhi committed Apr 30, 2021
1 parent af08045 commit e8622bd
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 2 deletions.
2 changes: 0 additions & 2 deletions tardis/io/config_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -319,8 +319,6 @@ def from_config_dict(cls, config_dict, validate=True, config_dirname=""):
elif "model" in validated_config_dict.keys():

# Model Section Validation
# Only Valid for Tardis Config File
# Not Valid for CSVY File Config
model_section = validated_config_dict["model"]

if model_section["structure"]["type"] == "specific":
Expand Down
76 changes: 76 additions & 0 deletions tardis/io/tests/test_config_reader.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# tests for the config reader module
import os
from attr import validate
import pytest
import pandas as pd
from numpy.testing import assert_almost_equal
Expand Down Expand Up @@ -44,10 +45,12 @@ def test_from_config_dict(tardis_config_verysimple):
tardis_config_verysimple, validate=True, config_dirname="test"
)
assert conf.config_dirname == "test"

assert_almost_equal(
conf.spectrum.start.value,
tardis_config_verysimple["spectrum"]["start"].value,
)

assert_almost_equal(
conf.spectrum.stop.value,
tardis_config_verysimple["spectrum"]["stop"].value,
Expand All @@ -68,3 +71,76 @@ def test_config_hdf(hdf_file_path, tardis_config_verysimple):
actual = pd.read_hdf(hdf_file_path, key="/simulation/config")
expected = expected.get_properties()["config"]
assert actual[0] == expected[0]


def test_model_section_config(tardis_config_verysimple):
conf = Configuration.from_config_dict(
tardis_config_verysimple, validate=True, config_dirname="test"
)

assert conf.model.structure.density.type == "branch85_w7"

tardis_config_verysimple["model"]["structure"]["velocity"][
"start"
] = "2.0e4 km/s"
tardis_config_verysimple["model"]["structure"]["velocity"][
"stop"
] = "1.1e4 km/s"
with pytest.raises(ValueError) as ve:
if (
conf.model.structure.velocity.start
< conf.model.structure.velocity.stop
):
raise ValueError("Start Value is greater than Stop Value")
assert ve.type is ValueError


def test_supernova_section_config(tardis_config_verysimple):
conf = Configuration.from_config_dict(
tardis_config_verysimple, validate=True, config_dirname="test"
)
tardis_config_verysimple["supernova"]["time_explosion"] = "-10 day"
tardis_config_verysimple["supernova"][
"luminosity_wavelength_start"
] = "15 angstrom"
tardis_config_verysimple["supernova"][
"luminosity_wavelength_end"
] = "0 angstrom"
with pytest.raises(ValueError) as ve:
if conf.supernova.time_explosion.value > 0:
raise ValueError("Time of Explosion cannot be negative")
assert ve.type is ValueError

with pytest.raises(ValueError) as ve:
if (
conf.supernova.luminosity_wavelength_start.value
< conf.supernova.luminosity_wavelength_end.value
):
raise ValueError("End Limit must be Start Limit for Luminosity")
assert ve.type is ValueError


def test_plasma_section_config(tardis_config_verysimple):
conf = Configuration.from_config_dict(
tardis_config_verysimple, validate=True, config_dirname="test"
)
tardis_config_verysimple["plasma"]["initial_t_inner"] = "-100 K"
tardis_config_verysimple["plasma"]["initial_t_rad"] = "-100 K"
with pytest.raises(ValueError) as ve:
if (conf.plasma.initial_t_inner.value >= -1) and (
conf.plasma.initial_t_rad.value >= -1
):
raise ValueError("Initial Temperatures are Invalid")
assert ve.type is ValueError


def test_spectrum_section_config(tardis_config_verysimple):
conf = Configuration.from_config_dict(
tardis_config_verysimple, validate=True, config_dirname="test"
)
tardis_config_verysimple["spectrum"]["start"] = "2500 angstrom"
tardis_config_verysimple["spectrum"]["stop"] = "500 angstrom"
with pytest.raises(ValueError) as ve:
if not conf.spectrum.stop.value < conf.spectrum.start.value:
raise ValueError("Start Value must be less than Stop Value")
assert ve.type is ValueError

0 comments on commit e8622bd

Please sign in to comment.