Skip to content

armgit / wger

Public Imported from GitHub
0

Self hosted FLOSS fitness/workout, nutrition and weight tracker

Make the ownership test real and drop the process-wide date patch

Roland Geider9f41b1c0cda6
diff --git a/wger/measurements/tests/test_aggregates.py b/wger/measurements/tests/test_aggregates.py
index c57d5d765..9f2e4a3e0 100644
--- a/wger/measurements/tests/test_aggregates.py
+++ b/wger/measurements/tests/test_aggregates.py
@@ -155,9 +155,20 @@ class AggregateApiTestCase(WgerTestCase):
self.assertEqual(response.status_code, 400)
def test_another_users_category_is_not_aggregated(self):
+ self.add(timezone.make_aware(datetime.datetime(2026, 5, 4, 8)), 60)
+ self.assertEqual(len(self.aggregate(bucket='day', tz='UTC')), 1)
+
+ self.user_login('admin')
+
+ self.assertEqual(self.aggregate(bucket='day', tz='UTC'), [])
+
+ def test_another_users_category_has_no_value_counts(self):
+ self.add(timezone.make_aware(datetime.datetime(2026, 5, 4, 8)), 60)
+ self.assertEqual(len(self.value_counts()), 1)
+
self.user_login('admin')
- self.assertEqual(self.aggregate(bucket='day'), [])
+ self.assertEqual(self.value_counts(), [])
def test_value_counts_count_how_often_a_value_occurred(self):
# A year of readings comes back as the distinct values it covers
diff --git a/wger/trophies/services/statistics.py b/wger/trophies/services/statistics.py
index 5a65ed181..601f09c76 100644
--- a/wger/trophies/services/statistics.py
+++ b/wger/trophies/services/statistics.py
@@ -99,7 +99,8 @@ class UserStatisticsService:
# Calculate streaks and other date-based stats
workout_dates = sorted({day for session in sessions if (day := session.local_day_in(tz))})
- current_streak, longest_streak = cls._calculate_streaks(workout_dates, tz)
+ today = timezone.localdate(timezone=tz)
+ current_streak, longest_streak = cls._calculate_streaks(workout_dates, today)
stats.current_streak = current_streak
stats.longest_streak = longest_streak
@@ -119,7 +120,7 @@ class UserStatisticsService:
stats.latest_workout_time = latest
# Calculate weekend workout streak
- weekend_streak, last_complete_weekend = cls._calculate_weekend_streak(workout_dates, tz)
+ weekend_streak, last_complete_weekend = cls._calculate_weekend_streak(workout_dates, today)
stats.weekend_workout_streak = weekend_streak
stats.last_complete_weekend_date = last_complete_weekend
@@ -274,7 +275,7 @@ class UserStatisticsService:
return total
@classmethod
- def _calculate_streaks(cls, workout_dates: list, tz: datetime.tzinfo) -> tuple:
+ def _calculate_streaks(cls, workout_dates: list, today: datetime.date) -> tuple:
"""
Calculate current and longest workout streaks.
@@ -284,7 +285,7 @@ class UserStatisticsService:
Args:
workout_dates: List of dates with workouts, sorted ascending
- tz: the timezone of the user the dates belong to
+ today: the current day in the timezone the dates were cut in
Returns:
Tuple of (current_streak, longest_streak)
@@ -299,8 +300,6 @@ class UserStatisticsService:
longest_streak = 1
streak = 1
- today = timezone.localdate(timezone=tz)
-
for i in range(1, len(unique_dates)):
if (unique_dates[i] - unique_dates[i - 1]).days == 1:
streak += 1
@@ -345,13 +344,13 @@ class UserStatisticsService:
return min(times), max(times)
@classmethod
- def _calculate_weekend_streak(cls, workout_dates: list, tz: datetime.tzinfo) -> tuple:
+ def _calculate_weekend_streak(cls, workout_dates: list, today: datetime.date) -> tuple:
"""
Calculate consecutive weekends with workouts on both Saturday and Sunday.
Args:
workout_dates: List of workout dates
- tz: the timezone of the user the dates belong to, decides "today"
+ today: the current day in the timezone the dates were cut in
Returns:
Tuple of (weekend_streak, last_complete_weekend_date)
@@ -398,7 +397,6 @@ class UserStatisticsService:
streak = 1
# Determine current streak
- today = timezone.localdate(timezone=tz)
last_complete = complete_weekends[-1]
# Find the most recent Saturday
diff --git a/wger/trophies/tests/test_statistics_streaks.py b/wger/trophies/tests/test_statistics_streaks.py
index e0d2b599b..9a404d688 100644
--- a/wger/trophies/tests/test_statistics_streaks.py
+++ b/wger/trophies/tests/test_statistics_streaks.py
@@ -15,7 +15,6 @@
# Standard Library
import datetime
-from unittest import mock
# Django
from django.contrib.auth.models import User
@@ -30,18 +29,7 @@ from wger.trophies.services.statistics import UserStatisticsService
FROZEN_TODAY = datetime.date(2024, 6, 19)
-"""A Wednesday; the most recent Saturday is 2024-06-15"""
-
-
-def frozen_today():
- """
- Freezes the current day so the streak calculations are deterministic
- """
-
- return mock.patch(
- 'wger.trophies.services.statistics.timezone.localdate',
- return_value=FROZEN_TODAY,
- )
+"""The day the calculations run on; a Wednesday, the most recent Saturday is 2024-06-15"""
def days_ago(days: int) -> datetime.date:
@@ -54,8 +42,7 @@ class CalculateStreaksTestCase(SimpleTestCase):
"""
def calculate(self, dates):
- with frozen_today():
- return UserStatisticsService._calculate_streaks(dates, timezone.get_default_timezone())
+ return UserStatisticsService._calculate_streaks(dates, FROZEN_TODAY)
def test_no_workouts(self):
self.assertEqual(self.calculate([]), (0, 0))
@@ -157,10 +144,7 @@ class CalculateWeekendStreakTestCase(SimpleTestCase):
"""
def calculate(self, dates):
- with frozen_today():
- return UserStatisticsService._calculate_weekend_streak(
- dates, timezone.get_default_timezone()
- )
+ return UserStatisticsService._calculate_weekend_streak(dates, FROZEN_TODAY)
def test_no_workouts(self):
self.assertEqual(self.calculate([]), (0, None))