def test_find_product():
    #adapted from https://github.com/s2t2/shopping-cart-screencast/blob/testing/shopping_cart_test.py
    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": 2,
        "name": "All-Seasons Salt",
        "department": "pantry",
        "aisle": "spices seasonings",
        "price": 4.99
    }, {
        "id": 4,
        "name": "Robust Golden Unsweetened Oolong Tea",
        "department": "beverages",
        "aisle": "tea",
        "price": 2.49
    }, {
        "id": 3,
        "name":
        "Smart Ones Classic Favorites Mini Rigatoni With Vodka Cream Sauce",
        "department": "frozen",
        "aisle": "frozen meals",
        "price": 6.99
    }]
    matching_product = find_product(4, products)
    assert matching_product["name"] == "Robust Golden Unsweetened Oolong Tea"
    assert matching_product["price"] == 2.49
    matching_product_str = find_product("4", products)
    assert matching_product_str[
        "name"] == "Robust Golden Unsweetened Oolong Tea"

    with pytest.raises(IndexError):
        find_product(20, products)
示例#3
0
def test_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},
    ]

    # return matching
    matching_product = find_product("1", products)
    assert matching_product["name"] == "Chocolate Sandwich Cookies"

    # IndexError if no match
    with pytest.raises(IndexError):
        find_product("2222", products)
示例#4
0
def test_find_product():

    #create a quick list to test
    product_list = [{
        "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
    }, {
        "id": 8,
        "name": "Cut Russet Potatoes Steam N' Mash",
        "department": "frozen",
        "aisle": "frozen produce",
        "price": 4.25
    }]

    # if there is a match, it should find and return a product
    product = find_product("7", product_list)
    assert product["name"] == "Pure Coconut Water With Orange"

    #the code below is taken from Prof. Rossetti's test examples
    # if there is no match, it should raise an IndexError
    with pytest.raises(IndexError):
        find_product("100", product_list)
def test_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
        },
    ]
    matching_product = find_product("3", products)
    assert matching_product["name"] == "Robust Golden Unsweetened Oolong Tea"
def test_find_product_validID():
    try:
        result = find_product("1")
        assert result.serial == "1" and result.name == "Chocolate Sandwich Cookies"
    except:
        assert False
def test_find_product_typeNumber():
    try:
        find_product(1)
        assert False
    except:
        assert True
def test_find_product_invalidID():
    try:
        find_product("40")
        assert False
    except:
        assert True