Files
AISHub/tests/test_retention.py
T
2026-05-04 08:13:38 +03:00

55 lines
1.7 KiB
Python

import asyncio
import time
from pathlib import Path
import pytest
from ais_hub.config import Config, StorageCfg
from ais_hub.core.stats import Stats
from ais_hub.storage import db as storage_db
from ais_hub.storage.retention import cleanup_once
@pytest.mark.asyncio
async def test_cleanup_once_removes_old_rows(tmp_path: Path):
cfg = Config()
cfg.storage.path = str(tmp_path / "t.db")
cfg.storage.retention.ais_dynamic_days = 1
cfg.storage.retention.gps_fix_days = 1
cfg.storage.retention.raw_nmea_days = 1
cfg.storage.retention.radio_telemetry_days = 1
conn = await storage_db.connect(cfg.storage.path)
now = time.time()
old = now - 5 * 86400
fresh = now - 60
await conn.executemany(
"INSERT INTO ais_dynamic (mmsi, ts, lat, lon, sog, cog, heading, nav_status, rot, raw_msg_type) "
"VALUES (?,?,?,?,?,?,?,?,?,?)",
[
(1, old, 0, 0, 0, 0, 0, 0, 0, 1),
(1, fresh, 0, 0, 0, 0, 0, 0, 0, 1),
],
)
await conn.executemany(
"INSERT INTO gps_fix (ts, lat, lon, sog, cog, alt, fix_quality, sats, hdop) "
"VALUES (?,?,?,?,?,?,?,?,?)",
[(old, 0, 0, 0, 0, 0, 1, 5, 1.0), (fresh, 0, 0, 0, 0, 0, 1, 5, 1.0)],
)
await conn.commit()
stats = Stats()
await cleanup_once(conn, cfg.storage, stats)
async with conn.execute("SELECT COUNT(*) FROM ais_dynamic") as cur:
(n_dyn,) = await cur.fetchone()
async with conn.execute("SELECT COUNT(*) FROM gps_fix") as cur:
(n_gps,) = await cur.fetchone()
assert n_dyn == 1
assert n_gps == 1
assert stats.counters.get("retention_deleted_ais_dynamic", 0) >= 1
assert stats.counters.get("retention_deleted_gps_fix", 0) >= 1
await storage_db.close(conn)