-
Notifications
You must be signed in to change notification settings - Fork 0
SQLite -> Postgres Test Migration #144
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
Merged
Merged
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| from src.core.database import server_url | ||
| from sqlalchemy import create_engine, text | ||
|
|
||
| engine = create_engine(server_url(sync=True), isolation_level="AUTOCOMMIT") | ||
|
|
||
| with engine.connect() as connection: | ||
| # Check if test database already exists | ||
| result = connection.execute( | ||
| text("SELECT 1 FROM pg_database WHERE datname = 'ocsl_test'") | ||
| ) | ||
| if result.fetchone(): | ||
| print("Test database 'ocsl_test' already exists") | ||
| else: | ||
| print("Creating test database 'ocsl_test'...") | ||
| connection.execute(text("CREATE DATABASE ocsl_test")) | ||
| print("Test database successfully created") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,7 @@ | ||
| import os | ||
|
|
||
| from sqlalchemy import text | ||
|
|
||
| os.environ["GOOGLE_MAPS_API_KEY"] = "invalid_google_maps_api_key_for_tests" | ||
|
|
||
| from typing import Any, AsyncGenerator, Callable | ||
|
|
@@ -12,8 +14,9 @@ | |
| import src.modules # Ensure all modules are imported so their entities are registered # noqa: F401 | ||
| from httpx import ASGITransport, AsyncClient | ||
| from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker, create_async_engine | ||
| from sqlalchemy.ext.asyncio.engine import AsyncEngine | ||
| from src.core.authentication import StringRole | ||
| from src.core.database import EntityBase, get_session | ||
| from src.core.database import EntityBase, database_url, get_session | ||
| from src.main import app | ||
| from src.modules.account.account_service import AccountService | ||
| from src.modules.complaint.complaint_service import ComplaintService | ||
|
|
@@ -29,20 +32,45 @@ | |
| from test.modules.police.police_utils import PoliceTestUtils | ||
| from test.modules.student.student_utils import StudentTestUtils | ||
|
|
||
| DATABASE_URL = "sqlite+aiosqlite:///:memory:" | ||
| DATABASE_URL = database_url("ocsl_test") | ||
|
|
||
| # =================================== Database ====================================== | ||
|
|
||
|
|
||
| @pytest_asyncio.fixture(scope="function") | ||
| async def test_session(): | ||
| @pytest_asyncio.fixture(autouse=True, scope="session", loop_scope="session") | ||
| async def test_engine(): | ||
| """Create engine and tables once per test session.""" | ||
| engine = create_async_engine(DATABASE_URL, echo=False) | ||
| async with engine.begin() as conn: | ||
| await conn.run_sync(EntityBase.metadata.drop_all) | ||
| await conn.run_sync(EntityBase.metadata.create_all) | ||
| TestAsyncSessionLocal = async_sessionmaker(engine, expire_on_commit=False, class_=AsyncSession) | ||
| yield engine | ||
| async with engine.begin() as conn: | ||
| await conn.run_sync(EntityBase.metadata.drop_all) | ||
| await engine.dispose() | ||
|
|
||
|
|
||
| @pytest_asyncio.fixture(scope="function") | ||
| async def test_session(test_engine: AsyncEngine): | ||
| """Create a new session and truncate all tables after each test.""" | ||
| TestAsyncSessionLocal = async_sessionmaker( | ||
| bind=test_engine, | ||
| expire_on_commit=False, | ||
| class_=AsyncSession, | ||
| ) | ||
|
|
||
| async with TestAsyncSessionLocal() as session: | ||
| yield session | ||
| await engine.dispose() | ||
|
|
||
| # Clean up: truncate all tables and reset sequences | ||
| async with test_engine.begin() as conn: | ||
| tables = [table.name for table in EntityBase.metadata.sorted_tables] | ||
| if tables: | ||
| # Disable foreign key checks, truncate, and reset sequences | ||
| await conn.execute(text("SET session_replication_role = 'replica';")) | ||
| for table in tables: | ||
| await conn.execute(text(f"TRUNCATE TABLE {table} RESTART IDENTITY CASCADE;")) | ||
| await conn.execute(text("SET session_replication_role = 'origin';")) | ||
|
Comment on lines
+65
to
+73
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Truncate instead of drop all tables so that tests don't take a million years |
||
|
|
||
|
|
||
| # =================================== Clients ======================================= | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Necessary for async session fixture shenanigans