-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
6 changed files
with
157 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -37,6 +37,7 @@ dependencies = [ | |
[project.optional-dependencies] | ||
dev = [ | ||
"pytest", | ||
"deepdiff", | ||
] | ||
|
||
[project.license] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 1 addition & 37 deletions
38
tests/deserialization_test.py → tests/020_deserialization_test.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import ontopint | ||
import deepdiff | ||
|
||
def test_default_keys(): | ||
"""test input data with default keys 'value' and 'unit' | ||
""" | ||
|
||
test = { | ||
"value": ontopint.ureg.Quantity( | ||
1.123, ontopint.ureg.from_ucum("eV") | ||
) | ||
} | ||
expected = { | ||
"value": 1.123, | ||
"unit": "qunit:EV" | ||
} | ||
result = ontopint.export_units(test) | ||
del result["@context"] | ||
assert (len(deepdiff.DeepDiff(expected, result).keys()) == 0) # no diff | ||
|
||
def test_custom_keys(): | ||
"""test input data with custom keys 'my_value' and 'my_unit' | ||
""" | ||
test = { | ||
"@context": { | ||
"qudt": "http://qudt.org/schema/qudt/", | ||
"qunit": "http://qudt.org/vocab/unit/", | ||
"qkind": "http://qudt.org/vocab/quantkind/", | ||
"my_unit": { | ||
"@id": "qudt:hasUnit", | ||
"@type": "@id" | ||
}, | ||
"my_value": "qudt:value", | ||
}, | ||
"my_value": ontopint.ureg.Quantity( | ||
1.123, ontopint.ureg.from_ucum("eV") | ||
) | ||
} | ||
expected = { | ||
"my_value": 1.123, | ||
"my_unit": "qunit:EV" | ||
} | ||
result = ontopint.export_units(test) | ||
del result["@context"] | ||
assert (len(deepdiff.DeepDiff(expected, result).keys()) == 0) # no diff | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import ontopint | ||
import deepdiff | ||
|
||
from common import _load_test_data, _recursive_items | ||
|
||
def test_default_keys(): | ||
"""test input data with default keys 'value' and 'unit' | ||
""" | ||
input_jsonld = _load_test_data("test_data_default_keys.jsonld") | ||
parsed_jsonld = ontopint.parse_units(input_jsonld) | ||
result = ontopint.export_units(parsed_jsonld) | ||
assert (len(deepdiff.DeepDiff(input_jsonld, result).keys()) == 0) # no diff | ||
|
||
def test_custom_keys(): | ||
"""test input data with custom keys 'my_value' and 'my_unit' | ||
""" | ||
input_jsonld = _load_test_data("test_data_custom_keys.jsonld") | ||
parsed_jsonld = ontopint.parse_units(input_jsonld) | ||
result = ontopint.export_units(parsed_jsonld) | ||
diff = deepdiff.DeepDiff(input_jsonld, result) | ||
print(deepdiff.DeepDiff(input_jsonld, result)) | ||
assert (len(deepdiff.DeepDiff(input_jsonld, result).keys()) == 0) # no diff | ||
|
||
test_custom_keys() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import json | ||
import os | ||
|
||
|
||
def _load_test_data(file_name: str) -> dict: | ||
"""loads a json file from the test data folder | ||
Parameters | ||
---------- | ||
file_name | ||
the name of the json file | ||
Returns | ||
------- | ||
the content of the file as dict | ||
""" | ||
data = {} | ||
with open(os.path.join(os.path.dirname(__file__), "data", file_name)) as f: | ||
data = json.load(f) | ||
return data | ||
|
||
def _recursive_items(dictionary: dict): | ||
"""Returns a generator of tuples for every key-value pair in the dict | ||
Parameters | ||
---------- | ||
dictionary | ||
any (nested) dict | ||
Yields | ||
------ | ||
iterator for key-value tuples of the dict | ||
""" | ||
for key, value in dictionary.items(): | ||
if type(value) is dict: | ||
yield (key, value) | ||
yield from _recursive_items(value) | ||
else: | ||
yield (key, value) |