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.
This commit is contained in:
@ -1,7 +1,6 @@
|
|||||||
import pytest
|
import pytest
|
||||||
from django.contrib.auth import get_user_model
|
from django.contrib.auth import get_user_model
|
||||||
from django.utils import timezone
|
from django.utils import timezone
|
||||||
|
|
||||||
from foods.models import Food
|
from foods.models import Food
|
||||||
from profiles.models import UserProfile
|
from profiles.models import UserProfile
|
||||||
from scrobbles.models import Scrobble
|
from scrobbles.models import Scrobble
|
||||||
@ -23,10 +22,9 @@ def user_with_custom_thresholds(db):
|
|||||||
u = User.objects.create_user(
|
u = User.objects.create_user(
|
||||||
username="custom", email="custom@example.com", password="testpass"
|
username="custom", email="custom@example.com", password="testpass"
|
||||||
)
|
)
|
||||||
profile, _ = UserProfile.objects.get_or_create(user=u)
|
u.profile.fasting_periodic_hours = 12
|
||||||
profile.fasting_periodic_hours = 12
|
u.profile.fasting_full_hours = 20
|
||||||
profile.fasting_full_hours = 20
|
u.profile.save()
|
||||||
profile.save()
|
|
||||||
return u
|
return u
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@ -2,7 +2,6 @@ from datetime import timedelta
|
|||||||
|
|
||||||
import time_machine
|
import time_machine
|
||||||
from django.utils import timezone
|
from django.utils import timezone
|
||||||
|
|
||||||
from drinks.models import Drink
|
from drinks.models import Drink
|
||||||
from lifeevents.models import LifeEvent
|
from lifeevents.models import LifeEvent
|
||||||
from scrobbles.models import Scrobble
|
from scrobbles.models import Scrobble
|
||||||
|
|||||||
@ -4,7 +4,6 @@ from django.db.models import Q
|
|||||||
from django.utils import timezone
|
from django.utils import timezone
|
||||||
from scrobbles.models import Scrobble
|
from scrobbles.models import Scrobble
|
||||||
|
|
||||||
|
|
||||||
DRINK_MEDIA_TYPES = [
|
DRINK_MEDIA_TYPES = [
|
||||||
Scrobble.MediaType.DRINK,
|
Scrobble.MediaType.DRINK,
|
||||||
Scrobble.MediaType.BEER,
|
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):
|
def _add_fasting_entry(fasting_by_date, day, entry, fasting_type):
|
||||||
existing = fasting_by_date.get(day)
|
existing = fasting_by_date.get(day)
|
||||||
if existing is None or (
|
if existing is None or (fasting_type == "full" and existing["type"] == "periodic"):
|
||||||
fasting_type == "full" and existing["type"] == "periodic"
|
|
||||||
):
|
|
||||||
fasting_by_date[day] = entry
|
fasting_by_date[day] = entry
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user