1+ # -*- coding: utf-8 -*-
2+ """Integration system test for database manager"""
3+
4+ import sys
5+ import os .path as op
6+ import psycopg2
7+
8+ sys .path .append (
9+ op .abspath (op .dirname (__file__ )) + '/../'
10+ )
11+
12+ import unittest
13+ from pgclient .client import DatabaseManager
14+ import random
15+
16+
17+ NAMES = ['Alex' , 'Andrea' , 'Ashley' , 'Casey' , 'Chris' , 'Dorian' , 'Jerry' ]
18+
19+
20+ class DatabaseManagerSystemTest (unittest .TestCase ):
21+ DB_USER = 'postgres'
22+ DB_PASSWORD = 'test'
23+ DB_NAME = 'test'
24+ TABLE_NAME = 'users'
25+
26+ def setUp (self ):
27+ dsn = 'user={} password={} dbname={} host=localhost' .format (
28+ self .DB_USER , self .DB_PASSWORD , self .DB_NAME )
29+ self .db_manager = DatabaseManager (dsn = dsn , pool_size = 10 )
30+
31+ try :
32+ self ._create_table ()
33+ except psycopg2 .DatabaseError :
34+ self ._drop_table ()
35+ self ._create_table ()
36+
37+ # Insert 100 entries
38+ with self .db_manager .cursor as cursor :
39+ for _ in range (100 ):
40+ insert_str = "INSERT INTO {} (username) VALUES (%s)" .format (
41+ self .TABLE_NAME )
42+ cursor .execute (insert_str , (random .choice (NAMES ),))
43+
44+ def _create_table (self ):
45+ # Init database with test data
46+ with self .db_manager .cursor as cursor :
47+ cursor .execute (
48+ "CREATE TABLE {} "
49+ "(id serial PRIMARY KEY, username VARCHAR NOT NULL );" .format (
50+ self .TABLE_NAME ))
51+ print ('Table {} has been created' .format (self .TABLE_NAME ))
52+
53+ def _drop_table (self ):
54+ with self .db_manager .cursor as cursor :
55+ cursor .execute ('DROP TABLE {}' .format (self .TABLE_NAME ))
56+ print ('Table {} has been dropped' .format (self .TABLE_NAME ))
57+
58+ def tearDown (self ):
59+ self ._drop_table ()
60+
61+ def test_cursor (self ):
62+ with self .db_manager .cursor as cursor :
63+ cursor .execute ('SELECT * FROM users' )
64+ result_set = cursor .fetchall ()
65+ self .assertEqual (len (result_set ), 100 )
66+
67+ def test_dict_cursor (self ):
68+ with self .db_manager .dict_cursor as cursor :
69+ cursor .execute ('SELECT * FROM users' )
70+ result_set = cursor .fetchall ()
71+ item = result_set [0 ]
72+ self .assertIn ('id' , item )
73+ self .assertIn ('username' , item )
74+ self .assertIn (item ['username' ], NAMES )
75+
76+ def test_named_tuple_cursor (self ):
77+ with self .db_manager .nt_cursor as cursor :
78+ cursor .execute ('SELECT * FROM users' )
79+ result_set = cursor .fetchall ()
80+ item = result_set [0 ]
81+ self .assertIsInstance (item .id , int )
82+ self .assertIsInstance (item .username , str )
83+
84+ def test_success_transaction (self ):
85+ with self .db_manager .cursor as transaction :
86+ insert_str = "INSERT INTO {} (username) VALUES (%s)" .format (
87+ self .TABLE_NAME )
88+ transaction .execute (insert_str , (random .choice (NAMES ), ))
89+ transaction .execute ('SELECT * FROM users' )
90+ result_set = transaction .fetchall ()
91+ self .assertEqual (len (result_set ), 101 )
92+
93+ def test_rollback_transaction (self ):
94+ with self .db_manager .cursor as transaction :
95+ with self .assertRaises (psycopg2 .DatabaseError ) as err :
96+ transaction .execute (
97+ "INSERT INTO {} (username) VALUES (%s)" .format (self .TABLE_NAME ),
98+ (None , ))
99+ self .assertIn ('null value in column' , err .exception .message )
100+
101+
102+ if __name__ == '__main__' :
103+ unittest .main ()
0 commit comments