Skip to content

Commit 2ecf2f8

Browse files
author
cortze
committed
upgrade the error handling at the db connection
1 parent 8d7f1b6 commit 2ecf2f8

File tree

2 files changed

+33
-20
lines changed

2 files changed

+33
-20
lines changed

unit_db_test/postgresql.py

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,16 @@
11
# postgresql.py
22
import pandas as pd
3+
import psycopg2 as pspg
34
import sqlalchemy as sa
45

56

7+
class NoConnectionToDBError(Exception):
8+
def __init__(self, cause):
9+
self.cause = cause
10+
11+
def cause(self):
12+
return self.cause
13+
614
def parse_db_endpoint_string(s):
715
user = s.split(":")[1].split("@")[0].split("//")[1]
816
password = s.split(":")[2].split("@")[0]
@@ -15,23 +23,27 @@ def parse_db_endpoint_string(s):
1523
class Postgres:
1624
def __init__(
1725
self,
18-
port=None,
19-
user=None,
20-
password=None,
21-
database=None,
22-
host="localhost",
23-
url=None,
26+
user: str,
27+
password: str,
28+
host: str,
29+
port: str,
30+
database: str,
2431
):
25-
self.port = port
2632
self.user = user
2733
self.password = password
34+
self.host = host
35+
self.port = port
2836
self.database = database
29-
if url is not None:
30-
self.engine = sa.create_engine(url=url)
31-
else:
32-
self.url = f"postgresql://{user}:{password}@{host}:{port}/{database}"
33-
self.engine = sa.create_engine(url=url)
34-
self.conn = self.engine.connect()
37+
self.url = f"postgresql://{user}:{password}@{host}:{port}/{database}"
38+
try:
39+
self.engine = sa.create_engine(url=self.url)
40+
self.conn = self.engine.connect()
41+
except pspg.OperationalError as dbError:
42+
print(f"Error connecting to the database: {dbError}")
43+
raise NoConnectionToDBError(dbError)
44+
except Exception as e:
45+
print(f"An unexpected error occurred: {e}")
46+
raise NoConnectionToDBError(e)
3547

3648
def get_df_from_sql_query(self, sql_query):
3749
return pd.read_sql_query(sa.text(sql_query), self.conn)

unit_db_test/testcase.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
import pandas as pd
33
import unittest
44

5-
from unit_db_test.postgresql import Postgres
6-
from unit_db_test.utils import load_credentials_from
5+
from unit_db_test.postgresql import Postgres, NoConnectionToDBError
6+
from unit_db_test.utils import load_credentials_from, NoEnvVariableError
77

88

99

@@ -26,9 +26,10 @@ def setUp(self):
2626
try:
2727
u, p, i, port, db, url = self.readCredentialsFromEnv()
2828
self.connectDB(u, p, i, port, db, url)
29-
except Exception as e:
30-
self.assertRaise(e)
31-
self.fail("unable to make connection to the DB")
29+
except NoEnvVariableError as envError:
30+
raise envError
31+
except NoConnectionToDBError as dbError:
32+
raise dbError
3233

3334
def setDown(self):
3435
self.db.close()
@@ -47,7 +48,7 @@ def assertCustomNullItemsInColumn(self, df: pd.DataFrame, column: str, target: i
4748
def assertNoRows(self, df: pd.DataFrame):
4849
self.assertNRows(df, 0)
4950

50-
def assertNRows(self, df: pd.DataFrame, target_rows: str):
51+
def assertNRows(self, df: pd.DataFrame, target_rows: int):
5152
if len(df) != target_rows:
52-
raise AssertionError(f"df has {len(df)} items, expected {target_rows}")
53+
raise AssertionError(f"df has {len(df)} rows, expected {target_rows}")
5354

0 commit comments

Comments
 (0)