Self hosted FLOSS fitness/workout, nutrition and weight tracker
Add test for fetching ingredient data from OFF
Roland Geider80742b4d0df8
diff --git a/wger/core/fixtures/test-licenses.json b/wger/core/fixtures/test-licenses.jsonindex 64585042d..fe44087a1 100644--- a/wger/core/fixtures/test-licenses.json+++ b/wger/core/fixtures/test-licenses.json@@ -16,5 +16,14 @@"full_name": "Another cool license 2.1","short_name": "ACL 2.1"}+ },+ {+ "pk": 5,+ "model": "core.license",+ "fields": {+ "url": "https://another-cool-license.org/acl-2.2",+ "full_name": "Another cool license 2.2",+ "short_name": "ACL 2.2"+ }}]diff --git a/wger/nutrition/models/ingredient.py b/wger/nutrition/models/ingredient.pyindex bda4f0681..ed36f88ea 100644--- a/wger/nutrition/models/ingredient.py+++ b/wger/nutrition/models/ingredient.py@@ -40,7 +40,7 @@ from django.utils.text import slugifyfrom django.utils.translation import gettext_lazy as _# Third Party-import openfoodfacts as off+import openfoodfacts# wgerfrom wger.core.models import Language@@ -459,9 +459,9 @@ class Ingredient(AbstractSubmissionModel, AbstractLicenseModel, models.Model):Searches OFF by barcode and creates a local ingredient from the result"""# wger- from wger.nutrition.models.off import extract_info_from_off+ from wger.nutrition.off import extract_info_from_off- result = off.products.get_product(code)+ result = openfoodfacts.products.get_product(code)if result['status'] != OFF_SEARCH_PRODUCT_FOUND:return Nonediff --git a/wger/nutrition/models/off.py b/wger/nutrition/off.pysimilarity index 100%rename from wger/nutrition/models/off.pyrename to wger/nutrition/off.pydiff --git a/wger/nutrition/tests/test_ingredient.py b/wger/nutrition/tests/test_ingredient.pyindex cff1950d3..791031209 100644--- a/wger/nutrition/tests/test_ingredient.py+++ b/wger/nutrition/tests/test_ingredient.py@@ -17,15 +17,21 @@import datetimeimport jsonfrom decimal import Decimal+from unittest.mock import patch# Djangofrom django.core.exceptions import ValidationErrorfrom django.urls import reverse+# Third Party+from rest_framework import status+# wgerfrom wger.core.models import Languagefrom wger.core.tests import api_base_test+from wger.core.tests.api_base_test import ApiBaseTestCasefrom wger.core.tests.base_testcase import (+ BaseTestCase,WgerAddTestCase,WgerDeleteTestCase,WgerEditTestCase,@@ -35,7 +41,11 @@ from wger.nutrition.models import (Ingredient,Meal,)-from wger.utils.constants import NUTRITION_TAB+from wger.utils.constants import (+ NUTRITION_TAB,+ OFF_SEARCH_PRODUCT_FOUND,+ OFF_SEARCH_PRODUCT_NOT_FOUND,+)class IngredientRepresentationTestCase(WgerTestCase):@@ -439,3 +449,94 @@ class IngredientApiTestCase(api_base_test.ApiBaseResourceTestCase):private_resource = Falseoverview_cached = Truedata = {'language': 1, 'license': 2}+++class IngredientModelTestCase(WgerTestCase):+ """+ Tests the ingredient model functions+ """++ off_response = {+ 'status': OFF_SEARCH_PRODUCT_FOUND,+ 'product': {+ 'code': '1234',+ 'lang': 'de',+ 'product_name': 'Foo with chocolate',+ 'generic_name': 'Foo with chocolate, 250g package',+ 'brands': 'The bar company',+ 'editors_tags': ['open food facts', 'MrX'],+ 'nutriments': {+ 'energy-kcal_100g': 120,+ 'proteins_100g': 10,+ 'carbohydrates_100g': 20,+ 'sugars_100g': 30,+ 'fat_100g': 40,+ 'saturated-fat_100g': 11,+ 'sodium_100g': 5,+ 'fiber_100g': None+ },+ }+ }++ off_response_no_results = {+ 'status': OFF_SEARCH_PRODUCT_NOT_FOUND,+ 'status_verbose': 'product not found'+ }++ @patch('openfoodfacts.products.get_product')+ def test_fetch_from_off_success(self, mock_get_product):+ """+ Tests creating an ingredient from OFF+ """+ mock_get_product.return_value = self.off_response++ ingredient = Ingredient.fetch_ingredient_from_off('1234')++ self.assertEqual(ingredient.name, 'Foo with chocolate')+ self.assertEqual(ingredient.code, '1234')+ self.assertEqual(ingredient.energy, 120)+ self.assertEqual(ingredient.protein, 10)+ self.assertEqual(ingredient.carbohydrates, 20)+ self.assertEqual(ingredient.fat, 40)+ self.assertEqual(ingredient.fat_saturated, 11)+ self.assertEqual(ingredient.sodium, 5)+ self.assertEqual(ingredient.fibres, None)+ self.assertEqual(ingredient.brand, 'The bar company')+ self.assertEqual(ingredient.license_author, 'open food facts, MrX')++ @patch('openfoodfacts.products.get_product')+ def test_fetch_from_off_no_results(self, mock_get_product):+ """+ Tests creating an ingredient from OFF+ """+ mock_get_product.return_value = self.off_response_no_results++ ingredient = Ingredient.fetch_ingredient_from_off('1234')+ self.assertIsNone(ingredient)+++class IngredientApiCodeSearch(BaseTestCase, ApiBaseTestCase):++ url = '/api/v2/ingredient/'++ @patch('wger.nutrition.models.Ingredient.fetch_ingredient_from_off')+ def test_search_existing_code(self, mock_fetch_from_off):+ """+ Test that when a code already exists, no off sync happens+ """+ response = self.client.get(self.url + '?code=1234567890987654321')+ mock_fetch_from_off.assert_not_called()++ self.assertEqual(response.status_code, status.HTTP_200_OK)+ self.assertEqual(response.data['count'], 1)++ @patch('wger.nutrition.models.Ingredient.fetch_ingredient_from_off')+ def test_search_new_code(self, mock_fetch_from_off):+ """+ Test that when a code isn't present, it will be fetched+ """+ response = self.client.get(self.url + '?code=122333444455555666666')+ mock_fetch_from_off.assert_called_with('122333444455555666666')++ self.assertEqual(response.status_code, status.HTTP_200_OK)+ self.assertEqual(response.data['count'], 0)