CoolFace
Apppublic

Steph680/ormuz

sourceHugging Faceupdated 2d agoView on Hugging Face
0likes
test_coherence_dates.py112 linesDownload Raw Back to tests
1# -*- coding: utf-8 -*-2"""Cohérence des dates et des compteurs en tête de tableau de bord.3 4Trois chiffres y coexistaient sans dire leur périmètre : la fraîcheur5PortWatch (publiée avec 5 à 8 jours de retard), le dernier jour AIS complet6*restreint aux marchands*, et la liste temps réel qui montre tous les types.7La page annonçait 13/09, 15/09 et 21/09 pour la même réalité.8"""9 10import asyncio11from datetime import timedelta12 13from app import api, db14from app.state import utc_now15 16 17def _jour(delta=0):18    return (utc_now().date() - timedelta(days=delta)).isoformat()19 20 21async def _transit(conn, mmsi, jour, direction="indetermine", ship_type=None):22    await conn.execute(23        "INSERT INTO transits (mmsi, date_utc, direction, lane, first_seen,"24        " last_seen, min_lon, max_lon, gap_inferred)"25        " VALUES (?, ?, ?, 'nord', ?, ?, 56.1, 56.9, 0)",26        (mmsi, jour, direction, jour + "T06:00:00", jour + "T13:55:00"))27    if ship_type is not None:28        await conn.execute(29            "INSERT OR REPLACE INTO vessels (mmsi, name, ship_type) VALUES (?, ?, ?)",30            (mmsi, f"NAVIRE {mmsi}", ship_type))31 32 33def test_statut_porte_la_fraicheur_ais(tmp_path, monkeypatch):34    """Le cartouche datait toute la page au dernier jour PortWatch."""35    monkeypatch.setenv("DB_PATH", str(tmp_path / "t.db"))36 37    async def scenario():38        await db.init_db()39        async with db.connect() as conn:40            await _transit(conn, 101, _jour(0))41            await conn.commit()42        return await api.status()43 44    assert asyncio.run(scenario())["ais_dernier_jour"] == _jour(0)45 46 47def test_statut_sans_transit_ne_ment_pas(tmp_path, monkeypatch):48    """Sans flux AIS, pas de seconde date inventée."""49    monkeypatch.setenv("DB_PATH", str(tmp_path / "t.db"))50 51    async def scenario():52        await db.init_db()53        return await api.status()54 55    assert asyncio.run(scenario())["ais_dernier_jour"] is None56 57 58def test_jour_en_cours_expose_a_part(tmp_path, monkeypatch):59    """Le jour en cours reste hors de la moyenne — il est partiel — mais il60    doit être visible : l'exclure faisait remonter un « dernier jour » vieux61    de six jours alors que du trafic marchand passait le matin même."""62    monkeypatch.setenv("DB_PATH", str(tmp_path / "t.db"))63 64    async def scenario():65        await db.init_db()66        async with db.connect() as conn:67            for jour, n in ((_jour(6), 8), (_jour(0), 3)):68                await conn.execute(69                    "INSERT INTO daily_stats (date_utc, direction, merchant_count,"70                    " cargo_count, tanker_count) VALUES (?, 'entrant', ?, ?, 0)",71                    (jour, n, n))72            await conn.commit()73        return await api.stats_today()74 75    out = asyncio.run(scenario())76    assert out["ais_7j"]["aujourdhui"] == 377    assert out["ais_7j"]["dernier_jour"]["date"] == _jour(6)   # dernier COMPLET78    assert out["ais_7j"]["moyenne"] == 8.0                      # sans le jour partiel79 80 81def test_flux_tous_types_reconcilie_la_liste(tmp_path, monkeypatch):82    """daily_stats ne compte que les marchands (70-89), pour rester comparable83    à PortWatch. Un remorqueur transitait donc sans apparaître nulle part,84    alors que la liste « derniers passages » l'affichait."""85    monkeypatch.setenv("DB_PATH", str(tmp_path / "t.db"))86 87    async def scenario():88        await db.init_db()89        async with db.connect() as conn:90            await _transit(conn, 101, _jour(0), ship_type=70)   # cargo91            await _transit(conn, 102, _jour(0), ship_type=52)   # remorqueur92            await _transit(conn, 103, _jour(0), ship_type=None)  # type inconnu93            await conn.commit()94        return await api.stats_today()95 96    out = asyncio.run(scenario())97    assert out["ais_flux"]["transits_aujourdhui"] == 398    assert out["ais_flux"]["dernier_transit"]["date"] == _jour(0)99 100 101def test_flux_vide_ne_fabrique_pas_de_date(tmp_path, monkeypatch):102    monkeypatch.setenv("DB_PATH", str(tmp_path / "t.db"))103 104    async def scenario():105        await db.init_db()106        return await api.stats_today()107 108    out = asyncio.run(scenario())109    assert out["ais_flux"]["dernier_transit"] is None110    assert out["ais_flux"]["transits_aujourdhui"] == 0111    assert out["ais_7j"]["aujourdhui"] == 0112