-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtest_nested_fetch.py
163 lines (140 loc) · 5.08 KB
/
test_nested_fetch.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import unittest
from nestedfetch import nested_get, nested_set, flatten_data
class TestNestedFetch(unittest.TestCase):
simple_get_data = {
"name": "Jesse Pinkman",
"details": {"address": {"city": "Albuquerque"}},
}
nested_get_data = {
"name": "Walter White",
"details": {"address": [{"city": "Albuquerque"}, {"city": "El Paso"}]},
}
nested_ll_get_data = {
"name": "Walter White",
"details": {
"address": [
{"city": "Albuquerque"},
{"city": "El Paso"},
{"state": "New Mexico"},
]
},
}
nested_list_get_data = {
"name": "Walter White",
"details": {"address": [[{"city": "Albuquerque"}, {"city": "El Paso"}]]},
}
flatten_data = {
"league": "Champions League",
"matches": [
{
"match_id": "match_1",
"goals": [
{
"time": 13,
"scorrer": "Lionel Messi",
"assist": "Luis Suarez",
"details": [
{"position": "outside-box"},
{"position": "right-side"},
],
},
{
"time": 78,
"scorrer": "Luis Suarez",
"assist": "Ivan Rakitic",
"details": [
{"position": "inside-box"},
{"position": "left-side"},
],
},
],
},
{
"match_id": "match_2",
"goals": [
{
"time": 36,
"scorrer": "C. Ronaldo",
"assist": "Luka Modric",
"details": [{"position": "penalty"}, {"position": "d-box"}],
}
],
},
],
}
flatten_test_data = [["This", "is"], ["flattened", "data"]]
flatten_nested_data = [[["This", "is"], ["flattened", "data"]]]
def test_simple_get_success(self):
res = nested_get(self.simple_get_data, ["details", "address", "city"])
self.assertEqual(res, "Albuquerque")
def test_nested_get_all_success(self):
res = nested_get(self.nested_get_data, ["details", "address", "city"])
self.assertEqual(res, ["Albuquerque", "El Paso"])
def test_nested_ll_get_all_success(self):
res = nested_get(
self.nested_ll_get_data, ["details", "address", "city"], default=None
)
self.assertEqual(res, ["Albuquerque", "El Paso", None])
def test_nested_get_with_index_success(self):
res = nested_get(self.nested_get_data, ["details", "address", "city", 0])
self.assertEqual(res, "Albuquerque")
def test_nested_list_get_with_index_success(self):
res = nested_get(self.nested_list_get_data, ["details", "address", 0, 0])
self.assertEqual(res, {"city": "Albuquerque"})
def test_nested_get_with_index_error(self):
res = nested_get(
self.nested_get_data, ["details", "address", "city", 5], default=None
)
self.assertEqual(res, None)
def test_nested_get_flatten(self):
res = nested_get(
self.flatten_data,
["matches", "goals", "scorrer"],
default=None,
flatten=True,
)
self.assertEqual(res, ["Lionel Messi", "Luis Suarez", "C. Ronaldo"])
def test_nested_get_ll_flatten(self):
res = nested_get(
self.flatten_data,
["matches", "goals", "details"],
default=None,
flatten=True,
)
self.assertEqual(
res,
[
{"position": "outside-box"},
{"position": "right-side"},
{"position": "inside-box"},
{"position": "left-side"},
{"position": "penalty"},
{"position": "d-box"},
],
)
def test_simple_set_success(self):
res = nested_set(self.simple_get_data, ["details", "address", "city"], "Denver")
self.assertEqual(res, 1)
def test_simple_set_build_success(self):
res = nested_set(
self.simple_get_data,
["details", "address", "state"],
"New Mexico",
create_missing=True,
)
self.assertEqual(res, 1)
self.assertEqual(
self.simple_get_data,
{
"name": "Jesse Pinkman",
"details": {"address": {"city": "Albuquerque", "state": "New Mexico"}},
},
)
def test_simple_flatten(self):
res = flatten_data(self.flatten_test_data)
self.assertEqual(res, ["This", "is", "flattened", "data"])
def test_nested_flatten(self):
res = flatten_data(self.flatten_nested_data)
self.assertEqual(res, ["This", "is", "flattened", "data"])
if __name__ == "__main__":
unittest.main()