-
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.
- Loading branch information
knokko
committed
Dec 2, 2024
1 parent
51e4e99
commit 982887f
Showing
4 changed files
with
92 additions
and
14 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 |
---|---|---|
@@ -1,2 +1,8 @@ | ||
def add_orderings(meta: dict): | ||
print('hello', meta) | ||
def add_orderings(target: dict) -> dict: | ||
next_ordering = 0 | ||
for value in target.values(): | ||
if isinstance(value, dict): | ||
value['ordering'] = next_ordering | ||
next_ordering += 1 | ||
add_orderings(value) | ||
return target |
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 was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,8 +1,87 @@ | ||
import unittest | ||
from .context import helpor | ||
from helpor.dict_ordering import add_orderings | ||
|
||
|
||
class TestDictOrdering(unittest.TestCase): | ||
def test_something(self): | ||
helpor.dict_ordering.add_orderings({"hello": "world"}) | ||
self.assertEqual(True, False) | ||
def test_empty_dict(self): | ||
self.assertEqual({}, add_orderings({})) | ||
|
||
def test_dict_without_nesting(self): | ||
self.assertEqual({'hello': 'world', 'counter': 1}, add_orderings({'hello': 'world', 'counter': 1})) | ||
|
||
def test_dict_with_one_level(self): | ||
original = { | ||
'nope': None, | ||
'first': { | ||
'go': 'not deep' | ||
}, | ||
'second': {}, | ||
'last': 1234 | ||
} | ||
expected = { | ||
'nope': None, | ||
'first': { | ||
'go': 'not deep', | ||
'ordering': 0 | ||
}, | ||
'second': { | ||
'ordering': 1 | ||
}, | ||
'last': 1234 | ||
} | ||
self.assertEqual(expected, add_orderings(original)) | ||
|
||
def test_deeper_dict(self): | ||
original = { | ||
'counter1': {'type': 'number'}, | ||
'counter2': {'type': 'number'}, | ||
'lets': { | ||
'type': 'list', | ||
'counter3': {'type': 'number'}, | ||
'go': { | ||
'type': 'object', | ||
'deeper': { | ||
'object1': { | ||
'hello': 'world' | ||
}, | ||
'object2': { | ||
'this is': 'deep enough' | ||
} | ||
} | ||
} | ||
} | ||
} | ||
expected = { | ||
'counter1': { | ||
'type': 'number', | ||
'ordering': 0 | ||
}, | ||
'counter2': { | ||
'type': 'number', | ||
'ordering': 1 | ||
}, | ||
'lets': { | ||
'type': 'list', | ||
'counter3': { | ||
'type': 'number', | ||
'ordering': 0 | ||
}, | ||
'go': { | ||
'type': 'object', | ||
'deeper': { | ||
'object1': { | ||
'hello': 'world', | ||
'ordering': 0 | ||
}, | ||
'object2': { | ||
'this is': 'deep enough', | ||
'ordering': 1 | ||
}, | ||
'ordering': 0 | ||
}, | ||
'ordering': 1 | ||
}, | ||
'ordering': 2 | ||
} | ||
} | ||
self.assertEqual(expected, add_orderings(original)) |