1 | #!/bin/python |
---|
2 | # -*- coding: utf-8 -*- |
---|
3 | |
---|
4 | """ |
---|
5 | Unit tests for gluon.sqlhtml |
---|
6 | """ |
---|
7 | import datetime |
---|
8 | import os |
---|
9 | import sys |
---|
10 | import unittest |
---|
11 | |
---|
12 | from gluon.sqlhtml import safe_int, SQLFORM, SQLTABLE |
---|
13 | |
---|
14 | DEFAULT_URI = os.getenv('DB', 'sqlite:memory') |
---|
15 | |
---|
16 | from gluon.dal import DAL, Field |
---|
17 | from pydal.objects import Table |
---|
18 | from gluon.tools import Auth, Mail |
---|
19 | from gluon.globals import Request, Response, Session |
---|
20 | from gluon.storage import Storage |
---|
21 | from gluon.languages import TranslatorFactory |
---|
22 | from gluon.http import HTTP |
---|
23 | from gluon.validators import * |
---|
24 | |
---|
25 | # TODO: Create these test... |
---|
26 | |
---|
27 | # class Test_add_class(unittest.TestCase): |
---|
28 | # def test_add_class(self): |
---|
29 | # pass |
---|
30 | |
---|
31 | |
---|
32 | # class Test_represent(unittest.TestCase): |
---|
33 | # def test_represent(self): |
---|
34 | # pass |
---|
35 | |
---|
36 | |
---|
37 | # class TestCacheRepresenter(unittest.TestCase): |
---|
38 | # def test___call__(self): |
---|
39 | # pass |
---|
40 | |
---|
41 | # def test___init__(self): |
---|
42 | # pass |
---|
43 | |
---|
44 | |
---|
45 | class Test_safe_int(unittest.TestCase): |
---|
46 | def test_safe_int(self): |
---|
47 | # safe int |
---|
48 | self.assertEqual(safe_int(1), 1) |
---|
49 | # not safe int |
---|
50 | self.assertEqual(safe_int('1x'), 0) |
---|
51 | # not safe int (alternate default) |
---|
52 | self.assertEqual(safe_int('1x', 1), 1) |
---|
53 | |
---|
54 | |
---|
55 | # class Test_safe_float(unittest.TestCase): |
---|
56 | # def test_safe_float(self): |
---|
57 | # pass |
---|
58 | |
---|
59 | |
---|
60 | # class Test_show_if(unittest.TestCase): |
---|
61 | # def test_show_if(self): |
---|
62 | # pass |
---|
63 | |
---|
64 | |
---|
65 | # class TestFormWidget(unittest.TestCase): |
---|
66 | # def test__attributes(self): |
---|
67 | # pass |
---|
68 | |
---|
69 | # def test_widget(self): |
---|
70 | # pass |
---|
71 | |
---|
72 | |
---|
73 | # class TestStringWidget(unittest.TestCase): |
---|
74 | # def test__attributes(self): |
---|
75 | # pass |
---|
76 | |
---|
77 | # def test_widget(self): |
---|
78 | # pass |
---|
79 | |
---|
80 | |
---|
81 | # class TestIntegerWidget(unittest.TestCase): |
---|
82 | # def test__attributes(self): |
---|
83 | # pass |
---|
84 | |
---|
85 | # def test_widget(self): |
---|
86 | # pass |
---|
87 | |
---|
88 | |
---|
89 | # class TestDoubleWidget(unittest.TestCase): |
---|
90 | # def test__attributes(self): |
---|
91 | # pass |
---|
92 | |
---|
93 | # def test_widget(self): |
---|
94 | # pass |
---|
95 | |
---|
96 | |
---|
97 | # class TestDecimalWidget(unittest.TestCase): |
---|
98 | # def test__attributes(self): |
---|
99 | # pass |
---|
100 | |
---|
101 | # def test_widget(self): |
---|
102 | # pass |
---|
103 | |
---|
104 | |
---|
105 | # class TestDateWidget(unittest.TestCase): |
---|
106 | # def test__attributes(self): |
---|
107 | # pass |
---|
108 | |
---|
109 | # def test_widget(self): |
---|
110 | # pass |
---|
111 | |
---|
112 | |
---|
113 | # class TestDatetimeWidget(unittest.TestCase): |
---|
114 | # def test__attributes(self): |
---|
115 | # pass |
---|
116 | |
---|
117 | # def test_widget(self): |
---|
118 | # pass |
---|
119 | |
---|
120 | |
---|
121 | # class TestTextWidget(unittest.TestCase): |
---|
122 | # def test__attributes(self): |
---|
123 | # pass |
---|
124 | |
---|
125 | # def test_widget(self): |
---|
126 | # pass |
---|
127 | |
---|
128 | |
---|
129 | # class TestJSONWidget(unittest.TestCase): |
---|
130 | # def test__attributes(self): |
---|
131 | # pass |
---|
132 | |
---|
133 | # def test_widget(self): |
---|
134 | # pass |
---|
135 | |
---|
136 | |
---|
137 | # class TestBooleanWidget(unittest.TestCase): |
---|
138 | # def test__attributes(self): |
---|
139 | # pass |
---|
140 | |
---|
141 | # def test_widget(self): |
---|
142 | # pass |
---|
143 | |
---|
144 | |
---|
145 | # class TestListWidget(unittest.TestCase): |
---|
146 | # def test__attributes(self): |
---|
147 | # pass |
---|
148 | |
---|
149 | # def test_widget(self): |
---|
150 | # pass |
---|
151 | |
---|
152 | |
---|
153 | # class TestMultipleOptionsWidget(unittest.TestCase): |
---|
154 | # def test__attributes(self): |
---|
155 | # pass |
---|
156 | |
---|
157 | # def test_widget(self): |
---|
158 | # pass |
---|
159 | |
---|
160 | |
---|
161 | # class TestRadioWidget(unittest.TestCase): |
---|
162 | # def test__attributes(self): |
---|
163 | # pass |
---|
164 | |
---|
165 | # def test_widget(self): |
---|
166 | # pass |
---|
167 | |
---|
168 | |
---|
169 | # class TestCheckboxesWidget(unittest.TestCase): |
---|
170 | # def test__attributes(self): |
---|
171 | # pass |
---|
172 | |
---|
173 | # def test_widget(self): |
---|
174 | # pass |
---|
175 | |
---|
176 | |
---|
177 | # class TestPasswordWidget(unittest.TestCase): |
---|
178 | # def test__attributes(self): |
---|
179 | # pass |
---|
180 | |
---|
181 | # def test_widget(self): |
---|
182 | # pass |
---|
183 | |
---|
184 | |
---|
185 | # class TestUploadWidget(unittest.TestCase): |
---|
186 | # def test__attributes(self): |
---|
187 | # pass |
---|
188 | |
---|
189 | # def test_represent(self): |
---|
190 | # pass |
---|
191 | |
---|
192 | # def test_widget(self): |
---|
193 | # pass |
---|
194 | |
---|
195 | |
---|
196 | # class TestAutocompleteWidget(unittest.TestCase): |
---|
197 | # def test___call__(self): |
---|
198 | # pass |
---|
199 | |
---|
200 | # def test___init__(self): |
---|
201 | # pass |
---|
202 | |
---|
203 | # def test_callback(self): |
---|
204 | # pass |
---|
205 | |
---|
206 | |
---|
207 | # class Test_formstyle_table3cols(unittest.TestCase): |
---|
208 | # def test_formstyle_table3cols(self): |
---|
209 | # pass |
---|
210 | |
---|
211 | |
---|
212 | # class Test_formstyle_table2cols(unittest.TestCase): |
---|
213 | # def test_formstyle_table2cols(self): |
---|
214 | # pass |
---|
215 | |
---|
216 | |
---|
217 | # class Test_formstyle_divs(unittest.TestCase): |
---|
218 | # def test_formstyle_divs(self): |
---|
219 | # pass |
---|
220 | |
---|
221 | |
---|
222 | # class Test_formstyle_inline(unittest.TestCase): |
---|
223 | # def test_formstyle_inline(self): |
---|
224 | # pass |
---|
225 | |
---|
226 | |
---|
227 | # class Test_formstyle_ul(unittest.TestCase): |
---|
228 | # def test_formstyle_ul(self): |
---|
229 | # pass |
---|
230 | |
---|
231 | |
---|
232 | # class Test_formstyle_bootstrap(unittest.TestCase): |
---|
233 | # def test_formstyle_bootstrap(self): |
---|
234 | # pass |
---|
235 | |
---|
236 | |
---|
237 | # class Test_formstyle_bootstrap3_stacked(unittest.TestCase): |
---|
238 | # def test_formstyle_bootstrap3_stacked(self): |
---|
239 | # pass |
---|
240 | |
---|
241 | |
---|
242 | # class Test_formstyle_bootstrap3_inline_factory(unittest.TestCase): |
---|
243 | # def test_formstyle_bootstrap3_inline_factory(self): |
---|
244 | # pass |
---|
245 | |
---|
246 | |
---|
247 | class TestSQLFORM(unittest.TestCase): |
---|
248 | |
---|
249 | def setUp(self): |
---|
250 | request = Request(env={}) |
---|
251 | request.application = 'a' |
---|
252 | request.controller = 'c' |
---|
253 | request.function = 'f' |
---|
254 | request.folder = 'applications/admin' |
---|
255 | response = Response() |
---|
256 | session = Session() |
---|
257 | T = TranslatorFactory('', 'en') |
---|
258 | session.connect(request, response) |
---|
259 | from gluon.globals import current |
---|
260 | current.request = request |
---|
261 | current.response = response |
---|
262 | current.session = session |
---|
263 | current.T = T |
---|
264 | self.db = DAL(DEFAULT_URI, check_reserved=['all']) |
---|
265 | self.auth = Auth(self.db) |
---|
266 | self.auth.define_tables(username=True, signature=False) |
---|
267 | self.db.define_table('t0', Field('tt', default='web2py'), self.auth.signature) |
---|
268 | self.auth.enable_record_versioning(self.db) |
---|
269 | # Create a user |
---|
270 | self.db.auth_user.insert(first_name='Bart', |
---|
271 | last_name='Simpson', |
---|
272 | username='user1', |
---|
273 | email='user1@test.com', |
---|
274 | password='password_123', |
---|
275 | registration_key=None, |
---|
276 | registration_id=None) |
---|
277 | |
---|
278 | self.db.commit() |
---|
279 | |
---|
280 | |
---|
281 | def test_SQLFORM(self): |
---|
282 | form = SQLFORM(self.db.auth_user) |
---|
283 | self.assertEqual(form.xml()[:5], b'<form') |
---|
284 | |
---|
285 | def test_represent_SQLFORM(self): |
---|
286 | id = self.db.t0.insert() |
---|
287 | self.db.t0.tt.represent = lambda value: value.capitalize() |
---|
288 | self.db.t0.tt.writable = False |
---|
289 | self.db.t0.tt.readable = True |
---|
290 | form = SQLFORM(self.db.t0, id) |
---|
291 | self.assertTrue(b'Web2py' in form.xml()) |
---|
292 | self.db.t0.tt.represent = lambda value, row: value.capitalize() |
---|
293 | form = SQLFORM(self.db.t0, id) |
---|
294 | self.assertTrue(b'Web2py' in form.xml()) |
---|
295 | |
---|
296 | # def test_assert_status(self): |
---|
297 | # pass |
---|
298 | |
---|
299 | # def test_createform(self): |
---|
300 | # pass |
---|
301 | |
---|
302 | # def test_accepts(self): |
---|
303 | # pass |
---|
304 | |
---|
305 | # def test_dictform(self): |
---|
306 | # pass |
---|
307 | |
---|
308 | # def test_smartdictform(self): |
---|
309 | # pass |
---|
310 | |
---|
311 | def test_factory(self): |
---|
312 | factory_form = SQLFORM.factory(Field('field_one', 'string', IS_NOT_EMPTY()), |
---|
313 | Field('field_two', 'string')) |
---|
314 | self.assertEqual(factory_form.xml()[:5], b'<form') |
---|
315 | |
---|
316 | def test_factory_applies_default_validators(self): |
---|
317 | from gluon import current |
---|
318 | |
---|
319 | factory_form = SQLFORM.factory( |
---|
320 | Field('a_date', type='date'), |
---|
321 | ) |
---|
322 | # Fake user input |
---|
323 | current.request.post_vars.update({ |
---|
324 | '_formname': 'no_table/create', |
---|
325 | 'a_date': '2018-09-14', |
---|
326 | '_formkey': '123', |
---|
327 | |
---|
328 | }) |
---|
329 | # Fake the formkey |
---|
330 | current.session['_formkey[no_table/create]'] = ['123'] |
---|
331 | |
---|
332 | self.assertTrue(factory_form.process().accepted) |
---|
333 | self.assertIsInstance(factory_form.vars.a_date, datetime.date) |
---|
334 | |
---|
335 | # def test_build_query(self): |
---|
336 | # pass |
---|
337 | |
---|
338 | # def test_search_menu(self): |
---|
339 | # pass |
---|
340 | |
---|
341 | def test_grid(self): |
---|
342 | grid_form = SQLFORM.grid(self.db.auth_user) |
---|
343 | self.assertEqual(grid_form.xml()[:4], b'<div') |
---|
344 | |
---|
345 | def test_smartgrid(self): |
---|
346 | smartgrid_form = SQLFORM.smartgrid(self.db.auth_user) |
---|
347 | self.assertEqual(smartgrid_form.xml()[:4], b'<div') |
---|
348 | |
---|
349 | class TestSQLTABLE(unittest.TestCase): |
---|
350 | def setUp(self): |
---|
351 | request = Request(env={}) |
---|
352 | request.application = 'a' |
---|
353 | request.controller = 'c' |
---|
354 | request.function = 'f' |
---|
355 | request.folder = 'applications/admin' |
---|
356 | response = Response() |
---|
357 | session = Session() |
---|
358 | T = TranslatorFactory('', 'en') |
---|
359 | session.connect(request, response) |
---|
360 | from gluon.globals import current |
---|
361 | current.request = request |
---|
362 | current.response = response |
---|
363 | current.session = session |
---|
364 | current.T = T |
---|
365 | self.db = DAL(DEFAULT_URI, check_reserved=['all']) |
---|
366 | self.auth = Auth(self.db) |
---|
367 | self.auth.define_tables(username=True, signature=False) |
---|
368 | self.db.define_table('t0', Field('tt'), self.auth.signature) |
---|
369 | self.auth.enable_record_versioning(self.db) |
---|
370 | # Create a user |
---|
371 | self.db.auth_user.insert(first_name='Bart', |
---|
372 | last_name='Simpson', |
---|
373 | username='user1', |
---|
374 | email='user1@test.com', |
---|
375 | password='password_123', |
---|
376 | registration_key=None, |
---|
377 | registration_id=None) |
---|
378 | |
---|
379 | self.db.commit() |
---|
380 | |
---|
381 | def test_SQLTABLE(self): |
---|
382 | rows = self.db(self.db.auth_user.id > 0).select(self.db.auth_user.ALL) |
---|
383 | sqltable = SQLTABLE(rows) |
---|
384 | self.assertEqual(sqltable.xml()[:7], b'<table>') |
---|
385 | |
---|
386 | |
---|
387 | # class TestExportClass(unittest.TestCase): |
---|
388 | # def test___init__(self): |
---|
389 | # pass |
---|
390 | |
---|
391 | # def test_export(self): |
---|
392 | # pass |
---|
393 | |
---|
394 | # def test_represented(self): |
---|
395 | # pass |
---|
396 | |
---|
397 | |
---|
398 | # class TestExporterTSV(unittest.TestCase): |
---|
399 | # def test___init__(self): |
---|
400 | # pass |
---|
401 | |
---|
402 | # def test_export(self): |
---|
403 | # pass |
---|
404 | |
---|
405 | # def test_represented(self): |
---|
406 | # pass |
---|
407 | |
---|
408 | |
---|
409 | # class TestExporterCSV(unittest.TestCase): |
---|
410 | # def test___init__(self): |
---|
411 | # pass |
---|
412 | |
---|
413 | # def test_export(self): |
---|
414 | # pass |
---|
415 | |
---|
416 | # def test_represented(self): |
---|
417 | # pass |
---|
418 | |
---|
419 | |
---|
420 | # class TestExporterCSV_hidden(unittest.TestCase): |
---|
421 | # def test___init__(self): |
---|
422 | # pass |
---|
423 | |
---|
424 | # def test_export(self): |
---|
425 | # pass |
---|
426 | |
---|
427 | # def test_represented(self): |
---|
428 | # pass |
---|
429 | |
---|
430 | |
---|
431 | # class TestExporterHTML(unittest.TestCase): |
---|
432 | # def test___init__(self): |
---|
433 | # pass |
---|
434 | |
---|
435 | # def test_export(self): |
---|
436 | # pass |
---|
437 | |
---|
438 | # def test_represented(self): |
---|
439 | # pass |
---|
440 | |
---|
441 | |
---|
442 | # class TestExporterXML(unittest.TestCase): |
---|
443 | # def test___init__(self): |
---|
444 | # pass |
---|
445 | |
---|
446 | # def test_export(self): |
---|
447 | # pass |
---|
448 | |
---|
449 | # def test_represented(self): |
---|
450 | # pass |
---|
451 | |
---|
452 | |
---|
453 | # class TestExporterJSON(unittest.TestCase): |
---|
454 | # def test___init__(self): |
---|
455 | # pass |
---|
456 | |
---|
457 | # def test_export(self): |
---|
458 | # pass |
---|
459 | |
---|
460 | # def test_represented(self): |
---|
461 | # pass |
---|