-
-
Notifications
You must be signed in to change notification settings - Fork 422
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
karthik11135
wants to merge
5
commits into
tardis-sn:master
Choose a base branch
from
karthik11135:tests/config_validator
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+74
−2
Open
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
ef86d7c
add tests for config validator
karthik11135 8839785
wrote tests for config_validator
karthik11135 7b3ab2f
fix indentation bug
karthik11135 a10f301
add email in .mailmap
karthik11135 0f7e452
remove irrelavent indentation change
karthik11135 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -119,6 +119,8 @@ Josh Shields <[email protected]> | |
Karan Desai <[email protected]> | ||
Karan Desai <[email protected]> karandesai-96 <[email protected]> | ||
|
||
Karthik Rishinarada <[email protected]> | ||
|
||
Kaushik Varanasi <[email protected]> | ||
Kaushik Varanasi <[email protected]> kaushik94 <[email protected]> | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does this
with
statement mean that these tests are expected to raiseValidationError
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Validation error is occurred when we use Draft4validator instead of extending it for accepting default values (Default_validator)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So are you testing the extended validator, or not?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah. I've tested both. Default validator and Draft4validator