Skip to content

armgit / wger

Public Imported from GitHub
0

Self hosted FLOSS fitness/workout, nutrition and weight tracker

Fix the other ingredient image tests

Roland Geider672f48cfe6d1
diff --git a/wger/core/tests/base_testcase.py b/wger/core/tests/base_testcase.py
index 7facae03b..8d962882e 100644
--- a/wger/core/tests/base_testcase.py
+++ b/wger/core/tests/base_testcase.py
@@ -33,7 +33,6 @@ from django.utils.translation import activate
# wger
from wger.utils.constants import TWOPLACES
-
STATUS_CODES_FAIL = (302, 403, 404)
@@ -76,7 +75,6 @@ def delete_testcase_add_methods(cls):
"""
for user in get_user_list(cls.user_fail):
-
def test_unauthorized(self):
self.user_login(user)
self.delete_object(fail=False)
@@ -84,7 +82,6 @@ def delete_testcase_add_methods(cls):
setattr(cls, f'test_unauthorized_{user}', test_unauthorized)
for user in get_user_list(cls.user_success):
-
def test_authorized(self):
self.user_login(user)
self.delete_object(fail=False)
@@ -161,6 +158,7 @@ class BaseTestCase():
settings.WGER_SETTINGS['DOWNLOAD_INGREDIENT_IMAGES'] = False
settings.WGER_SETTINGS['USE_CELERY'] = False
+ settings.WGER_SETTINGS['WGER_INSTANCE'] = 'http://localhost:8000'
def tearDown(self):
"""
diff --git a/wger/nutrition/sync.py b/wger/nutrition/sync.py
index 3f6fcfcba..b1d9bbebf 100644
--- a/wger/nutrition/sync.py
+++ b/wger/nutrition/sync.py
@@ -36,7 +36,6 @@ from wger.utils.constants import (
)
from wger.utils.requests import wger_headers
-
logger = logging.getLogger(__name__)
IMAGE_API = "{0}/api/v2/ingredient-image/"
@@ -47,7 +46,6 @@ def fetch_ingredient_image(pk: int):
from wger.nutrition.models import Ingredient
ingredient = Ingredient.objects.get(pk=pk)
- logger.info(f'Fetching image for ingredient {pk}')
if hasattr(ingredient, 'image'):
return
@@ -61,6 +59,7 @@ def fetch_ingredient_image(pk: int):
if settings.TESTING:
return
+ logger.info(f'Fetching image for ingredient {pk}')
if settings.WGER_SETTINGS['DOWNLOAD_INGREDIENT_IMAGES'] == DOWNLOAD_INGREDIENT_OFF:
fetch_image_from_off(ingredient)
elif settings.WGER_SETTINGS['DOWNLOAD_INGREDIENT_IMAGES'] == DOWNLOAD_INGREDIENT_WGER:
@@ -68,7 +67,7 @@ def fetch_ingredient_image(pk: int):
def fetch_image_from_wger_instance(ingredient):
- url = f"{settings.WGER_SETTINGS['WGER_INSTANCE']}/api/v2/ingredient-image/{ingredient.pk}"
+ url = f"{settings.WGER_SETTINGS['WGER_INSTANCE']}/api/v2/ingredient-image/{ingredient.pk}/"
logger.info(f'Trying to fetch image from WGER for {ingredient.name} (UUID: {ingredient.uuid})')
result = requests.get(url, headers=wger_headers()).json()
image_uuid = result['uuid']
diff --git a/wger/nutrition/tests/test_tasks.py b/wger/nutrition/tests/test_tasks.py
index f87c3fc16..f5d9d17fa 100644
--- a/wger/nutrition/tests/test_tasks.py
+++ b/wger/nutrition/tests/test_tasks.py
@@ -42,7 +42,7 @@ class MockOffResponse:
return {
"product": {
'image_front_url':
- 'https://images.openfoodfacts.org/images/products/00975957/front_en.5.400.jpg',
+ 'https://images.openfoodfacts.org/images/products/00975957/front_en.5.400.jpg',
'images': {
'front_en': {
'imgid': '12345',
@@ -68,8 +68,8 @@ class MockWgerApiResponse:
"uuid": "188324b5-587f-42d7-9abc-d2ca64c73d45",
"ingredient_id": "12345",
"ingredient_uuid": "e9baa8bd-84fc-4756-8d90-5b9739b06cf8",
- "image":
- "http://localhost:8000/media/ingredients/e9baa8bd-84fc-4756-8d90-5b9739b06cf8/188324b5-587f-42d7-9abc-d2ca64c73d45.jpg",
+ "image": "http://localhost:8000/media/ingredients/e9baa8bd-84fc-4756-8d90-5b9739b06cf8"
+ "/188324b5-587f-42d7-9abc-d2ca64c73d45.jpg",
"last_update": "2023-03-15T23:20:10.969369+01:00",
"size": 20179,
"source_url": "",
@@ -83,7 +83,9 @@ class FetchIngredientImageTestCase(WgerTestCase):
Test fetching an ingredient image
"""
- def test_source(self):
+ @patch('requests.get')
+ @patch.object(logger, 'info')
+ def test_source(self, mock_logger, mock_request):
"""
Test that sources other than OFF are ignored
"""
@@ -91,10 +93,15 @@ class FetchIngredientImageTestCase(WgerTestCase):
ingredient.source_name = 'blabla'
ingredient.save()
- result = fetch_ingredient_image(1)
- self.assertEqual(result, None)
+ with self.settings(WGER_SETTINGS={'DOWNLOAD_INGREDIENT_IMAGES': True}):
+ result = fetch_ingredient_image(1)
+ mock_logger.assert_not_called()
+ mock_request.assert_not_called()
+ self.assertEqual(result, None)
- def test_download_off_setting(self):
+ @patch('requests.get')
+ @patch.object(logger, 'info')
+ def test_download_off_setting(self, mock_logger, mock_request):
"""
Test that no images are fetched if the appropriate setting is not set
"""
@@ -104,6 +111,8 @@ class FetchIngredientImageTestCase(WgerTestCase):
with self.settings(WGER_SETTINGS={'DOWNLOAD_INGREDIENT_IMAGES': False}):
result = fetch_ingredient_image(1)
+ mock_logger.assert_not_called()
+ mock_request.assert_not_called()
self.assertEqual(result, None)
@patch('requests.get', return_value=MockOffResponse())
@@ -118,7 +127,8 @@ class FetchIngredientImageTestCase(WgerTestCase):
"""
with self.settings(
- WGER_SETTINGS={'DOWNLOAD_INGREDIENT_IMAGES': DOWNLOAD_INGREDIENT_OFF}, TESTING=False
+ WGER_SETTINGS={'DOWNLOAD_INGREDIENT_IMAGES': DOWNLOAD_INGREDIENT_OFF},
+ TESTING=False,
):
result = fetch_ingredient_image(1)
@@ -135,7 +145,6 @@ class FetchIngredientImageTestCase(WgerTestCase):
'https://world.openfoodfacts.org/api/v0/product/5055365635003.json',
headers=wger_headers()
)
-
mock_request.assert_any_call(
'https://images.openfoodfacts.org/images/products/00975957/front_en.5.400.jpg',
headers=wger_headers()
@@ -170,7 +179,8 @@ class FetchIngredientImageTestCase(WgerTestCase):
)
mock_request.assert_any_call(
- 'http://localhost:8000/api/v2/ingredient-image/1', headers=wger_headers()
+ 'http://localhost:8000/api/v2/ingredient-image/1/',
+ headers=wger_headers(),
)
mock_request.assert_any_call(
'http://localhost:8000/media/ingredients/e9baa8bd-84fc-4756-8d90-5b9739b06cf8'