Skip to content

armgit / wger

Public Imported from GitHub
0

Self hosted FLOSS fitness/workout, nutrition and weight tracker

Improve test that makes handled deleted users and proper trophy handling

Roland Geiderd86af66026f8
diff --git a/wger/core/tests/test_delete_user.py b/wger/core/tests/test_delete_user.py
index bb913ec93..7f2462a5c 100644
--- a/wger/core/tests/test_delete_user.py
+++ b/wger/core/tests/test_delete_user.py
@@ -14,6 +14,7 @@
# Standard Library
import logging
+from unittest import mock
# Django
from django.contrib.auth.models import User
@@ -26,7 +27,6 @@ from wger.manager.models import WorkoutSession
from wger.trophies.models import (
Trophy,
UserStatistics,
- UserTrophy,
)
from wger.trophies.services.statistics import UserStatisticsService
from wger.trophies.services.trophy import TrophyService
@@ -244,15 +244,15 @@ class UserDeleteTrophyIntegrationTestCase(WgerTestCase):
def test_delete_user_with_trophy_records(self):
"""
- Adds sessions/records, calls trophy system, ensures delete works without IntegrityError
+ Deleting a user removes the account without the workout cascade signals
+ recreating its statistics (a re-inserted UserStatistics row would orphan
+ and fail the deferred FK check on PostgreSQL).
"""
- logger.info('Testing user deletion after trophy system invocation')
user = User.objects.create_user(
username='trophyuser', email='[email protected]', password='testpass'
)
- session = WorkoutSession.objects.create(user=user, date=timezone.now().date())
- logger.info('Created WorkoutSession')
- trophy = Trophy.objects.create(
+ WorkoutSession.objects.create(user=user, date=timezone.now().date())
+ Trophy.objects.create(
name='DeleteTestTrophy',
trophy_type=0,
checker_class='workout_count_based',
@@ -260,10 +260,13 @@ class UserDeleteTrophyIntegrationTestCase(WgerTestCase):
is_active=True,
)
UserStatistics.objects.update_or_create(user=user, defaults={'total_workouts': 1})
- logger.info('Created Trophy and updated UserStatistics')
UserStatisticsService.update_statistics(user)
TrophyService.evaluate_all_trophies(user)
- logger.info('Trophy services invoked')
- user.delete()
- logger.info('User deleted')
+
+ with mock.patch(
+ 'wger.trophies.signals.UserStatisticsService.handle_workout_deletion'
+ ) as handle_deletion:
+ user.delete()
+
+ handle_deletion.assert_not_called()
self.assertEqual(User.objects.filter(username='trophyuser').count(), 0)