Ejemplo n.º 1
0
 def test_match_one_weight(self):
     food = Food.objects.create(name="Chicken")
     sample_weight1 = FoodWeight.objects.create(food=food,
                                                amount=1,
                                                desc="pinch",
                                                value=1)
     FoodWeight.objects.create(food=food, amount=1, desc="piece", value=1)
     # Chicken has no 'serving' weight so it should return last weight in the list
     assert match_one_weight(food, "serving") == food.weight.last()
     # Food with no weights should raise an Exception when matching weights
     food_with_no_weights = Food.objects.create(name="Null")
     with pytest.raises(AttributeError):
         match_one_weight(food_with_no_weights, "serving")
     assert match_one_weight(food, "pinch") == sample_weight1
Ejemplo n.º 2
0
 def get_weight(self) -> float:
     """
     Returns weight (in grams).
     """
     if not self.matched_food.weight.exists():
         raise IngredientError(
             self.matched_food,
             f"This food doesn't have any FoodWeight objects.")
     matched_weight = match_one_weight(self.matched_food, self.measurement)
     return float(matched_weight.value) * (self.amount /
                                           float(matched_weight.amount))
Ejemplo n.º 3
0
 def test_match_one_weight_returns_last_if_no_match(self):
     """Ensure that if match_one_weight can't match a weight it returns last in the list"""
     f = models.Food.objects.create(name="Chicken")
     w1 = models.FoodWeight.objects.create(food=f,
                                           amount=1,
                                           desc="pinch",
                                           value=1)
     w2 = models.FoodWeight.objects.create(food=f,
                                           amount=1,
                                           desc="cup",
                                           value=1)
     assert search.match_one_weight(f, 'teaspoon').id == w2.id