1 | #!/usr/bin/env python |
---|
2 | # -*- coding: utf-8 -*- |
---|
3 | """ |
---|
4 | Unit tests for gluon.dal |
---|
5 | """ |
---|
6 | |
---|
7 | import sys |
---|
8 | import os |
---|
9 | import unittest |
---|
10 | |
---|
11 | from gluon.dal import DAL, Field |
---|
12 | |
---|
13 | |
---|
14 | def tearDownModule(): |
---|
15 | try: |
---|
16 | os.unlink('dummy.db') |
---|
17 | except: |
---|
18 | pass |
---|
19 | |
---|
20 | |
---|
21 | class TestDALSubclass(unittest.TestCase): |
---|
22 | |
---|
23 | def testRun(self): |
---|
24 | from gluon.serializers import custom_json, xml |
---|
25 | from gluon import sqlhtml |
---|
26 | db = DAL(check_reserved=['all']) |
---|
27 | self.assertEqual(db.serializers['json'], custom_json) |
---|
28 | self.assertEqual(db.serializers['xml'], xml) |
---|
29 | self.assertEqual(db.representers['rows_render'], sqlhtml.represent) |
---|
30 | self.assertEqual(db.representers['rows_xml'], sqlhtml.SQLTABLE) |
---|
31 | db.close() |
---|
32 | |
---|
33 | def testSerialization(self): |
---|
34 | from gluon._compat import pickle |
---|
35 | db = DAL('sqlite:memory', check_reserved=['all']) |
---|
36 | db.define_table('t_a', Field('f_a')) |
---|
37 | db.t_a.insert(f_a='test') |
---|
38 | a = db(db.t_a.id > 0).select(cacheable=True) |
---|
39 | s = pickle.dumps(a) |
---|
40 | b = pickle.loads(s) |
---|
41 | self.assertEqual(a.db, b.db) |
---|
42 | db.t_a.drop() |
---|
43 | db.close() |
---|
44 | |
---|
45 | """ TODO: |
---|
46 | class TestDefaultValidators(unittest.TestCase): |
---|
47 | def testRun(self): |
---|
48 | pass |
---|
49 | """ |
---|
50 | |
---|
51 | |
---|
52 | def _prepare_exec_for_file(filename): |
---|
53 | module = [] |
---|
54 | if filename.endswith('.py'): |
---|
55 | filename = filename[:-3] |
---|
56 | elif os.path.split(filename)[1] == '__init__.py': |
---|
57 | filename = os.path.dirname(filename) |
---|
58 | else: |
---|
59 | raise IOError('The file provided (%s) is not a valid Python file.') |
---|
60 | filename = os.path.realpath(filename) |
---|
61 | dirpath = filename |
---|
62 | while True: |
---|
63 | dirpath, extra = os.path.split(dirpath) |
---|
64 | module.append(extra) |
---|
65 | if not os.path.isfile(os.path.join(dirpath, '__init__.py')): |
---|
66 | break |
---|
67 | sys.path.insert(0, dirpath) |
---|
68 | return '.'.join(module[::-1]) |
---|
69 | |
---|
70 | |
---|
71 | def load_pydal_tests_module(): |
---|
72 | path = os.path.dirname(os.path.abspath(__file__)) |
---|
73 | if not os.path.isfile(os.path.join(path, 'web2py.py')): |
---|
74 | i = 0 |
---|
75 | while i < 10: |
---|
76 | i += 1 |
---|
77 | if os.path.exists(os.path.join(path, 'web2py.py')): |
---|
78 | break |
---|
79 | path = os.path.abspath(os.path.join(path, '..')) |
---|
80 | pydal_test_path = os.path.join( |
---|
81 | path, "gluon", "packages", "dal", "tests", "__init__.py") |
---|
82 | mname = _prepare_exec_for_file(pydal_test_path) |
---|
83 | mod = __import__(mname) |
---|
84 | return mod |
---|
85 | |
---|
86 | |
---|
87 | def pydal_suite(): |
---|
88 | mod = load_pydal_tests_module() |
---|
89 | suite = unittest.TestSuite() |
---|
90 | tlist = [ |
---|
91 | getattr(mod, el) for el in mod.__dict__.keys() if el.startswith("Test") |
---|
92 | ] |
---|
93 | for t in tlist: |
---|
94 | suite.addTest(unittest.makeSuite(t)) |
---|
95 | return suite |
---|
96 | |
---|
97 | |
---|
98 | class TestDALAdapters(unittest.TestCase): |
---|
99 | def _run_tests(self): |
---|
100 | suite = pydal_suite() |
---|
101 | return unittest.TextTestRunner(verbosity=2).run(suite) |
---|
102 | |
---|
103 | def test_mysql(self): |
---|
104 | if os.environ.get('APPVEYOR'): |
---|
105 | return |
---|
106 | if os.environ.get('TRAVIS'): |
---|
107 | os.environ["DB"] = "mysql://root:@localhost/pydal" |
---|
108 | result = self._run_tests() |
---|
109 | self.assertTrue(result) |
---|