tests/testpgmanager.py
author Radek Brich <radek.brich@devl.cz>
Wed, 26 Sep 2012 16:21:59 +0200
changeset 43 a921669e913a
parent 37 5b0eb4b11940
child 48 b82c7c2fb5af
permissions -rwxr-xr-x
PgManager: rewrite RowDict class.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
37
Radek Brich <radek.brich@devl.cz>
parents: 22
diff changeset
     1
#!/usr/bin/env python3.2
22
680631f35d83 Add test for PgManager.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     2
680631f35d83 Add test for PgManager.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     3
import unittest
680631f35d83 Add test for PgManager.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     4
680631f35d83 Add test for PgManager.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     5
from config import Config
680631f35d83 Add test for PgManager.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     6
from pgtoolkit import pgmanager
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):
680631f35d83 Add test for PgManager.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    12
        cfg = Config('tests.conf')
680631f35d83 Add test for PgManager.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    13
        self.m = pgmanager.get_instance()
680631f35d83 Add test for PgManager.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    14
        self.m.create_conn(**cfg)
680631f35d83 Add test for PgManager.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    15
        self.m.create_conn('autocommit', isolation_level='autocommit', **cfg)
680631f35d83 Add test for PgManager.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    16
680631f35d83 Add test for PgManager.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    17
    def tearDown(self):
680631f35d83 Add test for PgManager.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    18
        self.m.destroy_conn()
680631f35d83 Add test for PgManager.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    19
        self.m.destroy_conn('autocommit')
680631f35d83 Add test for PgManager.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    20
680631f35d83 Add test for PgManager.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    21
    def test_connection_get_put(self):
680631f35d83 Add test for PgManager.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    22
        conn = self.m.get_conn()
680631f35d83 Add test for PgManager.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    23
680631f35d83 Add test for PgManager.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    24
        curs = conn.cursor()
680631f35d83 Add test for PgManager.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    25
        curs.execute('SELECT %s AS t', [True])
680631f35d83 Add test for PgManager.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    26
        d = curs.fetchone_dict()
680631f35d83 Add test for PgManager.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    27
        self.assertTrue(d.t)
680631f35d83 Add test for PgManager.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    28
680631f35d83 Add test for PgManager.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    29
        self.m.put_conn(conn)
680631f35d83 Add test for PgManager.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    30
680631f35d83 Add test for PgManager.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    31
    def test_connection_with(self):
680631f35d83 Add test for PgManager.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    32
        with self.m.cursor() as curs:
680631f35d83 Add test for PgManager.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    33
            curs.execute('SELECT %s AS t', [True])
680631f35d83 Add test for PgManager.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    34
            d = curs.fetchone_dict()
680631f35d83 Add test for PgManager.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    35
            self.assertTrue(d.t)
680631f35d83 Add test for PgManager.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    36
680631f35d83 Add test for PgManager.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    37
    def test_connection_autocommit(self):
680631f35d83 Add test for PgManager.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    38
        conn = self.m.get_conn('autocommit')
680631f35d83 Add test for PgManager.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    39
        curs = conn.cursor()
680631f35d83 Add test for PgManager.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    40
        name = 'test'
680631f35d83 Add test for PgManager.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    41
        curs.execute('DELETE FROM test WHERE name = %s', [name])
680631f35d83 Add test for PgManager.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    42
        curs.execute('INSERT INTO test (name) VALUES (%s)', [name])
680631f35d83 Add test for PgManager.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    43
        conn.close()
680631f35d83 Add test for PgManager.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    44
680631f35d83 Add test for PgManager.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    45
        conn = self.m.get_conn('autocommit')
680631f35d83 Add test for PgManager.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    46
        curs = conn.cursor()
680631f35d83 Add test for PgManager.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    47
        curs.execute('SELECT * FROM test WHERE name = %s', [name])
680631f35d83 Add test for PgManager.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    48
        self.assertTrue(curs.rowcount == 1)
680631f35d83 Add test for PgManager.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    49
        self.m.put_conn(conn)
680631f35d83 Add test for PgManager.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    50
680631f35d83 Add test for PgManager.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    51
if __name__ == '__main__':
680631f35d83 Add test for PgManager.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    52
    unittest.main()
680631f35d83 Add test for PgManager.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    53