Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
feat(onto creation): allow that resources and properties are not sort…
…ed by inheritance (DEV-626) (#167) * finished & tested * apply reviewer's feedback * improve type annotations * add unit tests for both new methods
- Loading branch information
Showing
2 changed files
with
110 additions
and
2 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
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,45 @@ | ||
"""unit tests for ontology creation""" | ||
import unittest | ||
import json | ||
from typing import Any | ||
|
||
from knora.dsplib.utils.onto_create_ontology import * | ||
|
||
|
||
class TestOntoCreation(unittest.TestCase): | ||
ontology: dict[str, Any] = json.loads('testdata/test-onto.json')['project']['ontologies'][0] | ||
|
||
def test_sort_resources(self) -> None: | ||
""" | ||
The 'resources' section of an onto is a list of dictionaries. The safest way to test | ||
that the sorted list contains the same dicts is to sort both lists according to the | ||
same criteria, and then test for list equality. | ||
""" | ||
onto_name: str = self.ontology['name'] | ||
unsorted_resources: list[dict[str, Any]] = self.ontology['resources'] | ||
sorted_resources = sort_resources(unsorted_resources, onto_name) | ||
|
||
unsorted_resources = sorted(unsorted_resources, key=lambda a: a['name']) | ||
sorted_resources = sorted(sorted_resources, key=lambda a: a['name']) | ||
|
||
self.assertListEqual(unsorted_resources, sorted_resources) | ||
|
||
|
||
def test_sort_prop_classes(self) -> None: | ||
""" | ||
The 'properties' section of an onto is a list of dictionaries. The safest way to test | ||
that the sorted list contains the same dicts is to sort both lists according to the | ||
same criteria, and then test for list equality. | ||
""" | ||
onto_name: str = self.ontology['name'] | ||
unsorted_props: list[dict[str, Any]] = self.ontology['resources'] | ||
sorted_props = sort_prop_classes(unsorted_props, onto_name) | ||
|
||
unsorted_props = sorted(unsorted_props, key=lambda a: a['name']) | ||
sorted_props = sorted(sorted_props, key=lambda a: a['name']) | ||
|
||
self.assertListEqual(unsorted_props, sorted_props) | ||
|
||
|
||
if __name__ == '__main__': | ||
unittest.main() |