def test_exact_values(self): # case 1.1 # tests when the amount of inventory is the same as required by the recipe one_times_recipe = {'bread': 10, 'peanutButter': 5, 'jam': 1} one_times_inventory = {'bread': 10, 'peanutButter': 5, 'jam': 1} # checks to see if the output is 1 self.assertEqual( how_many_servings(one_times_recipe, one_times_inventory), 1) # case 1.2 # tests when the inventory is double the recipe two_times_recipe = {'bread': 10, 'peanutButter': 5, 'jam': 1} two_times_inventory = {'bread': 20, 'peanutButter': 10, 'jam': 2} # checks to see if it returs 2 self.assertEqual( how_many_servings(two_times_recipe, two_times_inventory), 2) # case 1.3 # tests when inventory is 10 times the recipe ten_times_recipe = {'bread': 10, 'peanutButter': 5, 'jam': 1} ten_times_inventory = {'bread': 100, 'peanutButter': 50, 'jam': 10} # checks to see if it returs 10 self.assertEqual( how_many_servings(ten_times_recipe, ten_times_inventory), 10)
def test_zeros(self): # case 9.1 # tests when a ingredient required by the recipe has a value of 0 in the inventory dictionary first_zero_recipe = {'bread': 10, 'peanutButter': 5, 'jam': 2} first_zero_inventory = {'bread': 20, 'peanutButter': 30, 'jam': 0} # returns zero since, in the inventory, there is 0 'jam' while the recipe requires 2 self.assertEqual( how_many_servings(first_zero_recipe, first_zero_inventory), 0) # case 9.2 # tests when a value in recipe is 0 second_zero_recipe = {'bread': 10, 'peanutButter': 5, 'jam': 0} second_zero_inventory = {'bread': 20, 'peanutButter': 30, 'jam': 1} # In this case, it is as though that ingredient is not unnecessory; the 0 in the recipe can therefore be ignored – normal calculations are executed (ignoring the 0) self.assertEqual( how_many_servings(second_zero_recipe, second_zero_inventory), 2) # case 9.3 # tests when a the value of a key is 0 in both the recipe and inventory third_zero_recipe = {'bread': 10, 'peanutButter': 5, 'jam': 0} third_zero_inventory = {'bread': 20, 'peanutButter': 30, 'jam': 0} # like the 9.2 case, we do not require self.assertEqual( how_many_servings(third_zero_recipe, third_zero_inventory), 2)
def test_recipe(self): testRecipe=[[10, '-1'], ['peanutButter', 5], ['jam', 5]] # checks if TypeError is raised when recipe is not a dictionary with self.assertRaises(TypeError) as error: how_many_servings(testRecipe, self.inventory) self.assertEqual('Recipe has to be a dictionary', str(error.exception)) # checks if TypeError is raised when item in recipe is not a string testRecipe = dict(testRecipe) with self.assertRaises(TypeError) as error: how_many_servings(testRecipe, self.inventory) self.assertEqual('Item in recipe and inventory should be a string', str(error.exception)) # checks if TypeError is raised when the quantity of an item in the recipe is not an integer testRecipe['bread'] = testRecipe.pop(10) with self.assertRaises(TypeError) as error: how_many_servings(testRecipe, self.inventory) self.assertEqual('Quantities in recipe should be a positive integer', str(error.exception)) # checks if ValueError is raised when the quantity of an item in the recipe is a negative number testRecipe['bread'] = int(testRecipe['bread']) with self.assertRaises(ValueError) as error: how_many_servings(testRecipe, self.inventory) self.assertEqual('Quantities in recipe should be a positive integer', str(error.exception))
def test_not_exact_values(self): # case 2.1 # tests when the values are integers, but are not multiples of each other not_exact_recipe = {'bread': 10, 'peanutButter': 5, 'jam': 1} not_exact_inventory = {'bread': 99, 'peanutButter': 6, 'jam': 9} # even though there is an excess of 'jam' and 'bread,' there is only enough 'peanutButter' for 1 person; thus, it should return 1. # checks to see if it returns 1 self.assertEqual( how_many_servings(not_exact_recipe, not_exact_inventory), 1)
def test_insufficient_ingredients(self): # case 4.1 # tests when the amount of inventory is less than that of the recipe not_enough_recipe = {'bread': 10, 'peanutButter': 5, 'jam': 1} not_enough_inventory = {'bread': 1, 'peanutButter': 5, 'jam': 6} # since the inventory does not contain enough ingredients to even make one serving, it should return zero; the following checks to see if it does just that: self.assertEqual( how_many_servings(not_enough_recipe, not_enough_inventory), 0) # case 4.2 # tests when the inventory does not contain all the ingredients required by the recipe not_all_ingredients_recipe = {'bread': 10, 'peanutButter': 5, 'jam': 1} not_all_ingredients_inventory = { 'bread': 20, 'peanutButter': 30 # note that 'jam' is missing } # can't make even one serving in this case; thus, it should return 0. self.assertEqual( how_many_servings(not_all_ingredients_recipe, not_all_ingredients_inventory), 0) # case 4.3 # just like case 4.2, but the recipe requires 0 amount of the ingredient missing in the inventory # since the amount needed is 0, the fact that the ingredient is not present in the inventory can be ignored zero_not_all_ingredients_recipe = { 'bread': 10, 'peanutButter': 5, 'jam': 0 } zero_not_all_ingredients_inventory = { 'bread': 30, 'peanutButter': 30 # note that 'jam' is missing; however, the amount of 'jam' required by the recipe is 0 # thus, the fact that 'jam' is not in the inventory can be ignored } # we don't return 0; instead, we do the calculations, like in a normal case self.assertEqual( how_many_servings(zero_not_all_ingredients_recipe, zero_not_all_ingredients_inventory), 3)
def test_more_ingredients(self): # case 5.1 # tests when the inventory contains unnecessory ingredients in addition to the required ones # additional ingredients – 'USB cable' in this case – can just be ignored; they are not required by the recipe. Its value, therefore, should not be used in the calculations more_ingredients_recipe = {'bread': 10, 'peanutButter': 5, 'jam': 1} more_ingredients_inventory = { 'bread': 20, 'peanutButter': 30, 'jam': 3, 'USB cable': 12 } # checks to see if it ignore the unnecessory ingredient and returns the expected output self.assertEqual( how_many_servings(more_ingredients_recipe, more_ingredients_inventory), 2)
def test_inventory(self): testInventory=[['bread', None], ['peanutButter', 5], ['jam', 5]] # checks if TypeError is raised when inventory is not a dictionary with self.assertRaises(TypeError) as error: how_many_servings(self.recipe, testInventory) self.assertEqual('Inventory has to be a dictionary', str(error.exception)) # checks if TypeError is raised when the quantity of an item in the inventory is not a integer testInventory = dict(testInventory) with self.assertRaises(TypeError) as error: how_many_servings(self.recipe, testInventory) self.assertEqual('Quantities in inventory should be a positive integer', str(error.exception)) # checks if ValueError is raised when the quantity of an item in the inventory is a negative number testInventory['bread'] = -10 with self.assertRaises(ValueError) as error: how_many_servings(self.recipe, testInventory) self.assertEqual('Quantities in inventory should be a positive integer', str(error.exception))
def test_together(self): # standrard test given in example self.assertEqual(how_many_servings(self.recipe,self.inventory), 1) # checks when item in recipe not found in inventory del(self.inventory['bread']) self.assertEqual(how_many_servings(self.recipe,self.inventory), 0) # checks when inventory has more items than recipe self.inventory['egg'] = 10 self.inventory['bread'] = 20 self.assertEqual(how_many_servings(self.recipe,self.inventory), 1) # checks if the output decreases when not even one serving can be made self.inventory['jam'] = 0 self.assertEqual(how_many_servings(self.recipe,self.inventory), 0) # checks if the output increases when more than one servings can be made self.assertEqual(how_many_servings(self.recipe,self.inventoryTwenty), 20) # checks the output when recipe item has a quantity of 0 self.recipe['jam'] = 0 self.assertEqual(how_many_servings(self.recipe,self.inventoryTwenty), 20)
def test_no_recepie(self): recipe = {} inventory = {'bread': 20, 'peanutButter': 10, 'jam': 5} self.assertEqual(how_many_servings(recipe, inventory), "something is missing")
def test_1(self): recipe = {'bread': 10, 'peanutButter': 5, 'jam': 5} inventory = {'bread': 200, 'peanutButter': 100, 'jam': 50} self.assertEqual(how_many_servings(recipe, inventory), 10)
def test_not_enough_for_1(self): recipe = {'bread': 10, 'peanutButter': 5, 'jam': 5} inventory = {'bread': 20, 'peanutButter': 10, 'jam': 4} self.assertEqual(how_many_servings(recipe, inventory), 0)
def test_missing_items_in_inventory(self): recipe = {'bread': 10, 'peanutButter': 5, 'jam': 5} inventory = {'bread': 20, 'peanutButter': 10} self.assertEqual(how_many_servings(recipe, inventory), "something is missing")
def test_missing_items_in_recipie(self): recipe = {'bread': 10, 'peanutButter': 5} inventory = {'bread': 20, 'peanutButter': 10, 'jam': 5} self.assertEqual(how_many_servings(recipe, inventory), 2)