1 | from ._compat import unittest |
---|
2 | from ._adapt import DEFAULT_URI, IS_GAE, IS_IMAP, drop |
---|
3 | from pydal._compat import integer_types |
---|
4 | from pydal import DAL, Field |
---|
5 | from pydal.helpers.methods import smart_query |
---|
6 | |
---|
7 | |
---|
8 | @unittest.skipIf(IS_IMAP, "Skip nosql") |
---|
9 | class TestSmartQuery(unittest.TestCase): |
---|
10 | def testRun(self): |
---|
11 | db = DAL(DEFAULT_URI, check_reserved=["all"]) |
---|
12 | |
---|
13 | # ----------------------------------------------------------------------------- |
---|
14 | # Seems further imports are required for the commented field types below |
---|
15 | |
---|
16 | # db.define_table('referred_table', |
---|
17 | # Field('represent_field', 'string')) |
---|
18 | # NOTE : Don't forget to uncomment the line # drop(db.referred_table) at the very end below |
---|
19 | # if the above are uncommented |
---|
20 | |
---|
21 | db.define_table( |
---|
22 | "a_table", |
---|
23 | Field("string_field", "string"), |
---|
24 | Field("text_field", "text"), |
---|
25 | Field("boolean_field", "boolean"), |
---|
26 | Field("integer_field", "integer"), |
---|
27 | Field("double_field", "double"), |
---|
28 | # Field('decimal_field', 'decimal'), |
---|
29 | # Field('date_field', 'date'), |
---|
30 | # Field('time_field', 'time'), |
---|
31 | # Field('datetime_field', 'datetime'), |
---|
32 | # Field('reference_field', 'reference referred_table'), |
---|
33 | # Field('list_string_field', 'list:string'), |
---|
34 | # Field('list_integer_field', 'list:integer'), |
---|
35 | # Field('list_reference_field', 'list:reference referred_table') |
---|
36 | ) |
---|
37 | |
---|
38 | fields = [ |
---|
39 | db.a_table.id, |
---|
40 | db.a_table.string_field, |
---|
41 | db.a_table.text_field, |
---|
42 | db.a_table.boolean_field, |
---|
43 | db.a_table.integer_field, |
---|
44 | db.a_table.double_field, |
---|
45 | # db.a_table.decimal_field, |
---|
46 | # db.a_table.date_field, |
---|
47 | # db.a_table.time_field, |
---|
48 | # db.a_table.reference_field, |
---|
49 | # db.a_table.list_string_field, |
---|
50 | # db.a_table.list_integer_field, |
---|
51 | # db.a_table.list_reference_field |
---|
52 | ] |
---|
53 | # ----------------------------------------------------------------------------- |
---|
54 | |
---|
55 | # ----------------------------------------------------------------------------- |
---|
56 | # Test with boolean field |
---|
57 | # Operator under test |
---|
58 | # operators = \ |
---|
59 | # [(' starts with ','startswith'), |
---|
60 | # (' ends with ','endswith'), |
---|
61 | # ('contains', 'N/A'), |
---|
62 | # ('like', 'N/A') |
---|
63 | # ] |
---|
64 | # |
---|
65 | # |
---|
66 | |
---|
67 | keywords = "a_table.boolean_field = True" |
---|
68 | q = db.a_table.boolean_field == True |
---|
69 | smart_q = smart_query(fields, keywords) |
---|
70 | self.assertEqual(smart_q, q) |
---|
71 | |
---|
72 | if not IS_GAE: |
---|
73 | # Test string field query |
---|
74 | # starts with |
---|
75 | keywords = 'a_table.string_field starts with "pydal"' |
---|
76 | q = db.a_table.string_field.startswith("pydal") |
---|
77 | smart_q = smart_query(fields, keywords) |
---|
78 | self.assertEqual(smart_q, q) |
---|
79 | |
---|
80 | # ends with |
---|
81 | keywords = 'a_table.string_field ends with "Rocks!!"' |
---|
82 | q = db.a_table.string_field.endswith("Rocks!!") |
---|
83 | smart_q = smart_query(fields, keywords) |
---|
84 | self.assertEqual(smart_q, q) |
---|
85 | |
---|
86 | # contains |
---|
87 | keywords = 'a_table.string_field contains "Rocks"' |
---|
88 | q = db.a_table.string_field.contains("Rocks") |
---|
89 | smart_q = smart_query(fields, keywords) |
---|
90 | self.assertEqual(smart_q, q) |
---|
91 | |
---|
92 | # Don't work for some reason |
---|
93 | # # like |
---|
94 | # keywords = 'a_table.string_field like "%Rocks%"' |
---|
95 | # q = (db.a_table.string_field.like('%Rocks%')) |
---|
96 | # smart_q = smart_query(fields, keywords) |
---|
97 | # self.assertTrue(smart_q == q) |
---|
98 | # ----------------------------------------------------------------------------- |
---|
99 | |
---|
100 | # ----------------------------------------------------------------------------- |
---|
101 | # Tests with integer field |
---|
102 | # For generating these tests |
---|
103 | # def generate_tests(): |
---|
104 | # operators = \ |
---|
105 | # [('=', '='), |
---|
106 | # ('==', '='), |
---|
107 | # (' is ','='), |
---|
108 | # (' equal ', '='), |
---|
109 | # (' equals ', '='), |
---|
110 | # (' equal to ', '='), |
---|
111 | # ('<>', '!='), |
---|
112 | # (' not equal ', '!='), |
---|
113 | # (' not equal to ', '!='), |
---|
114 | # ('<', '<'), |
---|
115 | # (' less than ', '<'), |
---|
116 | # ('<=', '<='), |
---|
117 | # ('=<', '<='), |
---|
118 | # (' less or equal ', '<='), |
---|
119 | # (' less or equal than ', '<='), |
---|
120 | # (' equal or less ', '<='), |
---|
121 | # (' equal or less than ', '<='), |
---|
122 | # ('>', '>'), |
---|
123 | # (' greater than ', '>'), |
---|
124 | # ('=>', '>='), |
---|
125 | # ('>=', '>='), |
---|
126 | # (' greater or equal ', '>='), |
---|
127 | # (' greater or equal than ', '>='), |
---|
128 | # (' equal or greater ', '>='), |
---|
129 | # (' equal or greater than ', '>=')] # JUST APPEND MORE OPERATORS HERE |
---|
130 | # |
---|
131 | # for op in operators: |
---|
132 | # print """ |
---|
133 | # # {op} |
---|
134 | # keywords = 'a_table.integer_field {test_op} 1' |
---|
135 | # q = (db.a_table.integer_field {result_op} 1) |
---|
136 | # smart_q = smart_query(fields, keywords) |
---|
137 | # self.assertTrue(smart_q == q)""".format(op=op, |
---|
138 | # test_op=op[0], |
---|
139 | # result_op='==' if op[1] == '=' else op[1]) |
---|
140 | |
---|
141 | # ('=', '=') |
---|
142 | keywords = "a_table.integer_field = 1" |
---|
143 | q = db.a_table.integer_field == 1 |
---|
144 | smart_q = smart_query(fields, keywords) |
---|
145 | self.assertEqual(smart_q, q) |
---|
146 | |
---|
147 | # ('==', '=') |
---|
148 | keywords = "a_table.integer_field == 1" |
---|
149 | q = db.a_table.integer_field == 1 |
---|
150 | smart_q = smart_query(fields, keywords) |
---|
151 | self.assertEqual(smart_q, q) |
---|
152 | |
---|
153 | # (' is ','=') |
---|
154 | keywords = "a_table.integer_field is 1" |
---|
155 | q = db.a_table.integer_field == 1 |
---|
156 | smart_q = smart_query(fields, keywords) |
---|
157 | self.assertEqual(smart_q, q) |
---|
158 | |
---|
159 | # (' equal ', '=') |
---|
160 | keywords = "a_table.integer_field equal 1" |
---|
161 | q = db.a_table.integer_field == 1 |
---|
162 | smart_q = smart_query(fields, keywords) |
---|
163 | self.assertEqual(smart_q, q) |
---|
164 | |
---|
165 | # (' equals ', '=') |
---|
166 | keywords = "a_table.integer_field equals 1" |
---|
167 | q = db.a_table.integer_field == 1 |
---|
168 | smart_q = smart_query(fields, keywords) |
---|
169 | self.assertEqual(smart_q, q) |
---|
170 | |
---|
171 | # (' equal to ', '=') |
---|
172 | keywords = "a_table.integer_field equal to 1" |
---|
173 | q = db.a_table.integer_field == 1 |
---|
174 | smart_q = smart_query(fields, keywords) |
---|
175 | self.assertEqual(smart_q, q) |
---|
176 | |
---|
177 | # This one not allow over integer it seems |
---|
178 | # # ('<>', '!=') |
---|
179 | # keywords = 'a_table.integer_field <> 1' |
---|
180 | # q = (db.a_table.integer_field != 1) |
---|
181 | # smart_q = smart_query(fields, keywords) |
---|
182 | # self.assertTrue(smart_q == q) |
---|
183 | |
---|
184 | # (' not equal ', '!=') |
---|
185 | keywords = "a_table.integer_field not equal 1" |
---|
186 | q = db.a_table.integer_field != 1 |
---|
187 | smart_q = smart_query(fields, keywords) |
---|
188 | self.assertEqual(smart_q, q) |
---|
189 | |
---|
190 | # (' not equal to ', '!=') |
---|
191 | keywords = "a_table.integer_field not equal to 1" |
---|
192 | q = db.a_table.integer_field != 1 |
---|
193 | smart_q = smart_query(fields, keywords) |
---|
194 | self.assertEqual(smart_q, q) |
---|
195 | |
---|
196 | # ('<', '<') |
---|
197 | keywords = "a_table.integer_field < 1" |
---|
198 | q = db.a_table.integer_field < 1 |
---|
199 | smart_q = smart_query(fields, keywords) |
---|
200 | self.assertEqual(smart_q, q) |
---|
201 | |
---|
202 | # (' less than ', '<') |
---|
203 | keywords = "a_table.integer_field less than 1" |
---|
204 | q = db.a_table.integer_field < 1 |
---|
205 | smart_q = smart_query(fields, keywords) |
---|
206 | self.assertEqual(smart_q, q) |
---|
207 | |
---|
208 | # ('<=', '<=') |
---|
209 | keywords = "a_table.integer_field <= 1" |
---|
210 | q = db.a_table.integer_field <= 1 |
---|
211 | smart_q = smart_query(fields, keywords) |
---|
212 | self.assertEqual(smart_q, q) |
---|
213 | |
---|
214 | # This one is invalid, maybe we should remove it from smart_query |
---|
215 | # # ('=<', '<=') |
---|
216 | # keywords = 'a_table.integer_field =< 1' |
---|
217 | # q = (db.a_table.integer_field <= 1) |
---|
218 | # smart_q = smart_query(fields, keywords) |
---|
219 | # self.assertTrue(smart_q == q) |
---|
220 | |
---|
221 | # (' less or equal ', '<=') |
---|
222 | keywords = "a_table.integer_field less or equal 1" |
---|
223 | q = db.a_table.integer_field <= 1 |
---|
224 | smart_q = smart_query(fields, keywords) |
---|
225 | self.assertEqual(smart_q, q) |
---|
226 | |
---|
227 | # (' less or equal than ', '<=') |
---|
228 | keywords = "a_table.integer_field less or equal than 1" |
---|
229 | q = db.a_table.integer_field <= 1 |
---|
230 | smart_q = smart_query(fields, keywords) |
---|
231 | self.assertEqual(smart_q, q) |
---|
232 | |
---|
233 | # (' equal or less ', '<=') |
---|
234 | keywords = "a_table.integer_field equal or less 1" |
---|
235 | q = db.a_table.integer_field <= 1 |
---|
236 | smart_q = smart_query(fields, keywords) |
---|
237 | self.assertEqual(smart_q, q) |
---|
238 | |
---|
239 | # (' equal or less than ', '<=') |
---|
240 | keywords = "a_table.integer_field equal or less than 1" |
---|
241 | q = db.a_table.integer_field <= 1 |
---|
242 | smart_q = smart_query(fields, keywords) |
---|
243 | self.assertEqual(smart_q, q) |
---|
244 | |
---|
245 | # ('>', '>') |
---|
246 | keywords = "a_table.integer_field > 1" |
---|
247 | q = db.a_table.integer_field > 1 |
---|
248 | smart_q = smart_query(fields, keywords) |
---|
249 | self.assertEqual(smart_q, q) |
---|
250 | |
---|
251 | # (' greater than ', '>') |
---|
252 | keywords = "a_table.integer_field greater than 1" |
---|
253 | q = db.a_table.integer_field > 1 |
---|
254 | smart_q = smart_query(fields, keywords) |
---|
255 | self.assertEqual(smart_q, q) |
---|
256 | |
---|
257 | # This one is invalid, maybe we should remove it from smart_query |
---|
258 | # # ('=>', '>=') |
---|
259 | # keywords = 'a_table.integer_field => 1' |
---|
260 | # q = (db.a_table.integer_field >= 1) |
---|
261 | # smart_q = smart_query(fields, keywords) |
---|
262 | # self.assertTrue(smart_q == q) |
---|
263 | |
---|
264 | # ('>=', '>=') |
---|
265 | keywords = "a_table.integer_field >= 1" |
---|
266 | q = db.a_table.integer_field >= 1 |
---|
267 | smart_q = smart_query(fields, keywords) |
---|
268 | self.assertEqual(smart_q, q) |
---|
269 | |
---|
270 | # (' greater or equal ', '>=') |
---|
271 | keywords = "a_table.integer_field greater or equal 1" |
---|
272 | q = db.a_table.integer_field >= 1 |
---|
273 | smart_q = smart_query(fields, keywords) |
---|
274 | self.assertEqual(smart_q, q) |
---|
275 | |
---|
276 | # (' greater or equal than ', '>=') |
---|
277 | keywords = "a_table.integer_field greater or equal than 1" |
---|
278 | q = db.a_table.integer_field >= 1 |
---|
279 | smart_q = smart_query(fields, keywords) |
---|
280 | self.assertEqual(smart_q, q) |
---|
281 | |
---|
282 | # (' equal or greater ', '>=') |
---|
283 | keywords = "a_table.integer_field equal or greater 1" |
---|
284 | q = db.a_table.integer_field >= 1 |
---|
285 | smart_q = smart_query(fields, keywords) |
---|
286 | self.assertEqual(smart_q, q) |
---|
287 | |
---|
288 | # (' equal or greater than ', '>=') |
---|
289 | keywords = "a_table.integer_field equal or greater than 1" |
---|
290 | q = db.a_table.integer_field >= 1 |
---|
291 | smart_q = smart_query(fields, keywords) |
---|
292 | self.assertEqual(smart_q, q) |
---|
293 | # ----------------------------------------------------------------------------- |
---|
294 | |
---|
295 | # ----------------------------------------------------------------------------- |
---|
296 | # Belongs and not belongs |
---|
297 | |
---|
298 | # NOTE : The below tests don't works |
---|
299 | # Issue : https://github.com/web2py/pydal/issues/161 |
---|
300 | |
---|
301 | # (' in ', 'belongs') -> field.belongs(1, 2, 3) |
---|
302 | # keywords = 'a_table.integer_field in "1, 2, 3"' |
---|
303 | # q = (db.a_table.integer_field.belongs([1, 2, 3])) |
---|
304 | # smart_q = smart_query(fields, keywords) |
---|
305 | # self.assertEqual(smart_q, q) |
---|
306 | |
---|
307 | # keywords = 'a_table.id in "1, 2, 3"' |
---|
308 | # q = (db.a_table.id.belongs([1, 2, 3])) |
---|
309 | # smart_q = smart_query(fields, keywords) |
---|
310 | # self.assertEqual(smart_q, q) |
---|
311 | # |
---|
312 | # # (' not in ' , 'notbelongs'), |
---|
313 | # keywords = 'a_table.integer_field not in "1, 2, 3"' |
---|
314 | # q = (~db.a_table.id.belongs([1, 2, 3])) |
---|
315 | # smart_q = smart_query(fields, keywords) |
---|
316 | # self.assertTrue(smart_q == q) |
---|
317 | |
---|
318 | # ----------------------------------------------------------------------------- |
---|
319 | # cleanup table |
---|
320 | drop(db.a_table) |
---|
321 | # drop(db.referred_table) |
---|
322 | # ----------------------------------------------------------------------------- |
---|
323 | |
---|
324 | |
---|
325 | if __name__ == "__main__": |
---|
326 | unittest.main() |
---|