Self hosted FLOSS fitness/workout, nutrition and weight tracker
Add tests for the exercise sync function
Roland Geider4be26712557e
diff --git a/wger/core/templatetags/wger_extras.py b/wger/core/templatetags/wger_extras.pyindex 989fff42c..a5034c655 100644--- a/wger/core/templatetags/wger_extras.py+++ b/wger/core/templatetags/wger_extras.py@@ -34,6 +34,7 @@ from wger.utils.constants import ()from wger.utils.language import get_language_data+register = template.Library()diff --git a/wger/exercises/fixtures/test-exercises.json b/wger/exercises/fixtures/test-exercises.jsonindex 9a3200ccc..f5e9d187a 100644--- a/wger/exercises/fixtures/test-exercises.json+++ b/wger/exercises/fixtures/test-exercises.json@@ -45,7 +45,7 @@"pk": 2,"model": "exercises.exercisebase","fields": {- "uuid": "029a6eac67f242d9b71d9d692b0a35b0",+ "uuid": "ae3328ba-9a35-4731-bc23-5da50720c5aa","category": 2,"creation_date": "2014-11-01","update_date": "2018-11-01T00:00:00+01:00",@@ -153,7 +153,7 @@"pk": 2,"model": "exercises.exercise","fields": {- "uuid": "3275c4e6e7ba4368964b6b1a9d80ca7a",+ "uuid": "7524ca8d-032e-482d-ab18-40e8a97851f6","language": 2,"exercise_base": 2,"description": "",diff --git a/wger/exercises/models/exercise_alias.py b/wger/exercises/models/exercise_alias.pyindex fa9d6a384..b2fdad9a8 100644--- a/wger/exercises/models/exercise_alias.py+++ b/wger/exercises/models/exercise_alias.py@@ -32,7 +32,12 @@ class Alias(models.Model):"""Model for an exercise (name)alias"""- exercise = models.ForeignKey(Exercise, verbose_name=_('Exercise'), on_delete=models.CASCADE)+ exercise = models.ForeignKey(+ Exercise,+ verbose_name=_('Exercise'),+ on_delete=models.CASCADE,+ )+alias = models.CharField(max_length=200,verbose_name=_('Alias for an exercise'),diff --git a/wger/exercises/sync.py b/wger/exercises/sync.pyindex 65c1820ef..6616a433f 100644--- a/wger/exercises/sync.py+++ b/wger/exercises/sync.py@@ -11,6 +11,7 @@# GNU General Public License for more details.## You should have received a copy of the GNU Affero General Public License+# Standard Libraryimport os@@ -35,11 +36,13 @@ from wger.exercises.api.endpoints import (VIDEO_ENDPOINT,)from wger.exercises.models import (+ Alias,DeletionLog,Equipment,Exercise,ExerciseBase,ExerciseCategory,+ ExerciseComment,ExerciseImage,ExerciseVideo,Muscle,@@ -71,7 +74,7 @@ def sync_exercises(muscles = [Muscle.objects.get(pk=i['id']) for i in data['muscles']]muscles_sec = [Muscle.objects.get(pk=i['id']) for i in data['muscles_secondary']]- base, base_created = ExerciseBase.objects.get_or_create(+ base, base_created = ExerciseBase.objects.update_or_create(uuid=uuid,defaults={'category_id': category_id},)@@ -88,23 +91,27 @@ def sync_exercises(description = translation_data['description']language_id = translation_data['language']- translation, translation_created = Exercise.objects.get_or_create(+ translation, translation_created = Exercise.objects.update_or_create(uuid=trans_uuid,defaults={+ 'exercise_base': base,+ 'name': name,+ 'description': description,+ 'license_id': license_id,+ 'license_author': license_author,'language_id': language_id,- 'exercise_base': base},)out = f"- {'created' if translation_created else 'updated'} translation " \f"{translation.language.short_name} {trans_uuid} - {name}"print_fn(out)- translation.name = name- translation.description = description- translation.language_id = language_id- translation.license_id = license_id- translation.license_author = license_author- translation.save()+ for note in translation_data['notes']:+ ExerciseComment.objects.get_or_create(exercise=translation, comment=note)++ for alias in translation_data['aliases']:+ Alias.objects.get_or_create(exercise=translation, alias=alias)+print_fn('')if result['next']:diff --git a/wger/exercises/tests/test_sync.py b/wger/exercises/tests/test_sync.pyindex 97d02fcef..2578cb491 100644--- a/wger/exercises/tests/test_sync.py+++ b/wger/exercises/tests/test_sync.py@@ -29,6 +29,7 @@ from wger.exercises.sync import (delete_entries,sync_categories,sync_equipment,+ sync_exercises,sync_languages,sync_muscles,)@@ -232,6 +233,211 @@ class MockDeletionLogResponse:}+class MockExerciseResponse:++ def __init__(self):+ self.status_code = 200+ self.content = b''++ @staticmethod+ def json():+ return {+ "count": 1,+ "next": None,+ "previous": None,+ "results": [+ {+ "id": 123,+ "uuid": "1b020b3a-3732-4c7e-92fd-a0cec90ed69b",+ "creation_date": "2022-10-11",+ "category": {+ "id": 2,+ "name": "Another category"+ },+ "muscles": [+ {+ "id": 2,+ "name": "Biceps testii",+ "name_en": "Testceps",+ "is_front": True,+ "image_url_main": "/static/images/muscles/main/muscle-2.svg",+ "image_url_secondary": "/static/images/muscles/secondary/muscle-2.svg"+ }+ ],+ "muscles_secondary": [+ {+ "id": 4,+ "name": "Bigus Pectoralis",+ "name_en": "Testch",+ "is_front": True,+ "image_url_main": "/static/images/muscles/main/muscle-4.svg",+ "image_url_secondary": "/static/images/muscles/secondary/muscle-4.svg"+ }+ ],+ "equipment": [+ {+ "id": 2,+ "name": "Kettlebell"+ }+ ],+ "license": {+ "id": 2,+ "full_name": "Creative Commons Attribution Share Alike 4",+ "short_name": "CC-BY-SA 4",+ "url": "https://creativecommons.org/licenses/by-sa/4.0/deed.en"+ },+ "license_author": "Mr X",+ "images": [],+ "exercises": [+ {+ "id": 100,+ "uuid": "c788d643-150a-4ac7-97ef-84643c6419bf",+ "name": "Zweihandiges Kettlebell",+ "exercise_base": 123,+ "description": "Hier könnte Ihre Werbung stehen!",+ "creation_date": "2015-08-03",+ "language": 1,+ "aliases": [+ "Kettlebell mit zwei Händen"+ ],+ "notes": [+ "Wichtig die Übung richtig zu machen"+ ],+ "license": 2,+ "license_author": "Tester von der Testen",+ "author_history": [+ "wger Team",+ "Jürgen",+ "Tester von der Testen"+ ]+ },+ {+ "id": 101,+ "uuid": "ab4185dd-2e68-4579-af1f-0c03957c0a9e",+ "name": "2 Handed Kettlebell Swing",+ "exercise_base": 123,+ "description": "TBD",+ "creation_date": "2023-08-03",+ "language": 2,+ "aliases": [],+ "notes": [],+ "license": 2,+ "license_author": "Tester McTest",+ "author_history": [+ "Mrs Winterbottom",+ "Tester McTest"+ ]+ }+ ],+ "variations": 47,+ "videos": [],+ "author_history": [+ "Mrs Winterbottom"+ ],+ "total_authors_history": [+ "Mrs Winterbottom",+ "Tester McTest",+ "wger Team",+ "Jürgen",+ "Tester von der Testen"+ ]+ },+ {+ "id": 2,+ "uuid": "ae3328ba-9a35-4731-bc23-5da50720c5aa",+ "creation_date": "2022-10-11",+ "category": {+ "id": 3,+ "name": "Yet another category"+ },+ "muscles": [+ {+ "id": 2,+ "name": "Biceps testii",+ "name_en": "Testceps",+ "is_front": True,+ "image_url_main": "/static/images/muscles/main/muscle-2.svg",+ "image_url_secondary": "/static/images/muscles/secondary/muscle-2.svg"+ }+ ],+ "muscles_secondary": [+ {+ "id": 4,+ "name": "Bigus Pectoralis",+ "name_en": "Testch",+ "is_front": True,+ "image_url_main": "/static/images/muscles/main/muscle-4.svg",+ "image_url_secondary": "/static/images/muscles/secondary/muscle-4.svg"+ }+ ],+ "equipment": [+ {+ "id": 2,+ "name": "Kettlebell"+ }+ ],+ "license": {+ "id": 2,+ "full_name": "Creative Commons Attribution Share Alike 4",+ "short_name": "CC-BY-SA 4",+ "url": "https://creativecommons.org/licenses/by-sa/4.0/deed.en"+ },+ "license_author": "Mr X",+ "images": [],+ "exercises": [+ {+ "id": 123,+ "uuid": "7524ca8d-032e-482d-ab18-40e8a97851f6",+ "name": "A new, better, updated name",+ "exercise_base": 2,+ "description": "Two Handed Russian Style Kettlebell swing",+ "creation_date": "2015-08-03",+ "language": 1,+ "aliases": [+ "A new alias here",+ "yet another name"+ ],+ "notes": [+ "Foobar",+ ],+ "license": 2,+ "license_author": "Mr X",+ "author_history": [+ "Mr X"+ ]+ },+ {+ "id": 345,+ "uuid": "581338a1-8e52-405b-99eb-f0724c528bc8",+ "name": "Balançoire Kettlebell à 2 mains",+ "exercise_base": 2,+ "description": "Balançoire Kettlebell à deux mains de style russe",+ "creation_date": "2015-08-03",+ "language": 3,+ "aliases": [],+ "notes": [],+ "license": 2,+ "license_author": "Mr Y",+ "author_history": [+ "Mr Y",+ "Mr Z"+ ]+ }+ ],+ "variations": 47,+ "videos": [],+ "author_history": [+ "Mr X"+ ],+ "total_authors_history": [+ "Mr X",+ "Mr Z"+ ]+ },+ ]+ }++class TestSyncMethods(WgerTestCase):@patch('requests.get', return_value=MockLanguageResponse())@@ -306,3 +512,58 @@ class TestSyncMethods(WgerTestCase):self.assertEqual(Exercise.objects.count(), 8)self.assertRaises(Exercise.DoesNotExist, Exercise.objects.get, pk=3)self.assertRaises(ExerciseBase.DoesNotExist, ExerciseBase.objects.get, pk=1)++ @patch('requests.get', return_value=MockExerciseResponse())+ def test_exercise_sync(self, mock_request):+ self.assertEqual(ExerciseBase.objects.count(), 8)+ self.assertEqual(Exercise.objects.count(), 11)+ base = ExerciseBase.objects.get(uuid='ae3328ba-9a35-4731-bc23-5da50720c5aa')+ self.assertEqual(base.category_id, 2)++ sync_exercises(lambda x: x)++ mock_request.assert_called_with(+ 'https://wger.de/api/v2/exercisebaseinfo/?limit=100',+ headers=wger_headers(),+ )+ self.assertEqual(ExerciseBase.objects.count(), 9)+ self.assertEqual(Exercise.objects.count(), 14)++ # New base was created+ new_base = ExerciseBase.objects.get(uuid='1b020b3a-3732-4c7e-92fd-a0cec90ed69b')+ self.assertEqual(new_base.category_id, 2)+ self.assertEqual([e.id for e in new_base.equipment.all()], [2])+ self.assertEqual([m.id for m in new_base.muscles.all()], [2])+ self.assertEqual([m.id for m in new_base.muscles_secondary.all()], [4])++ translation_de = new_base.get_exercise('de')+ self.assertEqual(translation_de.language_id, 1)+ self.assertEqual(translation_de.name, 'Zweihandiges Kettlebell')+ self.assertEqual(translation_de.description, 'Hier könnte Ihre Werbung stehen!')+ self.assertEqual(translation_de.alias_set.first().alias, 'Kettlebell mit zwei Händen')+ self.assertEqual(+ translation_de.exercisecomment_set.first().comment,+ 'Wichtig die Übung richtig zu machen',+ )++ translation_en = new_base.get_exercise('en')+ self.assertEqual(translation_en.language_id, 2)+ self.assertEqual(translation_en.name, '2 Handed Kettlebell Swing')+ self.assertEqual(translation_en.description, 'TBD')++ # Existing base was updated+ base = ExerciseBase.objects.get(uuid='ae3328ba-9a35-4731-bc23-5da50720c5aa')+ self.assertEqual(base.category_id, 3)++ translation_de = base.get_exercise('de')+ self.assertEqual(translation_de.name, 'A new, better, updated name')+ self.assertEqual(translation_de.pk, 2)+ self.assertEqual(translation_de.alias_set.count(), 2)+ self.assertEqual(translation_de.alias_set.all()[0].alias, 'yet another name')+ self.assertEqual(translation_de.alias_set.all()[1].alias, 'A new alias here')++ self.assertEqual(translation_de.exercisecomment_set.count(), 1)+ self.assertEqual(translation_de.exercisecomment_set.first().comment, 'Foobar')++ translation_fr = base.get_exercise('fr')+ self.assertEqual(str(translation_fr.uuid), '581338a1-8e52-405b-99eb-f0724c528bc8')