From c21d6f566885142f63d8c088681e638566aa9180 Mon Sep 17 00:00:00 2001 From: Colin Powell Date: Sun, 19 Jul 2026 13:50:54 -0400 Subject: [PATCH] fix(tests): fix fasting custom threshold tests using Django ORM cache The user_with_custom_thresholds fixture used UserProfile.objects.get_or_create() which returns a separate Python instance from the one cached in user._state.fields_cache['profile']. When the fixture modified and saved the get_or_create instance, the cached instance on the user object still held default values (periodic=16, full=24). compute_fasting() accessed u.profile which returned the stale cached object. Fixed by modifying through u.profile directly, which operates on the same cached instance. Also applied black formatting to fasting.py. --- tests/trends_tests/conftest.py | 8 +++----- tests/trends_tests/test_fasting.py | 1 - vrobbler/apps/trends/trends/fasting.py | 5 +---- 3 files changed, 4 insertions(+), 10 deletions(-) diff --git a/tests/trends_tests/conftest.py b/tests/trends_tests/conftest.py index 0194d5f..7c06ae9 100644 --- a/tests/trends_tests/conftest.py +++ b/tests/trends_tests/conftest.py @@ -1,7 +1,6 @@ import pytest from django.contrib.auth import get_user_model from django.utils import timezone - from foods.models import Food from profiles.models import UserProfile from scrobbles.models import Scrobble @@ -23,10 +22,9 @@ def user_with_custom_thresholds(db): u = User.objects.create_user( username="custom", email="custom@example.com", password="testpass" ) - profile, _ = UserProfile.objects.get_or_create(user=u) - profile.fasting_periodic_hours = 12 - profile.fasting_full_hours = 20 - profile.save() + u.profile.fasting_periodic_hours = 12 + u.profile.fasting_full_hours = 20 + u.profile.save() return u diff --git a/tests/trends_tests/test_fasting.py b/tests/trends_tests/test_fasting.py index 027eb56..dd8b104 100644 --- a/tests/trends_tests/test_fasting.py +++ b/tests/trends_tests/test_fasting.py @@ -2,7 +2,6 @@ from datetime import timedelta import time_machine from django.utils import timezone - from drinks.models import Drink from lifeevents.models import LifeEvent from scrobbles.models import Scrobble diff --git a/vrobbler/apps/trends/trends/fasting.py b/vrobbler/apps/trends/trends/fasting.py index f7bc337..4a69379 100644 --- a/vrobbler/apps/trends/trends/fasting.py +++ b/vrobbler/apps/trends/trends/fasting.py @@ -4,7 +4,6 @@ from django.db.models import Q from django.utils import timezone from scrobbles.models import Scrobble - DRINK_MEDIA_TYPES = [ Scrobble.MediaType.DRINK, Scrobble.MediaType.BEER, @@ -68,9 +67,7 @@ def _classify_gap(gap_hours, periodic_hours, full_hours): def _add_fasting_entry(fasting_by_date, day, entry, fasting_type): existing = fasting_by_date.get(day) - if existing is None or ( - fasting_type == "full" and existing["type"] == "periodic" - ): + if existing is None or (fasting_type == "full" and existing["type"] == "periodic"): fasting_by_date[day] = entry