mirror of
https://github.com/idrainformatica/PecFlow.git
synced 2026-06-16 12:45:42 +02:00
fase 4
This commit is contained in:
@@ -22,6 +22,36 @@ os.environ["DATABASE_URL_SYNC"] = "sqlite:///./test_integration.db"
|
||||
os.environ["APP_ENV"] = "development"
|
||||
os.environ["APP_DEBUG"] = "false"
|
||||
|
||||
# ─── Compatibilità SQLite: tipi PostgreSQL non supportati ───────────────────
|
||||
# SQLite non conosce INET, JSONB, ARRAY – mappiamo a tipi base compatibili.
|
||||
# Deve essere eseguito PRIMA di importare i modelli ORM.
|
||||
from sqlalchemy.dialects.sqlite import base as _sqlite_base
|
||||
|
||||
|
||||
def _visit_inet(self, type_, **kw): # noqa: ARG001
|
||||
return "VARCHAR(45)"
|
||||
|
||||
def _visit_jsonb(self, type_, **kw): # noqa: ARG001
|
||||
return "JSON"
|
||||
|
||||
def _visit_array(self, type_, **kw): # noqa: ARG001
|
||||
# SQLite non ha array nativi; usiamo TEXT (serializzato come JSON)
|
||||
return "TEXT"
|
||||
|
||||
def _visit_tsvector(self, type_, **kw): # noqa: ARG001
|
||||
return "TEXT"
|
||||
|
||||
def _visit_tsquery(self, type_, **kw): # noqa: ARG001
|
||||
return "TEXT"
|
||||
|
||||
|
||||
_sqlite_base.SQLiteTypeCompiler.visit_INET = _visit_inet
|
||||
_sqlite_base.SQLiteTypeCompiler.visit_JSONB = _visit_jsonb
|
||||
_sqlite_base.SQLiteTypeCompiler.visit_ARRAY = _visit_array
|
||||
_sqlite_base.SQLiteTypeCompiler.visit_TSVECTOR = _visit_tsvector
|
||||
_sqlite_base.SQLiteTypeCompiler.visit_TSQUERY = _visit_tsquery
|
||||
# ─────────────────────────────────────────────────────────────────────────────
|
||||
|
||||
from app.database import Base
|
||||
from app.main import app
|
||||
|
||||
@@ -34,6 +64,26 @@ test_engine = create_async_engine(
|
||||
connect_args={"check_same_thread": False},
|
||||
)
|
||||
|
||||
# ─── Compatibilità SQLite: ARRAY come JSON in DML ────────────────────────────
|
||||
# SQLite non può fare binding di Python list come parametri SQL;
|
||||
# li convertiamo in JSON string prima dell'esecuzione.
|
||||
import json as _json
|
||||
from sqlalchemy import event as _sa_event
|
||||
|
||||
|
||||
@_sa_event.listens_for(test_engine.sync_engine, "before_cursor_execute", retval=True)
|
||||
def _sqlite_list_to_json(conn, cursor, statement, parameters, context, executemany):
|
||||
"""Converte le liste Python in stringhe JSON per la compatibilità con SQLite."""
|
||||
def _convert(params):
|
||||
if isinstance(params, (list, tuple)):
|
||||
return type(params)(_json.dumps(v) if isinstance(v, list) else v for v in params)
|
||||
return params
|
||||
|
||||
if executemany:
|
||||
return statement, [_convert(p) for p in parameters]
|
||||
return statement, _convert(parameters)
|
||||
# ─────────────────────────────────────────────────────────────────────────────
|
||||
|
||||
TestAsyncSessionLocal = async_sessionmaker(
|
||||
bind=test_engine,
|
||||
class_=AsyncSession,
|
||||
@@ -91,12 +141,18 @@ async def client(db_session: AsyncSession) -> AsyncGenerator[AsyncClient, None]:
|
||||
|
||||
@pytest_asyncio.fixture
|
||||
async def demo_tenant(db_session: AsyncSession):
|
||||
"""Crea un tenant di test."""
|
||||
"""
|
||||
Crea un tenant di test con ID e slug univoci per ogni test.
|
||||
|
||||
Usiamo UUID randomici per evitare conflitti UNIQUE quando i test
|
||||
vengono eseguiti nello stesso processo e il commit non viene rollbackato.
|
||||
"""
|
||||
from app.models.tenant import Tenant
|
||||
|
||||
tenant_id = uuid.uuid4()
|
||||
tenant = Tenant(
|
||||
id=uuid.UUID("aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa"),
|
||||
slug="test-tenant",
|
||||
id=tenant_id,
|
||||
slug=f"test-{tenant_id.hex[:12]}",
|
||||
name="Test Tenant",
|
||||
plan="pro",
|
||||
max_mailboxes=10,
|
||||
|
||||
Reference in New Issue
Block a user