tests/test_pgmanager.py
author Radek Brich <radek.brich@devl.cz>
Wed, 09 Jul 2014 18:03:54 +0200
changeset 104 d8ff52a0390f
parent 50 f71d3abbb18f
permissions -rw-r--r--
Rename to pydbkit.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
49
08e4dfe1b0cb Add test for MyManager (enable only when MySQLdb is available). Configure tests using pgtoolkit.conf (same as used by other executables).
Radek Brich <radek.brich@devl.cz>
parents: 48
diff changeset
     1
#!/usr/bin/env python3
22
680631f35d83 Add test for PgManager.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     2
48
b82c7c2fb5af PgManager: Fix logging, log queries before executing, possible exceptions are logged after. Add tests for RowDict. Add tests.py - runs all tests.
Radek Brich <radek.brich@devl.cz>
parents: 37
diff changeset
     3
from tests.config import Config
104
d8ff52a0390f Rename to pydbkit.
Radek Brich <radek.brich@devl.cz>
parents: 50
diff changeset
     4
from pydbkit import pgmanager
48
b82c7c2fb5af PgManager: Fix logging, log queries before executing, possible exceptions are logged after. Add tests for RowDict. Add tests.py - runs all tests.
Radek Brich <radek.brich@devl.cz>
parents: 37
diff changeset
     5
22
680631f35d83 Add test for PgManager.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     6
import unittest
680631f35d83 Add test for PgManager.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     7
680631f35d83 Add test for PgManager.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     8
680631f35d83 Add test for PgManager.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     9
class TestSuite(unittest.TestCase):
680631f35d83 Add test for PgManager.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    10
680631f35d83 Add test for PgManager.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    11
    def setUp(self):
104
d8ff52a0390f Rename to pydbkit.
Radek Brich <radek.brich@devl.cz>
parents: 50
diff changeset
    12
        cfg = Config('pydbkit.conf')
49
08e4dfe1b0cb Add test for MyManager (enable only when MySQLdb is available). Configure tests using pgtoolkit.conf (same as used by other executables).
Radek Brich <radek.brich@devl.cz>
parents: 48
diff changeset
    13
        test_db_conn_params = cfg['databases']['test']
22
680631f35d83 Add test for PgManager.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    14
        self.m = pgmanager.get_instance()
49
08e4dfe1b0cb Add test for MyManager (enable only when MySQLdb is available). Configure tests using pgtoolkit.conf (same as used by other executables).
Radek Brich <radek.brich@devl.cz>
parents: 48
diff changeset
    15
        self.m.create_conn(dsn=test_db_conn_params)
08e4dfe1b0cb Add test for MyManager (enable only when MySQLdb is available). Configure tests using pgtoolkit.conf (same as used by other executables).
Radek Brich <radek.brich@devl.cz>
parents: 48
diff changeset
    16
        self.m.create_conn('autocommit', isolation_level='autocommit', dsn=test_db_conn_params)
22
680631f35d83 Add test for PgManager.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    17
680631f35d83 Add test for PgManager.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    18
    def tearDown(self):
680631f35d83 Add test for PgManager.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    19
        self.m.destroy_conn()
680631f35d83 Add test for PgManager.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    20
        self.m.destroy_conn('autocommit')
680631f35d83 Add test for PgManager.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    21
680631f35d83 Add test for PgManager.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    22
    def test_connection_get_put(self):
680631f35d83 Add test for PgManager.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    23
        conn = self.m.get_conn()
680631f35d83 Add test for PgManager.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    24
680631f35d83 Add test for PgManager.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    25
        curs = conn.cursor()
680631f35d83 Add test for PgManager.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    26
        curs.execute('SELECT %s AS t', [True])
680631f35d83 Add test for PgManager.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    27
        d = curs.fetchone_dict()
680631f35d83 Add test for PgManager.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    28
        self.assertTrue(d.t)
680631f35d83 Add test for PgManager.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    29
680631f35d83 Add test for PgManager.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    30
        self.m.put_conn(conn)
680631f35d83 Add test for PgManager.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    31
680631f35d83 Add test for PgManager.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    32
    def test_connection_with(self):
680631f35d83 Add test for PgManager.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    33
        with self.m.cursor() as curs:
680631f35d83 Add test for PgManager.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    34
            curs.execute('SELECT %s AS t', [True])
680631f35d83 Add test for PgManager.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    35
            d = curs.fetchone_dict()
680631f35d83 Add test for PgManager.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    36
            self.assertTrue(d.t)
680631f35d83 Add test for PgManager.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    37
680631f35d83 Add test for PgManager.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    38
    def test_connection_autocommit(self):
680631f35d83 Add test for PgManager.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    39
        conn = self.m.get_conn('autocommit')
680631f35d83 Add test for PgManager.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    40
        curs = conn.cursor()
680631f35d83 Add test for PgManager.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    41
        name = 'test'
680631f35d83 Add test for PgManager.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    42
        curs.execute('DELETE FROM test WHERE name = %s', [name])
680631f35d83 Add test for PgManager.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    43
        curs.execute('INSERT INTO test (name) VALUES (%s)', [name])
680631f35d83 Add test for PgManager.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    44
        conn.close()
680631f35d83 Add test for PgManager.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    45
680631f35d83 Add test for PgManager.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    46
        conn = self.m.get_conn('autocommit')
680631f35d83 Add test for PgManager.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    47
        curs = conn.cursor()
680631f35d83 Add test for PgManager.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    48
        curs.execute('SELECT * FROM test WHERE name = %s', [name])
680631f35d83 Add test for PgManager.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    49
        self.assertTrue(curs.rowcount == 1)
680631f35d83 Add test for PgManager.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    50
        self.m.put_conn(conn)
680631f35d83 Add test for PgManager.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    51
49
08e4dfe1b0cb Add test for MyManager (enable only when MySQLdb is available). Configure tests using pgtoolkit.conf (same as used by other executables).
Radek Brich <radek.brich@devl.cz>
parents: 48
diff changeset
    52
22
680631f35d83 Add test for PgManager.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    53
if __name__ == '__main__':
680631f35d83 Add test for PgManager.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    54
    unittest.main()
680631f35d83 Add test for PgManager.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    55