63 lines
1.6 KiB
Python
63 lines
1.6 KiB
Python
from datetime import UTC, datetime
|
|
from uuid import uuid4
|
|
|
|
import pytest
|
|
from sqlalchemy import text
|
|
from sqlalchemy.exc import IntegrityError
|
|
|
|
from monlet_server.models import Incident
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_event_id_dedup_conflict_do_nothing(engine):
|
|
"""Global idempotency is enforced via event_ingest_dedup, not the partitioned events table."""
|
|
eid = str(uuid4())
|
|
async with engine.begin() as conn:
|
|
await conn.execute(
|
|
text("INSERT INTO event_ingest_dedup (event_id) VALUES (:e)"),
|
|
{"e": eid},
|
|
)
|
|
r = await conn.execute(
|
|
text(
|
|
"INSERT INTO event_ingest_dedup (event_id) VALUES (:e) "
|
|
"ON CONFLICT (event_id) DO NOTHING RETURNING event_id"
|
|
),
|
|
{"e": eid},
|
|
)
|
|
assert r.scalar_one_or_none() is None
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_partial_unique_open_incident(session):
|
|
key = "k1"
|
|
now = datetime.now(UTC)
|
|
eid = uuid4()
|
|
session.add(
|
|
Incident(
|
|
id=uuid4(),
|
|
incident_key=key,
|
|
agent_id="a",
|
|
check_id="c",
|
|
state="open",
|
|
severity="warning",
|
|
opened_at=now,
|
|
last_event_id=eid,
|
|
)
|
|
)
|
|
await session.commit()
|
|
session.add(
|
|
Incident(
|
|
id=uuid4(),
|
|
incident_key=key,
|
|
agent_id="a",
|
|
check_id="c",
|
|
state="open",
|
|
severity="critical",
|
|
opened_at=now,
|
|
last_event_id=eid,
|
|
)
|
|
)
|
|
with pytest.raises(IntegrityError):
|
|
await session.commit()
|
|
await session.rollback()
|