def test_serialize_multiple(self): """Test converting pantry to string.""" p = pantry.Pantry([ ingredient.Ingredient(id="egg", amount=1, unit="pc"), ingredient.Ingredient(id="garlic", amount=2, unit="clove"), ]) self.assertEqual("egg,1,pc;garlic,2,clove", str(p))
def test_parse_from_string(self): """Test getting pantries from strings.""" s = "egg,1,pc" p = pantry.Pantry.from_str(s) expected = pantry.Pantry([ingredient.Ingredient("egg", 1, "pc")]) self.assertEqual(expected, p) s = "egg,1,pc,flour,0.5,kg" p = pantry.Pantry.from_str(s) expected = pantry.Pantry([ ingredient.Ingredient("egg", 1, "pc"), ingredient.Ingredient("flour", 0.5, "kg"), ]) self.assertEqual(expected, p)
def test_advice_when_having_one_egg(self): """Check if advice is given when having one egg.""" pantry = Pantry([ingredient.Ingredient("egg", 1, "piece")]) adviser = DishAdvisor() advices = adviser.advise(pantry) # we expect one advice self.assertEqual(1, len(advices)) advice = advices[0] # we expect all these ingredients to show up in the advice. # the mushroom is an extra ingredient, and it shows up anyway! expected_ingredients = {"egg", "onion", "garlic", "mushroom"} advised_ingredients = set( map(lambda i: i.id, advice.missing_ingredients)) self.assertEqual(expected_ingredients, advised_ingredients) for mi in advice.missing_ingredients: # we also expect one of the missing ingredients to be 2 eggs if mi.id == "egg": self.assertEqual(2, mi.amount) # make sure the advice is reproducible advices = adviser.advise(pantry) self.assertEqual(1, len(advices))
def from_str(string: str): """Reconstruct a pantry from its string representation. :param string: an output of str(pantry) :return: a pantry object """ it = iter(string.split(",")) tuples = [(x, next(it), next(it)) for x in it] ingredients = [ ingredient.Ingredient(id=name, amount=amount, unit=unit) for (name, amount, unit) in tuples ] return Pantry(ingredients)
def test_pair_replacements(self): """Test pair replacements.""" # define recipe ingredients so that carrot replaces a plasternaka carrot = ingredient.Ingredient("carrot", 2, "piece") recipe_plasternaka = ingredient.RecipeIngredient( "plasternaka", 2, "piece", ingredient.IngredientRole.ALTERNATIVE, alternative=carrot, ) # put just 1 carrot into pantry pantry = Pantry([ingredient.Ingredient("carrot", 1, "piece")]) # check that carrot indeed replaces plasternaka missing, replaced = DishAdvisor._check_ingredient( pantry, recipe_plasternaka) self.assertTrue(replaced is not None) self.assertTrue(missing is not None) self.assertEqual("plasternaka", replaced.id) self.assertEqual("carrot", missing.id) self.assertEqual(1, missing.amount)
def test_advice_with_alternative(self): """Test advice with alternative.""" pantry = Pantry([ingredient.Ingredient("coconut cream", 400, "ml")]) advices = DishAdvisor().advise(pantry) # we expect one advice self.assertEqual(1, len(advices)) advice = advices[0] # it's the pumpkin soup, and it needs 4 ingredients self.assertEqual("pumpkin-soup", advice.recipe.id) self.assertEqual(4, len(advice.missing_ingredients)) # and there's no cream nor coconut cream missing = {i.id for i in advice.missing_ingredients} self.assertFalse("cream" in missing) self.assertFalse("coconut cream" in missing)
def test_serialize_to_string(self): """Test converting pantry to string.""" i = ingredient.Ingredient(id="egg", amount=1, unit="pc") p = pantry.Pantry([i]) self.assertEqual("egg,1,pc", str(p))