def test_get_product(): #Tests whether product identifier works correctly for all categories products = [{ "id": 5, "name": "Green Chile Anytime Sauce", "department": "pantry", "aisle": "marinades meat preparation", "price": 7.99 }, { "id": 6, "name": "Dry Nose Oil", "department": "personal care", "aisle": "cold flu allergy", "price": 21.99 }, { "id": 7, "name": "Pure Coconut Water With Orange", "department": "beverages", "aisle": "juice nectars", "price": 3.50 }] #def find_product(selected_id, products): #selected id, products #Ensure result is correct matching_product = find_product("5", products) assert matching_product["name"] == "Green Chile Anytime Sauce" assert matching_product["department"] == "pantry" assert matching_product["aisle"] == "marinades meat preparation" assert matching_product["price"] == 7.99 #Ensure error is processed when ID doesn't exist #Adapted from professor and: https://docs.pytest.org/en/latest/assert.html with pytest.raises(IndexError): find_product("50", products)
def test_find_product(): products = [ { "id": 1, "name": "Chocolate Sandwich Cookies", "department": "snacks", "aisle": "cookies cakes", "price": 3.50 }, { "id": 3, "name": "Robust Golden Unsweetened Oolong Tea", "department": "beverages", "aisle": "tea", "price": 2.49 }, { "id": 2, "name": "All-Seasons Salt", "department": "pantry", "aisle": "spices seasonings", "price": 4.99 }, ] # if there is a match, it should find and return a product matching_product = find_product("2", products) assert matching_product["name"] == "All-Seasons Salt" # if there is no match, it should raise an IndexError with pytest.raises(IndexError): find_product("2222", products)
def test_find_product(): products = [ {"id":1, "name": "Chocolate Sandwich Cookies", "department": "snacks", "aisle": "cookies cakes", "price": 3.50, "price_per": "item"}, {"id":3, "name": "Robust Golden Unsweetened Oolong Tea", "department": "beverages", "aisle": "tea", "price": 2.49, "price_per": "item"}, {"id":2, "name": "All-Seasons Salt", "department": "pantry", "aisle": "spices seasonings", "price": 4.99, "price_per": "item"}, {"id":4, "name": "Smart Ones Classic Favorites Mini Rigatoni With Vodka Cream Sauce", "department": "frozen", "aisle": "frozen meals", "price": 6.99, "price_per": "item"}, ] matching_product = find_product("2", products) assert matching_product["department"] == "pantry" matching_product = find_product("4", products) assert matching_product["name"] == "Smart Ones Classic Favorites Mini Rigatoni With Vodka Cream Sauce" with pytest.raises(IndexError): find_product("5968", products)
def test_find_product(): # tests the find_product() products = [{ "id": 1, "name": "Chocolate Sandwich Cookies", "department": "snacks", "aisle": "cookies cakes", "price": 3.50 }, { "id": 2, "name": "All-Seasons Salt", "department": "pantry", "aisle": "spices seasonings", "price": 4.99 }, { "id": 3, "name": "Robust Golden Unsweetened Oolong Tea", "department": "beverages", "aisle": "tea", "price": 2.49 }, { "id": 4, "name": "Smart Ones Classic Favorites Mini Rigatoni With Vodka Cream Sauce", "department": "frozen", "aisle": "frozen meals", "price": 6.99 }, { "id": 5, "name": "Green Chile Anytime Sauce", "department": "pantry", "aisle": "marinades meat preparation", "price": 7.99 }] # this assertion tests if find_product() returns the correct dictionary for a given id in a list of dictionaries matching_product = find_product("1", products) assert matching_product["aisle"] == "cookies cakes" # tests for error if invalid id number is passed as argument into find_product() with pytest.raises(IndexError): find_product("2345", products)