Skip to content

Commit

Permalink
Improve quantity check
Browse files Browse the repository at this point in the history
Give proper error if "definition" is not given and not any other
attributes either.
(In that case v is NoneType som you cannot check for "definition")

Signed-off-by: Erik Jaegervall <[email protected]>
  • Loading branch information
erikbosch committed Dec 11, 2023
1 parent 90c3d95 commit c91d792
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 3 deletions.
2 changes: 2 additions & 0 deletions tests/vspec/test_units/quantities_no_def.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Quantity but no definition which is mandatory
volume:
13 changes: 11 additions & 2 deletions tests/vspec/test_units/test_units.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@ def run_unit(vspec_file, unit_argument, expected_file, quantity_argument="",
os.system("rm -f out.txt")


def run_unit_error(vspec_file, unit_argument, grep_error):
def run_unit_error(vspec_file, unit_argument, grep_error, quantity_argument=""):
test_str = "../../../vspec2json.py --json-pretty " + \
vspec_file + " " + unit_argument + " out.json > out.txt 2>&1"
vspec_file + " " + unit_argument + " " + quantity_argument + " out.json > out.txt 2>&1"
result = os.system(test_str)
assert os.WIFEXITED(result)
# failure expected
Expand Down Expand Up @@ -165,3 +165,12 @@ def test_quantity_redefinition(change_test_dir):
"--unit-file units_all.yaml",
"expected_special.json",
"-q quantity_volym.yaml -q quantity_volym.yaml", True, "Redefinition of quantity volym")


def test_quantity_err_no_def(change_test_dir):
"""
Test scenario when definition is not given
"""
run_unit_error("signals_with_special_units.vspec", "-u units_all.yaml",
"No definition found for quantity volume",
"-q quantities_no_def.yaml")
3 changes: 2 additions & 1 deletion vspec/model/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
from typing import (
Sequence, Type, TypeVar, Optional, Dict, TextIO
)
from collections import abc

import yaml

Expand Down Expand Up @@ -228,7 +229,7 @@ def load_config_file(cls, config_file: str) -> int:
my_quantities = yaml.safe_load(my_yaml_file)
added_quantities = len(my_quantities)
for k, v in my_quantities.items():
if "definition" in v:
if isinstance(v, abc.Mapping) and "definition" in v:
definition = v["definition"]
else:
logging.error("No definition found for quantity %s", k)
Expand Down

0 comments on commit c91d792

Please sign in to comment.