fixup! WIP
All checks were successful
Run tests / run-tests (push) Successful in 1m30s

This commit is contained in:
2025-12-10 13:32:54 +09:00
parent 7f4a980eef
commit fa7ad3a258
8 changed files with 216 additions and 114 deletions

View File

@@ -3,6 +3,7 @@ import logging
import pytest
import sqlparse
from sqlalchemy import create_engine, event
from sqlalchemy.exc import OperationalError
from sqlalchemy.orm import Session
from dibbler.models import Base
@@ -35,21 +36,27 @@ class SqlParseFormatter(logging.Formatter):
return super().format(record)
@pytest.fixture(scope="function")
def sql_session(request):
"""Create a new SQLAlchemy session for testing."""
def pytest_configure(config):
"""Setup pretty SQL logging if --echo is enabled."""
logging.basicConfig()
logger = logging.getLogger("sqlalchemy.engine")
# TODO: it would be nice not to duplicate these logs.
# logging.NullHandler() does not seem to work here
handler = logging.StreamHandler()
handler.setFormatter(SqlParseFormatter())
logger.addHandler(handler)
echo = request.config.getoption("--echo")
engine = create_engine(
"sqlite:///:memory:",
echo=echo,
)
echo = config.getoption("--echo")
if echo:
logger.setLevel(logging.INFO)
@pytest.fixture(scope="function")
def sql_session(request):
"""Create a new SQLAlchemy session for testing."""
engine = create_engine("sqlite:///:memory:")
@event.listens_for(engine, "connect")
def set_sqlite_pragma(dbapi_connection, _connection_record):
@@ -60,3 +67,17 @@ def sql_session(request):
Base.metadata.create_all(engine)
with Session(engine) as sql_session:
yield sql_session
# FIXME: Declaring this hook seems to have a side effect where the database does not
# get reset between tests.
# @pytest.hookimpl(trylast=True)
# def pytest_runtest_call(item: pytest.Item):
# """Hook to format SQL statements in OperationalError exceptions."""
# try:
# item.runtest()
# except OperationalError as e:
# if e.statement is not None:
# formatted_sql = sqlparse.format(e.statement, reindent=True, keyword_case="upper")
# e.statement = "\n" + formatted_sql + "\n"
# raise e

View File

@@ -3,7 +3,6 @@ from datetime import datetime
from pprint import pprint
import pytest
from sqlalchemy.orm import Session
from dibbler.models import Product, Transaction, User
@@ -104,8 +103,8 @@ def test_user_balance_with_transfers(sql_session: Session) -> None:
assert user2_balance == 50 - 30
def test_user_balance_complex_history(sql_session: Session) -> None:
raise NotImplementedError("This test is not implemented yet.")
@pytest.mark.skip(reason="Not yet implemented")
def test_user_balance_complex_history(sql_session: Session) -> None: ...
def test_user_balance_penalty(sql_session: Session) -> None: