Merge remote-tracking branch 'origin/dev' into feat/moteur-regles-recommandations
This commit is contained in:
@@ -89,3 +89,60 @@ async def test_list_all_returns_an_empty_list_when_there_is_nothing(
|
||||
alertes = await depot.list_all(site_id=identifiant_site())
|
||||
|
||||
assert list(alertes) == []
|
||||
|
||||
|
||||
def _alerte_a_inserer(*, site_id: str, source_alert_id: str) -> Alert:
|
||||
return Alert(
|
||||
source_alert_id=source_alert_id,
|
||||
site_id=site_id,
|
||||
source="enervision",
|
||||
timestamp=datetime(2026, 9, 16, tzinfo=UTC),
|
||||
type="threshold",
|
||||
severity="high",
|
||||
message="Dépassement du seuil configuré",
|
||||
value=812.5,
|
||||
threshold=720.0,
|
||||
metric="consumption_kw",
|
||||
prediction_id=None,
|
||||
raw_data={},
|
||||
)
|
||||
|
||||
|
||||
async def test_create_many_inserts_every_alert(session: AsyncSession) -> None:
|
||||
site = await creer_site(session)
|
||||
depot = AlertRepository(session)
|
||||
|
||||
creees = await depot.create_many(
|
||||
[
|
||||
_alerte_a_inserer(site_id=site.site_id, source_alert_id="threshold:a"),
|
||||
_alerte_a_inserer(site_id=site.site_id, source_alert_id="threshold:b"),
|
||||
]
|
||||
)
|
||||
identifiants = [a.alert_id for a in creees]
|
||||
await session.rollback()
|
||||
|
||||
assert len(identifiants) == 2
|
||||
assert all(identifiant is not None for identifiant in identifiants)
|
||||
|
||||
|
||||
async def test_create_many_skips_a_duplicate_source_alert_id(session: AsyncSession) -> None:
|
||||
site = await creer_site(session)
|
||||
depot = AlertRepository(session)
|
||||
await depot.create_many(
|
||||
[_alerte_a_inserer(site_id=site.site_id, source_alert_id="threshold:rejouee")]
|
||||
)
|
||||
|
||||
rejouees = await depot.create_many(
|
||||
[_alerte_a_inserer(site_id=site.site_id, source_alert_id="threshold:rejouee")]
|
||||
)
|
||||
await session.rollback()
|
||||
|
||||
assert rejouees == []
|
||||
|
||||
|
||||
async def test_create_many_does_nothing_for_an_empty_list(session: AsyncSession) -> None:
|
||||
depot = AlertRepository(session)
|
||||
|
||||
creees = await depot.create_many([])
|
||||
|
||||
assert creees == []
|
||||
|
||||
@@ -29,6 +29,85 @@ async def creer_prediction(
|
||||
return prediction
|
||||
|
||||
|
||||
async def test_list_since_excludes_predictions_before_the_cutoff(session: AsyncSession) -> None:
|
||||
site = await creer_site(session)
|
||||
depot = PredictionRepository(session)
|
||||
dedans = await creer_prediction(
|
||||
session, site_id=site.site_id, target_at=datetime(2026, 9, 16, tzinfo=UTC)
|
||||
)
|
||||
await creer_prediction(
|
||||
session, site_id=site.site_id, target_at=datetime(2026, 9, 1, tzinfo=UTC)
|
||||
)
|
||||
|
||||
resultats = await depot.list_since(
|
||||
since=datetime(2026, 9, 10, tzinfo=UTC), site_id=site.site_id
|
||||
)
|
||||
identifiants = [p.prediction_id for p in resultats]
|
||||
await session.rollback()
|
||||
|
||||
assert identifiants == [dedans.prediction_id]
|
||||
|
||||
|
||||
async def test_list_since_excludes_predictions_that_are_not_available(
|
||||
session: AsyncSession,
|
||||
) -> None:
|
||||
site = await creer_site(session)
|
||||
depot = PredictionRepository(session)
|
||||
await creer_prediction(
|
||||
session,
|
||||
site_id=site.site_id,
|
||||
target_at=datetime(2026, 9, 16, tzinfo=UTC),
|
||||
status="insufficient_data",
|
||||
predicted_value=None,
|
||||
failure_reason="pas assez d'historique",
|
||||
)
|
||||
|
||||
resultats = await depot.list_since(since=datetime(2026, 9, 1, tzinfo=UTC), site_id=site.site_id)
|
||||
await session.rollback()
|
||||
|
||||
assert list(resultats) == []
|
||||
|
||||
|
||||
async def test_list_since_breaks_a_target_at_tie_by_ascending_prediction_id(
|
||||
session: AsyncSession,
|
||||
) -> None:
|
||||
# `prediction` n'a pas d'unicité sur `(site_id, target_at)` : deux runs de scoring sans
|
||||
# nouvelle lecture entre-temps produisent deux lignes `available` à la même cible. Sans ce
|
||||
# départage, `_detect_anomaly` retiendrait une ligne au hasard plutôt que le run le plus
|
||||
# récent.
|
||||
site = await creer_site(session)
|
||||
depot = PredictionRepository(session)
|
||||
cible = datetime(2026, 9, 16, tzinfo=UTC)
|
||||
premier_run = await creer_prediction(
|
||||
session, site_id=site.site_id, target_at=cible, predicted_value=10.0
|
||||
)
|
||||
second_run = await creer_prediction(
|
||||
session, site_id=site.site_id, target_at=cible, predicted_value=20.0
|
||||
)
|
||||
|
||||
resultats = await depot.list_since(since=datetime(2026, 9, 1, tzinfo=UTC), site_id=site.site_id)
|
||||
identifiants = [p.prediction_id for p in resultats]
|
||||
await session.rollback()
|
||||
|
||||
assert identifiants == [premier_run.prediction_id, second_run.prediction_id]
|
||||
|
||||
|
||||
async def test_list_since_filters_by_site_id(session: AsyncSession) -> None:
|
||||
premier = await creer_site(session)
|
||||
second = await creer_site(session)
|
||||
depot = PredictionRepository(session)
|
||||
voulue = await creer_prediction(session, site_id=premier.site_id)
|
||||
await creer_prediction(session, site_id=second.site_id)
|
||||
|
||||
resultats = await depot.list_since(
|
||||
since=datetime(2026, 8, 1, tzinfo=UTC), site_id=premier.site_id
|
||||
)
|
||||
identifiants = [p.prediction_id for p in resultats]
|
||||
await session.rollback()
|
||||
|
||||
assert identifiants == [voulue.prediction_id]
|
||||
|
||||
|
||||
async def test_latest_by_site_keeps_only_the_most_recent_target(session: AsyncSession) -> None:
|
||||
site = await creer_site(session)
|
||||
depot = PredictionRepository(session)
|
||||
|
||||
@@ -155,6 +155,79 @@ async def test_latest_for_site_ignores_the_readings_of_the_other_sites(
|
||||
assert trouvee is None
|
||||
|
||||
|
||||
async def test_list_since_orders_by_site_then_by_time_ascending(session: AsyncSession) -> None:
|
||||
site = await creer_site(session)
|
||||
depot = ReadingRepository(session)
|
||||
plus_recente = await creer_lecture(
|
||||
session, site_id=site.site_id, timestamp=datetime(2026, 9, 16, tzinfo=UTC)
|
||||
)
|
||||
plus_ancienne = await creer_lecture(
|
||||
session, site_id=site.site_id, timestamp=datetime(2026, 9, 15, tzinfo=UTC)
|
||||
)
|
||||
|
||||
resultats = await depot.list_since(since=datetime(2026, 9, 1, tzinfo=UTC), site_id=site.site_id)
|
||||
identifiants = [r.reading_id for r in resultats]
|
||||
await session.rollback()
|
||||
|
||||
assert identifiants == [plus_ancienne.reading_id, plus_recente.reading_id]
|
||||
|
||||
|
||||
async def test_list_since_excludes_readings_before_the_cutoff(session: AsyncSession) -> None:
|
||||
site = await creer_site(session)
|
||||
depot = ReadingRepository(session)
|
||||
dedans = await creer_lecture(
|
||||
session, site_id=site.site_id, timestamp=datetime(2026, 9, 16, tzinfo=UTC)
|
||||
)
|
||||
await creer_lecture(session, site_id=site.site_id, timestamp=datetime(2026, 9, 1, tzinfo=UTC))
|
||||
|
||||
resultats = await depot.list_since(
|
||||
since=datetime(2026, 9, 10, tzinfo=UTC), site_id=site.site_id
|
||||
)
|
||||
identifiants = [r.reading_id for r in resultats]
|
||||
await session.rollback()
|
||||
|
||||
assert identifiants == [dedans.reading_id]
|
||||
|
||||
|
||||
async def test_list_since_breaks_a_timestamp_tie_by_ascending_reading_id(
|
||||
session: AsyncSession,
|
||||
) -> None:
|
||||
# `uq_reading_source` autorise deux lignes au même `site_id`+`timestamp` quand la `source`
|
||||
# diffère (même piège que `latest_for_site`). Sans ce départage, `_detect_spike` traiterait
|
||||
# cette paire comme une variation réelle selon un ordre non garanti par le plan d'exécution.
|
||||
site = await creer_site(session)
|
||||
depot = ReadingRepository(session)
|
||||
horodatage = datetime(2026, 9, 16, tzinfo=UTC)
|
||||
premiere = await creer_lecture(
|
||||
session, site_id=site.site_id, timestamp=horodatage, source="api_history", consumption_kw=10
|
||||
)
|
||||
seconde = await creer_lecture(
|
||||
session, site_id=site.site_id, timestamp=horodatage, source="api_current", consumption_kw=42
|
||||
)
|
||||
|
||||
resultats = await depot.list_since(since=datetime(2026, 9, 1, tzinfo=UTC), site_id=site.site_id)
|
||||
identifiants = [r.reading_id for r in resultats]
|
||||
await session.rollback()
|
||||
|
||||
assert identifiants == [premiere.reading_id, seconde.reading_id]
|
||||
|
||||
|
||||
async def test_list_since_filters_by_site_id(session: AsyncSession) -> None:
|
||||
premier = await creer_site(session)
|
||||
second = await creer_site(session)
|
||||
depot = ReadingRepository(session)
|
||||
voulue = await creer_lecture(session, site_id=premier.site_id)
|
||||
await creer_lecture(session, site_id=second.site_id)
|
||||
|
||||
resultats = await depot.list_since(
|
||||
since=datetime(2026, 8, 1, tzinfo=UTC), site_id=premier.site_id
|
||||
)
|
||||
identifiants = [r.reading_id for r in resultats]
|
||||
await session.rollback()
|
||||
|
||||
assert identifiants == [voulue.reading_id]
|
||||
|
||||
|
||||
async def test_list_history_orders_the_readings_by_timestamp_descending(
|
||||
session: AsyncSession,
|
||||
) -> None:
|
||||
|
||||
Reference in New Issue
Block a user