Skip to content

Commit 5362e66

Browse files
committed
added tests
1 parent ad74b20 commit 5362e66

File tree

7 files changed

+104
-5
lines changed

7 files changed

+104
-5
lines changed

.idea/workspace.xml

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
124 Bytes
Binary file not shown.
10 Bytes
Binary file not shown.
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
import testing.postgresql
2+
from unittest import TestCase
3+
4+
from easy_postgres_engine.tests.test_table_schema import TEST_TABLE_SCHEMA
5+
from easy_postgres_engine.postgres_engine import PostgresEngine
6+
7+
8+
def create_table(postgresqlConnection):
9+
config = postgresqlConnection.dsn()
10+
dbEngine = PostgresEngine(
11+
databaseName=config['database'],
12+
user=config['user'],
13+
password=config.get('password'),
14+
port=config['port'],
15+
host=config['host']
16+
)
17+
dbEngine.create_table(schema=TEST_TABLE_SCHEMA)
18+
19+
20+
# Generate Postgresql class which shares the generated database
21+
Postgresql = testing.postgresql.PostgresqlFactory(cache_initialized_db=True, on_initialized=create_table)
22+
23+
24+
def tearDownModule():
25+
# clear cached database at end of tests
26+
Postgresql.clear_cache()
27+
28+
29+
class TestPostgresEngine(TestCase):
30+
31+
def __init__(self, methodName='runTest'):
32+
super(TestPostgresEngine, self).__init__(methodName=methodName)
33+
self.firstCustomerId = 10
34+
self.firstCustomerName = 'Mary'
35+
self.secondCustomerId = 50
36+
self.secondCustomerName = 'John'
37+
38+
def setUp(self):
39+
super().setUp()
40+
self.postgresql = Postgresql()
41+
config = self.postgresql.dsn()
42+
self.dbEngine = PostgresEngine(
43+
databaseName=config['database'],
44+
user=config['user'],
45+
password=config.get('password'),
46+
port=config['port'],
47+
host=config['host']
48+
)
49+
50+
def tearDown(self):
51+
super().tearDown()
52+
self.postgresql.stop()
53+
54+
def test_engine(self):
55+
# TODO(Mike): probably better to test insertion + selection queries separately, but DB persistence between test functions is awkward
56+
TEST_INSERTION_QUERY = """
57+
INSERT INTO
58+
tbl_example(customer_id, customer_name)
59+
VALUES
60+
(%(customerId)s, %(customerName)s)
61+
"""
62+
insertedId1 = self.dbEngine.run_update_query(
63+
query=TEST_INSERTION_QUERY,
64+
parameters={'customerId': self.firstCustomerId, 'customerName': self.firstCustomerName}
65+
)
66+
self.assertEqual(insertedId1, 1)
67+
insertedId2 = self.dbEngine.run_update_query(
68+
query=TEST_INSERTION_QUERY,
69+
parameters={'customerId': self.secondCustomerId, 'customerName': self.secondCustomerName}
70+
)
71+
self.assertEqual(insertedId2, 2)
72+
73+
queryResults = self.dbEngine.run_select_query(query='SELECT * FROM tbl_example')
74+
self.assertSequenceEqual(list(queryResults['customer_id'].values), [self.firstCustomerId, self.secondCustomerId])
75+
self.assertSequenceEqual(list(queryResults['customer_name'].values), [self.firstCustomerName, self.secondCustomerName])
76+
77+
specificQueryResults = self.dbEngine.run_select_query(
78+
query="""
79+
SELECT
80+
*
81+
FROM
82+
tbl_example
83+
WHERE
84+
customer_id = %(customerId)s
85+
""",
86+
parameters={'customerId': self.firstCustomerId}
87+
)
88+
self.assertEqual(len(specificQueryResults), 1)
89+
self.assertEqual(specificQueryResults['customer_name'].iloc[0], self.firstCustomerName)
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
TEST_TABLE_SCHEMA = """
2+
CREATE TABLE IF NOT EXISTS tbl_example
3+
(
4+
id SERIAL PRIMARY KEY,
5+
customer_id INTEGER,
6+
customer_name TEXT
7+
);
8+
"""

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
pandas==0.25.3
22
psycopg2-binary==2.8.4
3+
testing.postgresql==1.3.0

setup.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
keywords=['postgreSQL', 'postgres'],
1313
install_requires=[
1414
'pandas==0.25.3',
15-
'psycopg2-binary==2.8.4'
15+
'psycopg2-binary==2.8.4',
16+
'testing.postgresql==1.3.0'
1617
],
1718
classifiers=[
1819
"Programming Language :: Python :: 3",

0 commit comments

Comments
 (0)