@@ -23,35 +23,46 @@ def load_sql_query(file_name):
23
23
return file .read ()
24
24
25
25
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
+
36
39
# Compare with expected results
37
40
expected = expected_results [query_name ]["expected" ]
38
41
assert results_dict == expected , f"Error in { query_name } : { results_dict } != { expected } "
39
42
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")
43
46
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")
46
49
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")
49
52
50
53
# # Test that will fail
51
54
# def test_failing_active_patients_date(db_connection, expected_results):
52
55
# run_query_test(db_connection, expected_results, "active_patients_study_1368_0004", "failing_query_active_patients_date.sql")
53
56
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 )
55
66
56
67
57
68
0 commit comments