예제 #1
0
def test_cartitem_mapper_can_load_items(session):
    session.execute("INSERT INTO cart_items (itemid, sku, qty) VALUES "
                    '("item1", "AMUL-GHEE", 2),'
                    '("item2", "KAVAN-CHAPATHI", 5),'
                    '("item3", "MURUKU-SNAKCS", 6)')
    expected = [
        model.CartItem("item1", "AMUL-GHEE", 2),
        model.CartItem("item2", "KAVAN-CHAPATHI", 5),
        model.CartItem("item3", "MURUKU-SNAKCS", 6),
    ]
    assert session.query(model.CartItem).all() == expected
예제 #2
0
def test_cartitem_mapper_can_save_items(session):
    new_item = model.CartItem("item1", "KASHMIRI-CHILLY", 2)
    session.add(new_item)
    session.commit()

    rows = list(session.execute('SELECT itemid, sku, qty FROM "cart_items"'))
    assert rows == [("item1", "KASHMIRI-CHILLY", 2)]
예제 #3
0
def test_error_for_invalid_sku():
    item = model.CartItem("i1", "NONEXISTENTSKU", 10)
    product = model.Product("p1", "AREALSKU", 100,  100, 'A REAL SKU' , 100)
    repo = FakeRepository([product])

    with pytest.raises(services.InvalidSku, match="Invalid sku NONEXISTENTSKU"):
        services.add(item, repo, FakeSession())
예제 #4
0
def test_returns_item():
    item = model.CartItem("i1", "CHAKRI ATTA", 10)
    product = model.Product("p1", "CHAKRI ATTA", 100,  100, 'CHAKRI ATTA' , 16)
    repo = FakeRepository([product])

    result = services.add(item, repo, FakeSession())
    assert result == "p1"
예제 #5
0
def test_saving_items(session):
    product = model.Product("product1", "sku1", 100, 10, "IDAYAM", 5.65)
    item = model.CartItem("item1", "sku1", 2)
    product.add(item)
    session.add(product)
    session.commit()
    rows = list(session.execute('SELECT cartitem_id, product_id FROM "items"'))
    assert rows == [(product.id, item.id)]
예제 #6
0
def test_commits():
    item = model.CartItem("i1", "CHAKRI ATTA", 10)
    product = model.Product("p1", "CHAKRI ATTA", 100,  100, 'CHAKRI ATTA' , 16)
    repo = FakeRepository([product])
    session = FakeSession()

    services.add(item, repo, session)
    assert session.committed is True
예제 #7
0
def add_endpoint():
    session = get_session()
    repo = repository.SqlAlchemyRepository(session)
    item = model.CartItem(
        request.json["itemid"],
        request.json["sku"],
        request.json["qty"],
    )

    try:
        productref = services.add(item, repo, session)
    except (model.OutOfStock, services.InvalidSku) as e:
        return {"message": str(e)}, 400

    return {"productref": productref}, 201
예제 #8
0
def test_repository_can_retrieve_a_product_with_items(session):
    cartitem_id = insert_cart_item(session)
    product1_id = insert_product(session, "product1")
    insert_product(session, "product2")
    insert_item(session, cartitem_id, product1_id)

    repo = repository.SqlAlchemyRepository(session)
    retrieved = repo.get("product1")

    expected = model.Product("product1", "EASTERN-MASALA", 100, 10, "EASTERN",
                             1.65)
    assert retrieved == expected  # Product.__eq__ only compares reference
    assert retrieved.sku == expected.sku
    assert retrieved._purchased_quantity == expected._purchased_quantity
    assert retrieved._items == {
        model.CartItem("item1", "EASTERN-MASALA", 5),
    }
예제 #9
0
def test_retrieving_items(session):
    session.execute(
        'INSERT INTO cart_items (itemid, sku, qty) VALUES ("item1", "sku1", 12)'
    )
    [[ciid]] = session.execute(
        "SELECT id FROM cart_items WHERE itemid=:itemid AND sku=:sku",
        dict(itemid="item1", sku="sku1"),
    )
    session.execute(
        "INSERT INTO products (reference, sku, _purchased_quantity, maxAllowedPurchaseQty, brand, price)"
        ' VALUES ("product1", "sku1", 100, 10, "IDAYAM", 5.65)')
    [[pid]] = session.execute(
        "SELECT id FROM products WHERE reference=:ref AND sku=:sku",
        dict(ref="product1", sku="sku1"),
    )
    session.execute(
        "INSERT INTO items (cartitem_id, product_id) VALUES (:ciid, :pid)",
        dict(ciid=ciid, pid=pid),
    )

    product = session.query(model.Product).one()

    assert product._items == {model.CartItem("item1", "sku1", 12)}