This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
import pytest
|
||||
import logging
|
||||
|
||||
import pytest
|
||||
import sqlparse
|
||||
from sqlalchemy import create_engine, event
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
@@ -14,12 +16,36 @@ def pytest_addoption(parser):
|
||||
)
|
||||
|
||||
|
||||
class SqlParseFormatter(logging.Formatter):
|
||||
def format(self, record):
|
||||
recordMessage = record.getMessage()
|
||||
if not recordMessage.startswith("[") and any(
|
||||
recordMessage.startswith(keyword)
|
||||
for keyword in [
|
||||
"SELECT",
|
||||
"INSERT",
|
||||
"UPDATE",
|
||||
"DELETE",
|
||||
"WITH",
|
||||
]
|
||||
):
|
||||
formatted_sql = sqlparse.format(recordMessage, reindent=True, keyword_case="upper")
|
||||
record.msg = "\n" + formatted_sql
|
||||
|
||||
return super().format(record)
|
||||
|
||||
|
||||
@pytest.fixture(scope="function")
|
||||
def sql_session(request):
|
||||
"""Create a new SQLAlchemy session for testing."""
|
||||
|
||||
echo = request.config.getoption("--echo")
|
||||
logging.basicConfig()
|
||||
logger = logging.getLogger("sqlalchemy.engine")
|
||||
handler = logging.StreamHandler()
|
||||
handler.setFormatter(SqlParseFormatter())
|
||||
logger.addHandler(handler)
|
||||
|
||||
echo = request.config.getoption("--echo")
|
||||
engine = create_engine(
|
||||
"sqlite:///:memory:",
|
||||
echo=echo,
|
||||
|
||||
Reference in New Issue
Block a user