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.
76 lines
1.8 KiB
Python
76 lines
1.8 KiB
Python
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
|
|
|
|
User = get_user_model()
|
|
|
|
|
|
@pytest.fixture
|
|
def user(db):
|
|
u = User.objects.create_user(
|
|
username="fasting", email="fasting@example.com", password="testpass"
|
|
)
|
|
UserProfile.objects.get_or_create(user=u)
|
|
return u
|
|
|
|
|
|
@pytest.fixture
|
|
def user_with_custom_thresholds(db):
|
|
u = User.objects.create_user(
|
|
username="custom", email="custom@example.com", password="testpass"
|
|
)
|
|
u.profile.fasting_periodic_hours = 12
|
|
u.profile.fasting_full_hours = 20
|
|
u.profile.save()
|
|
return u
|
|
|
|
|
|
@pytest.fixture
|
|
def food(user):
|
|
return Food.objects.create(title="Test Food", base_run_time_seconds=1200)
|
|
|
|
|
|
@pytest.fixture
|
|
def drink(user):
|
|
from drinks.models import Drink
|
|
|
|
return Drink.objects.create(
|
|
title="Test Drink", base_run_time_seconds=120, calories=50
|
|
)
|
|
|
|
|
|
@pytest.fixture
|
|
def beer(user):
|
|
from drinks.models import Beer, BeerStyle
|
|
|
|
style = BeerStyle.objects.create(name="IPA")
|
|
b = Beer.objects.create(title="Test Beer", base_run_time_seconds=120)
|
|
b.styles.add(style)
|
|
return b
|
|
|
|
|
|
def make_food_scrobble(user, food, ts):
|
|
return Scrobble.objects.create(
|
|
user=user,
|
|
food=food,
|
|
media_type=Scrobble.MediaType.FOOD,
|
|
timestamp=ts,
|
|
played_to_completion=True,
|
|
)
|
|
|
|
|
|
def make_drink_scrobble(user, drink, ts, media_type=Scrobble.MediaType.DRINK):
|
|
kwargs = {"drink": drink} if media_type == Scrobble.MediaType.DRINK else {}
|
|
if media_type == Scrobble.MediaType.BEER:
|
|
kwargs = {"beer": drink}
|
|
return Scrobble.objects.create(
|
|
user=user,
|
|
media_type=media_type,
|
|
timestamp=ts,
|
|
played_to_completion=True,
|
|
**kwargs,
|
|
)
|