-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathtest_query.py
More file actions
293 lines (235 loc) · 11.5 KB
/
Copy pathtest_query.py
File metadata and controls
293 lines (235 loc) · 11.5 KB
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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
from __future__ import unicode_literals
from __future__ import print_function
from __future__ import division
from __future__ import absolute_import
from future import standard_library
standard_library.install_aliases()
from builtins import *
from mapr.ojai.ojai_query.OJAIQuery import OJAIQuery
from mapr.ojai.ojai_query.OJAIQueryCondition import OJAIQueryCondition
from mapr.ojai.ojai_query.QueryOp import QueryOp
try:
import unittest2 as unittest
except ImportError:
import unittest
class QueryTest(unittest.TestCase):
def test_simple_query(self):
query = OJAIQuery().select([True, 5, 123123124123]).build()
self.assertEqual(query.query_dict(), {'$select': ['True', '5', '123123124123']})
def test_condition(self):
query_condition = OJAIQueryCondition() \
.and_() \
.is_('age', QueryOp.GREATER_OR_EQUAL, 18) \
.is_('city', QueryOp.EQUAL, 'London').close().close()
query_condition.build()
self.assertEqual(query_condition.as_dictionary(),
{'$and': [{'$ge': {'age': 18}}, {'$eq': {'city': 'London'}}]})
def test_multiple_and(self):
qc = OJAIQueryCondition() \
.and_() \
.and_() \
.is_(field_path='age', op=QueryOp.GREATER_OR_EQUAL, value=18) \
.is_(field_path='city', op=QueryOp.EQUAL, value='London') \
.close() \
.and_() \
.is_(field_path='age', op=QueryOp.GREATER_OR_EQUAL, value=22) \
.is_(field_path='city', op=QueryOp.EQUAL, value='NY') \
.close() \
.close()
qc.build()
self.assertEqual(qc.as_dictionary(), {'$and': [
{'$and': [
{'$ge': {'age': 18}},
{'$eq': {'city': 'London'}}]},
{'$and': [
{'$ge': {'age': 22}},
{'$eq': {'city': 'NY'}}]}
]})
def test_and_with_separate_is(self):
qc = OJAIQueryCondition() \
.and_() \
.and_() \
.is_(field_path='age', op=QueryOp.GREATER_OR_EQUAL, value=18) \
.is_(field_path='city', op=QueryOp.EQUAL, value='London') \
.close() \
.and_() \
.is_(field_path='age', op=QueryOp.GREATER_OR_EQUAL, value=22) \
.is_(field_path='city', op=QueryOp.EQUAL, value='NY') \
.close() \
.is_('card', QueryOp.EQUAL, 'visa') \
.close()
qc.build()
self.assertEqual(qc.as_dictionary(), {'$and': [
{'$and': [
{'$ge': {'age': 18}},
{'$eq': {'city': 'London'}}]},
{'$and': [
{'$ge': {'age': 22}},
{'$eq': {'city': 'NY'}}]},
{'$eq': {'card': 'visa'}}
]})
def test_nested_and(self):
qc = OJAIQueryCondition().and_() \
.is_(field_path='card', op=QueryOp.NOT_EQUAL, value="visa") \
.or_() \
.is_(field_path='age', op=QueryOp.GREATER_OR_EQUAL, value=22) \
.is_(field_path='city', op=QueryOp.EQUAL, value='NY') \
.and_() \
.is_(field_path='age', op=QueryOp.GREATER_OR_EQUAL, value=18) \
.is_(field_path='city', op=QueryOp.EQUAL, value='London').close().close().close().build()
self.assertEqual(qc.as_dictionary(), {'$and': [
{'$ne': {u'card': u'visa'}},
{'$or': [
{'$ge': {u'age': 22}},
{'$eq': {u'city': u'NY'}},
{'$and': [
{'$ge': {u'age': 18}},
{'$eq': {u'city': u'London'}}
]}
]}
]})
def test_eq_condition(self):
qc = OJAIQueryCondition().and_().not_equals_('name', 'Joh').equals_('name', 'David').close().build()
self.assertEqual(qc.as_dictionary(), {'$and': [{'$ne': {'name': 'Joh'}}, {'$eq': {'name': 'David'}}]})
def test_nested_condition(self):
qc = OJAIQueryCondition().and_() \
.condition_(condition_to_add=OJAIQueryCondition()
.or_()
.is_(field_path='age', op=QueryOp.GREATER_OR_EQUAL, value=22)
.is_(field_path='city', op=QueryOp.EQUAL, value='NY').close()) \
.condition_(condition_to_add=OJAIQueryCondition()
.and_()
.is_(field_path='age', op=QueryOp.GREATER_OR_EQUAL, value=18)
.is_(field_path='city', op=QueryOp.EQUAL, value='London').close())
qc.close().build()
self.assertEqual(qc.as_dictionary(), {'$and': [
{'$or': [
{'$ge': {u'age': 22}},
{'$eq': {u'city': u'NY'}}]},
{'$and': [
{'$ge': {u'age': 18}},
{'$eq': {u'city': u'London'}}]}
]})
def test_full_query(self):
qc = OJAIQueryCondition().and_() \
.condition_(OJAIQueryCondition()
.or_()
.is_('age', QueryOp.GREATER_OR_EQUAL, 22)
.is_('city', QueryOp.EQUAL, 'NY').close()) \
.condition_(OJAIQueryCondition()
.and_()
.is_('age', QueryOp.GREATER_OR_EQUAL, 18)
.is_('city', QueryOp.EQUAL, 'London').close()).close().build()
query = OJAIQuery().select('name', 'age', 'city').where(qc).order_by('name').offset(500).limit(5).build()
self.assertEqual(query.query_dict(), {'$orderby': {'name': 'asc'},
'$offset': 500,
'$select': ['name', 'age', 'city'],
'$limit': 5,
'$where': {'$and': [
{'$or': [
{'$ge': {'age': 22}},
{'$eq': {'city': 'NY'}}]},
{'$and': [
{'$ge': {'age': 18}},
{'$eq': {'city': 'London'}}
]}
]}})
def test_query_check_existing_condition(self):
qc = OJAIQueryCondition().is_('age', QueryOp.GREATER_OR_EQUAL, 55).is_('age', QueryOp.GREATER_OR_EQUAL, 18) \
.close().build()
self.assertEqual(qc.as_dictionary(), {'$ge': {'age': 18}})
def test_query_condition_multiple_is(self):
qc = OJAIQueryCondition().is_('age', QueryOp.GREATER_OR_EQUAL, 55).is_('age', QueryOp.NOT_EQUAL, 18) \
.close().build()
self.assertEqual(qc.as_dictionary(), {'$ge': {'age': 55}, '$ne': {'age': 18}})
def test_in_condition(self):
qc = OJAIQueryCondition().in_('age', [20, 21, 22, 23, 24, 25]).close().build()
self.assertEqual(qc.as_dictionary(), {'$in': {'age': [20, 21, 22, 23, 24, 25]}})
def test_not_in_condition(self):
qc = OJAIQueryCondition().not_in_('age', [20, 21, 22, 23, 24, 25]).close().build()
self.assertEqual(qc.as_dictionary(), {'$notin': {'age': [20, 21, 22, 23, 24, 25]}})
def test_exists(self):
qc = OJAIQueryCondition().exists_('city').close().build()
self.assertEqual(qc.as_dictionary(), {'$exists': 'city'})
def test_not_exists(self):
qc = OJAIQueryCondition().not_exists_('city').close().build()
self.assertEqual(qc.as_dictionary(), {'$notexists': 'city'})
def test_like(self):
qc = OJAIQueryCondition().like_('card', 'visa').close().build()
self.assertEqual(qc.as_dictionary(), {'$like': {'card': 'visa'}})
def test_not_like(self):
qc = OJAIQueryCondition().not_like_('card', 'visa').close().build()
self.assertEqual(qc.as_dictionary(), {'$notlike': {'card': 'visa'}})
def test_empty_condition(self):
qc = OJAIQueryCondition()
self.assertTrue(qc.is_empty())
qc.is_('name', QueryOp.EQUAL, 'Doe').close()
self.assertTrue(qc.is_empty())
qc.build()
self.assertFalse(qc.is_empty())
def test_order_by(self):
query = OJAIQuery().order_by('name').build()
self.assertEqual(query.query_dict(), {'$orderby': {'name': 'asc'}})
query2 = OJAIQuery().order_by(['name', 'age']).build()
self.assertEqual(query2.query_dict(), {'$orderby': [{'name': 'asc'}, {'age': 'asc'}]})
query3 = OJAIQuery().order_by(['address.city', 'address.postal_code'], 'desc').build()
self.assertEqual(query3.query_dict(), {'$orderby': [{'address.city': 'desc'}, {'address.postal_code': 'desc'}]})
def test_empty_query(self):
query = OJAIQuery().build()
self.assertEqual(query.query_dict(), {})
self.assertEqual(query.to_json_str(), '{}')
def test_limit_bool(self):
with self.assertRaises(TypeError):
query = OJAIQuery().limit(True).build()
def test_offset_bool(self):
with self.assertRaises(TypeError):
query = OJAIQuery().offset(True).build()
def test_where_dict(self):
query = OJAIQuery().where({'$ne': {'name': 'Joh'}}).build()
self.assertEqual({'$where': {'$ne': {'name': 'Joh'}}}, query.query_dict())
def test_where_json_str(self):
query = OJAIQuery().where("{\"$eq\": {\"address.zipCode\": 95196}}").build()
self.assertEqual({'$where': {'$eq': {'address.zipCode': 95196}}}, query.query_dict())
def test_where_json_str_format(self):
query = OJAIQuery().where("{\"$eq\": {\"address.zipCode\": 95196}}").build()
self.assertEqual('{"$where": {"$eq": {"address.zipCode": 95196}}}', query.to_json_str())
def test_empty_where(self):
with self.assertRaises(AttributeError):
OJAIQuery().where({}).build()
with self.assertRaises(AttributeError):
OJAIQuery().where('').build()
with self.assertRaises(AttributeError):
OJAIQuery().where(u'').build()
with self.assertRaises(AttributeError):
OJAIQuery().where(OJAIQueryCondition().build()).build()
def test_order_by_empty(self):
with self.assertRaises(TypeError):
OJAIQuery().order_by([]).build()
with self.assertRaises(TypeError):
OJAIQuery().order_by('').build()
def test_element_and(self):
query_condition = OJAIQueryCondition()\
.element_and('grades[]')\
.equals_('dsc', 'history')\
.equals_('ev', 12)\
.close()\
.build()
self.assertEqual({'$elementAnd': {'grades[]': [
{'$eq': {'dsc': 'history'}},
{'$eq': {'ev': 12}}
]}},
query_condition.as_dictionary())
def test_empty_value_order_by(self):
with self.assertRaises(TypeError):
OJAIQuery().order_by('').build().to_json_str()
with self.assertRaises(TypeError):
OJAIQuery().order_by([]).build().to_json_str()
def test_incompatible_str_type(self):
from mapr.ojai.ojai_query.OJAIQuery import OJAIQuery
from mapr.ojai.ojai_query.OJAIQueryCondition import OJAIQueryCondition
id_field = '_id'
try:
condition = OJAIQueryCondition().equals_(id_field, id_field).close().build()
query = OJAIQuery().select([id_field])
except BaseException:
self.fail("Probably incompatible str type.")