Skip to content

Commit 66140b0

Browse files
committed
good sql unit testing strategy
1 parent 1cc2beb commit 66140b0

15 files changed

+1477
-596
lines changed

.gitignore

+1-5
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,5 @@ __pycache__/
22
.pytest_cache/
33
archive/
44
output.json
5-
data/validated_reference_dataset.xlsx
5+
#ata/validated_reference_dataset.xlsx
66

7-
# YAML validation rules instead of Python
8-
# rules_config.yaml
9-
# validation_rules_yaml.py
10-
# tests/test_sql_logic_yaml.py

data/validated_reference_dataset.xlsx

12.3 KB
Binary file not shown.
-12.4 KB
Binary file not shown.

expected_results/expected_results.json

+5
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,10 @@
2020
"expected": [
2121
{"week": "2017-31", "active_patients": 30}
2222
]
23+
},
24+
"active_patients_study_1368_0004": {
25+
"expected": [
26+
{"week": "2017-34", "active_patients": 60}
27+
]
2328
}
2429
}

expected_results/expected_results_negative.json

-5
This file was deleted.

rules_config.yaml

-29
This file was deleted.

sql_queries/active_patients_date.sql

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
SELECT week, active_patients
22
FROM clinical_trials
3-
WHERE week = "2017-31"
3+
WHERE week = "2017-31" AND study = "1368-0004"
44

5-
-- Como hacer para que el test podamos generalizarlo para que sea active_patients en una fecha concreta.
6-
-- De esta forma podemos pasarle distintas fechas y mirar si en todas el test passa.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
SELECT week, active_patients
2+
FROM clinical_trials
3+
WHERE week = "2017-XX" AND study = "1368-0004"
4+
-- error introduced week = "2017-XX" should be week = "2017-34"

tests/conftest.py

-12
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,3 @@ def db_connection():
1414
connection = engine.connect()
1515
yield connection
1616
connection.close()
17-
18-
@pytest.fixture(scope='module')
19-
def db_connection_negative():
20-
engine = create_engine('sqlite:///:memory:')
21-
file_path = os.path.join(os.path.dirname(__file__), '..', 'data', 'validated_reference_dataset_errors.xlsx')
22-
df = pd.read_excel(file_path)
23-
df.to_sql('clinical_trials', engine, if_exists='replace', index=False)
24-
25-
connection = engine.connect()
26-
yield connection
27-
connection.close()
28-

tests/pytest_html_report.html

+1,458-206
Large diffs are not rendered by default.

tests/test_negative_sql_logic_yaml.py

-116
This file was deleted.

tests/test_sql_logic.py

+8-57
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
11
# tests/test_sql_logic.py
22
import sys
33
import os
4-
# Add the parent directory to sys.path to locate validation_rules.py
5-
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))
6-
74
import pytest
85
import json
9-
from validation_rules import validate_entry, common_rules
106

117
@pytest.fixture(scope='module')
128
def expected_results():
@@ -16,7 +12,7 @@ def fetch_results_as_dict(results):
1612
return [dict(row) for row in results]
1713

1814
def load_expected_results():
19-
json_path = os.path.join(os.path.dirname(__file__), '..', 'expected_results.json')
15+
json_path = os.path.join(os.path.dirname(__file__), '..', 'expected_results/expected_results.json')
2016
with open(json_path) as f:
2117
return json.load(f)
2218

@@ -36,20 +32,23 @@ def run_query_test(db_connection, expected_results, query_name, sql_file):
3632
results = db_connection.execute(query_sql)
3733
results_dict = fetch_results_as_dict(results)
3834

39-
# Filter valid results
40-
filtered_results = [result for result in results_dict if validate_entry(result, common_rules)]
41-
4235
# Compare with expected results
4336
expected = expected_results[query_name]["expected"]
44-
assert filtered_results == expected, f"Error in {query_name}: {filtered_results} != {expected}"
37+
assert results_dict == expected, f"Error in {query_name}: {results_dict} != {expected}"
4538

39+
# Tests
4640
def test_active_sites_positive(db_connection, expected_results):
4741
run_query_test(db_connection, expected_results, "active_sites_positive", "active_sites_positive.sql")
4842

4943
def test_randomized_patients_positive(db_connection, expected_results):
5044
run_query_test(db_connection, expected_results, "randomized_patients_positive", "randomized_patients_positive.sql")
5145

46+
def test_active_patients_date(db_connection, expected_results):
47+
run_query_test(db_connection, expected_results, "active_patients_date", "active_patients_date.sql")
5248

49+
# Test that will fail
50+
def test_failing_active_patients_date(db_connection, expected_results):
51+
run_query_test(db_connection, expected_results, "active_patients_study_1368_0004", "failing_query_active_patients_date.sql")
5352

5453
# @pytest.mark.parametrize("query_name, sql_file", [
5554
# ("active_sites_positive", "active_sites_positive.sql"),
@@ -70,52 +69,4 @@ def test_randomized_patients_positive(db_connection, expected_results):
7069

7170

7271

73-
# import sys
74-
# import os
75-
# sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))
76-
# import pytest
77-
# import json
78-
# from validation_rules import validate_entry, common_rules
79-
80-
# def fetch_results_as_dict(results):
81-
# return [dict(row) for row in results]
82-
83-
# def load_expected_results():
84-
# json_path = os.path.join(os.path.dirname(__file__), '..', 'expected_results.json')
85-
# with open(json_path) as f:
86-
# return json.load(f)
87-
88-
# expected_results = load_expected_results()
89-
90-
# def load_sql_query(file_name):
91-
# sql_path = os.path.join(os.path.dirname(__file__), '..', 'sql_queries', file_name)
92-
# with open(sql_path, 'r') as file:
93-
# return file.read()
94-
95-
# def test_active_sites_positive(db_connection):
96-
# query_name = "active_sites_positive"
97-
# query_sql = load_sql_query("active_sites_positive.sql")
98-
# results = db_connection.execute(query_sql)
99-
# results_dict = fetch_results_as_dict(results)
100-
101-
# # Filtrar resultados válidos
102-
# filtered_results = [result for result in results_dict if validate_entry(result, common_rules)]
103-
104-
# # Comparar con resultados esperados
105-
# expected = expected_results[query_name]["expected"]
106-
# assert filtered_results == expected, f"Error in {query_name}: {filtered_results} != {expected}"
107-
108-
# def test_randomized_patients_positive(db_connection):
109-
# query_name = "randomized_patients_positive"
110-
# query_sql = load_sql_query("randomized_patients_positive.sql")
111-
# results = db_connection.execute(query_sql)
112-
# results_dict = fetch_results_as_dict(results)
113-
114-
# # Filtrar resultados válidos
115-
# filtered_results = [result for result in results_dict if validate_entry(result, common_rules)]
116-
117-
# # Comparar con resultados esperados
118-
# expected = expected_results[query_name]["expected"]
119-
# assert filtered_results == expected, f"Error in {query_name}: {filtered_results} != {expected}"
120-
12172

tests/test_sql_logic_yaml.py

-75
This file was deleted.

0 commit comments

Comments
 (0)