Skip to content

Commit 673f2a9

Browse files
author
cortze
committed
update db connection to testcase class
1 parent 2ecf2f8 commit 673f2a9

File tree

4 files changed

+38
-14
lines changed

4 files changed

+38
-14
lines changed

docker-compose.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,4 @@ services:
1010
- POSTGRES_PASSWORD=${DB_PASSWORD}
1111
- POSTGRES_DB=${DB_DATABASE}
1212
volumes:
13-
- ./.github/workflows/init_test_db.sql:/docker-entrypoint-initdb.d/docker_postgres_init.sql
13+
- ./psql/docker_init_test_db.sql:/docker-entrypoint-initdb.d/docker_postgres_init.sql

psql/docker_init_test_db.sql

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
-- Create custom tables for the testing
2+
CREATE TABLE IF NOT EXISTS test_table(
3+
id INT,
4+
text_col TEXT,
5+
attribute_col TEXT,
6+
bool_col BOOL,
7+
8+
PRIMARY KEY(id));
9+
10+
-- Insert dummy data into test_table
11+
INSERT INTO test_table(
12+
id,
13+
text_col,
14+
attribute_col,
15+
bool_col)
16+
VALUES
17+
(1, 'first', 'attr_1', false),
18+
(2, 'second', null, true),
19+
(3, 'third', 'attr_1', false),
20+
(4, 'fourth', 'attr_3', true);

unit_db_test/postgresql.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,8 @@ def __init__(
3939
self.engine = sa.create_engine(url=self.url)
4040
self.conn = self.engine.connect()
4141
except pspg.OperationalError as dbError:
42-
print(f"Error connecting to the database: {dbError}")
4342
raise NoConnectionToDBError(dbError)
4443
except Exception as e:
45-
print(f"An unexpected error occurred: {e}")
4644
raise NoConnectionToDBError(e)
4745

4846
def get_df_from_sql_query(self, sql_query):

unit_db_test/testcase.py

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,30 +9,36 @@
99

1010
class DBintegrityTest(unittest.TestCase):
1111
db_config_file = ".env"
12+
db = None
1213

1314
# Initialization and configuration of the database
14-
def dbCredentials(self):
15+
@classmethod
16+
def dbCredentials(cls):
1517
""" replace this function at each unitt db integrity test with the db credentials """
16-
self.db_config_file = ".env"
18+
cls.db_config_file = ".env"
1719

18-
def readCredentialsFromEnv(self):
19-
return load_credentials_from(self.db_config_file)
20+
@classmethod
21+
def readCredentialsFromEnv(cls):
22+
return load_credentials_from(cls.db_config_file)
2023

21-
def connectDB(self, user, password, ip, port, db, url):
22-
self.db = Postgres(user=user, password=password, host=ip, port=port, database=db, url=url)
24+
@classmethod
25+
def connectDB(cls, user, password, ip, port, db):
26+
cls.db = Postgres(user=user, password=password, host=ip, port=port, database=db)
2327

2428
# setup main function for the db connection
25-
def setUp(self):
29+
@classmethod
30+
def setUpClass(cls):
2631
try:
27-
u, p, i, port, db, url = self.readCredentialsFromEnv()
28-
self.connectDB(u, p, i, port, db, url)
32+
u, p, i, port, db = cls.readCredentialsFromEnv()
33+
cls.connectDB(u, p, i, port, db)
2934
except NoEnvVariableError as envError:
3035
raise envError
3136
except NoConnectionToDBError as dbError:
3237
raise dbError
3338

34-
def setDown(self):
35-
self.db.close()
39+
@classmethod
40+
def setDownClass(cls):
41+
cls.db.close()
3642

3743
# Extension of the pre-available assert-methods (Panda Oriented)
3844
def assertNotNullItemsInColumn(self, df: pd.DataFrame, column: str):

0 commit comments

Comments
 (0)