improved tests and limited conversion to existing units
This commit is contained in:
parent
f90a66af1e
commit
44cb2d9807
@ -18,10 +18,14 @@ def base_conversions(ingredient_list):
|
||||
if i.unit.base_unit:
|
||||
conversion_unit = i.unit.base_unit
|
||||
quantitiy = ureg.Quantity(f'{i.amount} {conversion_unit}')
|
||||
for u in CONVERT_TO_UNITS['metric'] + CONVERT_TO_UNITS['us'] + CONVERT_TO_UNITS['uk']:
|
||||
|
||||
# TODO allow setting which units to convert to
|
||||
units = Unit.objects.filter(base_unit__in=(CONVERT_TO_UNITS['metric'] + CONVERT_TO_UNITS['us'] + CONVERT_TO_UNITS['uk'])).all()
|
||||
|
||||
for u in units:
|
||||
try:
|
||||
converted = quantitiy.to(u)
|
||||
ingredient = Ingredient(amount=converted.m, unit=Unit(name=str(converted.units)), food=ingredient_list[0].food, )
|
||||
converted = quantitiy.to(u.base_unit)
|
||||
ingredient = Ingredient(amount=converted.m, unit=u, food=ingredient_list[0].food, )
|
||||
if not any((x.unit.name == ingredient.unit.name or x.unit.base_unit == ingredient.unit.name) for x in pint_converted_list):
|
||||
pint_converted_list.append(ingredient)
|
||||
except PintError:
|
||||
|
@ -42,4 +42,10 @@ def test_food_property(space_1, u1_s1):
|
||||
recipe_1.steps.add(step_2)
|
||||
|
||||
property_values = calculate_recipe_properties(recipe_1)
|
||||
|
||||
assert property_values[property_fat.id]['name'] == property_fat.name
|
||||
assert property_values[property_fat.id]['total_value'] == 525 # TODO manually validate those numbers
|
||||
assert property_values[property_fat.id]['food_values'][food_1.id] == 275 # TODO manually validate those numbers
|
||||
assert property_values[property_fat.id]['food_values'][food_2.id] == 250 # TODO manually validate those numbers
|
||||
print(property_values)
|
||||
# TODO more property tests
|
||||
|
@ -1,3 +1,5 @@
|
||||
from _decimal import Decimal
|
||||
|
||||
from django.contrib import auth
|
||||
from django_scopes import scopes_disabled
|
||||
|
||||
@ -8,6 +10,7 @@ from cookbook.models import Unit, Food, Ingredient, UnitConversion
|
||||
def test_unit_conversions(space_1, u1_s1):
|
||||
with scopes_disabled():
|
||||
unit_gram = Unit.objects.create(name='gram', base_unit='g', space=space_1)
|
||||
unit_kg = Unit.objects.create(name='kg', base_unit='kg', space=space_1)
|
||||
unit_pcs = Unit.objects.create(name='pcs', base_unit='', space=space_1)
|
||||
unit_floz1 = Unit.objects.create(name='fl. oz 1', base_unit='imperial_fluid_ounce', space=space_1) # US and UK use different volume systems (US vs imperial)
|
||||
unit_floz2 = Unit.objects.create(name='fl. oz 2', base_unit='fluid_ounce', space=space_1)
|
||||
@ -24,7 +27,11 @@ def test_unit_conversions(space_1, u1_s1):
|
||||
space=space_1,
|
||||
)
|
||||
|
||||
print(get_conversions(ingredient_food_1_gram))
|
||||
conversions = get_conversions(ingredient_food_1_gram)
|
||||
print(conversions)
|
||||
assert len(conversions) == 2
|
||||
assert next(x for x in conversions if x.unit == unit_kg) is not None
|
||||
assert next(x for x in conversions if x.unit == unit_kg).amount == 0.1
|
||||
|
||||
print('\n----------- TEST BASE CONVERSIONS - VOLUMES ---------------')
|
||||
|
||||
@ -35,7 +42,20 @@ def test_unit_conversions(space_1, u1_s1):
|
||||
space=space_1,
|
||||
)
|
||||
|
||||
print(get_conversions(ingredient_food_1_floz1))
|
||||
conversions = get_conversions(ingredient_food_1_floz1)
|
||||
assert len(conversions) == 2
|
||||
assert next(x for x in conversions if x.unit == unit_floz2) is not None
|
||||
assert next(x for x in conversions if x.unit == unit_floz2).amount == 96.07599404038842 # TODO validate value
|
||||
|
||||
print(conversions)
|
||||
|
||||
unit_pint = Unit.objects.create(name='pint', base_unit='pint', space=space_1)
|
||||
conversions = get_conversions(ingredient_food_1_floz1)
|
||||
assert len(conversions) == 3
|
||||
assert next(x for x in conversions if x.unit == unit_pint) is not None
|
||||
assert next(x for x in conversions if x.unit == unit_pint).amount == 6.004749627524276 # TODO validate value
|
||||
|
||||
print(conversions)
|
||||
|
||||
print('\n----------- TEST BASE CUSTOM CONVERSION - TO CUSTOM CONVERSION ---------------')
|
||||
UnitConversion.objects.create(
|
||||
@ -46,8 +66,13 @@ def test_unit_conversions(space_1, u1_s1):
|
||||
space=space_1,
|
||||
created_by=auth.get_user(u1_s1),
|
||||
)
|
||||
conversions = get_conversions(ingredient_food_1_gram)
|
||||
|
||||
print(get_conversions(ingredient_food_1_gram))
|
||||
assert len(conversions) == 3
|
||||
assert next(x for x in conversions if x.unit == unit_fantasy) is not None
|
||||
assert next(x for x in conversions if x.unit == unit_fantasy).amount == Decimal('133.700') # TODO validate value
|
||||
|
||||
print(conversions)
|
||||
|
||||
print('\n----------- TEST CUSTOM CONVERSION - NO PCS ---------------')
|
||||
ingredient_food_1_pcs = Ingredient.objects.create(
|
||||
@ -64,6 +89,8 @@ def test_unit_conversions(space_1, u1_s1):
|
||||
space=space_1,
|
||||
)
|
||||
|
||||
assert len(get_conversions(ingredient_food_1_pcs)) == 1
|
||||
assert len(get_conversions(ingredient_food_2_pcs)) == 1
|
||||
print(get_conversions(ingredient_food_1_pcs))
|
||||
print(get_conversions(ingredient_food_2_pcs))
|
||||
|
||||
@ -78,7 +105,13 @@ def test_unit_conversions(space_1, u1_s1):
|
||||
created_by=auth.get_user(u1_s1),
|
||||
)
|
||||
|
||||
print(get_conversions(ingredient_food_1_pcs))
|
||||
conversions = get_conversions(ingredient_food_1_pcs)
|
||||
assert len(conversions) == 3
|
||||
assert next(x for x in conversions if x.unit == unit_gram).amount == 1000
|
||||
assert next(x for x in conversions if x.unit == unit_kg).amount == 1
|
||||
print(conversions)
|
||||
|
||||
assert len(get_conversions(ingredient_food_2_pcs)) == 1
|
||||
print(get_conversions(ingredient_food_2_pcs))
|
||||
|
||||
print('\n----------- TEST CUSTOM CONVERSION - REVERSE CONVERSION ---------------')
|
||||
@ -92,6 +125,14 @@ def test_unit_conversions(space_1, u1_s1):
|
||||
created_by=auth.get_user(u1_s1),
|
||||
)
|
||||
|
||||
print(get_conversions(ingredient_food_1_pcs))
|
||||
print(get_conversions(ingredient_food_2_pcs))
|
||||
conversions = get_conversions(ingredient_food_1_pcs)
|
||||
assert len(conversions) == 3
|
||||
assert next(x for x in conversions if x.unit == unit_gram).amount == 1000
|
||||
assert next(x for x in conversions if x.unit == unit_kg).amount == 1
|
||||
print(conversions)
|
||||
|
||||
conversions = get_conversions(ingredient_food_2_pcs)
|
||||
assert len(conversions) == 3
|
||||
assert next(x for x in conversions if x.unit == unit_gram).amount == 1000
|
||||
assert next(x for x in conversions if x.unit == unit_kg).amount == 1
|
||||
print(conversions)
|
||||
|
Loading…
Reference in New Issue
Block a user