Skip to content

Commit

Permalink
Revert default dimensions checks for CLI validate-scenarios and updat…
Browse files Browse the repository at this point in the history
…e tests
  • Loading branch information
dc-almeida committed Oct 31, 2024
1 parent 2df7b82 commit 62f765e
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 40 deletions.
10 changes: 0 additions & 10 deletions nomenclature/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
from nomenclature.definition import DataStructureDefinition
from nomenclature.codelist import VariableCodeList
from nomenclature.processor import RegionProcessor
from nomenclature.config import NomenclatureConfig
from nomenclature.testing import assert_valid_structure, assert_valid_yaml

cli = click.Group()
Expand Down Expand Up @@ -321,13 +320,4 @@ def cli_validate_scenarios(input_file: Path, definitions: Path, dimensions: List
ValueError
If input_file validation fails against specified codelist(s).
"""
if not dimensions: # if "dimensions" were not specified
if definitions.parent / "nomenclature.yaml" in definitions.parent.iterdir():
dimensions = NomenclatureConfig.from_file(
definitions.parent / "nomenclature.yaml"
).dimensions
if not dimensions:
dimensions = [x.stem for x in definitions.iterdir() if x.is_dir()]
if not dimensions:
raise FileNotFoundError(f"`definitions` directory is empty: {definitions}")
DataStructureDefinition(definitions, dimensions).validate(IamDataFrame(input_file))
2 changes: 1 addition & 1 deletion tests/data/cli/structure_validation/nomenclature.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
dimensions:
- region
- variable
- variable
44 changes: 15 additions & 29 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,7 @@ def test_cli_run_workflow(tmp_path, simple_df):
@pytest.mark.parametrize(
"status, unit, dimensions, exit_code",
[
("valid_1", "EJ/yr", "region", 0),
("valid_1", "EJ/yr", ["region", "variable"], 0),
("invalid", "EJ", "variable", 1),
("valid_2", "EJ", "region", 0),
],
Expand All @@ -440,56 +440,42 @@ def test_cli_valid_scenarios(status, unit, exit_code, dimensions, 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" / "definitions"),
"--dimension",
dimensions,
],
]
+ dimension_args,
)
assert result_valid.exit_code == exit_code


@pytest.mark.parametrize(
"dimensions_src, path, unit, exit_code",
[
(
"nomenclature_yaml_dimensions",
"structure_validation",
"EJ/yr",
0,
), # defaults to nomenclature.yaml dimensions
(
"subfolders_dimensions",
"structure_validation_no_mappings",
"EJ",
1,
), # defaults to 'definitions' subfolders dimensions
],
)
def test_cli_valid_scenarios_implicit_dimensions(
dimensions_src, path, unit, exit_code, tmp_path
):
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", unit, 1, 2],
["m_a", "s_a", "World", "Primary Energy", "EJ/yr", 1, 2],
],
columns=IAMC_IDX + [2005, 2010],
)
).to_excel(tmp_path / f"{dimensions_src}_data.xlsx")
).to_excel(tmp_path / "valid_data.xlsx")
result_valid = runner.invoke(
cli,
[
"validate-scenarios",
str(tmp_path / f"{dimensions_src}_data.xlsx"),
str(tmp_path / "valid_data.xlsx"),
"--definitions",
str(MODULE_TEST_DATA_DIR / path / "definitions"),
str(MODULE_TEST_DATA_DIR / "structure_validation" / "definitions"),
],
)
assert result_valid.exit_code == exit_code
assert result_valid.exit_code == 0

0 comments on commit 62f765e

Please sign in to comment.