79 lines
3.2 KiB
Python
79 lines
3.2 KiB
Python
from datetime import UTC, datetime
|
|
from urllib.parse import urlsplit
|
|
from uuid import uuid4
|
|
|
|
import pytest
|
|
from sqlalchemy import delete, select
|
|
|
|
from ai_platform.infrastructure.config.settings import Settings
|
|
from ai_platform.infrastructure.persistence.database import make_session_factory
|
|
from ai_platform.infrastructure.persistence.models import AIProfile, AIProfileAuditEvent
|
|
from ai_platform.infrastructure.persistence.scoring import SqlAlchemyScoringRepository
|
|
from ai_platform.lead_scoring.application.admin import AdminScoringUseCases
|
|
from ai_platform.lead_scoring.application.repository import NewAIProfile
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_profile_provider_and_activation_audit_commit_together():
|
|
settings = Settings.load()
|
|
parsed = urlsplit(settings.database_url)
|
|
if parsed.path != "/ai_platform_test" or parsed.hostname not in {"localhost", "127.0.0.1"}:
|
|
pytest.skip("requires isolated local test database")
|
|
factory = make_session_factory(settings.database_url)
|
|
version = f"provider-audit-{uuid4().hex}"
|
|
actor = "test-admin@example.invalid"
|
|
occurred_at = datetime(2026, 9, 28, tzinfo=UTC)
|
|
original_active: set[str] = set()
|
|
try:
|
|
async with factory() as db:
|
|
profiles = (await db.scalars(select(AIProfile))).all()
|
|
original_active = {row.version for row in profiles if row.active}
|
|
cases = AdminScoringUseCases(
|
|
SqlAlchemyScoringRepository(db),
|
|
db.commit,
|
|
db.rollback,
|
|
frozenset({"openai_compatible"}),
|
|
lambda: occurred_at,
|
|
)
|
|
created = await cases.activate_profile(
|
|
NewAIProfile(
|
|
version,
|
|
"gemini-3.5-flash-lite",
|
|
"prompt",
|
|
30,
|
|
{},
|
|
"openai_compatible",
|
|
"http://127.0.0.1:8317",
|
|
),
|
|
actor,
|
|
)
|
|
assert created.provider == "openai_compatible"
|
|
async with factory() as db:
|
|
profile = await db.get(AIProfile, version)
|
|
assert (
|
|
profile is not None
|
|
and profile.active
|
|
and profile.provider == "openai_compatible"
|
|
and profile.base_url == "http://127.0.0.1:8317"
|
|
)
|
|
event = await db.scalar(
|
|
select(AIProfileAuditEvent).where(AIProfileAuditEvent.version == version)
|
|
)
|
|
assert event is not None
|
|
assert (event.actor, event.provider, event.occurred_at) == (
|
|
actor,
|
|
"openai_compatible",
|
|
occurred_at,
|
|
)
|
|
finally:
|
|
async with factory() as db:
|
|
await db.execute(
|
|
delete(AIProfileAuditEvent).where(AIProfileAuditEvent.version == version)
|
|
)
|
|
await db.execute(delete(AIProfile).where(AIProfile.version == version))
|
|
profiles = (await db.scalars(select(AIProfile))).all()
|
|
for row in profiles:
|
|
row.active = row.version in original_active
|
|
await db.commit()
|
|
await factory.kw["bind"].dispose()
|