-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_faking_postgres.py
84 lines (62 loc) · 2.1 KB
/
test_faking_postgres.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import json
from pathlib import Path
import pgdb
import pytest
import pytest_check as check
import testing.postgresql
import pandas as pd
from sqlalchemy import create_engine
TEST_ROOT = Path(__file__).resolve().parent
CONNECTION, engine, psql = None, None, None
total_test_cases = 0
def fake_postgres():
global CONNECTION, engine, psql
psql = testing.postgresql.Postgresql()
info = psql.dsn()
CONNECTION = pgdb.connect(user=info['user'], host=info['host'], database=info['database'], port=info['port'])
engine = create_engine(psql.url())
def disconnect_fake_postgres():
global psql, CONNECTION
psql = psql.stop()
CONNECTION.close()
def rollback_connection():
CONNECTION.rollback()
def load_data():
global total_test_cases
path = TEST_ROOT / "data"
files = sorted(Path.glob(path, "postgres*.csv"))
tests = []
for file in files:
tests.append(pd.read_csv(str(file)))
with open(str(path / 'postgres_test_ground_truth.json'), 'r') as infile:
ground_truth = json.load(infile)
total_test_cases = len(tests)
test_case_ids = list(range(total_test_cases))
return list(zip(tests, ground_truth, test_case_ids))
def sum_ages():
records = query_database(operation="""SELECT age FROM students""")
ages = [r.age for r in records]
return sum(ages)
def query_database(operation):
cursor = CONNECTION.cursor()
records = cursor.execute(operation).fetchall()
cursor.close()
return records
def create_table(students_df):
students_df.to_sql('students', engine, if_exists='replace')
@pytest.mark.parametrize('students_df, ground_truth_sum, test_case_id', load_data())
def test_sum_ages(students_df, ground_truth_sum, test_case_id):
global total_test_cases
if test_case_id == 0:
fake_postgres()
create_table(students_df)
ages_sum = sum_ages()
if test_case_id < total_test_cases - 1:
rollback_connection()
else:
disconnect_fake_postgres()
assert ground_truth_sum == ages_sum
if __name__ == '__main__':
data = load_data()
test_sum_ages(data[0][0], data[0][1], 0)
pass