Skip to content

Commit fed4ab7

Browse files
committed
Adding integration tests
1 parent 32f6fff commit fed4ab7

7 files changed

+75
-23
lines changed

src/db.py

+2-4
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ def search_issue_type_summary(params: IssueTypeSummaryParams):
139139

140140
sql_count = f"SELECT COUNT(*) FROM '{s3_uri}' {where_clause}"
141141
sql_results = f"""
142-
SELECT organisation, organisation_name, dataset, issue_type, fields,
142+
SELECT organisation, organisation_name, dataset, resource, issue_type, field,
143143
count_issues, severity, responsibility FROM '{s3_uri}' {where_clause} LIMIT ? OFFSET ?"""
144144

145145
logger.debug(sql_count)
@@ -188,9 +188,7 @@ def search_dataset_resource_mapping(params: CommonParams):
188188
query_params.append(params.organisation)
189189

190190
sql_count = f"SELECT COUNT(*) FROM '{s3_uri}' {where_clause}"
191-
sql_results = f"""
192-
SELECT organisation, dataset, resource, mapped_fields FROM
193-
'{s3_uri}' {where_clause} LIMIT ? OFFSET ?"""
191+
sql_results = f"SELECT * FROM '{s3_uri}' {where_clause} LIMIT ? OFFSET ?"
194192

195193
logger.debug(sql_count)
196194
logger.debug(sql_results)

tests/conftest.py

+11-7
Original file line numberDiff line numberDiff line change
@@ -68,13 +68,17 @@ def s3_bucket(s3_client, test_dir):
6868
Body=f,
6969
)
7070

71-
parquet_file1 = f"{test_dir}/../files/provision_summary.parquet"
72-
with open(parquet_file1, "rb") as f:
73-
s3_client.put_object(
74-
Bucket=bucket_name,
75-
Key=f"{os.environ['PERFORMANCE_BASE_PATH']}/provision_summary.parquet",
76-
Body=f,
77-
)
71+
perf_parquet_files = glob.glob(f"{test_dir}/../files/performance/*.parquet")
72+
for file_path in perf_parquet_files:
73+
file_name = os.path.basename(file_path)
74+
s3_key = f"{os.environ['PERFORMANCE_BASE_PATH']}/{file_name}"
75+
76+
with open(file_path, "rb") as f:
77+
s3_client.put_object(
78+
Bucket=bucket_name,
79+
Key=s3_key,
80+
Body=f,
81+
)
7882

7983
parquet_file2 = f"{test_dir}/../files/specification.parquet"
8084
with open(parquet_file2, "rb") as f:
Binary file not shown.
Binary file not shown.
Binary file not shown.

tests/integration/test_main.py

+62-12
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
from fastapi.testclient import TestClient
22
from main import app
3-
from unittest.mock import patch
43
import json
54

65
# Create a test client for the FastAPI app
@@ -15,7 +14,6 @@ def test_search_issues(s3_bucket):
1514
"limit": 10,
1615
}
1716

18-
# Test the function that interacts with DuckDB and S3 via LocalStack
1917
response = client.get("/log/issue", params=params)
2018

2119
# Validate the results from the search
@@ -75,15 +73,14 @@ def test_provision_summary(s3_bucket):
7573
), "Expected active endpoint count to be 1"
7674

7775

78-
def test_specification(s3_bucket, duckdb_connection):
76+
def test_specification(s3_bucket):
7977
# Prepare test params
8078
params = {
8179
"offset": 0,
8280
"limit": 8,
8381
}
8482

85-
with patch("db.duckdb.connect", return_value=duckdb_connection):
86-
response = client.get("/specification/specification", params=params)
83+
response = client.get("/specification/specification", params=params)
8784

8885
# Validate the results from the search
8986
assert response.status_code == 200
@@ -96,16 +93,15 @@ def test_specification(s3_bucket, duckdb_connection):
9693
assert len(response_data) > 0
9794

9895

99-
def test_specification_with_dataset(s3_bucket, duckdb_connection):
96+
def test_specification_with_dataset(s3_bucket):
10097
# Prepare test params
10198
params = {
10299
"offset": 0,
103100
"limit": 8,
104101
"dataset": "article-4-direction-area",
105102
}
106103

107-
with patch("db.duckdb.connect", return_value=duckdb_connection):
108-
response = client.get("/specification/specification", params=params)
104+
response = client.get("/specification/specification", params=params)
109105

110106
# Validate the results from the search
111107
assert response.status_code == 200
@@ -138,7 +134,6 @@ def test_issue_type_summary(s3_bucket):
138134
assert "X-Pagination-Total-Results" in response.headers
139135
assert response.headers["X-Pagination-Total-Results"] == str(11)
140136
assert response.headers["X-Pagination-Limit"] == "8"
141-
142137
assert len(response_data) > 0
143138
filtered_rows = [
144139
item
@@ -147,7 +142,62 @@ def test_issue_type_summary(s3_bucket):
147142
== "8c61c7b72902daeaaa462002e62d840ce3916defacd54db97986654b180ce250"
148143
]
149144

150-
assert len(filtered_rows) == 5
145+
assert len(filtered_rows) == 6
146+
assert sum(1 for item in filtered_rows if item.get("issue_type") == "patch") == 4
147+
assert (
148+
sum(
149+
item.get("count_issues")
150+
for item in filtered_rows
151+
if item.get("issue_type") == "patch"
152+
)
153+
== 807
154+
)
155+
156+
157+
def test_dataset_resource_mapping(s3_bucket):
158+
# Prepare test params
159+
params = {
160+
"organisation": "local-authority:BUC",
161+
"dataset": "article-4-direction-area",
162+
"offset": 0,
163+
"limit": 8,
164+
}
165+
response = client.get("/performance/dataset_resource_mapping", params=params)
166+
167+
# Validate the results from the search
168+
assert response.status_code == 200
169+
170+
response_data = response.json()
171+
assert "X-Pagination-Total-Results" in response.headers
172+
assert response.headers["X-Pagination-Total-Results"] == str(9)
173+
assert response.headers["X-Pagination-Limit"] == "8"
174+
assert len(response_data) > 0
175+
assert response_data[0]["mapping_field"] == "geometry"
176+
assert (
177+
response_data[0]["non_mapping_field"]
178+
== "start-date;entry-date;name;description;reference"
179+
)
180+
181+
182+
def test_endpoint_dataset_summary(s3_bucket):
183+
# Prepare test params
184+
params = {
185+
"organisation": "local-authority:BUC",
186+
"dataset": "article-4-direction-area",
187+
"offset": 0,
188+
"limit": 10,
189+
}
190+
response = client.get("/performance/endpoint_dataset_summary", params=params)
191+
192+
# Validate the results from the search
193+
assert response.status_code == 200
194+
195+
response_data = response.json()
196+
assert "X-Pagination-Total-Results" in response.headers
197+
assert response.headers["X-Pagination-Total-Results"] == str(7)
198+
assert response.headers["X-Pagination-Limit"] == "10"
199+
assert len(response_data) > 0
151200
assert (
152-
sum(1 for item in filtered_rows if item.get("issue_type") == "patch") == 3
153-
) # Ensure 3 have issue_type as "patch"
201+
response_data[0]["endpoint"]
202+
== "01de81578391eab3a1fdc9fcb92e559aa0a0bf1aafc777905d680fc0e189811e"
203+
)

0 commit comments

Comments
 (0)