Skip to content

Commit f792bad

Browse files
committed
test: add Redis support and enhance test infrastructure
Add Redis container to testing environment and improve test fixtures: - Configure Redis test instance on port 6380 (database 1) - Update test database scripts (PowerShell and Bash) to manage Redis container - Add Redis configuration to TestConfig - Create redis_client fixture in conftest.py for tests - Move logged_in_client fixture to conftest.py for broader reuse - Update documentation in tests/README.md - Prepare infrastructure for upcoming API and lock manager tests These changes support testing of Zenodo API integration and resource locking functionality.
1 parent e680f61 commit f792bad

12 files changed

+1496
-51
lines changed

tests/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,15 @@ This directory contains tests for the Heritrace application.
1111
- `test_extensions.py`: Tests for the extensions module
1212
- `test_routes.py`: Tests for the routes
1313
- `test_editor.py`: Tests for the editor module
14+
- `test_api.py`: Tests for the API routes
1415

1516
## Test Database Setup
1617

1718
Before running tests, you need to set up the test databases. The tests use separate database instances running on different ports than the main application:
1819

1920
- Test Dataset database on port 9999
2021
- Test Provenance database on port 9998
22+
- Test Redis instance on port 6380 (database 1)
2123

2224
### Starting Test Databases
2325

tests/Start-TestDatabases.ps1

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@ if (docker ps -a --format "{{.Names}}" | Select-String -Pattern "^test-provenanc
2828
docker rm -f test-provenance-db
2929
}
3030

31+
if (docker ps -a --format "{{.Names}}" | Select-String -Pattern "^test-redis$") {
32+
Write-Host "Removing existing test-redis container..."
33+
docker rm -f test-redis
34+
}
35+
3136
# Start Virtuoso for dataset database on port 9999 (different from dev port 8999)
3237
Write-Host "Starting test-dataset-db container..."
3338
docker run -d --name test-dataset-db `
@@ -46,6 +51,12 @@ docker run -d --name test-provenance-db `
4651
-v "${provDbPath}:/database" `
4752
openlink/virtuoso-opensource-7:latest
4853

54+
# Start Redis for resource locking on port 6380
55+
Write-Host "Starting test-redis container..."
56+
docker run -d --name test-redis `
57+
-p 6380:6379 `
58+
redis:7-alpine
59+
4960
# Wait for databases to be ready
5061
Write-Host "Waiting for test databases to start..."
5162
Start-Sleep -Seconds 30
@@ -67,4 +78,5 @@ docker exec test-provenance-db /opt/virtuoso-opensource/bin/isql -U dba -P dba e
6778

6879
Write-Host "Permissions set successfully."
6980
Write-Host "Dataset DB: http://localhost:9999/sparql"
70-
Write-Host "Provenance DB: http://localhost:9998/sparql"
81+
Write-Host "Provenance DB: http://localhost:9998/sparql"
82+
Write-Host "Redis: localhost:6380"

tests/Stop-TestDatabases.ps1

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,22 @@
11
# PowerShell script to stop test databases for HERITRACE
22

3-
Write-Host "Stopping test databases..."
3+
# Stop and remove the test database containers
4+
if (docker ps -a --format "{{.Names}}" | Select-String -Pattern "^test-dataset-db$") {
5+
Write-Host "Stopping and removing test-dataset-db container..."
6+
docker stop test-dataset-db
7+
docker rm test-dataset-db
8+
}
49

5-
# Stop and remove dataset database container
6-
docker stop test-dataset-db 2>$null
7-
docker rm test-dataset-db 2>$null
10+
if (docker ps -a --format "{{.Names}}" | Select-String -Pattern "^test-provenance-db$") {
11+
Write-Host "Stopping and removing test-provenance-db container..."
12+
docker stop test-provenance-db
13+
docker rm test-provenance-db
14+
}
815

9-
# Stop and remove provenance database container
10-
docker stop test-provenance-db 2>$null
11-
docker rm test-provenance-db 2>$null
16+
if (docker ps -a --format "{{.Names}}" | Select-String -Pattern "^test-redis$") {
17+
Write-Host "Stopping and removing test-redis container..."
18+
docker stop test-redis
19+
docker rm test-redis
20+
}
1221

13-
Write-Host "Test databases stopped and containers removed."
22+
Write-Host "Test databases stopped and removed."

tests/conftest.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from flask import Flask
1010
from flask.testing import FlaskClient, FlaskCliRunner
1111
from heritrace import create_app
12+
from redis import Redis
1213
from tests.test_config import TestConfig
1314

1415

@@ -32,3 +33,48 @@ def client(app: Flask) -> FlaskClient:
3233
def runner(app: Flask) -> FlaskCliRunner:
3334
"""A test CLI runner for the app."""
3435
return app.test_cli_runner()
36+
37+
38+
@pytest.fixture
39+
def redis_client() -> Generator[Redis, None, None]:
40+
"""Create a Redis client for testing.
41+
42+
This connects to the Redis instance running on port 6380 (database 1)
43+
which is started by the test database scripts.
44+
"""
45+
client = Redis.from_url(TestConfig.REDIS_URL)
46+
47+
# Clear any existing test data
48+
client.flushdb()
49+
50+
yield client
51+
52+
# Clean up after tests
53+
client.flushdb()
54+
55+
56+
@pytest.fixture
57+
def logged_in_client(client: FlaskClient) -> Generator[FlaskClient, None, None]:
58+
"""Create a client with a logged-in user session."""
59+
with client.session_transaction() as sess:
60+
sess["user_id"] = "0000-0000-0000-0000"
61+
sess["user_name"] = "Test User"
62+
sess["is_authenticated"] = True
63+
sess["lang"] = "en"
64+
sess["orcid"] = "0000-0000-0000-0000"
65+
sess["_fresh"] = True
66+
sess["_id"] = "test-session-id"
67+
sess["_user_id"] = "0000-0000-0000-0000"
68+
69+
# Add OAuth token information if needed
70+
sess["oauth_token"] = {
71+
"access_token": "test-access-token",
72+
"token_type": "bearer",
73+
"refresh_token": "test-refresh-token",
74+
"expires_in": 3600,
75+
"scope": ["openid", "/read-limited"],
76+
"name": "Test User",
77+
"orcid": "0000-0000-0000-0000",
78+
}
79+
80+
yield client

tests/start-test-databases.sh

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ if [ "$(docker ps -a -q -f name=test-provenance-db)" ]; then
1919
docker rm -f test-provenance-db
2020
fi
2121

22+
if [ "$(docker ps -a -q -f name=test-redis)" ]; then
23+
echo "Removing existing test-redis container..."
24+
docker rm -f test-redis
25+
fi
26+
2227
# Start Virtuoso for dataset database on port 9999 (different from dev port 8999)
2328
echo "Starting test-dataset-db container..."
2429
docker run -d --name test-dataset-db \
@@ -37,6 +42,12 @@ docker run -d --name test-provenance-db \
3742
-v "${CURRENT_DIR}/tests/test_provenance_db:/database" \
3843
openlink/virtuoso-opensource-7:latest
3944

45+
# Start Redis for resource locking on port 6380
46+
echo "Starting test-redis container..."
47+
docker run -d --name test-redis \
48+
-p 6380:6379 \
49+
redis:7-alpine
50+
4051
# Wait for databases to be ready
4152
echo "Waiting for test databases to start..."
4253
sleep 30
@@ -58,4 +69,5 @@ docker exec test-provenance-db /opt/virtuoso-opensource/bin/isql -U dba -P dba e
5869

5970
echo "Permissions set successfully."
6071
echo "Dataset DB: http://localhost:9999/sparql"
61-
echo "Provenance DB: http://localhost:9998/sparql"
72+
echo "Provenance DB: http://localhost:9998/sparql"
73+
echo "Redis: localhost:6380"

tests/stop-test-databases.sh

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,23 @@
11
#!/bin/bash
22
# Script to stop test databases for HERITRACE
33

4-
echo "Stopping test databases..."
4+
# Stop and remove the test database containers
5+
if [ "$(docker ps -a -q -f name=test-dataset-db)" ]; then
6+
echo "Stopping and removing test-dataset-db container..."
7+
docker stop test-dataset-db
8+
docker rm test-dataset-db
9+
fi
510

6-
# Stop and remove dataset database container
7-
docker stop test-dataset-db || true
8-
docker rm test-dataset-db || true
11+
if [ "$(docker ps -a -q -f name=test-provenance-db)" ]; then
12+
echo "Stopping and removing test-provenance-db container..."
13+
docker stop test-provenance-db
14+
docker rm test-provenance-db
15+
fi
916

10-
# Stop and remove provenance database container
11-
docker stop test-provenance-db || true
12-
docker rm test-provenance-db || true
17+
if [ "$(docker ps -a -q -f name=test-redis)" ]; then
18+
echo "Stopping and removing test-redis container..."
19+
docker stop test-redis
20+
docker rm test-redis
21+
fi
1322

14-
echo "Test databases stopped and containers removed."
23+
echo "Test databases stopped and removed."

tests/test_cache.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
{
2-
"last_initialization": "2025-02-28T19:17:31.729743",
2+
"last_initialization": "2025-03-03T16:15:23.269670",
33
"version": "1.0"
44
}

tests/test_config.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ class TestConfig(object):
4747
DATASET_DB_URL = "http://localhost:9999/sparql"
4848
PROVENANCE_DB_URL = "http://localhost:9998/sparql"
4949

50+
# Redis configuration for testing
51+
REDIS_URL = "redis://localhost:6380/1" # Use database 1 for testing on port 6380
52+
5053
# Database store types
5154
DATASET_IS_QUADSTORE = True
5255
PROVENANCE_IS_QUADSTORE = True

0 commit comments

Comments
 (0)