71 lines
2.0 KiB
Python
71 lines
2.0 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_conflict_do_nothing(engine):
|
|
eid = str(uuid4())
|
|
async with engine.begin() as conn:
|
|
await conn.execute(
|
|
text(
|
|
"INSERT INTO agents (agent_id, hostname, features, status, last_seen_at) "
|
|
"VALUES ('a','h',jsonb_build_object('push', true, 'metrics', false),'alive', now())"
|
|
)
|
|
)
|
|
await conn.execute(
|
|
text(
|
|
"INSERT INTO events (event_id, agent_id, check_id, observed_at, status, exit_code, duration_ms, incident_key)"
|
|
" VALUES (:e,'a','c', now(),'ok',0,1,'a:c')"
|
|
),
|
|
{"e": eid},
|
|
)
|
|
r = await conn.execute(
|
|
text(
|
|
"INSERT INTO events (event_id, agent_id, check_id, observed_at, status, exit_code, duration_ms, incident_key)"
|
|
" VALUES (:e,'a','c', now(),'ok',0,1,'a:c') 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()
|