-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add some direct tests for PgWrapper error handling
and also fix one issue they caught
- Loading branch information
Showing
2 changed files
with
35 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import unittest | ||
from os import getenv | ||
import psycopg2 | ||
|
||
from modules import PgWrapper | ||
|
||
pg_conn_str = getenv("SB_PG_DB", None) | ||
if not pg_conn_str: | ||
raise Exception("SB_PG_DB needs to be set") | ||
|
||
class PgWrapperTests(unittest.TestCase): | ||
def test_handles_when_connection_is_disconnected(self): | ||
dbwrapper = PgWrapper(pg_conn_str) | ||
|
||
self.assertEqual(1, dbwrapper.query_one("SELECT 1;")) | ||
|
||
# now we sneakily poke inside dbwrapper and fake a random network disconnection | ||
dbwrapper._conn.close() | ||
|
||
# but dbwrapper should recover | ||
self.assertEqual(1, dbwrapper.query_one("SELECT 1;")) | ||
|
||
def test_handles_connection_in_failed_state(self): | ||
dbwrapper = PgWrapper(pg_conn_str) | ||
|
||
self.assertEqual(1, dbwrapper.query_one("SELECT 1;")) | ||
|
||
# running some invalid sql puts the transaction into a failed state: | ||
self.assertRaises( | ||
psycopg2.errors.UndefinedTable, | ||
lambda: dbwrapper.query_all("SELECT * FROM nonexistent_table;")) | ||
|
||
# but dbwrapper should recover | ||
self.assertEqual(1, dbwrapper.query_one("SELECT 1;")) |