Skip to content

Commit

Permalink
Update testing
Browse files Browse the repository at this point in the history
  • Loading branch information
dc-almeida committed Oct 16, 2024
1 parent ca3a1c5 commit 0816561
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
dimensions:
- variable
repositories:
common-definitions:
url: https://github.com/IAMconsortium/common-definitions.git/
hash: 3b75bb9
definitions:
variable:
repository:
- common-definitions
illegal_characters: ['"' , ";"] # these are known to be present in common-definitions variables
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
- Primary Energy:
definition: Total primary energy consumption
unit: EJ/yr
- Primary Energy|Coal:
definition: Primary energy consumption of coal
unit: EJ/yr
info: Additional info with illegal " character
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
dimensions:
- variable
illegal_characters: ['"' , ";"]
28 changes: 24 additions & 4 deletions tests/test_codelist.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
MetaCodeList,
)
from nomenclature.config import NomenclatureConfig
from nomenclature.definition import DataStructureDefinition

from conftest import TEST_DATA_DIR, clean_up_external_repos

Expand Down Expand Up @@ -224,17 +225,36 @@ def test_to_csv():
@pytest.mark.parametrize(
"subfolder, match",
[
("char_in_str", r"Unexpected bracket in variable: 'Primary Energy\|{Feul}'"),
("char_in_list", r"Unexpected bracket in variable: 'Share\|Coal'"),
("char_in_dict", r"Unexpected bracket in variable: 'Primary Energy'"),
("tag_in_str", r"Unexpected character in variable: 'Primary Energy\|{Feul}'"),
("tag_in_list", r"Unexpected character in variable: 'Share\|Coal'"),
("tag_in_dict", r"Unexpected character in variable: 'Primary Energy'"),
],
)
def test_stray_tag_fails(subfolder, match):
"""Check that stray brackets from, e.g. typos in a tag, raises expected error"""
with raises(ValueError, match=match):
VariableCodeList.from_directory(
code_list = VariableCodeList.from_directory(
"variable", MODULE_TEST_DATA_DIR / "stray_tag" / subfolder
)
code_list.check_illegal_characters()


def test_illegal_char_fails():
"""Check that illegal character raises expected error."""
match = r"Unexpected character in variable: 'Primary Energy\|Coal'"
with raises(ValueError, match=match):
DataStructureDefinition(
MODULE_TEST_DATA_DIR / "illegal_chars" / "char_in_str" / "definitions"
)


def test_illegal_char_ignores_external_repo():
"""Check that external repos are excluded from this check."""
# the config includes illegal characters known to be in common-definitions
# the test will not raise errors as the check is skipped for external repos
DataStructureDefinition(
MODULE_TEST_DATA_DIR / "illegal_chars" / "char_in_external_repo" / "definitions"
)


def test_end_whitespace_fails():
Expand Down

0 comments on commit 0816561

Please sign in to comment.