Skip to content

Commit fc5d6ad

Browse files
André Luiz Abdalla Silveiraclaude
andcommitted
fix: compute start_date relative to end_date
start_date now defaults to end_date - 7d instead of now() - 7d, so an API call with only a past end_timestamp no longer raises ValueError. A future-only start still correctly raises since end defaults to now(). Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent e2cbd01 commit fc5d6ad

2 files changed

Lines changed: 42 additions & 22 deletions

File tree

backend/kernelCI_app/helpers/dateRange.py

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,24 @@ def resolve_date_range(
1414
If both start_timestamp and end_timestamp are provided (as Unix second
1515
strings), they are converted to timezone-aware datetimes.
1616
17-
Otherwise, end_date is set to now() and start_date to
18-
now() - 7 days.
17+
end_date defaults to now(); start_date defaults to end_date - 7 days.
18+
start_date is computed relative to end_date (not now()) so that a
19+
past end_date without a start_date still produces a valid range.
1920
20-
Raises ValueError if the timestamp strings are not valid numbers.
21+
Raises ValueError if the timestamp strings are not valid numbers,
22+
or if start_date ends up after end_date.
2123
"""
22-
end_date = now()
23-
start_date = end_date - timedelta(days=7)
24-
25-
if start_timestamp is not None:
26-
start_date = datetime.fromtimestamp(float(start_timestamp), tz=dt_timezone.utc)
27-
28-
if end_timestamp is not None:
29-
end_date = datetime.fromtimestamp(float(end_timestamp), tz=dt_timezone.utc)
24+
end_date = (
25+
datetime.fromtimestamp(float(end_timestamp), tz=dt_timezone.utc)
26+
if end_timestamp is not None
27+
else now()
28+
)
29+
30+
start_date = (
31+
datetime.fromtimestamp(float(start_timestamp), tz=dt_timezone.utc)
32+
if start_timestamp is not None
33+
else end_date - timedelta(days=7)
34+
)
3035

3136
if start_date > end_date:
3237
raise ValueError("start_date must be before end_date")

backend/kernelCI_app/tests/unitTests/helpers/dateRange_test.py

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -48,20 +48,26 @@ def test_only_start_provided(self, _mock_now):
4848
assert start == NOV_14_2023
4949
assert end == FIXED_NOW
5050

51-
@patch("kernelCI_app.helpers.dateRange.now", return_value=FIXED_NOW)
52-
def test_only_end_provided(self, _mock_now):
53-
"""Only end → start defaults to now()-7d."""
54-
# end_ts must be >= FIXED_NOW - 7d, so we use FIXED_NOW itself
55-
fixed_now_ts = str(int(FIXED_NOW.timestamp()))
56-
51+
def test_only_end_provided(self):
52+
"""Only end → start defaults to end-7d (not now()-7d)."""
5753
start, end = resolve_date_range(
58-
start_timestamp=None, end_timestamp=fixed_now_ts
54+
start_timestamp=None, end_timestamp=NOV_19_2023_TS
5955
)
6056

61-
assert start == FIXED_NOW - timedelta(days=7)
62-
assert end == datetime.fromtimestamp(
63-
int(FIXED_NOW.timestamp()), tz=dt_timezone.utc
64-
)
57+
assert end == NOV_19_2023
58+
assert start == NOV_19_2023 - timedelta(days=7)
59+
60+
def test_only_end_far_in_the_past(self):
61+
"""End date 30+ days ago without start still produces a valid range."""
62+
# 2023-01-15 00:00:00 UTC — ~10 months before NOV_14
63+
jan_15_ts = "1673740800"
64+
jan_15 = datetime(2023, 1, 15, 0, 0, 0, tzinfo=dt_timezone.utc)
65+
66+
start, end = resolve_date_range(start_timestamp=None, end_timestamp=jan_15_ts)
67+
68+
assert end == jan_15
69+
assert start == jan_15 - timedelta(days=7)
70+
assert start < end
6571

6672
def test_start_after_end_raises(self):
6773
"""start > end → ValueError (Nov 19 as start, Nov 14 as end)."""
@@ -70,6 +76,15 @@ def test_start_after_end_raises(self):
7076
start_timestamp=NOV_19_2023_TS, end_timestamp=NOV_14_2023_TS
7177
)
7278

79+
@patch("kernelCI_app.helpers.dateRange.now", return_value=FIXED_NOW)
80+
def test_future_start_without_end_raises(self, _mock_now):
81+
"""Start in the future with no end (defaults to now()) → ValueError."""
82+
# 2027-01-01 00:00:00 UTC — ~9 months after FIXED_NOW
83+
future_ts = "1798761600"
84+
85+
with pytest.raises(ValueError, match="start_date must be before end_date"):
86+
resolve_date_range(start_timestamp=future_ts, end_timestamp=None)
87+
7388
def test_float_timestamps(self):
7489
"""Fractional seconds are accepted (Nov 14 + 0.5s, Nov 19 + 0.9s)."""
7590
start, end = resolve_date_range(

0 commit comments

Comments
 (0)