Skip to content

Commit

Permalink
#34 add tests and fix sorting algo
Browse files Browse the repository at this point in the history
  • Loading branch information
mrwunderbar666 committed Sep 11, 2023
1 parent 3127a7d commit db4dd2c
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 3 deletions.
6 changes: 3 additions & 3 deletions flaskinventory/flaskdgraph/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
66 changes: 66 additions & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit db4dd2c

Please sign in to comment.