Skip to content

armgit / wger

Public Imported from GitHub
0

Self hosted FLOSS fitness/workout, nutrition and weight tracker

Add simple tests for the ingredient download tasks

Roland Geider5d12926a3385
diff --git a/wger/core/tests/base_testcase.py b/wger/core/tests/base_testcase.py
index d84360891..80f99299b 100644
--- a/wger/core/tests/base_testcase.py
+++ b/wger/core/tests/base_testcase.py
@@ -156,6 +156,9 @@ class BaseTestCase():
# https://django-axes.readthedocs.io/en/latest/3_usage.html#authenticating-users
settings.AXES_ENABLED = False
+ settings.WGER_SETTINGS['DOWNLOAD_FROM_OFF'] = False
+ settings.WGER_SETTINGS['USE_CELERY'] = False
+
def tearDown(self):
"""
Reset settings
diff --git a/wger/nutrition/fixtures/test-ingredients.json b/wger/nutrition/fixtures/test-ingredients.json
index e92594fce..a08efccd6 100644
--- a/wger/nutrition/fixtures/test-ingredients.json
+++ b/wger/nutrition/fixtures/test-ingredients.json
@@ -1,333 +1,416 @@
[
- {
- "pk": 1,
- "model": "nutrition.ingredient",
- "fields": {
- "status": "2",
- "license_author": "wger test",
- "update_date": "2013-02-12",
- "creation_date": "2013-01-01",
- "name": "Test ingredient 1",
- "language": 2,
- "sodium": "0.549",
- "energy": 176,
- "fat": "8.19",
- "carbohydrates_sugar": "0.0",
- "fat_saturated": "3.244",
- "fibres": "0.0",
- "protein": "25.63",
- "carbohydrates": "0.1251"
- }
- },
- {
- "pk": 2,
- "model": "nutrition.ingredient",
- "fields": {
- "status": "2",
- "license_author": "wger test",
- "name": "Ingredient, test, 2, organic, raw",
- "update_date": "2013-02-12",
- "creation_date": "2013-01-01",
- "language": 2,
- "sodium": "0.1",
- "energy": 300,
- "fat": "9",
- "carbohydrates_sugar": "1",
- "fat_saturated": "3",
- "fibres": "5.0",
- "protein": "25.63",
- "carbohydrates": "0.1251"
- }
- },
- {
- "pk": 3,
- "model": "nutrition.ingredient",
- "fields": {
- "status": "2",
- "license_author": "wger test",
- "update_date": "2013-03-22",
- "creation_date": "2013-02-03",
- "name": "Bachelor chow, now with flavour!",
- "language": 2,
- "sodium": "0.1",
- "energy": 300,
- "fat": "9",
- "carbohydrates_sugar": "1",
- "fat_saturated": "3",
- "fibres": "5.0",
- "protein": "25.63",
- "carbohydrates": "0.1551"
- }
- },
- {
- "pk": 4,
- "model": "nutrition.ingredient",
- "fields": {
- "status": "2",
- "license_author": "wger test",
- "update_date": "2013-02-12",
- "creation_date": "2013-04-05",
- "name": "Slurm",
- "language": 2,
- "sodium": "0.1",
- "energy": 500,
- "fat": "1",
- "carbohydrates_sugar": "99",
- "fat_saturated": "0",
- "fibres": "0",
- "protein": "0",
- "carbohydrates": "99.0"
- }
- },
- {
- "pk": 5,
- "model": "nutrition.ingredient",
- "fields": {
- "status": "2",
- "license_author": "wger test",
- "update_date": "2013-02-12",
- "creation_date": "2013-04-05",
- "name": "Another tasty ingredient",
- "language": 2,
- "sodium": "2.1",
- "energy": 10,
- "fat": "1",
- "carbohydrates_sugar": "4",
- "fat_saturated": "0",
- "fibres": "0",
- "protein": "0",
- "carbohydrates": "5.0"
- }
- },
- {
- "pk": 6,
- "model": "nutrition.ingredient",
- "fields": {
- "status": "2",
- "license_author": "wger test",
- "update_date": "2013-02-12",
- "creation_date": "2013-04-05",
- "name": "Raw ingredient",
- "language": 2,
- "sodium": "2.1",
- "energy": 180,
- "fat": "2",
- "carbohydrates_sugar": "4",
- "fat_saturated": "0",
- "fibres": "20",
- "protein": "10",
- "carbohydrates": "5.0"
- }
- },
- {
- "pk": 7,
- "model": "nutrition.ingredient",
- "fields": {
- "status": "1",
- "license_author": "test",
- "update_date": "2013-02-12",
- "creation_date": "2013-04-05",
- "name": "Pending ingredient",
- "language": 2,
- "sodium": "2.1",
- "energy": 180,
- "fat": "2",
- "carbohydrates_sugar": "4",
- "fat_saturated": "0",
- "fibres": "20",
- "protein": "10",
- "carbohydrates": "5.0"
- }
- },
- {
- "pk": 2126,
- "model": "nutrition.ingredient",
- "fields": {
- "status": "2",
- "license_author": "test",
- "update_date": "2013-02-12",
- "creation_date": "2013-04-05",
- "name": "Needed for guest user",
- "language": 2,
- "sodium": "2.1",
- "energy": 180,
- "fat": "2",
- "carbohydrates_sugar": "4",
- "fat_saturated": "0",
- "fibres": "20",
- "protein": "10",
- "carbohydrates": "5.0"
- }
- },
- {
- "pk": 154,
- "model": "nutrition.ingredient",
- "fields": {
- "status": "2",
- "license_author": "test",
- "update_date": "2013-02-12",
- "creation_date": "2013-04-05",
- "name": "Needed for guest user",
- "language": 2,
- "sodium": "2.1",
- "energy": 180,
- "fat": "2",
- "carbohydrates_sugar": "4",
- "fat_saturated": "0",
- "fibres": "20",
- "protein": "10",
- "carbohydrates": "5.0"
- }
- },
- {
- "pk": 5370,
- "model": "nutrition.ingredient",
- "fields": {
- "status": "2",
- "license_author": "test",
- "update_date": "2013-02-12",
- "creation_date": "2013-04-05",
- "name": "Needed for guest user",
- "language": 2,
- "sodium": "2.1",
- "energy": 180,
- "fat": "2",
- "carbohydrates_sugar": "4",
- "fat_saturated": "0",
- "fibres": "20",
- "protein": "10",
- "carbohydrates": "5.0"
- }
- },
- {
- "pk": 1643,
- "model": "nutrition.ingredient",
- "fields": {
- "status": "2",
- "license_author": "test",
- "update_date": "2013-02-12",
- "creation_date": "2013-04-05",
- "name": "Needed for guest user",
- "language": 2,
- "sodium": "2.1",
- "energy": 180,
- "fat": "2",
- "carbohydrates_sugar": "4",
- "fat_saturated": "0",
- "fibres": "20",
- "protein": "10",
- "carbohydrates": "5.0"
- }
- },
- {
- "pk": 17,
- "model": "nutrition.ingredient",
- "fields": {
- "status": "2",
- "license_author": "test",
- "update_date": "2013-02-12",
- "creation_date": "2013-04-05",
- "name": "Needed for guest user",
- "language": 2,
- "sodium": "2.1",
- "energy": 180,
- "fat": "2",
- "carbohydrates_sugar": "4",
- "fat_saturated": "0",
- "fibres": "20",
- "protein": "10",
- "carbohydrates": "5.0"
- }
- },
- {
- "pk": 3208,
- "model": "nutrition.ingredient",
- "fields": {
- "status": "2",
- "license_author": "test",
- "update_date": "2013-02-12",
- "creation_date": "2013-04-05",
- "name": "Needed for guest user",
- "language": 2,
- "sodium": "2.1",
- "energy": 180,
- "fat": "2",
- "carbohydrates_sugar": "4",
- "fat_saturated": "0",
- "fibres": "20",
- "protein": "10",
- "carbohydrates": "5.0"
- }
- },
- {
- "pk": 196,
- "model": "nutrition.ingredient",
- "fields": {
- "status": "2",
- "license_author": "test",
- "update_date": "2013-02-12",
- "creation_date": "2013-04-05",
- "name": "Needed for guest user",
- "language": 2,
- "sodium": "2.1",
- "energy": 180,
- "fat": "2",
- "carbohydrates_sugar": "4",
- "fat_saturated": "0",
- "fibres": "20",
- "protein": "10",
- "carbohydrates": "5.0"
- }
- },
-
- {
- "pk": 1,
- "model": "nutrition.ingredientweightunit",
- "fields": {
- "amount": "1",
- "gram": 109,
- "unit": 4,
- "ingredient": 1
- }
- },
- {
- "pk": 2,
- "model": "nutrition.ingredientweightunit",
- "fields": {
- "amount": "0.5",
- "gram": 223,
- "unit": 5,
- "ingredient": 1
- }
- },
- {
- "pk": 3,
- "model": "nutrition.ingredientweightunit",
- "fields": {
- "amount": "1",
- "gram": 12,
- "unit": 6,
- "ingredient": 1
- }
- },
- {
- "pk": 9874,
- "model": "nutrition.ingredientweightunit",
- "fields": {
- "amount": "1",
- "gram": 12,
- "unit": 6,
- "ingredient": 1
- }
- },
- {
- "pk": 5950,
- "model": "nutrition.ingredientweightunit",
- "fields": {
- "amount": "1",
- "gram": 12,
- "unit": 6,
- "ingredient": 1
- }
+ {
+ "pk": 1,
+ "model": "nutrition.ingredient",
+ "fields": {
+ "status": "2",
+ "license_author": "wger test",
+ "update_date": "2013-02-12",
+ "creation_date": "2013-01-01",
+ "name": "Test ingredient 1",
+ "language": 2,
+ "sodium": "0.549",
+ "energy": 176,
+ "fat": "8.19",
+ "carbohydrates_sugar": "0.0",
+ "fat_saturated": "3.244",
+ "fibres": "0.0",
+ "protein": "25.63",
+ "carbohydrates": "0.1251",
+ "code": "1234567890987654321",
+ "source_name": "Open Food Facts",
+ "source_url": "https://world.openfoodfacts.org/api/v0/product/5055365635003.json",
+ "last_imported": "2022-12-01T12:37:35.903Z",
+ "common_name": "foo",
+ "brand": "the food brand"
}
+ },
+ {
+ "pk": 2,
+ "model": "nutrition.ingredient",
+ "fields": {
+ "status": "2",
+ "license_author": "wger test",
+ "name": "Ingredient, test, 2, organic, raw",
+ "update_date": "2013-02-12",
+ "creation_date": "2013-01-01",
+ "language": 2,
+ "sodium": "0.1",
+ "energy": 300,
+ "fat": "9",
+ "carbohydrates_sugar": "1",
+ "fat_saturated": "3",
+ "fibres": "5.0",
+ "protein": "25.63",
+ "carbohydrates": "0.1251",
+ "code": "1234567890",
+ "source_name": "Open Food Facts",
+ "source_url": "https://world.openfoodfacts.org/api/v0/product/5055365635003.json",
+ "last_imported": "2022-12-10T12:37:35.903Z",
+ "common_name": "ing 2",
+ "brand": "the food brand"
+ }
+ },
+ {
+ "pk": 3,
+ "model": "nutrition.ingredient",
+ "fields": {
+ "status": "2",
+ "license_author": "wger test",
+ "update_date": "2013-03-22",
+ "creation_date": "2013-02-03",
+ "name": "Bachelor chow, now with flavour!",
+ "language": 2,
+ "sodium": "0.1",
+ "energy": 300,
+ "fat": "9",
+ "carbohydrates_sugar": "1",
+ "fat_saturated": "3",
+ "fibres": "5.0",
+ "protein": "25.63",
+ "carbohydrates": "0.1551",
+ "code": "1223334444",
+ "source_name": "Open Food Facts",
+ "source_url": "https://world.openfoodfacts.org/api/v0/product/5055365635003.json",
+ "last_imported": "2022-12-31T12:37:35.903Z",
+ "common_name": "the everything food",
+ "brand": "the food brand"
+ }
+ },
+ {
+ "pk": 4,
+ "model": "nutrition.ingredient",
+ "fields": {
+ "status": "2",
+ "license_author": "wger test",
+ "update_date": "2013-02-12",
+ "creation_date": "2013-04-05",
+ "name": "Slurm",
+ "language": 2,
+ "sodium": "0.1",
+ "energy": 500,
+ "fat": "1",
+ "carbohydrates_sugar": "99",
+ "fat_saturated": "0",
+ "fibres": "0",
+ "protein": "0",
+ "carbohydrates": "99.0",
+ "code": "0987654321",
+ "source_name": "Open Food Facts",
+ "source_url": "https://world.openfoodfacts.org/api/v0/product/5055365635003.json",
+ "last_imported": "2022-12-01T12:37:35.903Z",
+ "common_name": "foo",
+ "brand": "the food brand"
+ }
+ },
+ {
+ "pk": 5,
+ "model": "nutrition.ingredient",
+ "fields": {
+ "status": "2",
+ "license_author": "wger test",
+ "update_date": "2013-02-12",
+ "creation_date": "2013-04-05",
+ "name": "Another tasty ingredient",
+ "language": 2,
+ "sodium": "2.1",
+ "energy": 10,
+ "fat": "1",
+ "carbohydrates_sugar": "4",
+ "fat_saturated": "0",
+ "fibres": "0",
+ "protein": "0",
+ "carbohydrates": "5.0",
+ "code": "555555555",
+ "source_name": "Open Food Facts",
+ "source_url": "https://world.openfoodfacts.org/api/v0/product/5055365635003.json",
+ "last_imported": "2022-12-01T12:37:35.903Z",
+ "common_name": "foo",
+ "brand": "the food brand"
+ }
+ },
+ {
+ "pk": 6,
+ "model": "nutrition.ingredient",
+ "fields": {
+ "status": "2",
+ "license_author": "wger test",
+ "update_date": "2013-02-12",
+ "creation_date": "2013-04-05",
+ "name": "Raw ingredient",
+ "language": 2,
+ "sodium": "2.1",
+ "energy": 180,
+ "fat": "2",
+ "carbohydrates_sugar": "4",
+ "fat_saturated": "0",
+ "fibres": "20",
+ "protein": "10",
+ "carbohydrates": "5.0",
+ "code": "1112222333333334444444",
+ "source_name": "Open Food Facts",
+ "source_url": "https://world.openfoodfacts.org/api/v0/product/5055365635003.json",
+ "last_imported": "2022-12-01T12:37:35.903Z",
+ "common_name": "foo",
+ "brand": "the food brand"
+ }
+ },
+ {
+ "pk": 7,
+ "model": "nutrition.ingredient",
+ "fields": {
+ "status": "1",
+ "license_author": "test",
+ "update_date": "2013-02-12",
+ "creation_date": "2013-04-05",
+ "name": "Pending ingredient",
+ "language": 2,
+ "sodium": "2.1",
+ "energy": 180,
+ "fat": "2",
+ "carbohydrates_sugar": "4",
+ "fat_saturated": "0",
+ "fibres": "20",
+ "protein": "10",
+ "carbohydrates": "5.0",
+ "code": "11222222333",
+ "source_name": "wger",
+ "source_url": "https://world.openfoodfacts.org/api/v0/product/5055365635003.json",
+ "last_imported": "2022-12-01T12:37:35.903Z",
+ "common_name": "foo",
+ "brand": "the food brand"
+ }
+ },
+ {
+ "pk": 2126,
+ "model": "nutrition.ingredient",
+ "fields": {
+ "status": "2",
+ "license_author": "test",
+ "update_date": "2013-02-12",
+ "creation_date": "2013-04-05",
+ "name": "Needed for guest user",
+ "language": 2,
+ "sodium": "2.1",
+ "energy": 180,
+ "fat": "2",
+ "carbohydrates_sugar": "4",
+ "fat_saturated": "0",
+ "fibres": "20",
+ "protein": "10",
+ "carbohydrates": "5.0",
+ "code": "9988877766655",
+ "source_name": "Open Food Facts",
+ "source_url": "https://world.openfoodfacts.org/api/v0/product/5055365635003.json",
+ "last_imported": "2022-12-01T12:37:35.903Z",
+ "common_name": "foo",
+ "brand": "the food brand"
+ }
+ },
+ {
+ "pk": 154,
+ "model": "nutrition.ingredient",
+ "fields": {
+ "status": "2",
+ "license_author": "test",
+ "update_date": "2013-02-12",
+ "creation_date": "2013-04-05",
+ "name": "Needed for guest user",
+ "language": 2,
+ "sodium": "2.1",
+ "energy": 180,
+ "fat": "2",
+ "carbohydrates_sugar": "4",
+ "fat_saturated": "0",
+ "fibres": "20",
+ "protein": "10",
+ "carbohydrates": "5.0",
+ "code": "123454321",
+ "source_name": "Open Food Facts",
+ "source_url": "https://world.openfoodfacts.org/api/v0/product/5055365635003.json",
+ "last_imported": "2022-12-01T12:37:35.903Z",
+ "common_name": "foo",
+ "brand": "the food brand"
+ }
+ },
+ {
+ "pk": 5370,
+ "model": "nutrition.ingredient",
+ "fields": {
+ "status": "2",
+ "license_author": "test",
+ "update_date": "2013-02-12",
+ "creation_date": "2013-04-05",
+ "name": "Needed for guest user",
+ "language": 2,
+ "sodium": "2.1",
+ "energy": 180,
+ "fat": "2",
+ "carbohydrates_sugar": "4",
+ "fat_saturated": "0",
+ "fibres": "20",
+ "protein": "10",
+ "carbohydrates": "5.0",
+ "code": "0009999888777",
+ "source_name": "Open Food Facts",
+ "source_url": "https://world.openfoodfacts.org/api/v0/product/5055365635003.json",
+ "last_imported": "2022-12-01T12:37:35.903Z",
+ "common_name": "foo",
+ "brand": "the food brand"
+ }
+ },
+ {
+ "pk": 1643,
+ "model": "nutrition.ingredient",
+ "fields": {
+ "status": "2",
+ "license_author": "test",
+ "update_date": "2013-02-12",
+ "creation_date": "2013-04-05",
+ "name": "Needed for guest user",
+ "language": 2,
+ "sodium": "2.1",
+ "energy": 180,
+ "fat": "2",
+ "carbohydrates_sugar": "4",
+ "fat_saturated": "0",
+ "fibres": "20",
+ "protein": "10",
+ "carbohydrates": "5.0",
+ "code": "7787878787878787",
+ "source_name": "Open Food Facts",
+ "source_url": "https://world.openfoodfacts.org/api/v0/product/5055365635003.json",
+ "last_imported": "2022-12-01T12:37:35.903Z",
+ "common_name": "foo",
+ "brand": "the food brand"
+ }
+ },
+ {
+ "pk": 17,
+ "model": "nutrition.ingredient",
+ "fields": {
+ "status": "2",
+ "license_author": "test",
+ "update_date": "2013-02-12",
+ "creation_date": "2013-04-05",
+ "name": "Needed for guest user",
+ "language": 2,
+ "sodium": "2.1",
+ "energy": 180,
+ "fat": "2",
+ "carbohydrates_sugar": "4",
+ "fat_saturated": "0",
+ "fibres": "20",
+ "protein": "10",
+ "carbohydrates": "5.0",
+ "code": "29292929292929",
+ "source_name": "Open Food Facts",
+ "source_url": "https://world.openfoodfacts.org/api/v0/product/5055365635003.json",
+ "last_imported": "2022-12-01T12:37:35.903Z",
+ "common_name": "foo",
+ "brand": "the food brand"
+ }
+ },
+ {
+ "pk": 3208,
+ "model": "nutrition.ingredient",
+ "fields": {
+ "status": "2",
+ "license_author": "test",
+ "update_date": "2013-02-12",
+ "creation_date": "2013-04-05",
+ "name": "Needed for guest user",
+ "language": 2,
+ "sodium": "2.1",
+ "energy": 180,
+ "fat": "2",
+ "carbohydrates_sugar": "4",
+ "fat_saturated": "0",
+ "fibres": "20",
+ "protein": "10",
+ "carbohydrates": "5.0",
+ "code": "19191919191",
+ "source_name": "Open Food Facts",
+ "source_url": "https://world.openfoodfacts.org/api/v0/product/5055365635003.json",
+ "last_imported": "2022-12-01T12:37:35.903Z",
+ "common_name": "foo",
+ "brand": "the food brand"
+ }
+ },
+ {
+ "pk": 196,
+ "model": "nutrition.ingredient",
+ "fields": {
+ "status": "2",
+ "license_author": "test",
+ "update_date": "2013-02-12",
+ "creation_date": "2013-04-05",
+ "name": "Needed for guest user",
+ "language": 2,
+ "sodium": "2.1",
+ "energy": 180,
+ "fat": "2",
+ "carbohydrates_sugar": "4",
+ "fat_saturated": "0",
+ "fibres": "20",
+ "protein": "10",
+ "carbohydrates": "5.0",
+ "code": "4444444444444",
+ "source_name": "Open Food Facts",
+ "source_url": "https://world.openfoodfacts.org/api/v0/product/5055365635003.json",
+ "last_imported": "2022-12-01T12:37:35.903Z",
+ "common_name": "foo",
+ "brand": "the food brand"
+ }
+ },
+ {
+ "pk": 1,
+ "model": "nutrition.ingredientweightunit",
+ "fields": {
+ "amount": "1",
+ "gram": 109,
+ "unit": 4,
+ "ingredient": 1
+ }
+ },
+ {
+ "pk": 2,
+ "model": "nutrition.ingredientweightunit",
+ "fields": {
+ "amount": "0.5",
+ "gram": 223,
+ "unit": 5,
+ "ingredient": 1
+ }
+ },
+ {
+ "pk": 3,
+ "model": "nutrition.ingredientweightunit",
+ "fields": {
+ "amount": "1",
+ "gram": 12,
+ "unit": 6,
+ "ingredient": 1
+ }
+ },
+ {
+ "pk": 9874,
+ "model": "nutrition.ingredientweightunit",
+ "fields": {
+ "amount": "1",
+ "gram": 12,
+ "unit": 6,
+ "ingredient": 1
+ }
+ },
+ {
+ "pk": 5950,
+ "model": "nutrition.ingredientweightunit",
+ "fields": {
+ "amount": "1",
+ "gram": 12,
+ "unit": 6,
+ "ingredient": 1
+ }
+ }
]
diff --git a/wger/nutrition/tasks.py b/wger/nutrition/tasks.py
index 4afacfd08..a71d2a5af 100644
--- a/wger/nutrition/tasks.py
+++ b/wger/nutrition/tasks.py
@@ -47,6 +47,7 @@ def fetch_ingredient_image(pk: int):
def fetch_ingredient_image_function(pk: int):
# wger
from wger.nutrition.models import Ingredient
+
ingredient = Ingredient.objects.get(pk=pk)
logger.info(f'Fetching image for ingredient {pk}')
@@ -57,6 +58,9 @@ def fetch_ingredient_image_function(pk: int):
if ingredient.source_name != Source.OPEN_FOOD_FACTS.value:
return
+ if not ingredient.source_url:
+ return
+
if not settings.WGER_SETTINGS['DOWNLOAD_FROM_OFF']:
return
@@ -89,7 +93,8 @@ def fetch_ingredient_image_function(pk: int):
try:
image_id: str = image_data[image_name]['imgid']
uploader_name: str = image_data[image_id]['uploader']
- except KeyError:
+ except KeyError as e:
+ logger.info('could not load all image information, skipping...', e)
return
# Save to DB
diff --git a/wger/nutrition/tests/test_tasks.py b/wger/nutrition/tests/test_tasks.py
new file mode 100644
index 000000000..be00e0e9d
--- /dev/null
+++ b/wger/nutrition/tests/test_tasks.py
@@ -0,0 +1,129 @@
+# 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
+# along with Workout Manager. If not, see <http://www.gnu.org/licenses/>.
+# Standard Library
+
+# Standard Library
+from unittest.mock import (
+ MagicMock,
+ patch,
+)
+
+# wger
+from wger.core.tests.base_testcase import WgerTestCase
+from wger.nutrition.models import Ingredient
+from wger.nutrition.tasks import (
+ fetch_ingredient_image_function,
+ logger,
+)
+from wger.utils.requests import wger_user_agent
+
+
+loggerMock = MagicMock()
+
+
+class MockResponse:
+
+ def __init__(self):
+ self.status_code = 200
+ self.content = b'2000'
+
+ @staticmethod
+ def json():
+ return {
+ "product": {
+ 'image_front_url':
+ 'https://images.openfoodfacts.org/images/products/00975957/front_en.5.400.jpg',
+ 'images': {
+ 'front_en': {
+ 'imgid': '12345',
+ },
+ '12345': {
+ 'uploader': 'Mr Foobar'
+ }
+ }
+ },
+ }
+
+
+class FetchIngredientImageTestCase(WgerTestCase):
+ """
+ Test fetching an ingredient image
+ """
+
+ def test_source(self):
+ """
+ Test that sources other than OFF are ignored
+ """
+ ingredient = Ingredient.objects.get(pk=1)
+ ingredient.source_name = 'blabla'
+ ingredient.save()
+
+ result = fetch_ingredient_image_function(1)
+ self.assertEqual(result, None)
+
+ def test_download_off_setting(self):
+ """
+ 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_FROM_OFF': False}):
+ result = fetch_ingredient_image_function(1)
+ self.assertEqual(result, None)
+
+ @patch('requests.get', return_value=MockResponse())
+ @patch.object(logger, 'info')
+ def test_download_ingredient(self, mock_logger, mock_request):
+ """
+ Test that the image is correctly downloaded
+
+ While this way of testing is fragile and depends on what exactly (and when)
+ things are logged, it seems to work. Also, directly mocking the logger
+ object could probably be done better
+ """
+
+ with self.settings(WGER_SETTINGS={'DOWNLOAD_FROM_OFF': True}, TESTING=False):
+ result = fetch_ingredient_image_function(1)
+
+ log1 = mock_logger.mock_calls[0]
+ log2 = mock_logger.mock_calls[1]
+ log3 = mock_logger.mock_calls[2]
+ self.assertTrue(log1.called_with('Fetching image for ingredient 1'))
+ self.assertTrue(
+ log2.called_with(
+ 'Trying to fetch image from OFF for Test ingredient 1 (UUID: '
+ 'ed608788-811d-4d13-8854-6c42ba545d00)'
+ )
+ )
+ self.assertTrue(log3.called_with('Image successfully saved'))
+
+ request1 = mock_request.mock_calls[0]
+ request2 = mock_request.mock_calls[1]
+ self.assertTrue(
+ request1.called_with(
+ 'https://world.openfoodfacts.org/api/v0/product/5055365635003.json',
+ headers={'User-agent': wger_user_agent()}
+ )
+ )
+ self.assertTrue(
+ request2.called_with(
+ 'https://images.openfoodfacts.org/images/products/00975957/front_en.5.400.jpg',
+ headers={'User-agent': wger_user_agent()}
+ )
+ )
+
+ self.assertEqual(result, None)