def test_remove_unavailable_variants(cart, product): variant = product.variants.get() add_variant_to_cart(cart, variant) variant.quantity = 0 variant.save() utils.remove_unavailable_variants(cart) assert len(cart) == 0
def test_adding_same_variant(cart, product, taxes): variant = product.variants.get() add_variant_to_cart(cart, variant, 1) add_variant_to_cart(cart, variant, 2) assert len(cart) == 1 assert cart.quantity == 3 cart_total = TaxedMoney(net=Money('24.39', 'USD'), gross=Money(30, 'USD')) assert cart.get_subtotal(taxes=taxes) == cart_total
def test_view_checkout_delivery_method_without_delivery( request_cart, skill_without_delivery, client): variant = skill_without_delivery.variants.get() add_variant_to_cart(request_cart, variant) url = reverse('checkout:delivery-method') response = client.get(url) assert response.status_code == 302 assert get_redirect_location(response) == reverse('checkout:summary')
def test_replace_cart_line_form(cart, product): variant = product.variants.get() initial_quantity = 1 replaced_quantity = 4 add_variant_to_cart(cart, variant, initial_quantity) data = {'quantity': replaced_quantity} form = forms.ReplaceCartLineForm(data=data, cart=cart, variant=variant) assert form.is_valid() form.save() assert cart.quantity == replaced_quantity
def test_view_invalid_add_to_cart(client, skill, request_cart): variant = skill.variants.get() add_variant_to_cart(request_cart, variant, 2) response = client.post( reverse('skill:add-to-cart', kwargs={ 'slug': skill.get_slug(), 'skill_id': skill.pk }), {}) assert response.status_code == 200 assert request_cart.quantity == 2
def test_create_task_insufficient_stock(request_cart, customer_user, skill_without_delivery): variant = skill_without_delivery.variants.get() add_variant_to_cart(request_cart, variant, 10, check_quantity=False) request_cart.user = customer_user request_cart.billing_address = customer_user.default_billing_address request_cart.delivery_address = customer_user.default_billing_address request_cart.save() with pytest.raises(InsufficientStock): create_order(request_cart, 'tracking_code', discounts=None, taxes=None)
def test_replace_cartline_form_when_insufficient_stock(monkeypatch, cart, product): variant = product.variants.get() initial_quantity = 1 replaced_quantity = 4 add_variant_to_cart(cart, variant, initial_quantity) exception_mock = InsufficientStock(Mock(quantity_available=2)) monkeypatch.setattr( 'remote_works.skill.models.SkillVariant.check_quantity', Mock(side_effect=exception_mock)) data = {'quantity': replaced_quantity} form = forms.ReplaceCartLineForm(data=data, cart=cart, variant=variant) assert not form.is_valid() with pytest.raises(KeyError): form.save() assert cart.quantity == initial_quantity
def test_add_to_cart_form(cart, product): variant = product.variants.get() add_variant_to_cart(cart, variant, 3) data = {'quantity': 1} form = forms.AddToCartForm(data=data, cart=cart, product=product) form.get_variant = Mock(return_value=variant) assert form.is_valid() form.save() assert cart.lines.count() == 1 assert cart.lines.filter(variant=variant).exists() with pytest.raises(NotImplementedError): data = {'quantity': 1} form = forms.AddToCartForm(data=data, cart=cart, product=product) form.is_valid() data = {} form = forms.AddToCartForm(data=data, cart=cart, product=product) assert not form.is_valid()
def test_check_skill_availability_and_warn(monkeypatch, cart, product): variant = product.variants.get() add_variant_to_cart(cart, variant) monkeypatch.setattr('django.contrib.messages.warning', Mock(warning=Mock())) monkeypatch.setattr( 'remote_works.checkout.utils.contains_unavailable_variants', Mock(return_value=False)) utils.check_skill_availability_and_warn(MagicMock(), cart) assert len(cart) == 1 monkeypatch.setattr( 'remote_works.checkout.utils.contains_unavailable_variants', Mock(return_value=True)) monkeypatch.setattr( 'remote_works.checkout.utils.remove_unavailable_variants', lambda c: add_variant_to_cart(cart, variant, 0, replace=True)) utils.check_skill_availability_and_warn(MagicMock(), cart) assert len(cart) == 0
def test_delivery_detection(cart, product): assert not cart.is_delivery_required() variant = product.variants.get() add_variant_to_cart(cart, variant, replace=True) assert cart.is_delivery_required()
def test_getting_line(cart, product): variant = product.variants.get() assert cart.get_line(variant) is None add_variant_to_cart(cart, variant) assert cart.lines.get() == cart.get_line(variant)
def test_adding_invalid_quantity(cart, product): variant = product.variants.get() with pytest.raises(ValueError): add_variant_to_cart(cart, variant, -1)
def test_replacing_same_variant(cart, product): variant = product.variants.get() add_variant_to_cart(cart, variant, 1, replace=True) add_variant_to_cart(cart, variant, 2, replace=True) assert len(cart) == 1 assert cart.quantity == 2
def test_adding_zero_quantity(cart, product): variant = product.variants.get() add_variant_to_cart(cart, variant, 0) assert len(cart) == 0
def test_adding_without_checking(cart, product): variant = product.variants.get() add_variant_to_cart(cart, variant, 1000, check_quantity=False) assert len(cart) == 1