78 lines
2.7 KiB
Python
78 lines
2.7 KiB
Python
from datetime import datetime, timedelta
|
|
import sys
|
|
import os
|
|
|
|
# Dynamically add the project directory to PYTHONPATH
|
|
project_root = os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))
|
|
sys.path.insert(0, project_root)
|
|
|
|
from app.models import Reading
|
|
from app.routes.main import _compute_badges
|
|
from pytz import utc
|
|
|
|
|
|
def calculate_progress_badges_test_helper(readings, test_now=None):
|
|
total = len(readings)
|
|
timestamps = [(r.timestamp,) for r in sorted(readings, key=lambda x: x.timestamp, reverse=True)]
|
|
now_date = test_now.date() if test_now else datetime.utcnow().date()
|
|
return _compute_badges(total, timestamps, utc, now_local=now_date)
|
|
|
|
def test_no_readings():
|
|
readings = []
|
|
badges = calculate_progress_badges_test_helper(readings)
|
|
assert badges == []
|
|
|
|
|
|
def test_daily_streak():
|
|
now = datetime.utcnow()
|
|
readings = [
|
|
Reading(timestamp=now - timedelta(days=i)) for i in range(3)
|
|
] # 3-day streak
|
|
badges = calculate_progress_badges_test_helper(readings, test_now=now)
|
|
assert "Current Streak: 3 Days" in badges
|
|
|
|
|
|
def test_weekly_streak():
|
|
now = datetime.utcnow()
|
|
readings = [
|
|
Reading(timestamp=now - timedelta(days=i)) for i in range(7)
|
|
] # 7-day streak
|
|
badges = calculate_progress_badges_test_helper(readings, test_now=now)
|
|
assert "Logged Every Day for a Week" in badges
|
|
|
|
|
|
def test_morning_riser():
|
|
now = datetime.utcnow().replace(hour=8) # Morning readings
|
|
readings = [
|
|
Reading(timestamp=now - timedelta(days=i)) for i in range(7)
|
|
]
|
|
badges = calculate_progress_badges_test_helper(readings, test_now=now)
|
|
assert "Morning Riser: Logged Readings Every Morning for a Week" in badges
|
|
|
|
|
|
def test_night_owl():
|
|
now = datetime.utcnow().replace(hour=20) # Night readings
|
|
readings = [
|
|
Reading(timestamp=now - timedelta(days=i)) for i in range(7)
|
|
]
|
|
badges = calculate_progress_badges_test_helper(readings, test_now=now)
|
|
assert "Night Owl: Logged Readings Every Night for a Week" in badges
|
|
|
|
|
|
def test_milestones():
|
|
readings = [
|
|
Reading(timestamp=datetime.utcnow()) for _ in range(50)
|
|
] # 50 readings
|
|
badges = calculate_progress_badges_test_helper(readings)
|
|
assert "50 Readings Logged" in badges
|
|
assert "100 Readings Logged" not in badges # Ensure only the highest milestone
|
|
|
|
|
|
def test_no_streak_with_gaps():
|
|
now = datetime.utcnow()
|
|
readings = [
|
|
Reading(timestamp=now - timedelta(days=i * 2)) for i in range(3)
|
|
] # Gaps in dates
|
|
badges = calculate_progress_badges_test_helper(readings, test_now=now)
|
|
assert "Current Streak" not in badges
|