Skip to content

armgit / wger

Public Imported from GitHub
0

Self hosted FLOSS fitness/workout, nutrition and weight tracker

Add some tests for the gym and display modes

Roland Geiderd8eba56f291d
diff --git a/wger/manager/api/serializers.py b/wger/manager/api/serializers.py
index 7df55610e..3fe04ea00 100644
--- a/wger/manager/api/serializers.py
+++ b/wger/manager/api/serializers.py
@@ -325,7 +325,7 @@ class SlotDataSerializer(serializers.Serializer):
sets = SetConfigDataSerializer(many=True)
-class WorkoutDayDisplayDataSerializer(serializers.Serializer):
+class WorkoutDayDataDisplayModeSerializer(serializers.Serializer):
"""
WorkoutDayData serializer - display mode
"""
@@ -334,19 +334,19 @@ class WorkoutDayDisplayDataSerializer(serializers.Serializer):
date = serializers.DateField()
label = serializers.CharField()
day = DayNgSerializer()
- slots = SlotDataSerializer(many=True, source='slots_display')
+ slots = SlotDataSerializer(many=True, source='slots_display_mode')
-class WorkoutDayDataSerializer(serializers.Serializer):
+class WorkoutDayDataGymModeSerializer(serializers.Serializer):
"""
- WorkoutDayData serializer
+ WorkoutDayData serializer - gym mode
"""
iteration = serializers.IntegerField()
date = serializers.DateField()
label = serializers.CharField()
day = DayNgSerializer()
- slots = SlotDataSerializer(many=True)
+ slots = SlotDataSerializer(many=True, source='slots_gym_mode')
class WorkoutSerializer(serializers.ModelSerializer):
diff --git a/wger/manager/api/views.py b/wger/manager/api/views.py
index be79180cd..69a18af7b 100644
--- a/wger/manager/api/views.py
+++ b/wger/manager/api/views.py
@@ -37,8 +37,8 @@ from wger.manager.api.serializers import (
ScheduleStepSerializer,
SetSerializer,
SettingSerializer,
- WorkoutDayDataSerializer,
- WorkoutDayDisplayDataSerializer,
+ WorkoutDayDataDisplayModeSerializer,
+ WorkoutDayDataGymModeSerializer,
WorkoutLogSerializer,
WorkoutSerializer,
WorkoutSessionSerializer,
@@ -104,7 +104,7 @@ class RoutineViewSet(viewsets.ModelViewSet):
Return the day sequence of the routine
"""
return Response(
- WorkoutDayDisplayDataSerializer(self.get_object().date_sequence, many=True).data
+ WorkoutDayDataDisplayModeSerializer(self.get_object().date_sequence, many=True).data
)
@action(detail=True, url_path='date-sequence-gym-mode')
@@ -112,14 +112,16 @@ class RoutineViewSet(viewsets.ModelViewSet):
"""
Return the day sequence of the routine
"""
- return Response(WorkoutDayDataSerializer(self.get_object().date_sequence, many=True).data)
+ return Response(
+ WorkoutDayDataGymModeSerializer(self.get_object().date_sequence, many=True).data
+ )
@action(detail=True, url_path='current-day')
def current_day(self, request, pk):
"""
Return current day of the routine
"""
- return Response(WorkoutDayDataSerializer(self.get_object().current_day()).data)
+ return Response(WorkoutDayDataGymModeSerializer(self.get_object().current_day()).data)
@action(detail=True)
def structure(self, request, pk):
diff --git a/wger/manager/dataclasses.py b/wger/manager/dataclasses.py
index a29371f0e..c1eab971f 100644
--- a/wger/manager/dataclasses.py
+++ b/wger/manager/dataclasses.py
@@ -150,17 +150,17 @@ class WorkoutDayData:
label: str | None = None
@property
- def slots(self) -> List[SlotData]:
+ def slots_gym_mode(self) -> List[SlotData]:
if not self.day:
return []
- return self.day.get_slots(self.iteration)
+ return self.day.get_slots_gym_mode(self.iteration)
@property
- def slots_display(self) -> List[SlotData]:
+ def slots_display_mode(self) -> List[SlotData]:
"""Returns the slots optimized for display in the template"""
if not self.day:
return []
- return self.day.get_slots_display(self.iteration)
+ return self.day.get_slots_display_mode(self.iteration)
diff --git a/wger/manager/fixtures/test-routine-data.json b/wger/manager/fixtures/test-routine-data.json
index a43a8e936..bb0d854f8 100644
--- a/wger/manager/fixtures/test-routine-data.json
+++ b/wger/manager/fixtures/test-routine-data.json
@@ -79,5 +79,246 @@
"order": 2,
"comment": "test comment 456"
}
+ },
+ {
+ "pk": 3,
+ "model": "manager.slot",
+ "fields": {
+ "day": 1,
+ "order": 2,
+ "comment": "test comment 789"
+ }
+ },
+ {
+ "pk": 1,
+ "model": "manager.slotconfig",
+ "fields": {
+ "slot_id": 1,
+ "exercise_id": 1,
+ "repetition_unit": 1,
+ "repetition_rounding": 1,
+ "weight_unit": 1,
+ "weight_rounding": 1.25,
+ "order": 1,
+ "comment": "",
+ "type": "normal",
+ "class_name": null
+ }
+ },
+ {
+ "pk": 2,
+ "model": "manager.slotconfig",
+ "fields": {
+ "slot_id": 1,
+ "exercise_id": 2,
+ "repetition_unit": 1,
+ "repetition_rounding": 1,
+ "weight_unit": 1,
+ "weight_rounding": 1.25,
+ "order": 1,
+ "comment": "",
+ "type": "normal",
+ "class_name": null
+ }
+ },
+ {
+ "pk": 3,
+ "model": "manager.slotconfig",
+ "fields": {
+ "slot_id": 2,
+ "exercise_id": 3,
+ "repetition_unit": 1,
+ "repetition_rounding": 1,
+ "weight_unit": 1,
+ "weight_rounding": 1.25,
+ "order": 1,
+ "comment": "",
+ "type": "normal",
+ "class_name": null
+ }
+ },
+ {
+ "pk": 4,
+ "model": "manager.slotconfig",
+ "fields": {
+ "slot_id": 3,
+ "exercise_id": 3,
+ "repetition_unit": 1,
+ "repetition_rounding": 1,
+ "weight_unit": 1,
+ "weight_rounding": 1.25,
+ "order": 1,
+ "comment": "",
+ "type": "normal",
+ "class_name": null
+ }
+ },
+ {
+ "pk": 1,
+ "model": "manager.setsconfig",
+ "fields": {
+ "slot_config_id": 1,
+ "iteration": 1,
+ "trigger": "session",
+ "value": 5,
+ "operation": null,
+ "step": null,
+ "replace": true,
+ "need_log_to_apply": false
+ }
+ },
+ {
+ "pk": 2,
+ "model": "manager.setsconfig",
+ "fields": {
+ "slot_config_id": 2,
+ "iteration": 1,
+ "trigger": "session",
+ "value": 2,
+ "operation": null,
+ "step": null,
+ "replace": true,
+ "need_log_to_apply": false
+ }
+ },
+ {
+ "pk": 3,
+ "model": "manager.setsconfig",
+ "fields": {
+ "slot_config_id": 3,
+ "iteration": 1,
+ "trigger": "session",
+ "value": 5,
+ "operation": null,
+ "step": null,
+ "replace": true,
+ "need_log_to_apply": false
+ }
+ },
+ {
+ "pk": 4,
+ "model": "manager.setsconfig",
+ "fields": {
+ "slot_config_id": 4,
+ "iteration": 1,
+ "trigger": "session",
+ "value": 3,
+ "operation": null,
+ "step": null,
+ "replace": true,
+ "need_log_to_apply": false
+ }
+ },
+ {
+ "pk": 1,
+ "model": "manager.weightconfig",
+ "fields": {
+ "slot_config_id": 1,
+ "iteration": 1,
+ "trigger": "session",
+ "value": 80,
+ "operation": null,
+ "step": null,
+ "replace": true,
+ "need_log_to_apply": false
+ }
+ },
+ {
+ "pk": 2,
+ "model": "manager.weightconfig",
+ "fields": {
+ "slot_config_id": 2,
+ "iteration": 1,
+ "trigger": "session",
+ "value": 20,
+ "operation": null,
+ "step": null,
+ "replace": true,
+ "need_log_to_apply": false
+ }
+ },
+ {
+ "pk": 3,
+ "model": "manager.weightconfig",
+ "fields": {
+ "slot_config_id": 3,
+ "iteration": 1,
+ "trigger": "session",
+ "value": 80,
+ "operation": null,
+ "step": null,
+ "replace": true,
+ "need_log_to_apply": false
+ }
+ },
+ {
+ "pk": 4,
+ "model": "manager.weightconfig",
+ "fields": {
+ "slot_config_id": 4,
+ "iteration": 1,
+ "trigger": "session",
+ "value": 50,
+ "operation": null,
+ "step": null,
+ "replace": true,
+ "need_log_to_apply": false
+ }
+ },
+ {
+ "pk": 1,
+ "model": "manager.repsconfig",
+ "fields": {
+ "slot_config_id": 1,
+ "iteration": 1,
+ "trigger": "session",
+ "value": 4,
+ "operation": null,
+ "step": null,
+ "replace": true,
+ "need_log_to_apply": false
+ }
+ },
+ {
+ "pk": 2,
+ "model": "manager.repsconfig",
+ "fields": {
+ "slot_config_id": 2,
+ "iteration": 1,
+ "trigger": "session",
+ "value": 5,
+ "operation": null,
+ "step": null,
+ "replace": true,
+ "need_log_to_apply": false
+ }
+ },
+ {
+ "pk": 3,
+ "model": "manager.repsconfig",
+ "fields": {
+ "slot_config_id": 3,
+ "iteration": 1,
+ "trigger": "session",
+ "value": 6,
+ "operation": null,
+ "step": null,
+ "replace": true,
+ "need_log_to_apply": false
+ }
+ },
+ {
+ "pk": 4,
+ "model": "manager.repsconfig",
+ "fields": {
+ "slot_config_id": 4,
+ "iteration": 1,
+ "trigger": "session",
+ "value": 4,
+ "operation": null,
+ "step": null,
+ "replace": true,
+ "need_log_to_apply": false
+ }
}
]
diff --git a/wger/manager/models/day.py b/wger/manager/models/day.py
index 111df7d06..b50a2349a 100644
--- a/wger/manager/models/day.py
+++ b/wger/manager/models/day.py
@@ -127,7 +127,7 @@ class DayNg(models.Model):
return False
- def get_slots(self, iteration: int) -> List[SlotData]:
+ def get_slots_gym_mode(self, iteration: int) -> List[SlotData]:
"""
Return the sets for this day
"""
@@ -136,7 +136,7 @@ class DayNg(models.Model):
for s in self.slots.all()
]
- def get_slots_display(self, iteration: int) -> List[SlotData]:
+ def get_slots_display_mode(self, iteration: int) -> List[SlotData]:
"""
Return the sets for this day.
@@ -168,9 +168,11 @@ class DayNg(models.Model):
)
)
current_slot = None
+ elif not slot.get_exercises():
+ continue
else:
exercise_id = slot.get_exercises()[0]
- if slot.get_exercises()[0] != last_exercise_id:
+ if exercise_id != last_exercise_id:
current_slot = SlotData(
comment=slot.comment,
exercises=[exercise_id],
diff --git a/wger/manager/tests/test_day_ng.py b/wger/manager/tests/test_day_ng.py
new file mode 100644
index 000000000..7661e8e48
--- /dev/null
+++ b/wger/manager/tests/test_day_ng.py
@@ -0,0 +1,135 @@
+# This file is part of wger Workout Manager.
+#
+# wger Workout Manager is free software: you can redistribute it and/or modify
+# it under the terms of the GNU Affero General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# wger Workout Manager is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU Affero General Public License
+
+# wger
+from wger.core.tests.base_testcase import WgerTestCase
+from wger.manager.dataclasses import SetConfigData
+from wger.manager.models import DayNg
+
+
+class DaySlotTestCase(WgerTestCase):
+ """
+ Test that the correct slots are calculated
+ """
+
+ def test_slots_gym_mode(self):
+ """
+ Test that the correct slots are returned - gym mode
+ """
+
+ day = DayNg.objects.get(pk=1)
+ slots = day.get_slots_gym_mode(1)
+ set_configs = slots[0].sets
+
+ config_1 = SetConfigData(
+ slot_config_id=1,
+ exercise=1,
+ weight=80,
+ reps=4,
+ rir=None,
+ rest=None,
+ sets=1,
+ )
+ config_2 = SetConfigData(
+ slot_config_id=2,
+ exercise=2,
+ weight=20,
+ reps=5,
+ rir=None,
+ rest=None,
+ sets=1,
+ )
+
+ # Sets are returned interleaved from the supersets
+ self.assertEqual(len(set_configs), 7)
+ self.assertEqual(set_configs[0], config_1)
+ self.assertEqual(set_configs[1], config_2)
+ self.assertEqual(set_configs[2], config_1)
+ self.assertEqual(set_configs[3], config_2)
+ self.assertEqual(set_configs[4], config_1)
+ self.assertEqual(set_configs[5], config_1)
+ self.assertEqual(set_configs[6], config_1)
+
+ def test_slots_display_mode(self):
+ """
+ Test that the correct slots are returned - display mode
+ """
+
+ day = DayNg.objects.get(pk=1)
+ slots = day.get_slots_display_mode(1)
+
+ slot1 = slots[0]
+ set_configs_1 = slot1.sets
+
+ slot2 = slots[1]
+ set_configs_2 = slot2.sets
+
+ # Exercises in superset are returned individually, not interleaved
+ self.assertEqual(slot1.comment, 'test comment 123')
+ self.assertEqual(slot1.exercises, [1, 2])
+ self.assertEqual(len(set_configs_1), 2)
+ self.assertEqual(
+ set_configs_1[0],
+ SetConfigData(
+ slot_config_id=1,
+ exercise=1,
+ weight=80,
+ reps=4,
+ rir=None,
+ rest=None,
+ sets=5,
+ ),
+ )
+ self.assertEqual(
+ set_configs_1[1],
+ SetConfigData(
+ slot_config_id=2,
+ exercise=2,
+ weight=20,
+ reps=5,
+ rir=None,
+ rest=None,
+ sets=2,
+ ),
+ )
+
+ # If there are consecutive slots with the same exercise, group them
+ self.assertEqual(slot2.comment, 'test comment 456')
+ self.assertEqual(slot2.exercises, [3])
+ self.assertEqual(len(set_configs_2), 2)
+ self.assertEqual(
+ set_configs_2[0],
+ SetConfigData(
+ slot_config_id=3,
+ exercise=3,
+ weight=80,
+ reps=6,
+ rir=None,
+ rest=None,
+ sets=5,
+ ),
+ )
+
+ self.assertEqual(
+ set_configs_2[1],
+ SetConfigData(
+ slot_config_id=4,
+ exercise=3,
+ weight=50,
+ reps=4,
+ rir=None,
+ rest=None,
+ sets=3,
+ ),
+ )