def test_cart_contents_with_adjustment(client, cart): # set up cart contents i = factories.MyLineItemFactory(cart=cart) # add some more line items not attached to the cart factories.MyLineItemFactory() factories.MyLineItemFactory() # add an adjustment a = factories.CartDiscountFactory(cart=cart) # add more adjustments not attached to the cart factories.CartDiscountFactory() factories.CartDiscountFactory() resp = client.get('/_cart/') data = loads(resp.content.decode('utf-8')) discount = (-cart.get_subtotal() * (Decimal(a.percentage) / 100)).quantize( Decimal('.01'), rounding=ROUND_DOWN) assert data['adjustments'] == [{ 'type': 'CartDiscount', 'url': '/_cart/adjustment/{}/'.format(i.id), 'data': { 'percentage': a.percentage, }, 'total': str(discount), }] assert data['grand_total'] == str((i.product.unit_price * i.quantity) + discount)
def test_cart_contents(client, cart): # set up cart contents i = factories.MyLineItemFactory(cart=cart) # add some more line items not attached to the cart factories.MyLineItemFactory() factories.MyLineItemFactory() resp = client.get("/_cart/") data = loads(resp.content.decode("utf-8")) assert data["items"] == [ { "type": "MyLineItem", "url": "/_cart/{}/".format(i.id), "data": { "product": { "id": i.product.id, "name": i.product.name, "unit_price": str(i.product.unit_price), }, "quantity": i.quantity, }, "total": str(i.product.unit_price * i.quantity), } ] assert data["grand_total"] == str(i.product.unit_price * i.quantity)
def test_cart_contents_with_adjustment(client, cart): # set up cart contents i = factories.MyLineItemFactory(cart=cart) # add some more line items not attached to the cart factories.MyLineItemFactory() factories.MyLineItemFactory() # add an adjustment a = factories.CartDiscountFactory(cart=cart) # add more adjustments not attached to the cart factories.CartDiscountFactory() factories.CartDiscountFactory() resp = client.get("/_cart/") data = loads(resp.content.decode("utf-8")) discount = (-cart.get_subtotal() * (Decimal(a.percentage) / 100)).quantize( Decimal(".01"), rounding=ROUND_DOWN ) assert data["adjustments"] == [ { "type": "CartDiscount", "url": "/_cart/adjustment/{}/".format(i.id), "data": {"percentage": a.percentage}, "total": str(discount), } ] assert data["grand_total"] == str((i.product.unit_price * i.quantity) + discount)
def fill_cart(cart): factories.MyLineItemFactory(cart=cart) factories.MyLineItemFactory(cart=cart) factories.CartDiscountFactory(cart=cart, percentage=10) cart.delivery_address = factories.AustralianDeliveryAddressFactory() cart.payment_method = smodels.PipeCard.objects.create(card_id="Visa4242") if not cart.user: cart.email = Faker().safe_email() cart.save()
def test_change_cart_item(client, cart): i = factories.MyLineItemFactory(cart=cart) new_qty = i.quantity + 1 resp = client.patch( "/_cart/{}/".format(i.id), dumps({"data": { "quantity": new_qty }}), content_type="application/json", ) print(resp.content) assert resp.status_code == 200 assert smodels.MyLineItem.objects.get(id=i.id).quantity == new_qty
def test_cart_contents(client, cart): # set up cart contents i = factories.MyLineItemFactory(cart=cart) # add some more line items not attached to the cart factories.MyLineItemFactory() factories.MyLineItemFactory() resp = client.get('/_cart/') data = loads(resp.content.decode('utf-8')) assert data['items'] == [{ 'type': 'MyLineItem', 'url': '/_cart/{}/'.format(i.id), 'data': { 'product': { 'id': i.product.id, 'name': i.product.name, 'unit_price': str(i.product.unit_price), }, 'quantity': i.quantity, }, 'total': str(i.product.unit_price * i.quantity), }] assert data['grand_total'] == str(i.product.unit_price * i.quantity)
def test_view_cart_item(client, cart): i = factories.MyLineItemFactory(cart=cart) resp = client.get("/_cart/{}/".format(i.id)) data = loads(resp.content.decode("utf-8")) assert data == { "type": "MyLineItem", "data": { "product": { "id": i.product.id, "name": i.product.name, "unit_price": str(i.product.unit_price), }, "quantity": i.quantity, }, "total": str(i.quantity * i.product.unit_price), "url": "/_cart/{}/".format(i.id), }
def test_cannot_remove_cart_item_not_in_cart(client): i = factories.MyLineItemFactory() resp = client.delete("/_cart/{}/".format(i.id)) assert resp.status_code == 404 assert smodels.MyLineItem.objects.count() == 1
def test_remove_cart_item(client, cart): i = factories.MyLineItemFactory(cart=cart) resp = client.delete("/_cart/{}/".format(i.id)) assert resp.status_code == 204 assert smodels.MyLineItem.objects.count() == 0