Add tests

This commit is contained in:
Peter Stockings
2024-12-31 00:08:08 +11:00
parent d3eba9ba5b
commit 164b23e913
5 changed files with 82 additions and 9 deletions

View File

@@ -40,6 +40,9 @@ COPY . .
# Copy the built TailwindCSS assets from the first stage # Copy the built TailwindCSS assets from the first stage
COPY --from=tailwind-builder /app/app/static/css/tailwind.css ./app/static/css/tailwind.css COPY --from=tailwind-builder /app/app/static/css/tailwind.css ./app/static/css/tailwind.css
# Run tests during the build process
RUN pytest --maxfail=5 --disable-warnings -v || (echo "Tests failed. Exiting." && exit 1)
# Expose the port Flask will run on # Expose the port Flask will run on
EXPOSE 5000 EXPOSE 5000

View File

@@ -164,7 +164,6 @@ def calculate_progress_badges(readings):
sorted_readings = sorted(readings, key=lambda r: r.timestamp.date()) sorted_readings = sorted(readings, key=lambda r: r.timestamp.date())
streak_count = 1 streak_count = 1
daily_streak = True daily_streak = True
monthly_tracker = defaultdict(int)
# Start with the first reading # Start with the first reading
previous_date = sorted_readings[0].timestamp.date() previous_date = sorted_readings[0].timestamp.date()
@@ -178,9 +177,6 @@ def calculate_progress_badges(readings):
elif (current_date - previous_date).days > 1: elif (current_date - previous_date).days > 1:
daily_streak = False daily_streak = False
# Track monthly activity
monthly_tracker[current_date.strftime('%Y-%m')] += 1
previous_date = current_date previous_date = current_date
# Add streak badges # Add streak badges
@@ -192,11 +188,6 @@ def calculate_progress_badges(readings):
if daily_streak and streak_count >= 30 and previous_date == now: if daily_streak and streak_count >= 30 and previous_date == now:
badges.append("Monthly Streak") badges.append("Monthly Streak")
# Add calendar month streak badges
for month, count in monthly_tracker.items():
if count >= 30:
badges.append(f"Full Month of Logging: {month}")
if all(5 <= r.timestamp.hour < 12 for r in sorted_readings[-7:]): if all(5 <= r.timestamp.hour < 12 for r in sorted_readings[-7:]):
badges.append("Morning Riser: Logged Readings Every Morning for a Week") badges.append("Morning Riser: Logged Readings Every Morning for a Week")

2
pytyest.ini Normal file
View File

@@ -0,0 +1,2 @@
[pytest]
pythonpath = .

View File

@@ -5,6 +5,7 @@ click==8.1.8
colorama==0.4.6 colorama==0.4.6
dnspython==2.7.0 dnspython==2.7.0
email-validator==2.2.0 email-validator==2.2.0
exceptiongroup==1.2.2
flask==3.1.0 flask==3.1.0
Flask-Bcrypt==1.0.1 Flask-Bcrypt==1.0.1
Flask-Login==0.6.3 Flask-Login==0.6.3
@@ -16,16 +17,21 @@ gunicorn==23.0.0
humanize==4.11.0 humanize==4.11.0
idna==3.10 idna==3.10
importlib-metadata==8.5.0 importlib-metadata==8.5.0
iniconfig==2.0.0
itsdangerous==2.2.0 itsdangerous==2.2.0
jinja2==3.1.5 jinja2==3.1.5
Mako==1.3.8 Mako==1.3.8
MarkupSafe==3.0.2 MarkupSafe==3.0.2
packaging==24.2 packaging==24.2
pillow==11.0.0 pillow==11.0.0
pluggy==1.5.0
psycopg2==2.9.10 psycopg2==2.9.10
psycopg2-binary==2.9.10 psycopg2-binary==2.9.10
pytest==8.3.4
pytest-flask==1.3.0
pytz==2024.2 pytz==2024.2
SQLAlchemy==2.0.36 SQLAlchemy==2.0.36
tomli==2.2.1
typing-extensions==4.12.2 typing-extensions==4.12.2
werkzeug==3.1.3 werkzeug==3.1.3
wtforms==3.2.1 wtforms==3.2.1

71
tests/test_main.py Normal file
View File

@@ -0,0 +1,71 @@
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