Self hosted FLOSS fitness/workout, nutrition and weight tracker
Correctly test the correct function calls in the task tests
Roland Geider05b0d6c03522
diff --git a/wger/nutrition/fixtures/test-ingredients.json b/wger/nutrition/fixtures/test-ingredients.jsonindex a08efccd6..eb1006298 100644--- a/wger/nutrition/fixtures/test-ingredients.json+++ b/wger/nutrition/fixtures/test-ingredients.json@@ -3,6 +3,7 @@"pk": 1,"model": "nutrition.ingredient","fields": {+ "uuid": "7908c204-907f-4b1e-ad4e-f482e9769ade","status": "2","license_author": "wger test","update_date": "2013-02-12",@@ -29,6 +30,7 @@"pk": 2,"model": "nutrition.ingredient","fields": {+ "uuid": "44dc5966-73a2-4df7-8b15-f6d37a8990d9","status": "2","license_author": "wger test","name": "Ingredient, test, 2, organic, raw",@@ -55,6 +57,7 @@"pk": 3,"model": "nutrition.ingredient","fields": {+ "uuid": "f28c8a68-ecda-42db-a9c4-781ee2aee472","status": "2","license_author": "wger test","update_date": "2013-03-22",@@ -81,6 +84,7 @@"pk": 4,"model": "nutrition.ingredient","fields": {+ "uuid": "fdd662aa-696c-46c6-acae-b192ccc25f11","status": "2","license_author": "wger test","update_date": "2013-02-12",@@ -107,6 +111,7 @@"pk": 5,"model": "nutrition.ingredient","fields": {+ "uuid": "293957df-b5bb-4ca6-b883-2234185c5372","status": "2","license_author": "wger test","update_date": "2013-02-12",@@ -133,6 +138,7 @@"pk": 6,"model": "nutrition.ingredient","fields": {+ "uuid": "dfc5c622-027b-4f17-8141-dadd1ce7e3f1","status": "2","license_author": "wger test","update_date": "2013-02-12",@@ -159,6 +165,7 @@"pk": 7,"model": "nutrition.ingredient","fields": {+ "uuid": "42a41805-2caf-4c7e-9ff3-decbfee5f695","status": "1","license_author": "test","update_date": "2013-02-12",@@ -185,6 +192,7 @@"pk": 2126,"model": "nutrition.ingredient","fields": {+ "uuid": "db9e0502-2a54-472d-97e2-3761d1b286ee","status": "2","license_author": "test","update_date": "2013-02-12",@@ -211,6 +219,7 @@"pk": 154,"model": "nutrition.ingredient","fields": {+ "uuid": "7164629a-9f58-450b-8316-ad7954564171","status": "2","license_author": "test","update_date": "2013-02-12",@@ -237,6 +246,7 @@"pk": 5370,"model": "nutrition.ingredient","fields": {+ "uuid": "9d53cf0e-5100-480f-a320-2de51c70603b","status": "2","license_author": "test","update_date": "2013-02-12",@@ -263,6 +273,7 @@"pk": 1643,"model": "nutrition.ingredient","fields": {+ "uuid": "0187406a-7290-4199-a4dc-ccceb8cb55ea","status": "2","license_author": "test","update_date": "2013-02-12",@@ -289,6 +300,7 @@"pk": 17,"model": "nutrition.ingredient","fields": {+ "uuid": "16ade127-00b0-4bcf-906e-dd9730f5ade1","status": "2","license_author": "test","update_date": "2013-02-12",@@ -315,6 +327,7 @@"pk": 3208,"model": "nutrition.ingredient","fields": {+ "uuid": "59a73b2c-ead1-4001-b6a0-355c9cb366a7","status": "2","license_author": "test","update_date": "2013-02-12",@@ -341,6 +354,7 @@"pk": 196,"model": "nutrition.ingredient","fields": {+ "uuid": "a18665d0-24e3-4255-88aa-92f2831d568a","status": "2","license_author": "test","update_date": "2013-02-12",diff --git a/wger/nutrition/sync.py b/wger/nutrition/sync.pyindex 139a6b3e6..3f6fcfcba 100644--- a/wger/nutrition/sync.py+++ b/wger/nutrition/sync.py@@ -69,6 +69,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}"+ 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']try:diff --git a/wger/nutrition/tests/test_tasks.py b/wger/nutrition/tests/test_tasks.pyindex 812bc5be5..f87c3fc16 100644--- a/wger/nutrition/tests/test_tasks.py+++ b/wger/nutrition/tests/test_tasks.py@@ -15,10 +15,7 @@# Standard Library# Standard Library-from unittest.mock import (- MagicMock,- patch,-)+from unittest.mock import patch# wgerfrom wger.core.tests.base_testcase import WgerTestCase@@ -27,13 +24,13 @@ from wger.nutrition.sync import (fetch_ingredient_image,logger,)-from wger.utils.constants import DOWNLOAD_INGREDIENT_OFF+from wger.utils.constants import (+ DOWNLOAD_INGREDIENT_OFF,+ DOWNLOAD_INGREDIENT_WGER,+)from wger.utils.requests import wger_headers-loggerMock = MagicMock()--class MockOffResponse:def __init__(self):@@ -67,18 +64,17 @@ class MockWgerApiResponse:@staticmethoddef 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'- }- }- },+ "id": 1,+ "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",+ "last_update": "2023-03-15T23:20:10.969369+01:00",+ "size": 20179,+ "source_url": "",+ "license": 1,+ "license_author": "Tester McTest"}@@ -112,7 +108,7 @@ class FetchIngredientImageTestCase(WgerTestCase):@patch('requests.get', return_value=MockOffResponse())@patch.object(logger, 'info')- def test_download_ingredient(self, mock_logger, mock_request):+ def test_download_ingredient_off(self, mock_logger, mock_request):"""Test that the image is correctly downloaded@@ -126,31 +122,60 @@ class FetchIngredientImageTestCase(WgerTestCase):):result = fetch_ingredient_image(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)'- )+ # log1 = mock_logger.mock_calls[0]+ # print(log1)+ mock_logger.assert_any_call('Fetching image for ingredient 1')+ mock_logger.assert_any_call(+ 'Trying to fetch image from OFF for Test ingredient 1 (UUID: '+ '7908c204-907f-4b1e-ad4e-f482e9769ade)'+ )+ mock_logger.assert_any_call('Image successfully saved')++ mock_request.assert_any_call(+ '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()+ )++ self.assertEqual(result, None)++ @patch('requests.get', return_value=MockWgerApiResponse())+ @patch.object(logger, 'info')+ def test_download_ingredient_wger123(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_INGREDIENT_IMAGES': DOWNLOAD_INGREDIENT_WGER,+ 'WGER_INSTANCE': 'http://localhost:8000'+ },+ TESTING=False+ ):+ result = fetch_ingredient_image(1)++ mock_logger.assert_any_call('Fetching image for ingredient 1')+ mock_logger.assert_any_call(+ 'Trying to fetch image from WGER for Test ingredient 1 (UUID: '+ '7908c204-907f-4b1e-ad4e-f482e9769ade)')- 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=wger_headers()- )++ mock_request.assert_any_call(+ 'http://localhost:8000/api/v2/ingredient-image/1', headers=wger_headers())- self.assertTrue(- request2.called_with(- 'https://images.openfoodfacts.org/images/products/00975957/front_en.5.400.jpg',- headers=wger_headers()- )+ mock_request.assert_any_call(+ 'http://localhost:8000/media/ingredients/e9baa8bd-84fc-4756-8d90-5b9739b06cf8'+ '/188324b5-587f-42d7-9abc-d2ca64c73d45.jpg',+ headers=wger_headers())self.assertEqual(result, None)