"""Tests for `weekly_time_in_zone`.""" from __future__ import annotations import pandas as pd from openrun.model import weekly_time_in_zone def _seed(conn, aid: int, when: str, *, atype: str = "running", z1=0.0, z2=0.0, z3=0.0, z4=0.0, z5=0.0) -> None: conn.execute( """INSERT INTO activities (activity_id, start_time_local, activity_type, raw, fetched_at) VALUES (?, ?, ?, '{}', 'now')""", (aid, when, atype), ) conn.execute( """INSERT INTO activity_time_in_zone (activity_id, z1_s, z2_s, z3_s, z4_s, z5_s, total_s, source, computed_at) VALUES (?, ?, ?, ?, ?, ?, ?, 'fit', 'now')""", (aid, z1, z2, z3, z4, z5, z1 + z2 + z3 + z4 + z5), ) conn.commit() def test_weekly_tiz_sums_within_week(tmp_conn) -> None: """Three running activities in the same ISO week sum into one row.""" # 2026-05-04 is Mon, 2026-05-06 Wed, 2026-05-10 Sun — all the same Mon-anchored week. _seed(tmp_conn, 1, "2026-05-04 06:00:00", z1=600, z2=1200) _seed(tmp_conn, 2, "2026-05-06 06:00:00", z2=300, z3=900) _seed(tmp_conn, 3, "2026-05-10 06:00:00", z4=400) out = weekly_time_in_zone(tmp_conn) assert len(out) == 1 row = out.iloc[0] assert row["z1_s"] == 600 assert row["z2_s"] == 1500 assert row["z3_s"] == 900 assert row["z4_s"] == 400 assert row["z5_s"] == 0 assert row["total_s"] == 600 + 1200 + 300 + 900 + 400 assert row["n_activities"] == 3 def test_weekly_tiz_splits_across_weeks(tmp_conn) -> None: _seed(tmp_conn, 1, "2026-05-04 06:00:00", z2=1000) # week of 2026-05-04 _seed(tmp_conn, 2, "2026-05-12 06:00:00", z3=2000) # week of 2026-05-11 out = weekly_time_in_zone(tmp_conn) assert len(out) == 2 assert out.index.min() == pd.Timestamp("2026-05-04") assert out.index.max() == pd.Timestamp("2026-05-11") def test_weekly_tiz_filters_by_activity_type(tmp_conn) -> None: _seed(tmp_conn, 1, "2026-05-04 06:00:00", atype="running", z2=1000) _seed(tmp_conn, 2, "2026-05-04 18:00:00", atype="cycling", z2=2000) out = weekly_time_in_zone(tmp_conn) assert out.iloc[0]["z2_s"] == 1000 # cycling excluded by default def test_weekly_tiz_date_window(tmp_conn) -> None: _seed(tmp_conn, 1, "2026-04-01 06:00:00", z2=100) _seed(tmp_conn, 2, "2026-05-04 06:00:00", z2=500) _seed(tmp_conn, 3, "2026-06-01 06:00:00", z2=900) out = weekly_time_in_zone(tmp_conn, start="2026-05-01", end="2026-05-31") assert len(out) == 1 assert out.iloc[0]["z2_s"] == 500 def test_weekly_tiz_empty_returns_empty_frame_with_columns(tmp_conn) -> None: out = weekly_time_in_zone(tmp_conn) assert out.empty assert {"z1_s", "z2_s", "z3_s", "z4_s", "z5_s", "total_s", "n_activities"} <= set(out.columns)