Skip to content

Commit

Permalink
Add dimensions option to CLI validate-scenarios (#421)
Browse files Browse the repository at this point in the history
* Add dimensions option to CLI validate-scenarios

* Add dimensions from nomenclature.yaml to CLI validate-project

* Update tests

* Revert changes to validate-project testing

* Revert default dimensions checks for CLI validate-scenarios and update tests
  • Loading branch information
dc-almeida authored Oct 31, 2024
1 parent 0ae378f commit 4667a27
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 11 deletions.
22 changes: 20 additions & 2 deletions nomenclature/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -287,19 +287,37 @@ def cli_run_workflow(
type=click.Path(exists=True, path_type=Path),
default="definitions",
)
def cli_validate_scenarios(input_file: Path, definitions: Path):
@click.option(
"--dimension",
"dimensions",
help="Optional list of dimensions",
type=str,
multiple=True,
default=None,
)
def cli_validate_scenarios(input_file: Path, definitions: Path, dimensions: List[str]):
"""Validate a scenario file against the codelists of a project
Example
-------
$ nomenclature validate-scenarios <input-file>
--definitions <def-folder>
--dimension <folder1>
--dimension <folder2>
--dimension <folder3>
Parameters
----------
input_file : Path
Input data file, must be IAMC format, .xlsx or .csv
definitions : Path
Definitions folder with codelists, by default "definitions"
dimensions : List[str], optional
Dimensions to be checked, defaults to all sub-folders of `definitions`
Raises
------
ValueError
If input_file validation fails against specified codelist(s).
"""
DataStructureDefinition(definitions).validate(IamDataFrame(input_file))
DataStructureDefinition(definitions, dimensions).validate(IamDataFrame(input_file))
3 changes: 3 additions & 0 deletions tests/data/cli/structure_validation/nomenclature.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
dimensions:
- region
- variable
48 changes: 39 additions & 9 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -423,10 +423,15 @@ def test_cli_run_workflow(tmp_path, simple_df):


@pytest.mark.parametrize(
"status, unit, exit_code", [("valid", "EJ/yr", 0), ("invalid", "EJ", 1)]
"status, unit, dimensions, exit_code",
[
("valid_1", "EJ/yr", ["region", "variable"], 0),
("invalid", "EJ", "variable", 1),
("valid_2", "EJ", "region", 0),
],
)
def test_cli_valid_scenarios(status, unit, exit_code, tmp_path):
"""Check that CLI validates an IAMC dataset according to defined codelist."""
def test_cli_valid_scenarios(status, unit, exit_code, dimensions, tmp_path):
"""Check that CLI validates an IAMC dataset according to defined codelists."""
IamDataFrame(
pd.DataFrame(
[
Expand All @@ -435,17 +440,42 @@ def test_cli_valid_scenarios(status, unit, exit_code, tmp_path):
columns=IAMC_IDX + [2005, 2010],
)
).to_excel(tmp_path / f"{status}_data.xlsx")
dimensions = [dimensions] if isinstance(dimensions, str) else dimensions
dimension_args = []
for dim in dimensions:
dimension_args.append("--dimension")
dimension_args.append(dim)

result_valid = runner.invoke(
cli,
[
"validate-scenarios",
str(tmp_path / f"{status}_data.xlsx"),
"--definitions",
str(
MODULE_TEST_DATA_DIR
/ "structure_validation_no_mappings"
/ "definitions"
),
],
str(MODULE_TEST_DATA_DIR / "structure_validation" / "definitions"),
]
+ dimension_args,
)
assert result_valid.exit_code == exit_code


def test_cli_valid_scenarios_implicit_dimensions(tmp_path):
"""Check that CLI validates an IAMC dataset according to implicit dimensions codelists."""
IamDataFrame(
pd.DataFrame(
[
["m_a", "s_a", "World", "Primary Energy", "EJ/yr", 1, 2],
],
columns=IAMC_IDX + [2005, 2010],
)
).to_excel(tmp_path / "valid_data.xlsx")
result_valid = runner.invoke(
cli,
[
"validate-scenarios",
str(tmp_path / "valid_data.xlsx"),
"--definitions",
str(MODULE_TEST_DATA_DIR / "structure_validation" / "definitions"),
],
)
assert result_valid.exit_code == 0

0 comments on commit 4667a27

Please sign in to comment.