Self hosted FLOSS fitness/workout, nutrition and weight tracker
Fix tests whose assertions could never fail
Roland Geiderfdb9d15c5cc7
diff --git a/wger/core/tests/test_auth_social.py b/wger/core/tests/test_auth_social.pyindex 3cc1c2136..b55cbcef9 100644--- a/wger/core/tests/test_auth_social.py+++ b/wger/core/tests/test_auth_social.py@@ -63,15 +63,13 @@ class SocialAuthEnabledLoginPageTestCase(WgerTestCase):'<form method="post" action="/account/wger-social/login/"',)- @mock.patch(- 'allauth.socialaccount.adapter.DefaultSocialAccountAdapter.list_providers',- return_value=[],- )- def test_no_section_without_configured_providers(self, _):- """If no SocialApp rows exist, ``get_providers`` is empty and the section is hidden."""+ @override_settings(WGER_SOCIAL_PROVIDERS=[])+ def test_no_section_without_configured_providers(self):+ """The section is only rendered when WGER_SOCIAL_PROVIDERS is set."""response = self.client.get(reverse('core:user:login'))self.assertEqual(response.status_code, 200)- self.assertNotContains(response, 'or login with')+ self.assertFalse(response.context['use_social_auth'])+ self.assertNotContains(response, 'or sign in with')@override_settings(WGER_SOCIAL_PROVIDERS=['wger'])diff --git a/wger/exercises/tests/test_categories.py b/wger/exercises/tests/test_categories.pyindex 40a3401a8..13522955c 100644--- a/wger/exercises/tests/test_categories.py+++ b/wger/exercises/tests/test_categories.py@@ -49,7 +49,6 @@ class CategoryOverviewTestCase(WgerAccessTestCase):'manager2','general_manager1','manager3',- 'manager4','test','member1','member2',diff --git a/wger/exercises/tests/test_exercise.py b/wger/exercises/tests/test_exercise.pyindex f84d51ae6..b8b80e767 100644--- a/wger/exercises/tests/test_exercise.py+++ b/wger/exercises/tests/test_exercise.py@@ -163,11 +163,9 @@ class ExerciseCustomApiTestCase(ActstreamApiMixin, ExerciseCrudApiTestCase):Test that it is not possible to set the license for a newly createdexercise base (the license is always set to the default)"""- self.data['license'] = 3-self.authenticate('trainer1')- response = self.client.post(self.url, data=self.data)+ response = self.client.post(self.url, data={**self.data, 'license': 3})self.assertEqual(response.status_code, status.HTTP_201_CREATED)- exercise = Exercise.objects.get(pk=self.pk)+ exercise = Exercise.objects.get(pk=response.json()['id'])self.assertEqual(exercise.license_id, CC_BY_SA_4_LICENSE_ID)diff --git a/wger/exercises/tests/test_exercise_translation.py b/wger/exercises/tests/test_exercise_translation.pyindex ec7d3779c..33ba87005 100644--- a/wger/exercises/tests/test_exercise_translation.py+++ b/wger/exercises/tests/test_exercise_translation.py@@ -200,13 +200,11 @@ class ExerciseTranslationCustomApiTestCase(ActstreamApiMixin, ExerciseCrudApiTesTest that it is not possible to set the license for a newly createdexercise translation (the license is always set to the default)"""- self.data['license'] = CC_0_LICENSE_ID-self.authenticate('trainer1')- response = self.client.post(self.url, data=self.data)+ response = self.client.post(self.url, data={**self.data, 'license': CC_0_LICENSE_ID})self.assertEqual(response.status_code, status.HTTP_201_CREATED)- translation = Translation.objects.get(pk=self.pk)+ translation = Translation.objects.get(pk=response.json()['id'])self.assertEqual(translation.license_id, CC_BY_SA_4_LICENSE_ID)def test_post_without_description_succeeds(self):diff --git a/wger/exercises/tests/test_muscles.py b/wger/exercises/tests/test_muscles.pyindex 34312d5e1..ec47361c1 100644--- a/wger/exercises/tests/test_muscles.py+++ b/wger/exercises/tests/test_muscles.py@@ -55,7 +55,6 @@ class MuscleAdminOverviewTest(WgerAccessTestCase):'manager2','general_manager1','manager3',- 'manager4','test','member1','member2',diff --git a/wger/exercises/tests/test_sync.py b/wger/exercises/tests/test_sync.pyindex 5365e3ea9..285b5f9e3 100644--- a/wger/exercises/tests/test_sync.py+++ b/wger/exercises/tests/test_sync.py@@ -799,13 +799,11 @@ class TestSyncMethods(WgerTestCase):self.assertEqual(Exercise.objects.count(), 8)self.assertEqual(Translation.objects.count(), 11)- exercise1 = Exercise.objects.get(pk=1)- exercise2 = Exercise.objects.get(pk=4)- self.assertFalse(SlotEntry.objects.filter(exercise=exercise2).count())- self.assertFalse(WorkoutLog.objects.filter(exercise=exercise2).count())-- slot_entries = SlotEntry.objects.filter(exercise=exercise1)- logs = WorkoutLog.objects.filter(exercise=exercise1)+ # Materialize the PKs now, the querysets would be empty after the sync+ slot_entry_pks = list(SlotEntry.objects.filter(exercise_id=1).values_list('pk', flat=True))+ log_pks = list(WorkoutLog.objects.filter(exercise_id=1).values_list('pk', flat=True))+ self.assertTrue(slot_entry_pks)+ self.assertTrue(log_pks)handle_deleted_entries(print)@@ -818,10 +816,10 @@ class TestSyncMethods(WgerTestCase):self.assertRaises(Translation.DoesNotExist, Translation.objects.get, pk=3)self.assertRaises(Exercise.DoesNotExist, Exercise.objects.get, pk=1)- # Workouts and logs have been moved- for pk in slot_entries:+ # Workouts and logs have been moved to the replacement exercise+ for pk in slot_entry_pks:self.assertEqual(SlotEntry.objects.get(pk=pk).exercise_id, 2)- for pk in logs:+ for pk in log_pks:self.assertEqual(WorkoutLog.objects.get(pk=pk).exercise_id, 2)@patch('requests.get', return_value=MockDeletionLogResponse())diff --git a/wger/gym/tests/test_contract_options.py b/wger/gym/tests/test_contract_options.pyindex 4488ed7d9..0c47c6d15 100644--- a/wger/gym/tests/test_contract_options.py+++ b/wger/gym/tests/test_contract_options.py@@ -42,7 +42,6 @@ class AddContractOptionTestCase(WgerAddTestCase):'admin','general_manager1','manager3',- 'manager4','test','member1','member2',@@ -65,7 +64,6 @@ class EditContractOptionTestCase(WgerEditTestCase):'admin','general_manager1','manager3',- 'manager4','test','member1','member2',@@ -89,7 +87,6 @@ class DeleteContractOptionTestCase(WgerDeleteTestCase):'admin','general_manager1','manager3',- 'manager4','test','member1','member2',@@ -113,7 +110,6 @@ class AccessContractOptionOverviewTestCase(WgerAccessTestCase):'admin','general_manager1','manager3',- 'manager4','test','member1','member2',diff --git a/wger/gym/tests/test_contract_types.py b/wger/gym/tests/test_contract_types.pyindex 697c17f2e..591be6e7b 100644--- a/wger/gym/tests/test_contract_types.py+++ b/wger/gym/tests/test_contract_types.py@@ -39,7 +39,6 @@ class AddContractTypeTestCase(WgerAddTestCase):'admin','general_manager1','manager3',- 'manager4','test','member1','member2',@@ -62,7 +61,6 @@ class EditContractTypeTestCase(WgerEditTestCase):'admin','general_manager1','manager3',- 'manager4','test','member1','member2',@@ -86,7 +84,6 @@ class DeleteContractTypeTestCase(WgerDeleteTestCase):'admin','general_manager1','manager3',- 'manager4','test','member1','member2',@@ -110,7 +107,6 @@ class AccessContractTypeOverviewTestCase(WgerAccessTestCase):'admin','general_manager1','manager3',- 'manager4','test','member1','member2',diff --git a/wger/gym/tests/test_contracts.py b/wger/gym/tests/test_contracts.pyindex a1a474d54..b0928d0bc 100644--- a/wger/gym/tests/test_contracts.py+++ b/wger/gym/tests/test_contracts.py@@ -37,7 +37,6 @@ class AddContractTestCase(WgerAddTestCase):'admin','general_manager1','manager3',- 'manager4','test','member1','member2',@@ -58,7 +57,6 @@ class AccessContractTestCase(WgerAccessTestCase):'admin','general_manager1','manager3',- 'manager4','test','member1','member2',@@ -79,7 +77,6 @@ class AccessContractOverviewTestCase(WgerAccessTestCase):'admin','general_manager1','manager3',- 'manager4','test','member1','member2',@@ -102,7 +99,6 @@ class EditContractTestCase(WgerEditTestCase):'admin','general_manager1','manager3',- 'manager4','test','member1','member2',diff --git a/wger/gym/tests/test_export.py b/wger/gym/tests/test_export.pyindex ce7ee193d..80ecf82b8 100644--- a/wger/gym/tests/test_export.py+++ b/wger/gym/tests/test_export.py@@ -68,7 +68,6 @@ class GymMembersCsvExportTestCase(WgerTestCase):for username in ('manager3',- 'manager4','test','member1','member2',diff --git a/wger/gym/tests/test_inactive_members.py b/wger/gym/tests/test_inactive_members.pyindex 9d90e0416..9b7fe3387 100644--- a/wger/gym/tests/test_inactive_members.py+++ b/wger/gym/tests/test_inactive_members.py@@ -25,23 +25,24 @@ class EmailInactiveUserTestCase(WgerTestCase):Test email reminders for inactive users"""- def test_reminder(self, fail=False):+ def test_reminder(self):"""Test email reminders for inactive users"""call_command('inactive-members')- self.assertEqual(len(mail.outbox), 6)- recipment_list = [message.to[0] for message in mail.outbox]- trainer_list = [- '[email protected]',- '[email protected]',- '[email protected]',- '[email protected]',- '[email protected]',- ]- recipment_list.sort()- trainer_list.sort()-- self.assertEqual(recipment_list.sort(), trainer_list.sort())+ # Everybody in the gym with the gym_trainer permission and the+ # overview_inactive preference gets the overview, including the admin+ recipient_list = [message.to[0] for message in mail.outbox]+ self.assertCountEqual(+ recipient_list,+ [+ '[email protected]',+ '[email protected]',+ '[email protected]',+ '[email protected]',+ '[email protected]',+ '[email protected]',+ ],+ )diff --git a/wger/mailer/tests/test_gym_emails.py b/wger/mailer/tests/test_gym_emails.pyindex ab2a2da4e..60549cca3 100644--- a/wger/mailer/tests/test_gym_emails.py+++ b/wger/mailer/tests/test_gym_emails.py@@ -38,7 +38,6 @@ class AccessContractTestCase(WgerAccessTestCase):'admin','general_manager1','manager3',- 'manager4','test','member1','member2',diff --git a/wger/manager/tests/test_copy_routine.py b/wger/manager/tests/test_copy_routine.pyindex 3e67801bd..6a57ed805 100644--- a/wger/manager/tests/test_copy_routine.py+++ b/wger/manager/tests/test_copy_routine.py@@ -20,7 +20,11 @@ from django.urls import reverse# wgerfrom wger.core.tests.base_testcase import WgerTestCase-from wger.manager.models import Routine+from wger.manager.models import (+ Routine,+ SetsConfig,+ SlotEntry,+)logger = logging.getLogger(__name__)@@ -31,25 +35,27 @@ class CopyRoutineTestCase(WgerTestCase):Tests copying a routine or template"""- def copy_routine_and_assert(self):+ def copy_routine_and_assert(self, pk: int):"""Helper function to test copying routines"""# Copy the routinecount_before = Routine.objects.count()- self.client.get(reverse('manager:routine:copy', kwargs={'pk': '3'}))+ self.client.get(reverse('manager:routine:copy', kwargs={'pk': pk}))count_after = Routine.objects.count()self.assertEqual(count_after, count_before + 1)- routine_original = Routine.objects.get(pk=3)+ routine_original = Routine.objects.get(pk=pk)routine_copy = Routine.objects.latest('pk')self.assertEqual(routine_copy.name, routine_original.name)self.assertEqual(routine_copy.description, routine_original.description)- self.assertEqual(routine_copy.is_template, routine_original.is_template)- self.assertEqual(routine_copy.is_public, routine_original.is_public)++ # Copies are always private, regular routines+ self.assertFalse(routine_copy.is_template)+ self.assertFalse(routine_copy.is_public)self.assertEqual(routine_copy.start, datetime.date.today())self.assertEqual(routine_copy.end, datetime.date.today() + routine_original.duration)@@ -77,9 +83,9 @@ class CopyRoutineTestCase(WgerTestCase):slot_entries_original = slots_original[j].entries.all()slot_entries_copy = slots_copy[j].entries.all()- for l in range(slot_entries_original.count()):- entry_copy = slot_entries_copy[l]- entry_orig = slot_entries_original[l]+ for k in range(slot_entries_original.count()):+ entry_copy = slot_entries_copy[k]+ entry_orig = slot_entries_original[k]self.assertEqual(entry_orig.exercise_id, entry_copy.exercise_id)self.assertEqual(entry_orig.repetition_unit_id, entry_copy.repetition_unit_id)@@ -96,18 +102,26 @@ class CopyRoutineTestCase(WgerTestCase):def test_copy_workout_owner(self):"""- Test copying a workout as the owner user+ Test copying a full workout with days, slots, entries and configs"""- self.user_login('test')- self.copy_routine_and_assert()+ # Guard against empty fixtures, the deep comparison would silently+ # collapse into zero iterations+ self.assertTrue(Routine.objects.get(pk=1).days.count())+ self.assertTrue(SlotEntry.objects.filter(slot__day__routine_id=1).exists())+ self.assertTrue(SetsConfig.objects.filter(slot_entry__slot__day__routine_id=1).exists())- def test_copy_workout(self):+ self.user_login('admin')+ self.copy_routine_and_assert(pk=1)++ def test_copy_empty_routine(self):"""- Test copying a workout (not template)+ Test copying a routine without any days"""+ self.assertFalse(Routine.objects.get(pk=3).days.count())+self.user_login('test')- self.copy_routine_and_assert()+ self.copy_routine_and_assert(pk=3)def test_copy_workout_other(self):"""diff --git a/wger/nutrition/tests/test_nutritional_cache.py b/wger/nutrition/tests/test_nutritional_cache.pyindex 8f9ab3805..cb4285820 100644--- a/wger/nutrition/tests/test_nutritional_cache.py+++ b/wger/nutrition/tests/test_nutritional_cache.py@@ -90,4 +90,4 @@ class NutritionalPlanCacheTestCase(WgerTestCase):plan.get_nutritional_values()self.assertTrue(cache.get(cache_mapper.get_nutrition_cache_by_key(plan)))meal_item.delete()- self.assertFalse(cache.get(cache_mapper.get_nutrition_cache_by_key(meal_item)))+ self.assertFalse(cache.get(cache_mapper.get_nutrition_cache_by_key(plan)))diff --git a/wger/nutrition/tests/test_tasks.py b/wger/nutrition/tests/test_tasks.pyindex 52cae2b9b..0ffab1fe9 100644--- a/wger/nutrition/tests/test_tasks.py+++ b/wger/nutrition/tests/test_tasks.py@@ -119,9 +119,17 @@ class FetchIngredientImageTestCase(WgerTestCase):ingredient.source_name = 'blabla'ingredient.save()- with self.settings(WGER_SETTINGS={'DOWNLOAD_INGREDIENTS_FROM': 'FOO'}):+ with self.settings(+ TESTING=False,+ WGER_SETTINGS={+ 'DOWNLOAD_INGREDIENTS_FROM': DOWNLOAD_INGREDIENT_OFF,+ 'INGREDIENT_IMAGE_CHECK_INTERVAL': datetime.timedelta(days=5),+ },+ ):result = fetch_ingredient_image(1)- mock_logger.assert_not_called()++ # The log entry proves the TESTING guard was passed+ mock_logger.assert_any_call('Fetching image for ingredient 1')mock_request.assert_not_called()self.assertIsNone(result)@@ -131,15 +139,18 @@ class FetchIngredientImageTestCase(WgerTestCase):"""Test that no images are fetched if the appropriate setting is not set"""- ingredient = Ingredient.objects.get(pk=1)- ingredient.source_name = 'blabla'- ingredient.save()-- with self.settings(WGER_SETTINGS={'DOWNLOAD_INGREDIENTS_FROM': False}):+ with self.settings(+ TESTING=False,+ WGER_SETTINGS={+ 'DOWNLOAD_INGREDIENTS_FROM': False,+ 'INGREDIENT_IMAGE_CHECK_INTERVAL': datetime.timedelta(days=5),+ },+ ):result = fetch_ingredient_image(1)- mock_logger.assert_not_called()++ mock_logger.assert_any_call('No image backend configured, skipping...')mock_request.assert_not_called()- self.assertEqual(result, None)+ self.assertIsNone(result)@patch('openfoodfacts.api.ProductResource.get', autospec=True)@patch('wger.nutrition.models.Image.from_json', autospec=True)diff --git a/wger/trophies/tests/test_checkers.py b/wger/trophies/tests/test_checkers.pyindex 12bd7f1df..231befc69 100644--- a/wger/trophies/tests/test_checkers.py+++ b/wger/trophies/tests/test_checkers.py@@ -35,6 +35,7 @@ from wger.trophies.checkers.workout_count_based import WorkoutCountBasedCheckerfrom wger.trophies.models import (Trophy,UserStatistics,+ UserTrophy,)@@ -522,10 +523,21 @@ class PersonalRecordCheckerTestCase(WgerTestCase):context = checker.get_context_data()self.assertIsNotNone(context)+ def award_pr_trophy(self, checker: PersonalRecordChecker):+ """+ Awards the PR trophy, like the signal handler does after a positive check+ """+ UserTrophy.objects.create(+ user=self.user,+ trophy=self.trophy,+ context_data=checker.get_context_data(),+ )+def test_improvement_detected_and_context_values(self):log1 = WorkoutLog(user=self.user, exercise=self.exercise, repetitions=10, weight=100)checker1 = PersonalRecordChecker(self.user, self.trophy, {'log': log1})self.assertTrue(checker1.check())+ self.award_pr_trophy(checker1)log2 = WorkoutLog(user=self.user, exercise=self.exercise, repetitions=10, weight=110)checker2 = PersonalRecordChecker(self.user, self.trophy, {'log': log2})@@ -534,10 +546,11 @@ class PersonalRecordCheckerTestCase(WgerTestCase):context = checker2.get_context_data()self.assertIsNotNone(context)- def no_award_if_not_an_improvement(self):+ def test_no_award_if_not_an_improvement(self):log1 = WorkoutLog(user=self.user, exercise=self.exercise, repetitions=10, weight=100)checker1 = PersonalRecordChecker(self.user, self.trophy, {'log': log1})self.assertTrue(checker1.check())+ self.award_pr_trophy(checker1)# lower weightlog2 = WorkoutLog(user=self.user, exercise=self.exercise, repetitions=10, weight=90)diff --git a/wger/trophies/tests/test_overview.py b/wger/trophies/tests/test_overview.pyindex f8dbab7b4..21e37a47e 100644--- a/wger/trophies/tests/test_overview.py+++ b/wger/trophies/tests/test_overview.py@@ -35,7 +35,6 @@ class TrophiesOverviewTestCase(WgerAccessTestCase):'manager2','general_manager1','manager3',- 'manager4','test','member1','member2',