Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,6 @@ www/REFACTOR.md
www/reload-frontend
server/test.sqlite
CLAUDE.local.md
./www/.pnpm-store
www/.env.development
www/.env.production
6 changes: 3 additions & 3 deletions CLAUDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,13 @@ uv run celery -A reflector.worker.app beat
**Testing:**
```bash
# Run all tests with coverage
uv run pytest
REDIS_HOST=localhost CELERY_BROKER_URL=redis://localhost:6379/1 CELERY_RESULT_BACKEND=redis://localhost:6379/1 uv run pytest

# Run specific test file
uv run pytest tests/test_transcripts.py
REDIS_HOST=localhost CELERY_BROKER_URL=redis://localhost:6379/1 CELERY_RESULT_BACKEND=redis://localhost:6379/1 uv run pytest tests/test_transcripts.py

# Run tests with verbose output
uv run pytest -v
REDIS_HOST=localhost CELERY_BROKER_URL=redis://localhost:6379/1 CELERY_RESULT_BACKEND=redis://localhost:6379/1 uv run pytest -v
Comment on lines +45 to +51
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, again this is specific to you because you change your env to have stuff running in a docker, then add stuff about how to run without your env again.

We could do the same way for REDIS_HOST to be populated with pytest-docker the same way as postgres.

```

**Process Audio Files:**
Expand Down
52 changes: 50 additions & 2 deletions server/tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os
import subprocess
from tempfile import NamedTemporaryFile
from unittest.mock import patch

Expand Down Expand Up @@ -27,16 +28,63 @@ def vcr_config():
"filter_headers": [("authorization", "DUMMY_API_KEY")],
}


@pytest.fixture(scope="session")
def docker_compose_file(pytestconfig):
return os.path.join(str(pytestconfig.rootdir), "tests", "docker-compose.test.yml")


@pytest.fixture(scope="session")
def docker_compose_project_name():
"""Use a consistent project name to avoid creating new networks each run."""
return "reflector_test"


@pytest.fixture(scope="session", autouse=True)
def cleanup_docker_resources():
"""Clean up Docker test resources before and after test session."""

def cleanup():
# Stop and remove any existing test containers
# This will also remove the reflector_test network if it exists
subprocess.run(
[
"docker",
"compose",
"-p",
"reflector_test",
"down",
"-v",
"--remove-orphans",
],
capture_output=True,
cwd=os.path.dirname(os.path.dirname(os.path.abspath(__file__))),
)
Comment on lines +49 to +61
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion: Add a timeout parameter to the subprocess call to prevent tests from hanging indefinitely if Docker operations take too long or get stuck. [general, importance: 7]

Suggested change
subprocess.run(
[
"docker",
"compose",
"-p",
"reflector_test",
"down",
"-v",
"--remove-orphans",
],
capture_output=True,
cwd=os.path.dirname(os.path.dirname(os.path.abspath(__file__))),
)
subprocess.run(
[
"docker",
"compose",
"-p",
"reflector_test",
"down",
"-v",
"--remove-orphans",
],
capture_output=True,
cwd=os.path.dirname(os.path.dirname(os.path.abspath(__file__))),
timeout=60, # Add timeout to prevent hanging
)

# Clean up any unused networks (includes orphaned pytest networks)
# This is safe - only removes networks with no attached containers
subprocess.run(["docker", "network", "prune", "-f"], capture_output=True)
Comment on lines +63 to +64
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion: Check the return code of the subprocess call to ensure the Docker command executed successfully. If the command fails, it could silently fail and cause test issues. [general, importance: 7]

Suggested change
# This is safe - only removes networks with no attached containers
subprocess.run(["docker", "network", "prune", "-f"], capture_output=True)
# Clean up any unused networks (includes orphaned pytest networks)
# This is safe - only removes networks with no attached containers
result = subprocess.run(["docker", "network", "prune", "-f"], capture_output=True)
if result.returncode != 0:
print(f"Warning: Docker network prune failed: {result.stderr.decode()}")

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, i don't want the project to prune my network, some network can be created without being attached to a container, does not mean you want to prune. Filter to pytest then


# Clean before tests
cleanup()

yield

# Clean after tests
cleanup()


@pytest.fixture(scope="session")
def postgres_service(docker_ip, docker_services):
"""Ensure that PostgreSQL service is up and responsive."""
port = docker_services.port_for("postgres_test", 5432)
try:
port = docker_services.port_for("postgres_test", 5432)
except Exception as e:
# If Docker services fail to start, clean up and retry
subprocess.run(["docker", "network", "prune", "-f"], capture_output=True)
subprocess.run(
["docker", "compose", "-p", "reflector_test", "down", "-v"],
capture_output=True,
)
raise pytest.skip(f"Docker services failed to start: {e}")
Comment on lines +82 to +87
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion: Add error handling for the cleanup commands in the exception handler. If these cleanup commands fail, they could mask the original error and make debugging more difficult. [general, importance: 8]

Suggested change
subprocess.run(["docker", "network", "prune", "-f"], capture_output=True)
subprocess.run(
["docker", "compose", "-p", "reflector_test", "down", "-v"],
capture_output=True,
)
raise pytest.skip(f"Docker services failed to start: {e}")
# If Docker services fail to start, clean up and retry
try:
subprocess.run(["docker", "network", "prune", "-f"], capture_output=True, check=False)
subprocess.run(
["docker", "compose", "-p", "reflector_test", "down", "-v"],
capture_output=True,
check=False,
)
except Exception as cleanup_error:
print(f"Warning: Cleanup after Docker failure also failed: {cleanup_error}")
raise pytest.skip(f"Docker services failed to start: {e}")


def is_responsive():
try:
Expand Down
Loading