Skip to content

Commit

Permalink
Fix for general config failing without definitions (#288)
Browse files Browse the repository at this point in the history
* Modify missing definition directory error condition

* Add test for using only general config

* Fix bug that broke tests

* Parametrize external definitions repo test

* Apply suggestions from code review

Co-authored-by: Daniel Huppmann <[email protected]>

* Fix tests

---------

Co-authored-by: Daniel Huppmann <[email protected]>
  • Loading branch information
phackstock and danielhuppmann authored Oct 11, 2023
1 parent 7196480 commit 75d3a63
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 6 deletions.
7 changes: 4 additions & 3 deletions nomenclature/definition.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,14 @@ def __init__(self, path, dimensions=None):
if not isinstance(path, Path):
path = Path(path)

if not path.is_dir():
raise NotADirectoryError(f"Definitions directory not found: {path}")

if (file := path.parent / "nomenclature.yaml").exists():
self.config = NomenclatureConfig.from_file(file=file)
else:
self.config = None

if not path.is_dir() and (self.config is None or not self.config.repositories):
raise NotADirectoryError(f"Definitions directory not found: {path}")

self.dimensions = dimensions or ["region", "variable"]
for dim in self.dimensions:
codelist_cls = SPECIAL_CODELIST.get(dim, CodeList)
Expand Down
10 changes: 10 additions & 0 deletions tests/data/general-config-only/nomenclature.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
repositories:
common-definitions:
url: https://github.com/IAMconsortium/common-definitions.git/
definitions:
region:
repository: common-definitions
country: true
variable:
repository: common-definitions
repository_dimension_path: definitions/variable
8 changes: 5 additions & 3 deletions tests/test_definition.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,16 @@ def test_empty_codelist_raises():
DataStructureDefinition(TEST_DATA_DIR / "simple_codelist")


def test_definition_from_general_config():
@pytest.mark.parametrize("workflow_folder", ["general-config-only", "general-config"])
def test_definition_from_general_config(workflow_folder):
obs = DataStructureDefinition(
TEST_DATA_DIR / "general-config" / "definitions",
TEST_DATA_DIR / workflow_folder / "definitions",
dimensions=["region", "variable"],
)
try:
# explicitly defined in `general-config-definitions/region/regions.yaml`
assert "Region A" in obs.region
if workflow_folder == "general-config":
assert "Region A" in obs.region
# imported from https://github.com/IAMconsortium/common-definitions repo
assert "World" in obs.region
# added via general-config definitions
Expand Down

0 comments on commit 75d3a63

Please sign in to comment.