Self hosted FLOSS fitness/workout, nutrition and weight tracker
Add tests for image sync and update handling in download_exercise_images
shihabce3fdf72cdb9
diff --git a/test_image_sync_example.py b/test_image_sync_example.pynew file mode 100644index 000000000..e69de29bbdiff --git a/wger/exercises/tests/test_sync.py b/wger/exercises/tests/test_sync.pyindex 0d05515df..da248e8cd 100644--- a/wger/exercises/tests/test_sync.py+++ b/wger/exercises/tests/test_sync.py@@ -25,10 +25,12 @@ from wger.exercises.models import (Equipment,Exercise,ExerciseCategory,+ ExerciseImage,Muscle,Translation,)from wger.exercises.sync import (+ download_exercise_images,handle_deleted_entries,sync_categories,sync_equipment,@@ -559,6 +561,56 @@ class MockExerciseResponse:# yapf: enable+class MockImageResponse:+ def __init__(self):+ self.status_code = 200+ self.content = b''++ # yapf: disable+ @staticmethod+ def json():+ return {+ "count": 2,+ "next": None,+ "previous": None,+ "results": [+ {+ "id": 1,+ "uuid": "existing-image-uuid-1234",+ "exercise": 1,+ "exercise_uuid": "acad3949-36fb-4481-9a72-be2ddae2bc05",+ "image": "https://wger.de/media/exercise-images/1/test.jpg",+ "is_main": True,+ "style": "2",+ "license": 2,+ "license_title": "Updated Image Title",+ "license_object_url": "https://example.com/updated",+ "license_author": "Updated Author",+ "license_author_url": "https://author.updated.com",+ "license_derivative_source_url": "https://source.updated.com",+ "author_history": ["Author 1"]+ },+ {+ "id": 2,+ "uuid": "new-image-uuid-5678",+ "exercise": 2,+ "exercise_uuid": "ae3328ba-9a35-4731-bc23-5da50720c5aa",+ "image": "https://wger.de/media/exercise-images/2/newtest.jpg",+ "is_main": False,+ "style": "4",+ "license": 1,+ "license_title": "New Image Title",+ "license_object_url": "https://example.com/new",+ "license_author": "New Author",+ "license_author_url": "https://author.new.com",+ "license_derivative_source_url": "https://source.new.com",+ "author_history": ["Author 2"]+ }+ ]+ }+ # yapf: enable++class TestSyncMethods(WgerTestCase):@patch('requests.get', return_value=MockLanguageResponse())def test_language_sync(self, mock_request):@@ -729,3 +781,50 @@ class TestSyncMethods(WgerTestCase):translation_fr = exercise.get_translation('fr')self.assertEqual(str(translation_fr.uuid), '581338a1-8e52-405b-99eb-f0724c528bc8')++ @patch('requests.get', return_value=MockImageResponse())+ def test_image_sync(self, mock_request):+ """Test that download_exercise_images updates existing images and creates new ones"""++ exercise = Exercise.objects.get(uuid='acad3949-36fb-4481-9a72-be2ddae2bc05')+ existing_image = ExerciseImage.objects.create(+ uuid='existing-image-uuid-1234',+ exercise=exercise,+ is_main=False,+ style='1',+ license_id=1,+ license_title='Old Title',+ license_author='Old Author',+ )++ initial_image_count = ExerciseImage.objects.count()++ download_exercise_images(lambda x: x)++ mock_request.assert_called()++ # Existing image was updated, not recreated+ self.assertEqual(ExerciseImage.objects.count(), initial_image_count + 1)++ updated_image = ExerciseImage.objects.get(uuid='existing-image-uuid-1234')+ self.assertEqual(updated_image.pk, existing_image.pk)+ self.assertEqual(updated_image.is_main, True)+ self.assertEqual(updated_image.style, '2')+ self.assertEqual(updated_image.license_id, 2)+ self.assertEqual(updated_image.license_title, 'Updated Image Title')+ self.assertEqual(updated_image.license_author, 'Updated Author')+ self.assertEqual(updated_image.license_object_url, 'https://example.com/updated')+ self.assertEqual(updated_image.license_author_url, 'https://author.updated.com')+ self.assertEqual(+ updated_image.license_derivative_source_url,+ 'https://source.updated.com',+ )++ # Check that a new image was created+ new_image = ExerciseImage.objects.get(uuid='new-image-uuid-5678')+ self.assertEqual(new_image.exercise_id, 2)+ self.assertEqual(new_image.is_main, False)+ self.assertEqual(new_image.style, '4')+ self.assertEqual(new_image.license_id, 1)+ self.assertEqual(new_image.license_title, 'New Image Title')+ self.assertEqual(new_image.license_author, 'New Author')