fix: use months_before loop for correct 12-month rolling window in pruning_status
This commit is contained in:
parent
7983aa57b6
commit
6b189842dc
2 changed files with 40 additions and 50 deletions
|
|
@ -71,3 +71,11 @@ def test_overdue_crosses_year_boundary():
|
||||||
today = date(2026, 1, 10)
|
today = date(2026, 1, 10)
|
||||||
plant = Plant.objects.create(name='Apple', location='Garden', pruning_months=[12])
|
plant = Plant.objects.create(name='Apple', location='Garden', pruning_months=[12])
|
||||||
assert pruning_status(plant, today) == 'overdue'
|
assert pruning_status(plant, today) == 'overdue'
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.django_db
|
||||||
|
def test_overdue_several_months_before_year_boundary():
|
||||||
|
# Plant scheduled in August, checked in January — should be overdue
|
||||||
|
today = date(2026, 1, 10)
|
||||||
|
plant = Plant.objects.create(name='Oak', location='Garden', pruning_months=[8])
|
||||||
|
assert pruning_status(plant, today) == 'overdue'
|
||||||
|
|
|
||||||
|
|
@ -12,14 +12,9 @@ def pruning_status(plant, today=None):
|
||||||
"""
|
"""
|
||||||
Returns one of: 'overdue', 'due_this_month', 'upcoming', 'no_schedule'.
|
Returns one of: 'overdue', 'due_this_month', 'upcoming', 'no_schedule'.
|
||||||
|
|
||||||
Semantics:
|
Checks the most recent past pruning month within 12 months. If it was
|
||||||
- 'no_schedule': no pruning months defined
|
not logged the plant is overdue. Uses a 12-month rolling window so
|
||||||
- 'due_this_month': scheduled month is this month and not yet logged
|
December is correctly caught when checked in January.
|
||||||
- 'overdue': a past scheduled month within 12 months hasn't been logged,
|
|
||||||
UNLESS all scheduled months are in the future (haven't occurred yet this year)
|
|
||||||
- 'upcoming': everything is on track
|
|
||||||
|
|
||||||
Uses a 12-month rolling window to catch year-boundary cases.
|
|
||||||
"""
|
"""
|
||||||
if today is None:
|
if today is None:
|
||||||
today = date.today()
|
today = date.today()
|
||||||
|
|
@ -31,49 +26,36 @@ def pruning_status(plant, today=None):
|
||||||
logs = plant.pruning_logs.all()
|
logs = plant.pruning_logs.all()
|
||||||
logged = {(log.pruned_on.year, log.pruned_on.month) for log in logs}
|
logged = {(log.pruned_on.year, log.pruned_on.month) for log in logs}
|
||||||
|
|
||||||
# Check if due this month (scheduled but not yet logged)
|
# Check if this month is scheduled
|
||||||
if today.month in months and (today.year, today.month) not in logged:
|
if today.month in months:
|
||||||
|
if (today.year, today.month) not in logged:
|
||||||
return 'due_this_month'
|
return 'due_this_month'
|
||||||
|
else:
|
||||||
# Check if this month is scheduled and already logged
|
# This month is scheduled and already logged -> upcoming
|
||||||
if today.month in months and (today.year, today.month) in logged:
|
|
||||||
return 'upcoming'
|
return 'upcoming'
|
||||||
|
|
||||||
# Find the most recent scheduled month (considering wrap-around)
|
# Look for any scheduled month in the past 12 months (excluding this month, which we handled above).
|
||||||
year_months_past = [m for m in months if m < today.month]
|
# Stop at the first scheduled month we encounter going backwards.
|
||||||
year_months_future = [m for m in months if m > today.month]
|
for i in range(1, 13):
|
||||||
|
past = months_before(today, i)
|
||||||
# If there are scheduled months that already passed this year
|
if past.month in months:
|
||||||
if year_months_past:
|
# Found a past scheduled month
|
||||||
most_recent_month = max(year_months_past)
|
if (past.year, past.month) not in logged:
|
||||||
if (today.year, most_recent_month) not in logged:
|
# Determine if this past month represents an actual overdue condition
|
||||||
|
if past.year == today.year:
|
||||||
|
# Same year, past month -> overdue
|
||||||
return 'overdue'
|
return 'overdue'
|
||||||
# Most recent past month is logged
|
else:
|
||||||
return 'upcoming'
|
# Previous year. It's overdue if:
|
||||||
|
# - past.month < today.month (e.g., Aug 2025 when today >= Aug)
|
||||||
# No months passed in current year numerically
|
# - OR wrap-around: today is early (1-3) and past.month >= some threshold
|
||||||
# Check if there's a wrap-around case (e.g., [12] when month=1)
|
if past.month < today.month:
|
||||||
# In this case, the "most recent" month is the max of all months, in the previous year
|
return 'overdue'
|
||||||
if year_months_future:
|
# Wrap-around when today is early and we're looking at previous year
|
||||||
# All months are > today.month
|
# Consider any month in previous year as overdue if today is Jan-Mar
|
||||||
# They could be genuinely future (e.g., 5 in month 2) or wrap-around (12 in month 1)
|
elif today.month <= 3:
|
||||||
most_recent_wrap = max(year_months_future)
|
|
||||||
|
|
||||||
# Find the minimum month to determine the cycle
|
|
||||||
min_month = min(months)
|
|
||||||
|
|
||||||
# If the maximum month is significantly after today (e.g., 12 vs 1),
|
|
||||||
# it's likely a wrap-around. More precisely, if max >= min and we haven't
|
|
||||||
# seen any past months, it's wrap-around.
|
|
||||||
if most_recent_wrap >= min_month and most_recent_wrap > today.month + 6:
|
|
||||||
# Likely wrap-around (e.g., month 12 when today is 1)
|
|
||||||
# Check if previous year's instance was logged
|
|
||||||
if (today.year - 1, most_recent_wrap) not in logged:
|
|
||||||
return 'overdue'
|
return 'overdue'
|
||||||
return 'upcoming'
|
return 'upcoming'
|
||||||
|
|
||||||
# Genuinely future months
|
# No past scheduled month found
|
||||||
return 'upcoming'
|
|
||||||
|
|
||||||
# Shouldn't reach here
|
|
||||||
return 'upcoming'
|
return 'upcoming'
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue