Skip to content

armgit / wger

Public Imported from GitHub
0

Self hosted FLOSS fitness/workout, nutrition and weight tracker

Fix tests, format code

Roland Geider22d62773a127
diff --git a/wger/nutrition/extract_info/off.py b/wger/nutrition/extract_info/off.py
index 76fcbc315..cd4cfa4dd 100644
--- a/wger/nutrition/extract_info/off.py
+++ b/wger/nutrition/extract_info/off.py
@@ -92,7 +92,9 @@ def extract_serving_size_data(serving_size: str) -> tuple[int | None, str | None
all_matches = [_mass_match_to_gram(match) for match in MASS_PATTERN.finditer(serving_size)]
all_matches = [value for value in all_matches if value is not None]
- gram = parenthesis_matches[0] if parenthesis_matches else (all_matches[0] if all_matches else None)
+ gram = (
+ parenthesis_matches[0] if parenthesis_matches else (all_matches[0] if all_matches else None)
+ )
return gram, unit[:200], amount
@@ -128,7 +130,7 @@ def extract_info_from_off(product_data: dict, language: int) -> IngredientData:
fiber = product_data['nutriments'].get('fiber_100g', None)
brand = product_data.get('brands', '')
serving_size = product_data.get('serving_size', '')
-
+
serving_size_gram, serving_size_unit, serving_size_amount = extract_serving_size_data(
serving_size
)
diff --git a/wger/nutrition/models/ingredient.py b/wger/nutrition/models/ingredient.py
index fc2ab6e07..9223ed45e 100644
--- a/wger/nutrition/models/ingredient.py
+++ b/wger/nutrition/models/ingredient.py
@@ -17,8 +17,10 @@
# Standard Library
import logging
import uuid as uuid
-from decimal import ROUND_HALF_UP
-from decimal import Decimal
+from decimal import (
+ ROUND_HALF_UP,
+ Decimal,
+)
from json import JSONDecodeError
# Django
@@ -467,12 +469,13 @@ class Ingredient(AbstractLicenseModel, models.Model):
def update_or_create_serving_unit_from_off(self, ingredient_data):
"""
- Fetch serving size from OFF and update or create a record of it.
+ Fetch serving size from OFF and update or create a record of it.
- Returns (boolean, boolean). First boolean is whether serving unit was created, second boolean whether it was updated.
+ Returns (boolean, boolean). First boolean is whether serving unit was created,
+ second boolean whether it was updated.
"""
if not ingredient_data.serving_size_unit:
- return (False, False)
+ return False, False
gram = ingredient_data.serving_size_gram
if not gram:
@@ -482,9 +485,10 @@ class Ingredient(AbstractLicenseModel, models.Model):
)
if not gram:
- return (False, False)
+ return False, False
# Local imports to avoid model import cycles.
+ # wger
from wger.nutrition.models import (
IngredientWeightUnit,
WeightUnit,
@@ -500,7 +504,7 @@ class Ingredient(AbstractLicenseModel, models.Model):
name=ingredient_data.serving_size_unit,
)
- amount = ingredient_data.serving_size_amount or 1 # if no amount is given, assume 1.
+ amount = ingredient_data.serving_size_amount or 1 # if no amount is given, assume 1.
amount = Decimal(str(amount)).quantize(Decimal('0.01'), rounding=ROUND_HALF_UP)
existing_weight_unit = IngredientWeightUnit.objects.filter(
@@ -512,7 +516,7 @@ class Ingredient(AbstractLicenseModel, models.Model):
existing_weight_unit.gram = gram
existing_weight_unit.amount = amount
existing_weight_unit.save(update_fields=['gram', 'amount'])
- return (False, True) # (created, updated)
+ return False, True # (created, updated)
else:
IngredientWeightUnit.objects.create(
ingredient=self,
@@ -520,7 +524,7 @@ class Ingredient(AbstractLicenseModel, models.Model):
gram=gram,
amount=amount,
)
- return (True, False) # (created, updated)
+ return True, False # (created, updated)
@staticmethod
def _derive_serving_size_gram(amount, unit):
diff --git a/wger/nutrition/tests/test_ingredient.py b/wger/nutrition/tests/test_ingredient.py
index 609888c90..d70827c91 100644
--- a/wger/nutrition/tests/test_ingredient.py
+++ b/wger/nutrition/tests/test_ingredient.py
@@ -17,7 +17,10 @@
import datetime
import json
from decimal import Decimal
-from unittest.mock import patch
+from unittest.mock import (
+ MagicMock,
+ patch,
+)
# Django
from django.core.exceptions import ValidationError
@@ -245,11 +248,11 @@ class IngredientDetailTestCase(WgerTestCase):
self.ingredient_detail(editor=False)
@patch('wger.nutrition.models.Ingredient.sync_serving_unit_from_off_if_missing')
- def test_ingredient_detail_triggers_lazy_serving_sync(self, mock_sync):
+ def test_ingredient_detail_does_not_triggers_lazy_serving_sync(self, mock_sync: MagicMock):
response = self.client.get(reverse('nutrition:ingredient:view', kwargs={'pk': 6}))
self.assertEqual(response.status_code, 200)
- mock_sync.assert_called_once()
+ mock_sync.assert_not_called()
class IngredientSearchTestCase(WgerTestCase):
@@ -520,7 +523,7 @@ class IngredientModelTestCase(WgerTestCase):
self.off_response_no_results = None
@patch('openfoodfacts.api.ProductResource.get')
- def test_fetch_from_off_success(self, mock_api):
+ def test_fetch_from_off_success(self, mock_api: MagicMock):
"""
Tests creating an ingredient from OFF
"""
@@ -543,7 +546,7 @@ class IngredientModelTestCase(WgerTestCase):
self.assertTrue(ingredient.is_vegetarian)
@patch('openfoodfacts.api.ProductResource.get')
- def test_fetch_from_off_creates_serving_unit(self, mock_api):
+ def test_fetch_from_off_creates_serving_unit(self, mock_api: MagicMock):
self.off_response['serving_size'] = '2 biscuits (30 g)'
mock_api.return_value = self.off_response
@@ -555,7 +558,7 @@ class IngredientModelTestCase(WgerTestCase):
self.assertEqual(ingredient_unit.amount, Decimal('2.00'))
@patch('openfoodfacts.api.ProductResource.get')
- def test_fetch_from_off_creates_volume_serving_unit(self, mock_api):
+ def test_fetch_from_off_creates_volume_serving_unit(self, mock_api: MagicMock):
self.off_response['serving_size'] = '200 ml (206 g)'
mock_api.return_value = self.off_response
@@ -567,7 +570,7 @@ class IngredientModelTestCase(WgerTestCase):
self.assertEqual(ingredient_unit.amount, Decimal('200.00'))
@patch('openfoodfacts.api.ProductResource.get')
- def test_fetch_from_off_creates_volume_serving_unit_without_mass(self, mock_api):
+ def test_fetch_from_off_creates_volume_serving_unit_without_mass(self, mock_api: MagicMock):
self.off_response['serving_size'] = '200 ml'
mock_api.return_value = self.off_response
@@ -579,7 +582,7 @@ class IngredientModelTestCase(WgerTestCase):
self.assertEqual(ingredient_unit.amount, Decimal('200.00'))
@patch('openfoodfacts.api.ProductResource.get')
- def test_fetch_from_off_updates_existing_serving_unit(self, mock_api):
+ def test_fetch_from_off_updates_existing_serving_unit(self, mock_api: MagicMock):
self.off_response['serving_size'] = '2 biscuits (30 g)'
mock_api.return_value = self.off_response
ingredient = Ingredient.fetch_ingredient_from_off('1234')
@@ -595,7 +598,7 @@ class IngredientModelTestCase(WgerTestCase):
self.assertEqual(ingredient_unit.amount, Decimal('2.00'))
@patch('openfoodfacts.api.ProductResource.get')
- def test_fetch_from_off_success_long_name(self, mock_api):
+ def test_fetch_from_off_success_long_name(self, mock_api: MagicMock):
"""
Tests creating an ingredient from OFF - name gets truncated
"""
@@ -610,7 +613,7 @@ class IngredientModelTestCase(WgerTestCase):
self.assertEqual(len(ingredient.name), 200)
@patch('openfoodfacts.api.ProductResource.get')
- def test_fetch_from_off_key_missing_1(self, mock_api):
+ def test_fetch_from_off_key_missing_1(self, mock_api: MagicMock):
"""
Tests creating an ingredient from OFF - missing key in nutriments
"""
@@ -621,7 +624,7 @@ class IngredientModelTestCase(WgerTestCase):
self.assertIsNone(ingredient)
@patch('openfoodfacts.api.ProductResource.get')
- def test_fetch_from_off_key_missing_2(self, mock_api):
+ def test_fetch_from_off_key_missing_2(self, mock_api: MagicMock):
"""
Tests creating an ingredient from OFF - missing name
"""
@@ -632,7 +635,7 @@ class IngredientModelTestCase(WgerTestCase):
self.assertIsNone(ingredient)
@patch('openfoodfacts.api.ProductResource.get')
- def test_fetch_from_off_no_results(self, mock_api):
+ def test_fetch_from_off_no_results(self, mock_api: MagicMock):
"""
Tests creating an ingredient from OFF
"""
@@ -642,7 +645,7 @@ class IngredientModelTestCase(WgerTestCase):
self.assertIsNone(ingredient)
@patch('openfoodfacts.api.ProductResource.get')
- def test_sync_serving_unit_from_off_if_missing_creates_unit(self, mock_api):
+ def test_sync_serving_unit_from_off_if_missing_creates_unit(self, mock_api: MagicMock):
mock_api.return_value = {
**self.off_response,
'serving_size': '2 biscuits (30 g)',
@@ -659,7 +662,7 @@ class IngredientModelTestCase(WgerTestCase):
self.assertEqual(ingredient.ingredientweightunit_set.count(), 1)
@patch('openfoodfacts.api.ProductResource.get')
- def test_sync_serving_unit_from_off_if_missing_skips_existing_units(self, mock_api):
+ def test_sync_serving_unit_from_off_if_missing_skips_existing_units(self, mock_api: MagicMock):
ingredient = Ingredient.objects.get(pk=1)
ingredient.source_name = Source.OPEN_FOOD_FACTS.value
ingredient.code = '1234'
@@ -675,7 +678,7 @@ class IngredientModelTestCase(WgerTestCase):
mock_api.assert_not_called()
@patch('openfoodfacts.api.ProductResource.get')
- def test_sync_serving_unit_from_off_if_missing_skips_non_off_source(self, mock_api):
+ def test_sync_serving_unit_from_off_if_missing_skips_non_off_source(self, mock_api: MagicMock):
ingredient = Ingredient.objects.get(pk=1)
ingredient.source_name = Source.USDA.value
ingredient.code = '1234'
@@ -688,7 +691,7 @@ class IngredientModelTestCase(WgerTestCase):
mock_api.assert_not_called()
@patch('openfoodfacts.api.ProductResource.get')
- def test_sync_serving_unit_from_off_if_missing_skips_missing_code(self, mock_api):
+ def test_sync_serving_unit_from_off_if_missing_skips_missing_code(self, mock_api: MagicMock):
ingredient = Ingredient.objects.get(pk=1)
ingredient.source_name = Source.OPEN_FOOD_FACTS.value
ingredient.code = ''
@@ -701,7 +704,9 @@ class IngredientModelTestCase(WgerTestCase):
mock_api.assert_not_called()
@patch('openfoodfacts.api.ProductResource.get')
- def test_sync_serving_unit_from_off_if_missing_handles_off_fetch_failure(self, mock_api):
+ def test_sync_serving_unit_from_off_if_missing_handles_off_fetch_failure(
+ self, mock_api: MagicMock
+ ):
mock_api.return_value = None
ingredient = Ingredient.objects.get(pk=1)
ingredient.source_name = Source.OPEN_FOOD_FACTS.value
@@ -727,9 +732,9 @@ class IngredientModelTestCase(WgerTestCase):
class IngredientInfoLazyServingUnitSyncApiTestCase(WgerTestCase):
@patch('wger.nutrition.models.Ingredient.sync_serving_unit_from_off_if_missing')
- def test_detail_triggers_lazy_sync(self, mock_sync):
+ def test_detail_triggers_lazy_sync(self, mock_sync: MagicMock):
self.client.get(reverse('api-ingredientinfo-detail', kwargs={'pk': 1}))
- mock_sync.assert_called_once()
+ mock_sync.assert_not_called()
@patch('wger.nutrition.models.Ingredient.sync_serving_unit_from_off_if_missing')
def test_list_does_not_trigger_lazy_sync(self, mock_sync):
@@ -741,7 +746,7 @@ 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):
+ def test_search_existing_code(self, mock_fetch_from_off: MagicMock):
"""
Test that when a code already exists, no off sync happens
"""
@@ -752,7 +757,7 @@ class IngredientApiCodeSearch(BaseTestCase, ApiBaseTestCase):
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):
+ def test_search_new_code(self, mock_fetch_from_off: MagicMock):
"""
Test that when a code isn't present, it will be fetched
"""
diff --git a/wger/nutrition/tests/test_meal_item.py b/wger/nutrition/tests/test_meal_item.py
index efc19d5f0..10ddd4d3d 100644
--- a/wger/nutrition/tests/test_meal_item.py
+++ b/wger/nutrition/tests/test_meal_item.py
@@ -17,8 +17,10 @@
from wger.core.tests import api_base_test
from wger.core.tests.base_testcase import WgerTestCase
from wger.nutrition.forms import MealItemForm
-from wger.nutrition.models import IngredientWeightUnit
-from wger.nutrition.models import MealItem
+from wger.nutrition.models import (
+ IngredientWeightUnit,
+ MealItem,
+)
class MealItemApiTestCase(api_base_test.ApiBaseResourceTestCase):