Skip to content

Commit

Permalink
Add tier attribute tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dc-almeida committed Oct 29, 2024
1 parent 41ff25b commit 6a512e6
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 0 deletions.
4 changes: 4 additions & 0 deletions tests/data/codelist/tier_attribute/invalid/tag_fuel.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
- Fuel:
- Coal:
description: coal
tier: +1
11 changes: 11 additions & 0 deletions tests/data/codelist/tier_attribute/invalid/variables.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
- Primary Energy:
description: Total primary energy consumption
unit: EJ/yr
- Primary Energy|{Fuel}:
description: Primary energy consumption of {Fuel}
unit: EJ/yr
tier: 1
- Share|{Fuel}:
description: Share of {Fuel} in the total primary energy mix
unit: '%'
tier: 2
4 changes: 4 additions & 0 deletions tests/data/codelist/tier_attribute/valid/tag_fuel.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
- Fuel:
- Coal:
description: coal
tier: ^1
6 changes: 6 additions & 0 deletions tests/data/codelist/tier_attribute/valid/tag_sector.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
- Sector:
- Agriculture:
description: agriculture sector including fishing
tier: ^1
- Industry:
description: industrial sector excluding non-energy use (e.g.feedstocks)
11 changes: 11 additions & 0 deletions tests/data/codelist/tier_attribute/valid/variables.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
- Primary Energy:
description: Total primary energy consumption
unit: EJ/yr
- Primary Energy|{Fuel}|{Sector}:
description: Primary energy consumption of {Fuel}
unit: EJ/yr
tier: 1
- Share|{Fuel}:
description: Share of {Fuel} in the total primary energy mix
unit: '%'
tier: 2
30 changes: 30 additions & 0 deletions tests/test_codelist.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,36 @@ def test_tags_in_list_attributes():
assert getattr(code[code_name], attr_name) == value


def test_tier_attribute_in_tags():
"""Check for tier attribute functionality ('tier' in tags upgrade CodeList's):
1) 'tier' is not added when not present in Code or tag;
2) 'tier' is/are upgraded when present in Code and matching tag(s)
3) 'tier' in tag only accepts two values: '^1' and '^2'"""
code_list = VariableCodeList.from_directory(
"variable", MODULE_TEST_DATA_DIR / "tier_attribute" / "valid"
)
# check tier attribute is upgraded correctly
assert code_list["Share|Coal"].tier == 3
assert code_list["Primary Energy|Coal|Industry"].tier == 2

# check multiple tier attributes upgrade cumulatively
assert code_list["Primary Energy|Coal|Agriculture"].tier == 3

# check codes without tier attributes don't change
assert not code_list["Primary Energy"].tier


def test_misformatted_tier_fails():
"""Check misformatted 'tier' attributes raise errors"""

match = "Tag 'Primary Energy|Coal' includes misformatted 'tier' attribute. "
"Allowed values for tier attributes in tags are '^1' or '^2'."
with pytest.raises(ValueError, match=match):
VariableCodeList.from_directory(
"variable", MODULE_TEST_DATA_DIR / "tier_attribute" / "invalid"
)


def test_region_codelist():
"""Check replacing top-level hierarchy of yaml file as attribute for regions"""
code = RegionCodeList.from_directory(
Expand Down

0 comments on commit 6a512e6

Please sign in to comment.