| 1 | """ |
|---|
| 2 | unit tests for module logilab.common.db |
|---|
| 3 | """ |
|---|
| 4 | |
|---|
| 5 | import socket |
|---|
| 6 | |
|---|
| 7 | from logilab.common.testlib import TestCase, unittest_main |
|---|
| 8 | from logilab.common.db import * |
|---|
| 9 | from logilab.common.db import PREFERED_DRIVERS |
|---|
| 10 | from logilab.common.adbh import _SqliteAdvFuncHelper, _PGAdvFuncHelper |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | class PreferedDriverTC(TestCase): |
|---|
| 14 | def setUp(self): |
|---|
| 15 | self.drivers = {"pg":[('foo', None), ('bar', None)]} |
|---|
| 16 | self.drivers = {'pg' : ["foo", "bar"]} |
|---|
| 17 | |
|---|
| 18 | def testNormal(self): |
|---|
| 19 | set_prefered_driver('pg','bar', self.drivers) |
|---|
| 20 | self.assertEquals('bar', self.drivers['pg'][0]) |
|---|
| 21 | |
|---|
| 22 | def testFailuresDb(self): |
|---|
| 23 | try: |
|---|
| 24 | set_prefered_driver('oracle','bar', self.drivers) |
|---|
| 25 | self.fail() |
|---|
| 26 | except UnknownDriver, exc: |
|---|
| 27 | self.assertEquals(exc.args[0], 'Unknown database oracle') |
|---|
| 28 | |
|---|
| 29 | def testFailuresDriver(self): |
|---|
| 30 | try: |
|---|
| 31 | set_prefered_driver('pg','baz', self.drivers) |
|---|
| 32 | self.fail() |
|---|
| 33 | except UnknownDriver, exc: |
|---|
| 34 | self.assertEquals(exc.args[0], 'Unknown module baz for pg') |
|---|
| 35 | |
|---|
| 36 | def testGlobalVar(self): |
|---|
| 37 | # XXX: Is this test supposed to be useful ? Is it supposed to test |
|---|
| 38 | # set_prefered_driver ? |
|---|
| 39 | old_drivers = PREFERED_DRIVERS['postgres'][:] |
|---|
| 40 | expected = old_drivers[:] |
|---|
| 41 | expected.insert(0, expected.pop(expected.index('pgdb'))) |
|---|
| 42 | set_prefered_driver('postgres', 'pgdb') |
|---|
| 43 | self.assertEquals(PREFERED_DRIVERS['postgres'], expected) |
|---|
| 44 | set_prefered_driver('postgres', 'psycopg') |
|---|
| 45 | # self.assertEquals(PREFERED_DRIVERS['postgres'], old_drivers) |
|---|
| 46 | expected.insert(0, expected.pop(expected.index('psycopg'))) |
|---|
| 47 | self.assertEquals(PREFERED_DRIVERS['postgres'], expected) |
|---|
| 48 | |
|---|
| 49 | |
|---|
| 50 | class GetCnxTC(TestCase): |
|---|
| 51 | def setUp(self): |
|---|
| 52 | if not socket.gethostname().startswith('crater'): |
|---|
| 53 | self.skip("those tests require specific DB configuration") |
|---|
| 54 | self.host = 'localhost' # None |
|---|
| 55 | self.db = 'template1' |
|---|
| 56 | self.user = 'adim' |
|---|
| 57 | self.passwd = 'adim' |
|---|
| 58 | |
|---|
| 59 | def testPsyco(self): |
|---|
| 60 | set_prefered_driver('postgres', 'psycopg') |
|---|
| 61 | try: |
|---|
| 62 | cnx = get_connection('postgres', |
|---|
| 63 | self.host, self.db, self.user, self.passwd, |
|---|
| 64 | quiet=1) |
|---|
| 65 | except ImportError: |
|---|
| 66 | self.skip('python-psycopg is not installed') |
|---|
| 67 | |
|---|
| 68 | def testPgdb(self): |
|---|
| 69 | set_prefered_driver('postgres', 'pgdb') |
|---|
| 70 | try: |
|---|
| 71 | cnx = get_connection('postgres', |
|---|
| 72 | self.host, self.db, self.user, self.passwd, |
|---|
| 73 | quiet=1) |
|---|
| 74 | except ImportError: |
|---|
| 75 | self.skip('python-pgsql is not installed') |
|---|
| 76 | |
|---|
| 77 | def testPgsql(self): |
|---|
| 78 | set_prefered_driver('postgres', 'pyPgSQL.PgSQL') |
|---|
| 79 | try: |
|---|
| 80 | cnx = get_connection('postgres', |
|---|
| 81 | self.host, self.db, self.user, self.passwd, |
|---|
| 82 | quiet=1) |
|---|
| 83 | except ImportError: |
|---|
| 84 | self.skip('python-pygresql is not installed') |
|---|
| 85 | |
|---|
| 86 | def testMysql(self): |
|---|
| 87 | set_prefered_driver('mysql', 'MySQLdb') |
|---|
| 88 | try: |
|---|
| 89 | cnx = get_connection('mysql', self.host, database='', user='root', |
|---|
| 90 | quiet=1) |
|---|
| 91 | except ImportError: |
|---|
| 92 | self.skip('python-mysqldb is not installed') |
|---|
| 93 | except Exception, ex: |
|---|
| 94 | # no mysql running ? |
|---|
| 95 | import MySQLdb |
|---|
| 96 | if not (isinstance(ex, MySQLdb.OperationalError) and ex.args[0] == 2003): |
|---|
| 97 | raise |
|---|
| 98 | |
|---|
| 99 | def test_connection_wrap(self): |
|---|
| 100 | """Tests the connection wrapping""" |
|---|
| 101 | try: |
|---|
| 102 | cnx = get_connection('postgres', |
|---|
| 103 | self.host, self.db, self.user, self.passwd, |
|---|
| 104 | quiet=1) |
|---|
| 105 | except ImportError: |
|---|
| 106 | self.skip('postgresql dbapi module not installed') |
|---|
| 107 | self.failIf(isinstance(cnx, PyConnection), |
|---|
| 108 | 'cnx should *not* be a PyConnection instance') |
|---|
| 109 | cnx = get_connection('postgres', |
|---|
| 110 | self.host, self.db, self.user, self.passwd, |
|---|
| 111 | quiet=1, pywrap = True) |
|---|
| 112 | self.failUnless(isinstance(cnx, PyConnection), |
|---|
| 113 | 'cnx should be a PyConnection instance') |
|---|
| 114 | |
|---|
| 115 | |
|---|
| 116 | def test_cursor_wrap(self): |
|---|
| 117 | """Tests cursor wrapping""" |
|---|
| 118 | try: |
|---|
| 119 | cnx = get_connection('postgres', |
|---|
| 120 | self.host, self.db, self.user, self.passwd, |
|---|
| 121 | quiet=1, pywrap = True) |
|---|
| 122 | except ImportError: |
|---|
| 123 | self.skip('postgresql dbapi module not installed') |
|---|
| 124 | cursor = cnx.cursor() |
|---|
| 125 | self.failUnless(isinstance(cursor, PyCursor), |
|---|
| 126 | 'cnx should be a PyCursor instance') |
|---|
| 127 | |
|---|
| 128 | |
|---|
| 129 | class DBAPIAdaptersTC(TestCase): |
|---|
| 130 | """Tests DbApi adapters management""" |
|---|
| 131 | |
|---|
| 132 | def setUp(self): |
|---|
| 133 | """Memorize original PREFERED_DRIVERS""" |
|---|
| 134 | self.old_drivers = PREFERED_DRIVERS['postgres'][:] |
|---|
| 135 | self.host = 'crater.logilab.fr' |
|---|
| 136 | self.db = 'gincotest2' |
|---|
| 137 | self.user = 'adim' |
|---|
| 138 | self.passwd = 'adim' |
|---|
| 139 | |
|---|
| 140 | def tearDown(self): |
|---|
| 141 | """Reset PREFERED_DRIVERS as it was""" |
|---|
| 142 | PREFERED_DRIVERS['postgres'] = self.old_drivers |
|---|
| 143 | |
|---|
| 144 | def test_raise(self): |
|---|
| 145 | self.assertRaises(UnknownDriver, get_dbapi_compliant_module, 'pougloup') |
|---|
| 146 | |
|---|
| 147 | def test_pgdb_types(self): |
|---|
| 148 | """Tests that NUMBER really wraps all number types""" |
|---|
| 149 | PREFERED_DRIVERS['postgres'] = ['pgdb'] |
|---|
| 150 | #set_prefered_driver('postgres', 'pgdb') |
|---|
| 151 | try: |
|---|
| 152 | module = get_dbapi_compliant_module('postgres') |
|---|
| 153 | except ImportError: |
|---|
| 154 | self.skip('postgresql pgdb module not installed') |
|---|
| 155 | number_types = ('int2', 'int4', 'serial', |
|---|
| 156 | 'int8', 'float4', 'float8', |
|---|
| 157 | 'numeric', 'bool', 'money') |
|---|
| 158 | for num_type in number_types: |
|---|
| 159 | yield self.assertEquals, num_type, module.NUMBER |
|---|
| 160 | yield self.assertNotEquals, 'char', module.NUMBER |
|---|
| 161 | |
|---|
| 162 | def test_pypgsql_getattr(self): |
|---|
| 163 | """Tests the getattr() delegation for pyPgSQL""" |
|---|
| 164 | set_prefered_driver('postgres', 'pyPgSQL.PgSQL') |
|---|
| 165 | try: |
|---|
| 166 | module = get_dbapi_compliant_module('postgres') |
|---|
| 167 | except ImportError: |
|---|
| 168 | self.skip('postgresql dbapi module not installed') |
|---|
| 169 | try: |
|---|
| 170 | binary = module.BINARY |
|---|
| 171 | except AttributeError, err: |
|---|
| 172 | raise |
|---|
| 173 | self.fail(str(err)) |
|---|
| 174 | |
|---|
| 175 | def test_adv_func_helper(self): |
|---|
| 176 | try: |
|---|
| 177 | module = get_dbapi_compliant_module('postgres') |
|---|
| 178 | except ImportError: |
|---|
| 179 | self.skip('postgresql dbapi module not installed') |
|---|
| 180 | self.failUnless(isinstance(module.adv_func_helper, _PGAdvFuncHelper)) |
|---|
| 181 | try: |
|---|
| 182 | module = get_dbapi_compliant_module('sqlite') |
|---|
| 183 | except ImportError: |
|---|
| 184 | self.skip('sqlite dbapi module not installed') |
|---|
| 185 | self.failUnless(isinstance(module.adv_func_helper, _SqliteAdvFuncHelper)) |
|---|
| 186 | |
|---|
| 187 | |
|---|
| 188 | if __name__ == '__main__': |
|---|
| 189 | unittest_main() |
|---|