/
test_collection_filter.py
270 lines (198 loc) · 7.93 KB
/
test_collection_filter.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
# flake8: noqa for E501
import pytest
from collection_filter import collection_filter
class Test_CollectionFilter(object):
# ################################################################
# Input data as dict
# ################################################################
def test_DataDictWithFieldsEmpty_ReturnOriginalData(self):
# Arrange
data = {'foo': 'bar'}
fields = None
# Act
result = collection_filter(data, fields)
# Assert
assert data == result
def test_DataDictWithFieldsEmptyString_ReturnOriginalData(self):
# Arrange
data = {'foo': 'bar'}
fields = ''
# Act
result = collection_filter(data, fields)
# Assert
assert data == result
def test_DataDictWithFieldsOneElement_ReturnSubsetData(self):
# Arrange
data = {'foo': 1, 'bar': 2}
fields = 'foo'
# Act
result = collection_filter(data, fields)
# Assert
assert result == {'foo': 1}
def test_DataDictWithFieldsTwoElement_ReturnSubsetData(self):
# Arrange
data = {'foo': 1, 'bar': 2, 'Alice': 'someone', 'Bob': 'Say Hi!'}
fields = 'foo,Alice'
# Act
result = collection_filter(data, fields)
# Assert
assert result == {'foo': 1, 'Alice': 'someone'}
def test_DataDictTwoLevelWithFieldsTwoElement_ReturnSubsetData(self):
# Arrange
data = {'foo': {'Alice': 'someone', 'Bob': 'Say Hi!', 'orange': 'banana'}}
fields = 'foo.Alice,foo.orange'
# Act
result = collection_filter(data, fields)
# Assert
assert result == {'foo': {'Alice': 'someone', 'orange': 'banana'}}
def test_DataDictIncludeListWithFieldsSimpleKeyQuery_ReturnSubsetData(self):
# Arrange
data = {'foo': 1, 'aList': [{'elem1': 1}, {'elem2': 2}, {'elem3': 3}]}
fields = 'aList'
# Act
result = collection_filter(data, fields)
# Assert
assert result == {'aList': [{'elem1': 1}, {'elem2': 2}, {'elem3': 3}]}
def test_DataDictIncludeListWithFieldsArrayKeyQuery_ReturnSubsetData(self):
# Arrange
data = {'foo': 1, 'aList': [{'elem1': 1}, {'elem2': 2}, {'elem3': 3}]}
fields = 'aList[]'
# Act
result = collection_filter(data, fields)
# Assert
assert result == {'aList': [{'elem1': 1}, {'elem2': 2}, {'elem3': 3}]}
def test_DataDictIncludeListWithFieldsDeepKeyQuery_ReturnSubsetData(self):
# Arrange
data = {'foo': 1, 'aList': [{'elem1': 1}, {'elem2': 2}, {'elem3': 3}]}
fields = 'aList[].elem1'
# Act
result = collection_filter(data, fields)
# Assert
assert result == {'aList': [{'elem1': 1}, {}, {}]}
def test_DataDictIncludeListWithTwoFieldsDeepKeyQuery_ReturnUnionSubsetData(self):
# Arrange
data = {'foo': 1, 'aList': [{'foo': 1, 'bar': 2, 'dontcare': 99}, {'foo': 3, 'bar': 4, 'dontcare': 100}]}
fields = 'aList[].foo,aList[].bar'
# Act
result = collection_filter(data, fields)
# Assert
assert result == {'aList': [{'foo': 1, 'bar': 2}, {'foo': 3, 'bar': 4}]}
def test_DataDictIncludeListComplexWithFieldsDeepKeyQuery_ReturnSubsetData(self):
# Arrange
data = {'foo': 1, 'aList': [{'elem1': {'foo': 1, 'bar': 2}}, {'elem2': {'foo': 'bar'}}, {'elem3': {'foo': 'bar'}}]}
fields = 'aList[].elem1.foo'
# Act
result = collection_filter(data, fields)
# Assert
assert result == {'aList': [{'elem1': {'foo': 1}}, {}, {}]}
def test_DataDictWithFieldsTwoLevelQuery_ReturnDeepData(self):
# Arrange
data = {'aDict': {'bar': 2, 'Alice': 'someone', 'Bob': 'Say Hi!'}}
fields = 'aDict.Alice'
# Act
result = collection_filter(data, fields)
# Assert
assert result == {'aDict': {'Alice': 'someone'}}
def test_DataDictWithFieldsArrayQuery_RaiseException(self):
# Arrange
data = {'foo': 1, 'bar': 2}
fields = '[].foo'
# Act & Assert
with pytest.raises(AssertionError):
result = collection_filter(data, fields)
# ################################################################
# Input data as list
# ################################################################
def test_DataListOfObjectWithFieldsEmpty_ReturnOriginalData(self):
# Arrange
data = [{'foo': 1}, {'bar': 2}]
fields = None
# Act
result = collection_filter(data, fields)
# Assert
assert data == result
def test_DataListWithFieldsArrayQuery_ReturnOriginalList(self):
# Arrange
data = [{'foo': 1}, {'bar': 2}]
fields = '[]'
# Act
result = collection_filter(data, fields)
# Assert
assert result == data
def test_DataListOfObjectWithFieldsEmptyString_ReturnOriginalData(self):
# Arrange
data = [{'foo': 1}, {'bar': 2}]
fields = ''
# Act
result = collection_filter(data, fields)
# Assert
assert data == result
def test_DataListWithFieldsArrayQuery_ReturnSubsetOfListData(self):
# Arrange
data = [{'foo': 1, 'bar': 2}, {'foo': 3, 'bar': 4, 'orange': 'banana'}]
fields = '[].foo'
# Act
result = collection_filter(data, fields)
# Assert
assert result == [{'foo': 1}, {'foo': 3}]
def test_DataListWithFieldsTwoArrayQuery_ReturnSubsetOfListData(self):
# Arrange
data = [{'foo': {'apple': 1, 'orange': 2}}, {'foo': {'apple': 3, 'orange': 4, 'banana': 5}}]
fields = '[].foo.apple,[].foo.orange'
# Act
result = collection_filter(data, fields)
# Assert
assert result == [{'foo': {'apple': 1, 'orange': 2}}, {'foo': {'apple': 3, 'orange': 4}}]
def test_DataListWithFieldsNonexistingArrayQuery_ReturnEmptyListData(self):
# Arrange
data = [{'foo': 1, 'bar': 2}, {'foo': 3, 'bar': 4, 'orange': 'banana'}]
fields = '[].nonexisted'
# Act
result = collection_filter(data, fields)
# Assert
assert result == [{}, {}]
def test_DataListWithFieldOneKey_RaiseException(self):
# Arrange
data = [{'foo': 1}, {'bar': 2}]
fields = 'foo'
# Act & Assert
with pytest.raises(AssertionError):
result = collection_filter(data, fields)
# ################################################################
# Input data which is NOT supported
# ################################################################
def test_DataPrimitiveTypeWithFieldsDontCare_RaiseException(self):
# Arrange
data = 1
fields = 'foo'
# Act & Assert
with pytest.raises(AssertionError):
result = collection_filter(data, fields)
def test_DataListOfPrimitiveTypeWithFieldsDontCare_RaiseException(self):
# Arrange
data = [1, 2, 3]
fields = 'foo'
# Act & Assert
with pytest.raises(AssertionError):
result = collection_filter(data, fields)
def test_DataListOfListWithFieldsDontCare_RaiseException(self):
# Arrange
data = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
fields = 'foo'
# Act & Assert
with pytest.raises(AssertionError):
result = collection_filter(data, fields)
def test_DataListOfPrimitiveTypeWithFieldsArrayQuery_RaiseException(self):
# Arrange
data = [1, 2, 3]
fields = '[].address'
# Act & Assert
with pytest.raises(AssertionError):
result = collection_filter(data, fields)
def test_DataListOfListWithFieldsArrayQuery_RaiseException(self):
# Arrange
data = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
fields = '[].address'
# Act & Assert
with pytest.raises(AssertionError):
result = collection_filter(data, fields)