Skip to content
Snippets Groups Projects
Commit a385fee5 authored by Florian Spreckelsen's avatar Florian Spreckelsen
Browse files

TST: Add unit test for record creation

parent 107598cf
No related branches found
No related tags found
2 merge requests!198REL: Release 0.10.0,!187F unit
Pipeline #56394 failed
...@@ -167,9 +167,9 @@ following. ...@@ -167,9 +167,9 @@ following.
ValueWithUnitElt: ValueWithUnitElt:
type: TextElement type: TextElement
match_name: ^my_prop$ match_name: ^my_prop$
match_value: "^(?P<number>\d+\.?\d*)\s+(?P<unit>.+)" # Extract value and unit from a string which match_value: "^(?P<number>\\d+\\.?\\d*)\s+(?P<unit>.+)" # Extract value and unit from a string which
# has a number followed by at least one whitespace # has a number followed by at least one whitespace
# character followed by a unit. # character followed by a unit.
records: records:
MyRecord: MyRecord:
MyProp: MyProp:
......
...@@ -316,3 +316,57 @@ def test_record_parents(): ...@@ -316,3 +316,57 @@ def test_record_parents():
assert rec.parents[0].name == 'Stuff' # default parent stays if no parent is given on assert rec.parents[0].name == 'Stuff' # default parent stays if no parent is given on
# lower levels # lower levels
assert len(rec.parents) == 1 assert len(rec.parents) == 1
def test_units():
"""Test the correct setting of units."""
crawler_definition = load_definition(UNITTESTDIR / "test_unit_cfood.yml")
converter_registry = create_converter_registry(crawler_definition)
data = {
"value_with_unit": "1.1 m",
"array_with_units": [
"1.1 cm",
"2.2 cm"
]
}
records = scan_structure_elements(DictElement(name="", value=data), crawler_definition,
converter_registry)
assert len(records) == 1
rec = records[0]
# This is hard-coded in cfood:
assert rec.get_property("may_be_overwritten") is not None
assert rec.get_property("may_be_overwritten").value == "12"
assert rec.get_property("may_be_overwritten").unit == "K"
# Those are set from data
assert rec.get_property("value_with_unit") is not None
assert rec.get_property("value_with_unit").value == "1.1"
assert rec.get_property("value_with_unit").unit == "m"
assert rec.get_property("list_with_unit") is not None
assert rec.get_property("list_with_unit").value == ["1.1", "2.2"]
assert rec.get_property("list_with_unit").unit == "cm"
# Contradictory units
data = {
"array_with_units": [
"1.1 K",
"45 W"
]
}
with raises(RuntimeError) as rte:
records = scan_structure_elements(DictElement(name="", value=data), crawler_definition,
converter_registry)
assert "Property 'list_with_unit' has contradictory units" in str(rte.value)
# Overwrite value and unit
data = {
"may_be_overwritten": "400 °C"
}
records = scan_structure_elements(DictElement(name="", value=data), crawler_definition,
converter_registry)
assert len(records) == 1
rec = records[0]
# Now set from data
assert rec.get_property("may_be_overwritten") is not None
assert rec.get_property("may_be_overwritten").value == "400"
assert rec.get_property("may_be_overwritten").unit == "°C"
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment