Skip to content

Commit 12d1f07

Browse files
committed
Adding DuckDB connection explicitly for tests
1 parent b40bbfb commit 12d1f07

File tree

3 files changed

+23
-9
lines changed

3 files changed

+23
-9
lines changed

Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ coverage-unit:
99
pytest --cov=src tests/unit/
1010

1111
coverage-integration:
12-
pytest --cov=src --cov-append --cov-fail-under=90 tests/integration/
12+
pytest --cov=src --cov-append --cov-fail-under=75 tests/integration/
1313

1414
lint:: black-check flake8
1515

tests/conftest.py

+15-4
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,24 @@
11
import os
22
import pytest
3+
import duckdb
34
import boto3
45
from testcontainers.localstack import LocalStackContainer
56
from botocore.exceptions import ClientError
67

8+
79
os.environ["AWS_ACCESS_KEY_ID"] = "test"
810
os.environ["AWS_SECRET_ACCESS_KEY"] = "test"
911
os.environ["AWS_DEFAULT_REGION"] = "eu-west-2"
1012
os.environ["COLLECTION_BUCKET"] = "test-bucket"
1113
os.environ["ISSUES_BASE_PATH"] = "test/path"
1214
os.environ["USE_AWS_CREDENTIAL_CHAIN"] = "false"
13-
os.environ["DUCKDB_S3_USE_SSL"] = "false"
1415

1516

1617
@pytest.fixture(scope="module")
1718
def localstack_container():
1819
# Start LocalStack container
1920
with LocalStackContainer() as localstack:
2021
# Wait for the service to be ready
21-
os.environ[
22-
"DUCKDB_S3_ENDPOINT"
23-
] = f"s3.localhost.localstack.cloud: {localstack.get_exposed_port(4566)}"
2422
yield localstack
2523

2624

@@ -37,6 +35,19 @@ def s3_client(localstack_container):
3735
return s3
3836

3937

38+
@pytest.fixture(scope="module")
39+
def duckdb_connection(localstack_container):
40+
# Set up a DuckDB in-memory database
41+
conn = duckdb.connect()
42+
# Configure DuckDB to connect to S3 via LocalStack
43+
conn.execute(
44+
f"SET s3_endpoint = '{localstack_container.get_url().lstrip('http://')}';"
45+
)
46+
conn.execute("SET s3_url_style = 'path';")
47+
yield conn
48+
conn.close()
49+
50+
4051
@pytest.fixture(scope="module")
4152
def test_dir(request):
4253
return os.path.dirname(request.module.__file__)

tests/integration/test_main.py

+7-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from fastapi.testclient import TestClient
2+
from unittest.mock import patch
23
from main import app
34
import json
45

@@ -7,7 +8,7 @@
78
client = TestClient(app)
89

910

10-
def test_search_issues(s3_bucket):
11+
def test_search_issues(s3_bucket, duckdb_connection):
1112
# Prepare test params
1213
params = {
1314
"dataset": "conservation-area",
@@ -16,7 +17,8 @@ def test_search_issues(s3_bucket):
1617
}
1718

1819
# Test the function that interacts with DuckDB and S3 via LocalStack
19-
response = client.get("/log/issue", params=params)
20+
with patch("db.duckdb.connect", return_value=duckdb_connection):
21+
response = client.get("/log/issue", params=params)
2022

2123
# Validate the results from the search
2224
assert response.status_code == 200
@@ -33,12 +35,13 @@ def test_search_issues(s3_bucket):
3335
)
3436

3537

36-
def test_search_issues_no_parameters():
38+
def test_search_issues_no_parameters(duckdb_connection):
3739
# Prepare test params
3840
params = {}
3941

4042
# Test the function that interacts with DuckDB and S3 via LocalStack
41-
response = client.get("/log/issue", params=params)
43+
with patch("db.duckdb.connect", return_value=duckdb_connection):
44+
response = client.get("/log/issue", params=params)
4245

4346
response_json = json.loads(response.content.decode("utf-8"))
4447
details = response_json.get("detail", [])

0 commit comments

Comments
 (0)