72 lines
2.2 KiB
Python
72 lines
2.2 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 calculate_progress_badges
|
|
|
|
|
|
|
|
def test_no_readings():
|
|
readings = []
|
|
badges = calculate_progress_badges(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(readings)
|
|
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(readings)
|
|
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(readings)
|
|
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(readings)
|
|
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(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(readings)
|
|
assert "Current Streak" not in badges
|