From ef86d7c28bf25efe531b46e8cc8b08282883cdb1 Mon Sep 17 00:00:00 2001 From: Karthik Rishinarada Date: Tue, 7 Jan 2025 00:04:04 +0530 Subject: [PATCH 1/4] add tests for config validator --- .../tests/test_config_validator.py | 74 ++++++++++++++++++- 1 file changed, 72 insertions(+), 2 deletions(-) diff --git a/tardis/io/configuration/tests/test_config_validator.py b/tardis/io/configuration/tests/test_config_validator.py index ee96b004089..03bbb486a5d 100644 --- a/tardis/io/configuration/tests/test_config_validator.py +++ b/tardis/io/configuration/tests/test_config_validator.py @@ -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) From 7b3ab2faae20d04df4d35bd5f762f239de3f3b3e Mon Sep 17 00:00:00 2001 From: Karthik Rishinarada Date: Tue, 7 Jan 2025 00:19:38 +0530 Subject: [PATCH 2/4] fix indentation bug --- tardis/workflows/standard_tardis_workflow.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tardis/workflows/standard_tardis_workflow.py b/tardis/workflows/standard_tardis_workflow.py index e4ed2433799..d4c11e6820b 100644 --- a/tardis/workflows/standard_tardis_workflow.py +++ b/tardis/workflows/standard_tardis_workflow.py @@ -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" From a10f30158b82c1a8f6fcf581c131e138fab18ba7 Mon Sep 17 00:00:00 2001 From: Karthik Rishinarada Date: Tue, 7 Jan 2025 00:27:01 +0530 Subject: [PATCH 3/4] add email in .mailmap --- .mailmap | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.mailmap b/.mailmap index ed00506c00f..95ec8111936 100644 --- a/.mailmap +++ b/.mailmap @@ -119,6 +119,8 @@ Josh Shields Karan Desai Karan Desai karandesai-96 +Karthik Rishinarada + Kaushik Varanasi Kaushik Varanasi kaushik94 From 0f7e4523b93e17b8fc455006f9d1c197b012bcdb Mon Sep 17 00:00:00 2001 From: Karthik Rishinarada Date: Thu, 23 Jan 2025 10:18:07 +0530 Subject: [PATCH 4/4] remove irrelavent indentation change --- tardis/workflows/standard_tardis_workflow.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tardis/workflows/standard_tardis_workflow.py b/tardis/workflows/standard_tardis_workflow.py index d4c11e6820b..e4ed2433799 100644 --- a/tardis/workflows/standard_tardis_workflow.py +++ b/tardis/workflows/standard_tardis_workflow.py @@ -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"