Skip to content

armgit / wger

Public Imported from GitHub
0

Self hosted FLOSS fitness/workout, nutrition and weight tracker

fix: barcode fetch result was never returned

Anja Klipic8483c7c566cc
diff --git a/wger/nutrition/api/filtersets.py b/wger/nutrition/api/filtersets.py
index 4d86ad6fd..90b14c9cc 100644
--- a/wger/nutrition/api/filtersets.py
+++ b/wger/nutrition/api/filtersets.py
@@ -61,12 +61,14 @@ class IngredientFilterSet(filters.FilterSet):
if not value:
return queryset
- queryset = queryset.filter(code=value)
- if queryset.count() == 0:
+ result = queryset.filter(code=value)
+ if not result.exists():
logger.debug('barcode not found locally, trying to fetch ingredient from OFF')
- Ingredient.fetch_ingredient_from_off(value)
+ ingredient = Ingredient.fetch_ingredient_from_off(value)
+ if ingredient is not None:
+ result = queryset.filter(pk=ingredient.pk)
- return queryset
+ return result
def search_name_fulltext(self, queryset, name, value):
"""
diff --git a/wger/nutrition/tests/test_search_api.py b/wger/nutrition/tests/test_search_api.py
index 105354c45..d0200dd0d 100644
--- a/wger/nutrition/tests/test_search_api.py
+++ b/wger/nutrition/tests/test_search_api.py
@@ -13,12 +13,16 @@
# 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
+from unittest.mock import patch
+
# Third Party
from rest_framework import status
# wger
from wger.core.tests.api_base_test import ApiBaseTestCase
from wger.core.tests.base_testcase import BaseTestCase
+from wger.nutrition.models import Ingredient
class SearchIngredientApiTestCase(BaseTestCase, ApiBaseTestCase):
@@ -150,3 +154,40 @@ class SearchIngredientApiTestCase(BaseTestCase, ApiBaseTestCase):
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(response.data['count'], 6)
+
+ def test_barcode_found_locally(self):
+ """
+ A barcode already in the database is returned without calling OFF
+ """
+ response = self.client.get(self.url + '?code=1234567890987654321')
+
+ self.assertEqual(response.status_code, status.HTTP_200_OK)
+ self.assertEqual(response.data['count'], 1)
+ self.assertEqual(response.data['results'][0]['id'], 1)
+
+ @patch('wger.nutrition.api.filtersets.Ingredient.fetch_ingredient_from_off')
+ def test_barcode_not_found_locally_off_succeeds(self, mock_fetch):
+ """
+ A barcode absent locally is fetched from OFF and the created ingredient is returned
+ """
+ mock_fetch.return_value = Ingredient.objects.get(pk=2)
+
+ response = self.client.get(self.url + '?code=0000000000000')
+
+ mock_fetch.assert_called_once_with('0000000000000')
+ self.assertEqual(response.status_code, status.HTTP_200_OK)
+ self.assertEqual(response.data['count'], 1)
+ self.assertEqual(response.data['results'][0]['id'], 2)
+
+ @patch('wger.nutrition.api.filtersets.Ingredient.fetch_ingredient_from_off')
+ def test_barcode_not_found_locally_off_returns_none(self, mock_fetch):
+ """
+ A barcode unknown on OFF returns an empty result
+ """
+ mock_fetch.return_value = None
+
+ response = self.client.get(self.url + '?code=0000000000000')
+
+ mock_fetch.assert_called_once_with('0000000000000')
+ self.assertEqual(response.status_code, status.HTTP_200_OK)
+ self.assertEqual(response.data['count'], 0)