Skip to content

Commit 08f2a94

Browse files
authored
Merge pull request #2 from bernatsort/dev
2 parents f023878 + d3a9f3d commit 08f2a94

File tree

2 files changed

+74
-19
lines changed

2 files changed

+74
-19
lines changed

README.md

+45-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,45 @@
1-
# Unit Testing SQL Queries Using Python
1+
# SQL Unit Testing in Python
2+
3+
This project demonstrates how to perform unit testing for SQL queries using Python. The goal is to ensure the accuracy and reliability of SQL logic used in data processing and analytics. The project leverages pytest for testing, SQLAlchemy for database interaction, and GitHub Actions for CI/CD.
4+
5+
6+
## Prerequisites
7+
8+
- Python 3.12
9+
- pip
10+
11+
## Setup
12+
13+
1. **Clone the repository**
14+
```sh
15+
git clone https://github.com/bernatsort/sql-unit-testing-python.git
16+
cd sql-unit-testing-python
17+
```
18+
19+
2. **Create a virtual environment**
20+
```sh
21+
python -m venv venv
22+
source venv/bin/activate # On Windows use `venv\Scripts\activate`
23+
```
24+
25+
3. **Install dependencies**
26+
```sh
27+
pip install --upgrade pip
28+
pip install pytest sqlalchemy pandas openpyxl
29+
```
30+
31+
## Running the Tests
32+
33+
- **Run all tests**
34+
```sh
35+
pytest
36+
```
37+
38+
## CI/CD Pipeline
39+
40+
The project uses GitHub Actions for continuous integration. The CI pipeline is configured to:
41+
42+
1. **Checkout the repository**
43+
2. **Set up Python 3.12**
44+
3. **Install dependencies**
45+

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)