Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tests: config validator is now tested properly #2926

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .mailmap
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,8 @@ Josh Shields <shields.joshua.v@gmail.com>
Karan Desai <karandesai_96@live.com>
Karan Desai <karandesai_96@live.com> karandesai-96 <karandesai_96@live.com>

Karthik Rishinarada <karthikrk11135@gmail.com>

Kaushik Varanasi <kaushik.varanasi1@gmail.com>
Kaushik Varanasi <kaushik.varanasi1@gmail.com> kaushik94 <kaushik.varanasi1@gmail.com>

Expand Down
74 changes: 72 additions & 2 deletions tardis/io/configuration/tests/test_config_validator.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,72 @@
# TODO: Write tests for the new validator
pass
import pytest
from jsonschema import Draft4Validator
from pathlib import Path
from astropy import units as u
from jsonschema.exceptions import ValidationError

from tardis.io.configuration.config_validator import (
_yaml_handler,
validate_dict,
validate_yaml,
extend_with_default,
is_quantity
)

CONFIGURATION_TESTS_DIR = Path(__file__).resolve().parent
CONFIG_DATA_VERYSIMPLE = CONFIGURATION_TESTS_DIR / "data/tardis_configv1_verysimple.yml"


def test_yaml_handler(tardis_config_verysimple):
res = _yaml_handler(f"file://{CONFIG_DATA_VERYSIMPLE}")
assert res == tardis_config_verysimple

with pytest.raises(Exception):
_yaml_handler("/mock/example.yml")

with pytest.raises(FileNotFoundError):
_yaml_handler("file:///example.yml")


def test_is_quantity():
quantity_val_1 = 5 * u.m
quantity_val_2 = 5

assert is_quantity(None, quantity_val_2) == False
assert is_quantity(None, quantity_val_1) == True


def test_extend_with_default(tardis_config_verysimple):
Default_Validator = extend_with_default(Draft4Validator)

# Using the validator that extended from Draft4
dict_with_Default = validate_dict(tardis_config_verysimple, validator=Default_Validator)
assert dict_with_Default["plasma"]["initial_t_inner"] == "-1 K"

with pytest.raises(ValidationError):
dict_with_Draft4 = validate_dict(tardis_config_verysimple, validator=Draft4Validator)


def test_validate_dict(tardis_config_verysimple):
config_dict_verysimple = validate_dict(tardis_config_verysimple)

assert config_dict_verysimple["montecarlo"]["seed"] == tardis_config_verysimple["montecarlo"]["seed"]

# Checks for default value when not provided
assert config_dict_verysimple["plasma"]["initial_t_inner"] == "-1 K"
assert config_dict_verysimple["supernova"]["luminosity_wavelength_start"] == "0 angstrom"

# ValidationError because Draft4Validator cannot process default values
# Context: cannot assign default value for model_isotope_time_0 in abundances which is a required field
with pytest.raises(ValidationError):
validate_dict(tardis_config_verysimple, validator=Draft4Validator)


def test_validate_yaml():
path = CONFIG_DATA_VERYSIMPLE
dict = _yaml_handler(f"file://{CONFIG_DATA_VERYSIMPLE}")

# Provided config path retrieves validated dict
assert validate_yaml(path)["spectrum"]["num"] == dict["spectrum"]["num"]

with pytest.raises(ValidationError):
validate_yaml(path, validator=Draft4Validator)
2 changes: 1 addition & 1 deletion tardis/workflows/standard_tardis_workflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ def get_convergence_estimates(self, transport_state):
"Absorbed": [absorbed_luminosity.value, "value"],
"Requested": [self.luminosity_requested.value, "value"],
}
self.update_convergence_plot_data(plot_data)
self.update_convergence_plot_data(plot_data)

logger.info(
f"\n\tLuminosity emitted = {emitted_luminosity:.3e}\n"
Expand Down
Loading