Sfoglia il codice sorgente

Anchor a plug-energy test to local midnight, not the wall clock (issue #2938)

test_nothing_derivable_before_the_first_midnight failed for 31 minutes of
every day and passed for the other 23.5 hours -- the shape that reads as
ordinary flakiness and gets re-run rather than fixed. @ojimpo hit it running
the full suite at 22:10 UTC, stashed his branch to confirm it reproduced on
clean dev, and measured the window minute by minute instead of guessing.

The test asserts that nothing can be derived when the only snapshot was
taken after this local midnight, and it placed that snapshot at a raw
wall-clock offset -- now minus thirty minutes. Its comment, "taken this
morning, after midnight", is the premise, and it is only true away from
the boundary. For the first half hour of each local day, now minus thirty
minutes lands before local midnight, where it is a perfectly good baseline:
_counter_at finds it and today comes back 1.5 rather than None.

The window is local 00:00 to 00:30, which is 22:00 to 22:30 UTC under CEST
and 23:00 to 23:30 under CET -- it moves with DST, since the module pins
Europe/Berlin in an autouse fixture and an outer TZ makes no difference.

Nothing is wrong with the production code. A snapshot from before local
midnight genuinely is a valid baseline for today, and derive_today_yesterday
is right to treat it as one. Only the test's premise breaks at the boundary.

The snapshot is now anchored to local_day_start(now) plus thirty minutes,
which is the idiom the other nine snapshot writes in this file already use
and the reason none of them can drift. Replayed across 5760 minutes covering
four days, including both DST switch days: the old expression fails 31
minutes per day, the new one fails none.
maziggy 1 settimana fa
parent
commit
ed84f0f74c

File diff suppressed because it is too large
+ 0 - 0
CHANGELOG.md


+ 4 - 1
backend/tests/unit/services/test_plug_energy_history.py

@@ -83,7 +83,10 @@ async def test_nothing_derivable_before_the_first_midnight(db_session):
     plug = await _plug(db_session)
     plug = await _plug(db_session)
     now = datetime.now(timezone.utc)
     now = datetime.now(timezone.utc)
     # Snapshot taken this morning, after midnight — no baseline for the day.
     # Snapshot taken this morning, after midnight — no baseline for the day.
-    await _snapshot(db_session, plug.id, now - timedelta(minutes=30), 102.0)
+    # Anchored to the boundary, not to the wall clock: `now - 30 minutes` is
+    # before local midnight for the first half hour of every local day, which
+    # made this the only test in the file that could drift (#2938).
+    await _snapshot(db_session, plug.id, local_day_start(now) + timedelta(minutes=30), 102.0)
 
 
     today, yesterday = await derive_today_yesterday(db_session, plug.id, live_total_kwh=103.5)
     today, yesterday = await derive_today_yesterday(db_session, plug.id, live_total_kwh=103.5)
 
 

Some files were not shown because too many files changed in this diff