|
1
|
|
|
import unittest |
|
2
|
|
|
|
|
3
|
|
|
from flatten_dict import flatten |
|
4
|
|
|
|
|
5
|
|
|
|
|
6
|
|
|
class Tests(unittest.TestCase): |
|
7
|
|
|
TESTS = { |
|
8
|
|
|
"Basics": [ |
|
9
|
|
|
{"input": {"key": "value"}, "answer": {"key": "value"}}, |
|
10
|
|
|
{ |
|
11
|
|
|
"input": {"key": {"deeper": {"more": {"enough": "value"}}}}, |
|
12
|
|
|
"answer": {"key/deeper/more/enough": "value"}, |
|
13
|
|
|
}, |
|
14
|
|
|
{"input": {"empty": {}}, "answer": {"empty": ""}}, |
|
15
|
|
|
{ |
|
16
|
|
|
"input": { |
|
17
|
|
|
"name": {"first": "One", "last": "Drone"}, |
|
18
|
|
|
"job": "scout", |
|
19
|
|
|
"recent": {}, |
|
20
|
|
|
"additional": {"place": {"zone": "1", "cell": "2"}}, |
|
21
|
|
|
}, |
|
22
|
|
|
"answer": { |
|
23
|
|
|
"name/first": "One", |
|
24
|
|
|
"name/last": "Drone", |
|
25
|
|
|
"job": "scout", |
|
26
|
|
|
"recent": "", |
|
27
|
|
|
"additional/place/zone": "1", |
|
28
|
|
|
"additional/place/cell": "2", |
|
29
|
|
|
}, |
|
30
|
|
|
}, |
|
31
|
|
|
], |
|
32
|
|
|
"Extra": [ |
|
33
|
|
|
{ |
|
34
|
|
|
"input": { |
|
35
|
|
|
"name": {"first": "Second", "last": "Drone", "nick": {}}, |
|
36
|
|
|
"job": { |
|
37
|
|
|
"1": "scout", |
|
38
|
|
|
"2": "worker", |
|
39
|
|
|
"3": "writer", |
|
40
|
|
|
"4": "reader", |
|
41
|
|
|
"5": "learner", |
|
42
|
|
|
}, |
|
43
|
|
|
"recent": { |
|
44
|
|
|
"places": {"earth": {"Louvre": "2015", "NY": "2017", "NP": ""}}, |
|
45
|
|
|
"times": {"XX": {"1964": "Yes"}, "XXI": {"2064": "Nope"}}, |
|
46
|
|
|
}, |
|
47
|
|
|
}, |
|
48
|
|
|
"answer": { |
|
49
|
|
|
"job/1": "scout", |
|
50
|
|
|
"recent/places/earth/NY": "2017", |
|
51
|
|
|
"job/3": "writer", |
|
52
|
|
|
"job/2": "worker", |
|
53
|
|
|
"job/5": "learner", |
|
54
|
|
|
"job/4": "reader", |
|
55
|
|
|
"recent/places/earth/NP": "", |
|
56
|
|
|
"recent/places/earth/Louvre": "2015", |
|
57
|
|
|
"recent/times/XX/1964": "Yes", |
|
58
|
|
|
"recent/times/XXI/2064": "Nope", |
|
59
|
|
|
"name/first": "Second", |
|
60
|
|
|
"name/last": "Drone", |
|
61
|
|
|
"name/nick": "", |
|
62
|
|
|
}, |
|
63
|
|
|
}, |
|
64
|
|
|
{ |
|
65
|
|
|
"input": {"Hm": {"What": {"is": {"here": {"?": {}}}}}}, |
|
66
|
|
|
"answer": {"Hm/What/is/here/?": ""}, |
|
67
|
|
|
}, |
|
68
|
|
|
{ |
|
69
|
|
|
"input": {"flat": "yep", "root": "yep", "who": "iam"}, |
|
70
|
|
|
"answer": {"flat": "yep", "root": "yep", "who": "iam"}, |
|
71
|
|
|
}, |
|
72
|
|
|
{ |
|
73
|
|
|
"input": { |
|
74
|
|
|
"1": "X", |
|
75
|
|
|
"3": {"31": {"312": "V"}, "34": {"345": {"3458": {"34580": "X"}}}}, |
|
76
|
|
|
}, |
|
77
|
|
|
"answer": {"1": "X", "3/31/312": "V", "3/34/345/3458/34580": "X"}, |
|
78
|
|
|
}, |
|
79
|
|
|
{ |
|
80
|
|
|
"input": { |
|
81
|
|
|
"glossary": { |
|
82
|
|
|
"title": "example glossary", |
|
83
|
|
|
"GlossDiv": { |
|
84
|
|
|
"title": "S", |
|
85
|
|
|
"GlossList": { |
|
86
|
|
|
"GlossEntry": { |
|
87
|
|
|
"ID": "SGML", |
|
88
|
|
|
"SortAs": "SGML", |
|
89
|
|
|
"GlossTerm": "Standard Generalized Markup Language", |
|
90
|
|
|
"Acronym": "SGML", |
|
91
|
|
|
"Abbrev": "ISO 8879:1986", |
|
92
|
|
|
"GlossDef": { |
|
93
|
|
|
"para": "A meta-markup language, used to create markup languages such as DocBook.", |
|
94
|
|
|
"GlossSeeAlso": {"1": "GML", "2": "XML"}, |
|
95
|
|
|
}, |
|
96
|
|
|
"GlossSee": "markup", |
|
97
|
|
|
} |
|
98
|
|
|
}, |
|
99
|
|
|
}, |
|
100
|
|
|
}, |
|
101
|
|
|
"source": "http://json.org/example", |
|
102
|
|
|
}, |
|
103
|
|
|
"answer": { |
|
104
|
|
|
"glossary/GlossDiv/GlossList/GlossEntry/GlossDef/para": "A meta-markup language, used to create markup languages such as DocBook.", |
|
105
|
|
|
"glossary/title": "example glossary", |
|
106
|
|
|
"glossary/GlossDiv/GlossList/GlossEntry/Abbrev": "ISO 8879:1986", |
|
107
|
|
|
"glossary/GlossDiv/GlossList/GlossEntry/SortAs": "SGML", |
|
108
|
|
|
"glossary/GlossDiv/GlossList/GlossEntry/Acronym": "SGML", |
|
109
|
|
|
"glossary/GlossDiv/GlossList/GlossEntry/GlossTerm": "Standard Generalized Markup Language", |
|
110
|
|
|
"glossary/GlossDiv/title": "S", |
|
111
|
|
|
"source": "http://json.org/example", |
|
112
|
|
|
"glossary/GlossDiv/GlossList/GlossEntry/GlossDef/GlossSeeAlso/2": "XML", |
|
113
|
|
|
"glossary/GlossDiv/GlossList/GlossEntry/ID": "SGML", |
|
114
|
|
|
"glossary/GlossDiv/GlossList/GlossEntry/GlossDef/GlossSeeAlso/1": "GML", |
|
115
|
|
|
"glossary/GlossDiv/GlossList/GlossEntry/GlossSee": "markup", |
|
116
|
|
|
}, |
|
117
|
|
|
}, |
|
118
|
|
|
], |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
def test_Basics(self): |
|
122
|
|
|
for i in self.TESTS['Basics']: |
|
123
|
|
|
assert flatten(i['input']) == i['answer'], i['input'] |
|
124
|
|
|
|
|
125
|
|
|
def test_Extra(self): |
|
126
|
|
|
for i in self.TESTS['Extra']: |
|
127
|
|
|
assert flatten(i['input']) == i['answer'], i['input'] |
|
128
|
|
|
|
|
129
|
|
|
|
|
130
|
|
|
if __name__ == "__main__": # pragma: no cover |
|
131
|
|
|
unittest.main() |
|
132
|
|
|
|