mirror of
https://github.com/evroon/bracket.git
synced 2026-07-31 18:26:23 -04:00
- Rewrite database.py with asyncpg pool-based DatabasePool class - Support named params (:param) → positional ($N) conversion - Transaction context manager using contextvars for connection reuse - Replace all SQLAlchemy query building with raw SQL in routes and sql modules - Update utils/db.py to use raw SQL strings instead of SQLAlchemy Select - Update utils/db_init.py to use local engine creation for DDL only - Update all test files to use string table names and raw SQL - Remove databases and sqlalchemy-stubs from dependencies - Keep sqlalchemy only for schema.py definitions and alembic migrations
85 lines
3.4 KiB
Python
85 lines
3.4 KiB
Python
import pytest
|
|
|
|
from bracket.database import database
|
|
from bracket.models.db.court import Court
|
|
from bracket.utils.db import fetch_one_parsed_certain
|
|
from bracket.utils.dummy_records import DUMMY_COURT1, DUMMY_MOCK_TIME, DUMMY_TEAM1
|
|
from bracket.utils.http import HTTPMethod
|
|
from tests.integration_tests.api.shared import SUCCESS_RESPONSE, send_tournament_request
|
|
from tests.integration_tests.models import AuthContext
|
|
from tests.integration_tests.sql import assert_row_count_and_clear, inserted_court, inserted_team
|
|
|
|
|
|
@pytest.mark.asyncio(loop_scope="session")
|
|
async def test_courts_endpoint(
|
|
startup_and_shutdown_uvicorn_server: None, auth_context: AuthContext
|
|
) -> None:
|
|
async with inserted_team(
|
|
DUMMY_TEAM1.model_copy(update={"tournament_id": auth_context.tournament.id})
|
|
):
|
|
async with inserted_court(
|
|
DUMMY_COURT1.model_copy(update={"tournament_id": auth_context.tournament.id})
|
|
) as court_inserted:
|
|
assert await send_tournament_request(HTTPMethod.GET, "courts", auth_context, {}) == {
|
|
"data": [
|
|
{
|
|
"created": DUMMY_MOCK_TIME.isoformat().replace("+00:00", "Z"),
|
|
"id": court_inserted.id,
|
|
"name": "Court 1",
|
|
"tournament_id": auth_context.tournament.id,
|
|
}
|
|
],
|
|
}
|
|
|
|
|
|
@pytest.mark.asyncio(loop_scope="session")
|
|
async def test_create_court(
|
|
startup_and_shutdown_uvicorn_server: None, auth_context: AuthContext
|
|
) -> None:
|
|
body = {"name": "Some new name", "active": True}
|
|
response = await send_tournament_request(HTTPMethod.POST, "courts", auth_context, json=body)
|
|
assert response["data"]["name"] == body["name"]
|
|
await assert_row_count_and_clear("courts", 1)
|
|
|
|
|
|
@pytest.mark.asyncio(loop_scope="session")
|
|
async def test_delete_court(
|
|
startup_and_shutdown_uvicorn_server: None, auth_context: AuthContext
|
|
) -> None:
|
|
async with inserted_team(
|
|
DUMMY_TEAM1.model_copy(update={"tournament_id": auth_context.tournament.id})
|
|
):
|
|
async with inserted_court(
|
|
DUMMY_COURT1.model_copy(update={"tournament_id": auth_context.tournament.id})
|
|
) as court_inserted:
|
|
assert (
|
|
await send_tournament_request(
|
|
HTTPMethod.DELETE, f"courts/{court_inserted.id}", auth_context
|
|
)
|
|
== SUCCESS_RESPONSE
|
|
)
|
|
await assert_row_count_and_clear("courts", 0)
|
|
|
|
|
|
@pytest.mark.asyncio(loop_scope="session")
|
|
async def test_update_court(
|
|
startup_and_shutdown_uvicorn_server: None, auth_context: AuthContext
|
|
) -> None:
|
|
body = {"name": "Some new name"}
|
|
async with inserted_team(
|
|
DUMMY_TEAM1.model_copy(update={"tournament_id": auth_context.tournament.id})
|
|
):
|
|
async with inserted_court(
|
|
DUMMY_COURT1.model_copy(update={"tournament_id": auth_context.tournament.id})
|
|
) as court_inserted:
|
|
response = await send_tournament_request(
|
|
HTTPMethod.PUT, f"courts/{court_inserted.id}", auth_context, json=body
|
|
)
|
|
updated_court = await fetch_one_parsed_certain(
|
|
database, Court, "SELECT * FROM courts WHERE id = :id", {"id": court_inserted.id}
|
|
)
|
|
assert updated_court.name == body["name"]
|
|
assert response["data"]["name"] == body["name"]
|
|
|
|
await assert_row_count_and_clear("courts", 1)
|