From 164b23e913805370e049fb9c071105e90eb10645 Mon Sep 17 00:00:00 2001 From: Peter Stockings Date: Tue, 31 Dec 2024 00:08:08 +1100 Subject: [PATCH] Add tests --- Dockerfile | 3 ++ app/routes/main.py | 9 ------ pytyest.ini | 2 ++ requirements.txt | 6 ++++ tests/test_main.py | 71 ++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 82 insertions(+), 9 deletions(-) create mode 100644 pytyest.ini create mode 100644 tests/test_main.py diff --git a/Dockerfile b/Dockerfile index 8f43904..18f479d 100644 --- a/Dockerfile +++ b/Dockerfile @@ -40,6 +40,9 @@ COPY . . # Copy the built TailwindCSS assets from the first stage 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 5000 diff --git a/app/routes/main.py b/app/routes/main.py index d5eb30f..c58f08a 100644 --- a/app/routes/main.py +++ b/app/routes/main.py @@ -164,7 +164,6 @@ def calculate_progress_badges(readings): sorted_readings = sorted(readings, key=lambda r: r.timestamp.date()) streak_count = 1 daily_streak = True - monthly_tracker = defaultdict(int) # Start with the first reading previous_date = sorted_readings[0].timestamp.date() @@ -178,9 +177,6 @@ def calculate_progress_badges(readings): elif (current_date - previous_date).days > 1: daily_streak = False - # Track monthly activity - monthly_tracker[current_date.strftime('%Y-%m')] += 1 - previous_date = current_date # Add streak badges @@ -192,11 +188,6 @@ def calculate_progress_badges(readings): if daily_streak and streak_count >= 30 and previous_date == now: 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:]): badges.append("Morning Riser: Logged Readings Every Morning for a Week") diff --git a/pytyest.ini b/pytyest.ini new file mode 100644 index 0000000..ec92f39 --- /dev/null +++ b/pytyest.ini @@ -0,0 +1,2 @@ +[pytest] +pythonpath = . diff --git a/requirements.txt b/requirements.txt index cee4eec..5b88314 100644 --- a/requirements.txt +++ b/requirements.txt @@ -5,6 +5,7 @@ click==8.1.8 colorama==0.4.6 dnspython==2.7.0 email-validator==2.2.0 +exceptiongroup==1.2.2 flask==3.1.0 Flask-Bcrypt==1.0.1 Flask-Login==0.6.3 @@ -16,16 +17,21 @@ gunicorn==23.0.0 humanize==4.11.0 idna==3.10 importlib-metadata==8.5.0 +iniconfig==2.0.0 itsdangerous==2.2.0 jinja2==3.1.5 Mako==1.3.8 MarkupSafe==3.0.2 packaging==24.2 pillow==11.0.0 +pluggy==1.5.0 psycopg2==2.9.10 psycopg2-binary==2.9.10 +pytest==8.3.4 +pytest-flask==1.3.0 pytz==2024.2 SQLAlchemy==2.0.36 +tomli==2.2.1 typing-extensions==4.12.2 werkzeug==3.1.3 wtforms==3.2.1 diff --git a/tests/test_main.py b/tests/test_main.py new file mode 100644 index 0000000..193ddaf --- /dev/null +++ b/tests/test_main.py @@ -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