1010)
1111
1212import unittest
13- from pgclient .client import DatabaseManager
1413import random
14+ from pgclient .client import PostgresClient
1515
1616
1717NAMES = ['Alex' , 'Andrea' , 'Ashley' , 'Casey' , 'Chris' , 'Dorian' , 'Jerry' ]
1818
1919
20- class DatabaseManagerSystemTest (unittest .TestCase ):
20+ class PostgresClientSystemTest (unittest .TestCase ):
2121 DB_USER = 'postgres'
2222 DB_PASSWORD = 'test'
2323 DB_NAME = 'test'
@@ -26,7 +26,12 @@ class DatabaseManagerSystemTest(unittest.TestCase):
2626 def setUp (self ):
2727 dsn = 'user={} password={} dbname={} host=localhost' .format (
2828 self .DB_USER , self .DB_PASSWORD , self .DB_NAME )
29- self .db_manager = DatabaseManager (dsn = dsn , pool_size = 10 )
29+ try :
30+ self .pg_client = PostgresClient (dsn = dsn , pool_size = 10 )
31+ except psycopg2 .OperationalError as err :
32+ print ('Check that postgres docker container is started. '
33+ 'Check README for more information' )
34+ raise psycopg2 .OperationalError (err .message )
3035
3136 try :
3237 self ._create_table ()
@@ -35,69 +40,66 @@ def setUp(self):
3540 self ._create_table ()
3641
3742 # Insert 100 entries
38- with self .db_manager .cursor as cursor :
43+ with self .pg_client .cursor as cursor :
3944 for _ in range (100 ):
4045 insert_str = "INSERT INTO {} (username) VALUES (%s)" .format (
4146 self .TABLE_NAME )
4247 cursor .execute (insert_str , (random .choice (NAMES ),))
4348
4449 def _create_table (self ):
4550 # Init database with test data
46- with self .db_manager .cursor as cursor :
51+ with self .pg_client .cursor as cursor :
4752 cursor .execute (
4853 "CREATE TABLE {} "
49- "(id serial PRIMARY KEY , username VARCHAR NOT NULL );" .format (
54+ "(id SERIAL , username VARCHAR NOT NULL );" .format (
5055 self .TABLE_NAME ))
5156 print ('Table {} has been created' .format (self .TABLE_NAME ))
5257
5358 def _drop_table (self ):
54- with self .db_manager .cursor as cursor :
59+ with self .pg_client .cursor as cursor :
5560 cursor .execute ('DROP TABLE {}' .format (self .TABLE_NAME ))
5661 print ('Table {} has been dropped' .format (self .TABLE_NAME ))
5762
5863 def tearDown (self ):
5964 self ._drop_table ()
6065
6166 def test_cursor (self ):
62- with self .db_manager .cursor as cursor :
67+ with self .pg_client .cursor as cursor :
6368 cursor .execute ('SELECT * FROM users' )
6469 result_set = cursor .fetchall ()
6570 self .assertEqual (len (result_set ), 100 )
6671
6772 def test_dict_cursor (self ):
68- with self .db_manager .dict_cursor as cursor :
73+ with self .pg_client .dict_cursor as cursor :
6974 cursor .execute ('SELECT * FROM users' )
70- result_set = cursor .fetchall ()
75+ result_set = cursor .fetchall ()
7176 item = result_set [0 ]
7277 self .assertIn ('id' , item )
7378 self .assertIn ('username' , item )
7479 self .assertIn (item ['username' ], NAMES )
7580
7681 def test_named_tuple_cursor (self ):
77- with self .db_manager .nt_cursor as cursor :
82+ with self .pg_client .nt_cursor as cursor :
7883 cursor .execute ('SELECT * FROM users' )
79- result_set = cursor .fetchall ()
84+ result_set = cursor .fetchall ()
8085 item = result_set [0 ]
8186 self .assertIsInstance (item .id , int )
8287 self .assertIsInstance (item .username , str )
8388
8489 def test_success_transaction (self ):
85- with self .db_manager .cursor as transaction :
90+ with self .pg_client .cursor as transaction :
8691 insert_str = "INSERT INTO {} (username) VALUES (%s)" .format (
8792 self .TABLE_NAME )
8893 transaction .execute (insert_str , (random .choice (NAMES ), ))
8994 transaction .execute ('SELECT * FROM users' )
90- result_set = transaction .fetchall ()
95+ result_set = transaction .fetchall ()
9196 self .assertEqual (len (result_set ), 101 )
9297
9398 def test_rollback_transaction (self ):
94- with self .db_manager .cursor as transaction :
99+ # Insert null username must cause an error
100+ with self .pg_client .cursor as transaction :
95101 with self .assertRaises (psycopg2 .DatabaseError ) as err :
96102 transaction .execute (
97103 "INSERT INTO {} (username) VALUES (%s)" .format (self .TABLE_NAME ),
98104 (None , ))
99- self .assertIn ('null value in column' , err .exception .message )
100-
101-
102- if __name__ == '__main__' :
103- unittest .main ()
105+ self .assertIn ('null value in column' , err .exception .message )
0 commit comments