source: OpenRLabs-Git/deploy/rlabs-docker/web2py-rlabs/gluon/tests/test_dal.py @ 42095c5

mainqndtest v1.1.1
Last change on this file since 42095c5 was 42bd667, checked in by David Fuertes <dfuertes@…>, 4 years ago

Historial Limpio

  • Property mode set to 100755
File size: 3.0 KB
Line 
1#!/usr/bin/env python
2# -*- coding: utf-8 -*-
3"""
4    Unit tests for gluon.dal
5"""
6
7import sys
8import os
9import unittest
10
11from gluon.dal import DAL, Field
12
13
14def tearDownModule():
15    try:
16        os.unlink('dummy.db')
17    except:
18        pass
19
20
21class 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:
46class TestDefaultValidators(unittest.TestCase):
47    def testRun(self):
48        pass
49"""
50
51
52def _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
71def 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
87def 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
98class 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)
Note: See TracBrowser for help on using the repository browser.