def test_ingredient_string(self): """Format Ingredient as a string. """ pancakes = Recipe.objects.get(name='Pancakes') egg = Food.objects.get(name='egg') butter = Food.objects.get(name='butter') ounce = Unit.objects.get(name='ounce') beaten, created = Preparation.objects.get_or_create(name='beaten') melted, created = Preparation.objects.get_or_create(name='melted') # quantity == 1 eggs = Ingredient(recipe=pancakes, quantity=1, food=egg) self.assertEqual(str(eggs), '1 egg') # quantity > 1 eggs.quantity = 2 self.assertEqual(str(eggs), '2 eggs') # with preparation eggs.preparation = beaten self.assertEqual(str(eggs), '2 eggs, beaten') # with preparation, and optional eggs.optional = True self.assertEqual(str(eggs), '2 eggs, beaten (optional)') # Ingredient with units # quantity == 1 ounces_butter = Ingredient(recipe=pancakes, quantity=1, unit=ounce, food=butter) self.assertEqual(str(ounces_butter), '1 ounce butter') # quantity > 1 ounces_butter.quantity = 8 self.assertEqual(str(ounces_butter), '8 ounces butter') # optional ounces_butter.optional = True self.assertEqual(str(ounces_butter), '8 ounces butter (optional)') # optional, with preparation ounces_butter.preparation = melted self.assertEqual(str(ounces_butter), '8 ounces butter, melted (optional)')
def test_ingredient_nutrition_info_recalculation(self): """Recalculate IngredientNutritionInfo on save. """ egg = Food.objects.get(name='egg') pancakes = Recipe.objects.get(name='Pancakes') # Create an ingredient eggs = Ingredient(recipe=pancakes, quantity=2, food=egg) eggs.save() egg_NI = FoodNutritionInfo.objects.get(food=egg, quantity=1, unit=None) # Nutrition info should equal 2 eggs total_NI = egg_NI * 2.0 self.assertTrue(eggs.nutrition_info.is_equal(total_NI)) # Make it 3 eggs eggs.quantity = 3 eggs.save() # Ensure nutrition reflects 3 eggs now total_NI = egg_NI * 3.0 self.assertTrue(eggs.nutrition_info.is_equal(total_NI))