diff --git a/flaskinventory/flaskdgraph/utils.py b/flaskinventory/flaskdgraph/utils.py index b445c3d..1d0a701 100644 --- a/flaskinventory/flaskdgraph/utils.py +++ b/flaskinventory/flaskdgraph/utils.py @@ -40,14 +40,14 @@ def validate_uid(uid: Any) -> Union[str, bool]: def restore_sequence(d: dict, sortkey = 'sequence') -> None: sortable_keys = list(filter(lambda x: x.endswith('|' + sortkey), d.keys())) - print('found the following predicates', sortable_keys) for facet in sortable_keys: predicate = facet.replace('|' + sortkey, '') if predicate not in d: # skip over edge attributes continue - print('restoring sequence for', predicate) - correct_sequence = [d[predicate][d[facet][k]] for k, v in d[facet].items()] + correct_sequence = list(range(len(d[predicate]))) + for k, v in d[facet].items(): + correct_sequence[int(v)] = d[predicate][int(k)] d[predicate] = correct_sequence def recursive_restore_sequence(l: list, sortkey = 'sequence') -> None: diff --git a/tests/test_utils.py b/tests/test_utils.py new file mode 100644 index 0000000..cef738e --- /dev/null +++ b/tests/test_utils.py @@ -0,0 +1,66 @@ +import unittest + +if __name__ == "__main__": + from sys import path + from os.path import dirname + from requests import HTTPError + import unittest + + path.append(dirname(path[0])) + +from flaskinventory.flaskdgraph.utils import restore_sequence, recursive_restore_sequence + +class TestUtils(unittest.TestCase): + + def test_restore_sequence(self): + d = {'_authors_fallback': ['Author B', 'Author D', 'Author A', 'Author C'], + '_authors_fallback|sequence': + {'0': 1, + '1': 3, + '2': 0, + '3': 2}} + + solution = ['Author A', 'Author B', 'Author C', 'Author D'] + + restore_sequence(d) + + self.assertListEqual(d['_authors_fallback'], solution) + + def test_recursive_restore_sequence(self): + l = [{ + '_authors_fallback': ['Author B', 'Author D', 'Author A', 'Author C'], + '_authors_fallback|sequence': { + '0': 1, + '1': 3, + '2': 0, + '3': 2} + }, + { + '_authors_fallback': ['Author D', 'Author B', 'Author C', 'Author A'], + '_authors_fallback|sequence': { + '0': 3, + '1': 1, + '2': 2, + '3': 0} + }, + { + '_authors_fallback': ['Author A'], + '_authors_fallback|sequence': { + '0': 0} + }, + { + '_authors_fallback': ['Author A'] + } + ] + + solution = ['Author A', 'Author B', 'Author C', 'Author D'] + recursive_restore_sequence(l) + + self.assertListEqual(l[0]['_authors_fallback'], solution) + self.assertListEqual(l[1]['_authors_fallback'], solution) + self.assertListEqual(l[2]['_authors_fallback'], ['Author A']) + + + +if __name__ == "__main__": + unittest.main(verbosity=2)