-
Notifications
You must be signed in to change notification settings - Fork 10
Use Isolated Test Database for Reliable and Consistent Test Runs #230
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 4 commits
51a5866
de644e2
60f92c0
1d33e16
66d8359
995e653
29b1c6e
54079dc
8554c58
c8d0abf
b34b408
083e53e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,38 +2,39 @@ | |
|
|
||
| import pytest | ||
| from fastapi.testclient import TestClient | ||
| from sqlmodel import Session, delete | ||
| from sqlmodel import Session, SQLModel, create_engine | ||
|
|
||
| from app.core.config import settings | ||
| from app.core.db import engine, init_db | ||
| from app.main import app | ||
| from app.models import ( | ||
| APIKey, | ||
| Organization, | ||
| Project, | ||
| ProjectUser, | ||
| User, | ||
| OpenAI_Thread, | ||
| Credential, | ||
| ) | ||
| from app.api.deps import get_db | ||
| from app.tests.utils.user import authentication_token_from_email | ||
| from app.tests.utils.utils import get_superuser_token_headers | ||
|
|
||
|
|
||
| test_engine = create_engine(str(settings.SQLALCHEMY_TEST_DATABASE_URI)) | ||
|
|
||
|
|
||
| @pytest.fixture(scope="session", autouse=True) | ||
| def db() -> Generator[Session, None, None]: | ||
| with Session(engine) as session: | ||
| with Session(test_engine) as session: | ||
| # Drop all tables and recreate them | ||
| SQLModel.metadata.drop_all(test_engine) | ||
|
||
| SQLModel.metadata.create_all(test_engine) | ||
|
|
||
| init_db(session) | ||
| yield session | ||
| # Delete data in reverse dependency order | ||
| session.execute(delete(ProjectUser)) # Many-to-many relationship | ||
| session.execute(delete(Project)) | ||
| session.execute(delete(Credential)) | ||
| session.execute(delete(Organization)) | ||
| session.execute(delete(APIKey)) | ||
| session.execute(delete(User)) | ||
| session.execute(delete(OpenAI_Thread)) | ||
| session.commit() | ||
|
|
||
|
|
||
| # Override the get_db dependency to use test session | ||
| @pytest.fixture(scope="session", autouse=True) | ||
| def override_get_db(db: Session): | ||
| def _get_test_db(): | ||
| yield db | ||
|
|
||
| app.dependency_overrides[get_db] = _get_test_db | ||
| yield | ||
| app.dependency_overrides.clear() | ||
|
|
||
|
|
||
| @pytest.fixture(scope="module") | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17,6 +17,20 @@ services: | |
| - POSTGRES_PASSWORD=${POSTGRES_PASSWORD?Variable not set} | ||
| - POSTGRES_USER=${POSTGRES_USER?Variable not set} | ||
| - POSTGRES_DB=${POSTGRES_DB?Variable not set} | ||
| - POSTGRES_DB_TEST=${POSTGRES_DB_TEST?Variable not set} | ||
| # Wait for main DB to be ready, then create test DB if it doesn't exist | ||
| command: > | ||
|
||
| bash -c " | ||
| docker-entrypoint.sh postgres & | ||
| pid=$$! | ||
| sleep 5 | ||
| until psql -U $${POSTGRES_USER} -d $${POSTGRES_DB} -c 'SELECT 1'; do | ||
| echo 'Waiting for PostgreSQL...' | ||
| sleep 1 | ||
| done | ||
| psql -U $${POSTGRES_USER} -d $${POSTGRES_DB} -c 'CREATE DATABASE \"${POSTGRES_DB_TEST}\";' || true | ||
| wait $$pid | ||
| " | ||
|
|
||
| adminer: | ||
| image: adminer | ||
|
|
@@ -69,6 +83,7 @@ services: | |
| - POSTGRES_SERVER=db | ||
| - POSTGRES_PORT=${POSTGRES_PORT} | ||
| - POSTGRES_DB=${POSTGRES_DB} | ||
| - POSTGRES_DB_TEST=${POSTGRES_DB_TEST} | ||
| - POSTGRES_USER=${POSTGRES_USER?Variable not set} | ||
| - POSTGRES_PASSWORD=${POSTGRES_PASSWORD?Variable not set} | ||
| - SENTRY_DSN=${SENTRY_DSN} | ||
|
|
@@ -107,6 +122,7 @@ services: | |
| - POSTGRES_SERVER=db | ||
| - POSTGRES_PORT=${POSTGRES_PORT} | ||
| - POSTGRES_DB=${POSTGRES_DB} | ||
| - POSTGRES_DB_TEST=${POSTGRES_DB_TEST} | ||
| - POSTGRES_USER=${POSTGRES_USER?Variable not set} | ||
| - POSTGRES_PASSWORD=${POSTGRES_PASSWORD?Variable not set} | ||
| - SENTRY_DSN=${SENTRY_DSN} | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@avirajsingh7
Is this intentional?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@kurund was part of my previous commit where I was modifying this file.
Either way it is fine
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Might be good to revert then