fixed unit conversion division by 0

This commit is contained in:
vabene1111
2025-09-29 22:14:19 +02:00
parent 6adf077ee5
commit 02e43730bd
36 changed files with 68 additions and 5 deletions

View File

@@ -135,8 +135,9 @@ class UnitConversionHelper:
:param food: base food :param food: base food
:return: converted ingredient object from base amount/unit/food :return: converted ingredient object from base amount/unit/food
""" """
if uc.food is None or uc.food == food: if (uc.food is None or uc.food == food) and uc.converted_amount > 0 and uc.base_amount > 0:
if unit == uc.base_unit: if unit == uc.base_unit:
return Ingredient(amount=amount * (uc.converted_amount / uc.base_amount), unit=uc.converted_unit, food=food, space=self.space) return Ingredient(amount=amount * (uc.converted_amount / uc.base_amount), unit=uc.converted_unit, food=food, space=self.space)
else: else:
return Ingredient(amount=amount * (uc.base_amount / uc.converted_amount), unit=uc.base_unit, food=food, space=self.space) return Ingredient(amount=amount * (uc.base_amount / uc.converted_amount), unit=uc.base_unit, food=food, space=self.space)
return None

View File

@@ -185,3 +185,32 @@ def test_unit_conversions(space_1, space_2, u1_s1):
assert next(x for x in conversions if x.unit == unit_kg_space_2) is not None assert next(x for x in conversions if x.unit == unit_kg_space_2) is not None
assert abs(next(x for x in conversions if x.unit == unit_kg_space_2).amount - Decimal(0.1)) < 0.0001 assert abs(next(x for x in conversions if x.unit == unit_kg_space_2).amount - Decimal(0.1)) < 0.0001
print(conversions) print(conversions)
def test_conversion_with_zero(space_1, space_2, u1_s1):
with scopes_disabled():
uch = UnitConversionHelper(space_1)
unit_gram = Unit.objects.create(name='gram', base_unit='g', space=space_1)
unit_fantasy = Unit.objects.create(name='Fantasy Unit', base_unit=None, space=space_1)
food_1 = Food.objects.create(name='Test Food 1', space=space_1)
ingredient_food_1_gram = Ingredient.objects.create(
food=food_1,
unit=unit_gram,
amount=100,
space=space_1,
)
print('\n----------- TEST BASE CUSTOM CONVERSION - TO CUSTOM CONVERSION ---------------')
UnitConversion.objects.create(
base_amount=0,
base_unit=unit_gram,
converted_amount=0,
converted_unit=unit_fantasy,
space=space_1,
created_by=auth.get_user(u1_s1),
)
conversions = uch.get_conversions(ingredient_food_1_gram)
assert len(conversions) == 1 # conversion always includes the ingredient, if count is 1 no other conversion was found

View File

@@ -19,11 +19,11 @@
</v-row> </v-row>
<v-row> <v-row>
<v-col md="6"> <v-col md="6">
<v-number-input :label="$t('Amount')" :step="10" v-model="editingObj.baseAmount" control-variant="stacked" :precision="3"></v-number-input> <v-number-input :label="$t('Amount')" :step="10" v-model="editingObj.baseAmount" control-variant="stacked" :precision="3" :min="0.001"></v-number-input>
</v-col> </v-col>
<v-col md="6"> <v-col md="6">
<!-- TODO fix card overflow invisible, overflow-visible class is not working --> <!-- TODO fix card overflow invisible, overflow-visible class is not working -->
<model-select :label="$t('Unit')" v-model="editingObj.baseUnit" model="Unit"></model-select> <model-select v-model="editingObj.baseUnit" model="Unit"></model-select>
</v-col> </v-col>
</v-row> </v-row>
<v-row class="mt-0"> <v-row class="mt-0">
@@ -33,11 +33,11 @@
</v-row> </v-row>
<v-row> <v-row>
<v-col md="6"> <v-col md="6">
<v-number-input :label="$t('Amount')" :step="10" v-model="editingObj.convertedAmount" control-variant="stacked" :precision="3"></v-number-input> <v-number-input :label="$t('Amount')" :step="10" v-model="editingObj.convertedAmount" control-variant="stacked" :precision="3" :min="0.001"></v-number-input>
</v-col> </v-col>
<v-col md="6"> <v-col md="6">
<!-- TODO fix card overflow invisible, overflow-visible class is not working --> <!-- TODO fix card overflow invisible, overflow-visible class is not working -->
<model-select :label="$t('Unit')" v-model="editingObj.convertedUnit" model="Unit"></model-select> <model-select v-model="editingObj.convertedUnit" model="Unit"></model-select>
</v-col> </v-col>
</v-row> </v-row>
<v-row> <v-row>

View File

@@ -115,6 +115,7 @@
"Fats": "", "Fats": "",
"File": "", "File": "",
"Files": "", "Files": "",
"Finish": "",
"Food": "", "Food": "",
"FoodInherit": "", "FoodInherit": "",
"FoodNotOnHand": "", "FoodNotOnHand": "",

View File

@@ -112,6 +112,7 @@
"Fats": "Мазнини", "Fats": "Мазнини",
"File": "Файл", "File": "Файл",
"Files": "Файлове", "Files": "Файлове",
"Finish": "",
"Food": "Храна", "Food": "Храна",
"FoodInherit": "Хранителни наследствени полета", "FoodInherit": "Хранителни наследствени полета",
"FoodNotOnHand": "Нямате {храна} под ръка.", "FoodNotOnHand": "Нямате {храна} под ръка.",

View File

@@ -154,6 +154,7 @@
"Fats": "Greixos", "Fats": "Greixos",
"File": "Arxiu", "File": "Arxiu",
"Files": "Arxius", "Files": "Arxius",
"Finish": "",
"First_name": "Nom", "First_name": "Nom",
"Food": "Aliment", "Food": "Aliment",
"FoodInherit": "Camps Heretats", "FoodInherit": "Camps Heretats",

View File

@@ -153,6 +153,7 @@
"Fats": "Tuky", "Fats": "Tuky",
"File": "Soubor", "File": "Soubor",
"Files": "Soubory", "Files": "Soubory",
"Finish": "",
"First_name": "Jméno", "First_name": "Jméno",
"Food": "Potravina", "Food": "Potravina",
"FoodInherit": "Propisovatelná pole potraviny", "FoodInherit": "Propisovatelná pole potraviny",

View File

@@ -154,6 +154,7 @@
"Fats": "Fedtstoffer", "Fats": "Fedtstoffer",
"File": "Fil", "File": "Fil",
"Files": "Filer", "Files": "Filer",
"Finish": "",
"First_name": "Fornavn", "First_name": "Fornavn",
"Food": "Mad", "Food": "Mad",
"FoodInherit": "Nedarvelige mad felter", "FoodInherit": "Nedarvelige mad felter",

View File

@@ -212,6 +212,7 @@
"Fats": "Fette", "Fats": "Fette",
"File": "Datei", "File": "Datei",
"Files": "Dateien", "Files": "Dateien",
"Finish": "Fertigstellen",
"FinishedAt": "Fertig um", "FinishedAt": "Fertig um",
"First": "Erstes", "First": "Erstes",
"First_name": "Vorname", "First_name": "Vorname",

View File

@@ -154,6 +154,7 @@
"Fats": "Λιπαρά", "Fats": "Λιπαρά",
"File": "Αρχείο", "File": "Αρχείο",
"Files": "Αρχεία", "Files": "Αρχεία",
"Finish": "",
"First_name": "Όνομα", "First_name": "Όνομα",
"Food": "Φαγητό", "Food": "Φαγητό",
"FoodInherit": "Πεδία φαγητών που κληρονομούνται", "FoodInherit": "Πεδία φαγητών που κληρονομούνται",

View File

@@ -210,6 +210,7 @@
"Fats": "Fats", "Fats": "Fats",
"File": "File", "File": "File",
"Files": "Files", "Files": "Files",
"Finish": "Finish",
"FinishedAt": "Finished at", "FinishedAt": "Finished at",
"First": "First", "First": "First",
"First_name": "First Name", "First_name": "First Name",

View File

@@ -207,6 +207,7 @@
"Fats": "Grasas", "Fats": "Grasas",
"File": "Archivo", "File": "Archivo",
"Files": "Archivos", "Files": "Archivos",
"Finish": "",
"FinishedAt": "Finaliza a las", "FinishedAt": "Finaliza a las",
"First": "Primero", "First": "Primero",
"First_name": "Nombre", "First_name": "Nombre",

View File

@@ -151,6 +151,7 @@
"Fats": "Rasvat", "Fats": "Rasvat",
"File": "Tiedosto", "File": "Tiedosto",
"Files": "Tiedostot", "Files": "Tiedostot",
"Finish": "",
"First_name": "Etunimi", "First_name": "Etunimi",
"Food": "Ruoka", "Food": "Ruoka",
"FoodInherit": "Ruoan perinnölliset kentät", "FoodInherit": "Ruoan perinnölliset kentät",

View File

@@ -210,6 +210,7 @@
"Fats": "Matières grasses", "Fats": "Matières grasses",
"File": "Fichier", "File": "Fichier",
"Files": "Fichiers", "Files": "Fichiers",
"Finish": "",
"FinishedAt": "Terminé à", "FinishedAt": "Terminé à",
"First": "Premier", "First": "Premier",
"First_name": "Prénom", "First_name": "Prénom",

View File

@@ -154,6 +154,7 @@
"Fats": "שומנים", "Fats": "שומנים",
"File": "קובץ", "File": "קובץ",
"Files": "קבצים", "Files": "קבצים",
"Finish": "",
"First_name": "שם פרטי", "First_name": "שם פרטי",
"Food": "אוכל", "Food": "אוכל",
"FoodInherit": "ערכי מזון", "FoodInherit": "ערכי מזון",

View File

@@ -154,6 +154,7 @@
"Fats": "Masti", "Fats": "Masti",
"File": "Datoteka", "File": "Datoteka",
"Files": "Datoteke", "Files": "Datoteke",
"Finish": "",
"First_name": "Ime", "First_name": "Ime",
"Food": "Namirnica", "Food": "Namirnica",
"FoodInherit": "Nasljedna polja namirnice", "FoodInherit": "Nasljedna polja namirnice",

View File

@@ -137,6 +137,7 @@
"Fats": "Zsírok", "Fats": "Zsírok",
"File": "Fájl", "File": "Fájl",
"Files": "Fájlok", "Files": "Fájlok",
"Finish": "",
"First_name": "Keresztnév", "First_name": "Keresztnév",
"Food": "Alapanyag", "Food": "Alapanyag",
"FoodInherit": "", "FoodInherit": "",

View File

@@ -69,6 +69,7 @@
"Fats": "", "Fats": "",
"File": "", "File": "",
"Files": "", "Files": "",
"Finish": "",
"Food": "Սննդամթերք", "Food": "Սննդամթերք",
"FromBalance": "", "FromBalance": "",
"Fulltext": "", "Fulltext": "",

View File

@@ -126,6 +126,7 @@
"Fats": "Lemak", "Fats": "Lemak",
"File": "Berkas", "File": "Berkas",
"Files": "File", "Files": "File",
"Finish": "",
"First_name": "", "First_name": "",
"Food": "", "Food": "",
"FoodInherit": "", "FoodInherit": "",

View File

@@ -153,6 +153,7 @@
"Fats": "", "Fats": "",
"File": "", "File": "",
"Files": "", "Files": "",
"Finish": "",
"First_name": "", "First_name": "",
"Food": "", "Food": "",
"FoodInherit": "", "FoodInherit": "",

View File

@@ -211,6 +211,7 @@
"Fats": "Grassi", "Fats": "Grassi",
"File": "File", "File": "File",
"Files": "File", "Files": "File",
"Finish": "",
"FinishedAt": "Finito alle", "FinishedAt": "Finito alle",
"First": "Primo", "First": "Primo",
"First_name": "Nome", "First_name": "Nome",

View File

@@ -139,6 +139,7 @@
"Fats": "", "Fats": "",
"File": "", "File": "",
"Files": "", "Files": "",
"Finish": "",
"First_name": "", "First_name": "",
"Food": "", "Food": "",
"FoodInherit": "", "FoodInherit": "",

View File

@@ -154,6 +154,7 @@
"Fats": "", "Fats": "",
"File": "", "File": "",
"Files": "", "Files": "",
"Finish": "",
"First_name": "", "First_name": "",
"Food": "", "Food": "",
"FoodInherit": "", "FoodInherit": "",

View File

@@ -146,6 +146,7 @@
"Fats": "Fett", "Fats": "Fett",
"File": "Fil", "File": "Fil",
"Files": "Filer", "Files": "Filer",
"Finish": "",
"First_name": "Fornavn", "First_name": "Fornavn",
"Food": "Matretter", "Food": "Matretter",
"FoodInherit": "Arvbare felt for matvarer", "FoodInherit": "Arvbare felt for matvarer",

View File

@@ -211,6 +211,7 @@
"Fats": "Vetten", "Fats": "Vetten",
"File": "Bestand", "File": "Bestand",
"Files": "Bestanden", "Files": "Bestanden",
"Finish": "",
"FinishedAt": "Afgerond op", "FinishedAt": "Afgerond op",
"First": "Eerste", "First": "Eerste",
"First_name": "Voornaam", "First_name": "Voornaam",

View File

@@ -180,6 +180,7 @@
"Fats": "Tłuszcze", "Fats": "Tłuszcze",
"File": "Plik", "File": "Plik",
"Files": "Pliki", "Files": "Pliki",
"Finish": "",
"First_name": "Imię", "First_name": "Imię",
"Food": "Żywność", "Food": "Żywność",
"FoodInherit": "Pola dziedziczone w żywności", "FoodInherit": "Pola dziedziczone w żywności",

View File

@@ -103,6 +103,7 @@
"Fats": "Gorduras", "Fats": "Gorduras",
"File": "Ficheiro", "File": "Ficheiro",
"Files": "Ficheiros", "Files": "Ficheiros",
"Finish": "",
"Food": "Comida", "Food": "Comida",
"FoodInherit": "Campos herdados por comida", "FoodInherit": "Campos herdados por comida",
"FoodNotOnHand": "Não têm {food} disponível.", "FoodNotOnHand": "Não têm {food} disponível.",

View File

@@ -209,6 +209,7 @@
"Fats": "Gorduras", "Fats": "Gorduras",
"File": "Arquivo", "File": "Arquivo",
"Files": "Arquivos", "Files": "Arquivos",
"Finish": "",
"FinishedAt": "Finalizado em", "FinishedAt": "Finalizado em",
"First": "Primeiro", "First": "Primeiro",
"First_name": "Primeiro Nome", "First_name": "Primeiro Nome",

View File

@@ -133,6 +133,7 @@
"Fats": "Grăsimi", "Fats": "Grăsimi",
"File": "Fișier", "File": "Fișier",
"Files": "Fișiere", "Files": "Fișiere",
"Finish": "",
"First_name": "Prenume", "First_name": "Prenume",
"Food": "Mâncare", "Food": "Mâncare",
"FoodInherit": "Câmpuri moștenite de alimente", "FoodInherit": "Câmpuri moștenite de alimente",

View File

@@ -210,6 +210,7 @@
"Fats": "Жиры", "Fats": "Жиры",
"File": "Файл", "File": "Файл",
"Files": "Файлы", "Files": "Файлы",
"Finish": "",
"FinishedAt": "Завершено в", "FinishedAt": "Завершено в",
"First": "Первый", "First": "Первый",
"First_name": "Имя", "First_name": "Имя",

View File

@@ -210,6 +210,7 @@
"Fats": "Maščobe", "Fats": "Maščobe",
"File": "Datoteka", "File": "Datoteka",
"Files": "Datoteke", "Files": "Datoteke",
"Finish": "",
"FinishedAt": "Končano ob", "FinishedAt": "Končano ob",
"First": "Prvi", "First": "Prvi",
"First_name": "Ime", "First_name": "Ime",

View File

@@ -191,6 +191,7 @@
"Fats": "Fett", "Fats": "Fett",
"File": "Fil", "File": "Fil",
"Files": "Filer", "Files": "Filer",
"Finish": "",
"First_name": "Förnamn", "First_name": "Förnamn",
"Food": "Livsmedel", "Food": "Livsmedel",
"FoodInherit": "Ärftliga livsmedels fält", "FoodInherit": "Ärftliga livsmedels fält",

View File

@@ -154,6 +154,7 @@
"Fats": "Yağlar", "Fats": "Yağlar",
"File": "Dosya", "File": "Dosya",
"Files": "Dosyalar", "Files": "Dosyalar",
"Finish": "",
"First_name": "İsim", "First_name": "İsim",
"Food": "Yiyecek", "Food": "Yiyecek",
"FoodInherit": "Yiyeceğin Devralınabileceği Alanlar", "FoodInherit": "Yiyeceğin Devralınabileceği Alanlar",

View File

@@ -137,6 +137,7 @@
"Fats": "Жири", "Fats": "Жири",
"File": "Файл", "File": "Файл",
"Files": "Файли", "Files": "Файли",
"Finish": "",
"Food": "Їжа", "Food": "Їжа",
"FoodInherit": "Пола Успадкованої Їжі", "FoodInherit": "Пола Успадкованої Їжі",
"FoodNotOnHand": "У вас немає {food} на руках.", "FoodNotOnHand": "У вас немає {food} на руках.",

View File

@@ -154,6 +154,7 @@
"Fats": "脂肪", "Fats": "脂肪",
"File": "文件", "File": "文件",
"Files": "文件", "Files": "文件",
"Finish": "",
"First_name": "名", "First_name": "名",
"Food": "食物", "Food": "食物",
"FoodInherit": "食物可继承的字段", "FoodInherit": "食物可继承的字段",

View File

@@ -209,6 +209,7 @@
"Fats": "脂肪", "Fats": "脂肪",
"File": "檔案", "File": "檔案",
"Files": "檔案", "Files": "檔案",
"Finish": "",
"FinishedAt": "完成於", "FinishedAt": "完成於",
"First": "第一個", "First": "第一個",
"First_name": "名字", "First_name": "名字",