Skip to content

Commit 3060373

Browse files
committed
parametrized tests and exception handling
1 parent a99f897 commit 3060373

File tree

1 file changed

+29
-18
lines changed

1 file changed

+29
-18
lines changed

tests/test_sql_logic.py

+29-18
Original file line numberDiff line numberDiff line change
@@ -23,35 +23,46 @@ def load_sql_query(file_name):
2323
return file.read()
2424

2525
def run_query_test(db_connection, expected_results, query_name, sql_file):
26-
"""
27-
Helper function to run a SQL query and validate its results.
28-
It encapsulates the common logic for running a SQL query, fetching results,
29-
filtering valid results, and comparing them with expected results.
30-
This function is called by the individual test functions to perform the shared logic.
31-
"""
32-
query_sql = load_sql_query(sql_file)
33-
results = db_connection.execute(text(query_sql))
34-
results_dict = fetch_results_as_dict(results)
35-
26+
try:
27+
"""
28+
Helper function to run a SQL query and validate its results.
29+
It encapsulates the common logic for running a SQL query, fetching results,
30+
filtering valid results, and comparing them with expected results.
31+
This function is called by the individual test functions to perform the shared logic.
32+
"""
33+
query_sql = load_sql_query(sql_file)
34+
results = db_connection.execute(text(query_sql))
35+
results_dict = fetch_results_as_dict(results)
36+
except Exception as e:
37+
pytest.fail(f"Error al ejecutar la consulta {query_name}: {str(e)}")
38+
3639
# Compare with expected results
3740
expected = expected_results[query_name]["expected"]
3841
assert results_dict == expected, f"Error in {query_name}: {results_dict} != {expected}"
3942

40-
# Tests
41-
def test_active_sites_positive(db_connection, expected_results):
42-
run_query_test(db_connection, expected_results, "active_sites_positive", "active_sites_positive.sql")
43+
# # Tests
44+
# def test_active_sites_positive(db_connection, expected_results):
45+
# run_query_test(db_connection, expected_results, "active_sites_positive", "active_sites_positive.sql")
4346

44-
def test_randomized_patients_positive(db_connection, expected_results):
45-
run_query_test(db_connection, expected_results, "randomized_patients_positive", "randomized_patients_positive.sql")
47+
# def test_randomized_patients_positive(db_connection, expected_results):
48+
# run_query_test(db_connection, expected_results, "randomized_patients_positive", "randomized_patients_positive.sql")
4649

47-
def test_active_patients_date(db_connection, expected_results):
48-
run_query_test(db_connection, expected_results, "active_patients_date", "active_patients_date.sql")
50+
# def test_active_patients_date(db_connection, expected_results):
51+
# run_query_test(db_connection, expected_results, "active_patients_date", "active_patients_date.sql")
4952

5053
# # Test that will fail
5154
# def test_failing_active_patients_date(db_connection, expected_results):
5255
# run_query_test(db_connection, expected_results, "active_patients_study_1368_0004", "failing_query_active_patients_date.sql")
5356

54-
57+
# Parametrized test
58+
@pytest.mark.parametrize("query_name, sql_file", [
59+
("active_sites_positive", "active_sites_positive.sql")
60+
,("randomized_patients_positive", "randomized_patients_positive.sql")
61+
,("active_patients_date", "active_patients_date.sql")
62+
# ,("active_patients_study_1368_0004", "failing_query_active_patients_date.sql")
63+
])
64+
def test_sql_queries(db_connection, expected_results, query_name, sql_file):
65+
run_query_test(db_connection, expected_results, query_name, sql_file)
5566

5667

5768

0 commit comments

Comments
 (0)