Skip to content

Saleor Core: the high performance, composable, headless commerce API.

Remove unused fixtures from tests

Anna Siwik3840684febe4
diff --git a/saleor/cart/checkout/views/validators.py b/saleor/cart/checkout/views/validators.py
index 6be460de00..19b4fe75d9 100644
--- a/saleor/cart/checkout/views/validators.py
+++ b/saleor/cart/checkout/views/validators.py
@@ -9,7 +9,7 @@ from ....shipping.models import ShippingMethodCountry
def validate_cart(view):
"""Decorate a view making it require a non-empty cart.
- If the cart is empty redirects to the cart details.
+ If the cart is empty, redirect to the cart details.
"""
@wraps(view)
def func(request, cart):
diff --git a/saleor/product/urls.py b/saleor/product/urls.py
index 25af552dcb..8f1f39accb 100644
--- a/saleor/product/urls.py
+++ b/saleor/product/urls.py
@@ -8,6 +8,6 @@ urlpatterns = [
url(r'^category/(?P<path>[a-z0-9-_/]+?)-(?P<category_id>[0-9]+)/$',
views.category_index, name='category'),
url(r'(?P<slug>[a-z0-9-_]+?)-(?P<product_id>[0-9]+)/add/$',
- views.product_add_to_cart, name="add-to-cart"),
+ views.product_add_to_cart, name='add-to-cart'),
url(r'^collection/(?P<slug>[a-z0-9-_/]+?)-(?P<pk>[0-9]+)/$',
views.collection_index, name='collection')]
diff --git a/tests/conftest.py b/tests/conftest.py
index 509f394057..22f8e94dd9 100644
--- a/tests/conftest.py
+++ b/tests/conftest.py
@@ -355,18 +355,19 @@ def voucher(db): # pylint: disable=W0613
@pytest.fixture()
def order_with_lines(
- order, product_type, default_category, shipping_method, taxes):
+ order, product_type, default_category, shipping_method, vatlayer):
+ taxes = vatlayer
product = Product.objects.create(
name='Test product', price=Money('10.00', 'USD'),
product_type=product_type, category=default_category)
variant = ProductVariant.objects.create(
- product=product, sku='SKU_A', cost_price=Money(1, 'USD'), quantity=0,
- quantity_allocated=0)
+ product=product, sku='SKU_A', cost_price=Money(1, 'USD'), quantity=5,
+ quantity_allocated=3)
order.lines.create(
product_name=variant.display_product(),
product_sku=variant.sku,
is_shipping_required=variant.is_shipping_required(),
- quantity=1,
+ quantity=3,
variant=variant,
unit_price=variant.get_price(taxes=taxes),
tax_rate=taxes['standard']['value'])
@@ -375,13 +376,13 @@ def order_with_lines(
name='Test product 2', price=Money('20.00', 'USD'),
product_type=product_type, category=default_category)
variant = ProductVariant.objects.create(
- product=product, sku='SKU_B', cost_price=Money(2, 'USD'), quantity=0,
- quantity_allocated=0)
+ product=product, sku='SKU_B', cost_price=Money(2, 'USD'), quantity=2,
+ quantity_allocated=2)
order.lines.create(
product_name=variant.display_product(),
product_sku=variant.sku,
is_shipping_required=variant.is_shipping_required(),
- quantity=1,
+ quantity=2,
variant=variant,
unit_price=variant.get_price(taxes=taxes),
tax_rate=taxes['standard']['value'])
@@ -399,51 +400,6 @@ def order_with_lines(
return order
-def order_with_lines_and_stock(
- order, product_type, default_category, shipping_method):
- product = Product.objects.create(
- name='Test product', price=Money('10.00', 'USD'),
- product_type=product_type, category=default_category)
- variant = ProductVariant.objects.create(
- product=product, sku='SKU_A', cost_price=Money(1, 'USD'), quantity=5,
- quantity_allocated=3)
- order.lines.create(
- order=order,
- product_name=product.name,
- product_sku='SKU_A',
- is_shipping_required=product.product_type.is_shipping_required,
- quantity=3,
- unit_price_net=Decimal('30.00'),
- unit_price_gross=Decimal('30.00'),
- variant=variant)
- product = Product.objects.create(
- name='Test product 2', price=Money('20.00', 'USD'),
- product_type=product_type, category=default_category)
- variant = ProductVariant.objects.create(
- product=product, sku='SKU_B', cost_price=Money(2, 'USD'), quantity=2,
- quantity_allocated=2)
- order.lines.create(
- order=order,
- product_name=product.name,
- product_sku='SKU_B',
- is_shipping_required=product.product_type.is_shipping_required,
- quantity=2,
- unit_price_net=Decimal('20.00'),
- unit_price_gross=Decimal('20.00'),
- variant=variant)
-
- order.shipping_address = order.billing_address.get_copy()
- order.shipping_method_name = shipping_method.name
- method = shipping_method.price_per_country.get()
- order.shipping_method = method
- order.shipping_price = method.get_total_price()
- order.save()
- recalculate_order(order)
- order.refresh_from_db()
- return order
-
-
@pytest.fixture()
def fulfilled_order(order_with_lines):
order = order_with_lines
@@ -466,13 +422,6 @@ def draft_order(order_with_lines):
return order_with_lines
-def draft_order_with_stock(order_with_lines_and_stock):
- order_with_lines_and_stock.status = OrderStatus.DRAFT
- order_with_lines_and_stock.save(update_fields=['status'])
- return order_with_lines_and_stock
-
-
@pytest.fixture()
def payment_waiting(order_with_lines):
return order_with_lines.payments.create(
diff --git a/tests/dashboard/test_order.py b/tests/dashboard/test_order.py
index 33dfed41ab..03cffb6de5 100644
--- a/tests/dashboard/test_order.py
+++ b/tests/dashboard/test_order.py
@@ -414,15 +414,14 @@ def test_view_cancel_order_line(admin_client, draft_order):
@pytest.mark.integration
-def test_view_change_order_line_quantity(admin_client, draft_order_with_stock):
- order = draft_order_with_stock
- lines_before_quantity_change = order.lines.all()
+def test_view_change_order_line_quantity(admin_client, draft_order):
+ lines_before_quantity_change = draft_order.lines.all()
lines_before_quantity_change_count = lines_before_quantity_change.count()
line = lines_before_quantity_change.first()
url = reverse(
'dashboard:orderline-change-quantity',
- kwargs={'order_pk': order.pk, 'line_pk': line.pk})
+ kwargs={'order_pk': draft_order.pk, 'line_pk': line.pk})
response = admin_client.get(url)
assert response.status_code == 200
response = admin_client.post(url, {'quantity': 2}, follow=True)
@@ -430,7 +429,7 @@ def test_view_change_order_line_quantity(admin_client, draft_order_with_stock):
# check redirection
assert redirect_status_code == 302
assert redirected_to == reverse(
- 'dashboard:order-details', kwargs={'order_pk': order.id})
+ 'dashboard:order-details', kwargs={'order_pk': draft_order.id})
# success messages should appear after redirect
assert response.context['messages']
lines_after = Order.objects.get().lines.all()
@@ -563,18 +562,17 @@ def test_view_fulfillment_packing_slips_without_shipping(
assert response['content-type'] == 'application/pdf'
-def test_view_add_variant_to_order(
- admin_client, order_with_lines_and_stock):
- order = order_with_lines_and_stock
- order.status = OrderStatus.DRAFT
- order.save()
+def test_view_add_variant_to_order(admin_client, order_with_lines):
+ order_with_lines.status = OrderStatus.DRAFT
+ order_with_lines.save()
variant = ProductVariant.objects.get(sku='SKU_A')
line = OrderLine.objects.get(product_sku='SKU_A')
line_quantity_before = line.quantity
added_quantity = 2
url = reverse(
- 'dashboard:add-variant-to-order', kwargs={'order_pk': order.pk})
+ 'dashboard:add-variant-to-order',
+ kwargs={'order_pk': order_with_lines.pk})
data = {'variant': variant.pk, 'quantity': added_quantity}
response = admin_client.post(url, data)
@@ -582,7 +580,7 @@ def test_view_add_variant_to_order(
line.refresh_from_db()
assert response.status_code == 302
assert get_redirect_location(response) == reverse(
- 'dashboard:order-details', kwargs={'order_pk': order.pk})
+ 'dashboard:order-details', kwargs={'order_pk': order_with_lines.pk})
assert line.quantity == line_quantity_before + added_quantity
diff --git a/tests/test_asynchronous_tasks.py b/tests/test_asynchronous_tasks.py
index 108a1da99a..bd52c4a433 100644
--- a/tests/test_asynchronous_tasks.py
+++ b/tests/test_asynchronous_tasks.py
@@ -6,9 +6,8 @@ from saleor.order.emails import (
@pytest.mark.integration
def test_email_sending_asynchronously(
- transactional_db, celery_app, celery_worker,
- order_with_lines_and_stock):
- order = send_order_confirmation.delay(order_with_lines_and_stock.pk)
- payment = send_payment_confirmation.delay(order_with_lines_and_stock.pk)
+ transactional_db, celery_app, celery_worker, order_with_lines):
+ order = send_order_confirmation.delay(order_with_lines.pk)
+ payment = send_payment_confirmation.delay(order_with_lines.pk)
order.get()
payment.get()
diff --git a/tests/test_cart.py b/tests/test_cart.py
index eae2cdec3d..5ef1206f01 100644
--- a/tests/test_cart.py
+++ b/tests/test_cart.py
@@ -492,25 +492,21 @@ def test_view_empty_cart(client, request_cart):
assert response.status_code == 200
-def test_view_cart_without_taxes(client, sale, product, request_cart):
- variant = product.variants.get()
- request_cart.add(variant, 1)
+def test_view_cart_without_taxes(client, sale, request_cart_with_item):
response = client.get(reverse('cart:index'))
response_cart_line = response.context[0]['cart_lines'][0]
- cart_line = request_cart.lines.first()
+ cart_line = request_cart_with_item.lines.first()
assert not response_cart_line['get_total'].tax.amount
assert not response_cart_line['get_total'] == cart_line.get_total()
assert response.status_code == 200
def test_view_cart_with_taxes(
- settings, client, sale, product, request_cart, vatlayer):
+ settings, client, sale, request_cart_with_item, vatlayer):
settings.DEFAULT_COUNTRY = 'PL'
- variant = product.variants.get()
- request_cart.add(variant, 1)
response = client.get(reverse('cart:index'))
response_cart_line = response.context[0]['cart_lines'][0]
- cart_line = request_cart.lines.first()
+ cart_line = request_cart_with_item.lines.first()
assert response_cart_line['get_total'].tax.amount
assert not response_cart_line['get_total'] == cart_line.get_total(
taxes=vatlayer)
@@ -518,55 +514,48 @@ def test_view_cart_with_taxes(
def test_view_update_cart_quantity(
- client, local_currency, product, request_cart):
- variant = product.variants.get()
- request_cart.add(variant, 1)
+ client, local_currency, request_cart_with_item):
+ variant = request_cart_with_item.lines.get().variant
response = client.post(
reverse('cart:update-line', kwargs={'variant_id': variant.pk}),
data={'quantity': 3}, HTTP_X_REQUESTED_WITH='XMLHttpRequest')
assert response.status_code == 200
- assert request_cart.quantity == 3
+ assert request_cart_with_item.quantity == 3
def test_view_update_cart_quantity_with_taxes(
- client, local_currency, product, request_cart, vatlayer):
- variant = product.variants.get()
- request_cart.add(variant, 1)
+ client, local_currency, request_cart_with_item, vatlayer):
+ variant = request_cart_with_item.lines.get().variant
response = client.post(
reverse('cart:update-line', kwargs={'variant_id': variant.id}),
{'quantity': 3}, HTTP_X_REQUESTED_WITH='XMLHttpRequest')
assert response.status_code == 200
- assert request_cart.quantity == 3
+ assert request_cart_with_item.quantity == 3
-def test_view_invalid_update_cart(client, product, request_cart):
- variant = product.variants.get()
- request_cart.add(variant, 1)
+def test_view_invalid_update_cart(client, request_cart_with_item):
+ variant = request_cart_with_item.lines.get().variant
response = client.post(
reverse('cart:update-line', kwargs={'variant_id': variant.pk}),
data={}, HTTP_X_REQUESTED_WITH='XMLHttpRequest')
resp_decoded = json.loads(response.content.decode('utf-8'))
assert response.status_code == 400
assert 'error' in resp_decoded.keys()
- assert request_cart.quantity == 1
+ assert request_cart_with_item.quantity == 1
def test_cart_page_without_openexchagerates(
- client, product, request_cart, settings):
+ client, request_cart_with_item, settings):
settings.OPENEXCHANGERATES_API_KEY = None
- variant = product.variants.get()
- request_cart.add(variant, 1)
response = client.get(reverse('cart:index'))
context = response.context
assert context['local_cart_total'] is None
def test_cart_page_with_openexchagerates(
- client, monkeypatch, product, request_cart, settings):
+ client, monkeypatch, request_cart_with_item, settings):
settings.DEFAULT_COUNTRY = 'PL'
settings.OPENEXCHANGERATES_API_KEY = 'fake-key'
- variant = product.variants.get()
- request_cart.add(variant, 1)
response = client.get(reverse('cart:index'))
context = response.context
assert context['local_cart_total'] is None
@@ -578,18 +567,17 @@ def test_cart_page_with_openexchagerates(
assert context['local_cart_total'].currency == 'PLN'
-def test_cart_summary_page(settings, client, product, request_cart, vatlayer):
+def test_cart_summary_page(settings, client, request_cart_with_item, vatlayer):
settings.DEFAULT_COUNTRY = 'PL'
- variant = product.variants.get()
- request_cart.add(variant, 1)
response = client.get(reverse('cart:cart-summary'))
assert response.status_code == 200
content = response.context
- assert content['quantity'] == request_cart.quantity
- cart_total = request_cart.get_subtotal(taxes=vatlayer)
+ assert content['quantity'] == request_cart_with_item.quantity
+ cart_total = request_cart_with_item.get_subtotal(taxes=vatlayer)
assert content['total'] == cart_total
assert len(content['lines']) == 1
cart_line = content['lines'][0]
+ variant = request_cart_with_item.lines.get().variant
assert cart_line['variant'] == variant.name
assert cart_line['quantity'] == 1
@@ -602,11 +590,9 @@ def test_cart_summary_page_empty_cart(client, request_cart):
def test_cart_line_total_with_discount_and_taxes(
- sale, request_cart, product, taxes):
+ sale, request_cart_with_item, taxes):
sales = Sale.objects.all()
- variant = product.variants.get()
- request_cart.add(variant, 1)
- line = request_cart.lines.first()
+ line = request_cart_with_item.lines.first()
assert line.get_total(discounts=sales, taxes=taxes) == TaxedMoney(
net=Money('4.07', 'USD'), gross=Money('5.00', 'USD'))
diff --git a/tests/test_checkout.py b/tests/test_checkout.py
index 2b5633bde2..4c9f121d8b 100644
--- a/tests/test_checkout.py
+++ b/tests/test_checkout.py
@@ -33,13 +33,12 @@ from saleor.discount.models import NotApplicable, Voucher
is_shipping_required=Mock(return_value=False)),
302, reverse('cart:index'))])
def test_index_view(cart, status_code, url, rf, monkeypatch):
- request = rf.get('cart:checkout-index', follow=True)
- request.user = cart.user
- request.discounts = []
- request.taxes = None
monkeypatch.setattr(
'saleor.cart.utils.get_cart_from_request', lambda req, qs: cart)
+ request = rf.get('cart:checkout-index', follow=True)
+
response = views.index_view(request)
+
assert response.status_code == status_code
assert response.url == url
@@ -52,15 +51,13 @@ def test_checkout_discount(request_cart_with_item, sale, vatlayer):
def test_checkout_create_order_insufficient_stock(
- request_cart, customer_user, product):
- product_type = product.product_type
- product_type.is_shipping_required = False
- product_type.save()
- variant = product.variants.get()
+ request_cart, customer_user, product_without_shipping):
+ variant = product_without_shipping.variants.get()
request_cart.add(variant, quantity=10, check_quantity=False)
request_cart.user = customer_user
request_cart.billing_address = customer_user.default_billing_address
request_cart.save()
+
with pytest.raises(InsufficientStock):
create_order(
request_cart, 'tracking_code', discounts=None, taxes=None)
diff --git a/tests/test_checkout_integration.py b/tests/test_checkout_integration.py
index 4dcd0945ce..57271113f6 100644
--- a/tests/test_checkout_integration.py
+++ b/tests/test_checkout_integration.py
@@ -139,11 +139,10 @@ def test_checkout_flow_authenticated_user(
assert len(payment.get_purchased_items()) == len(order.lines.all())
-def test_address_without_shipping(request_cart_with_item, client):
- line = request_cart_with_item.lines.get()
- product_type = line.variant.product.product_type
- product_type.is_shipping_required = False
- product_type.save()
+def test_address_without_shipping(
+ request_cart, product_without_shipping, client):
+ variant = product_without_shipping.variants.get()
+ request_cart.add(variant)
response = client.get(reverse('cart:checkout-shipping-address'))
assert response.status_code == 302
@@ -151,11 +150,9 @@ def test_address_without_shipping(request_cart_with_item, client):
def test_shipping_method_without_shipping(
- request_cart_with_item, client):
- line = request_cart_with_item.lines.get()
- product_type = line.variant.product.product_type
- product_type.is_shipping_required = False
- product_type.save()
+ request_cart, product_without_shipping, client):
+ variant = product_without_shipping.variants.get()
+ request_cart.add(variant)
response = client.get(reverse('cart:checkout-shipping-method'))
assert response.status_code == 302
diff --git a/tests/test_order.py b/tests/test_order.py
index 1572b83fec..b153fc0f3e 100644
--- a/tests/test_order.py
+++ b/tests/test_order.py
@@ -79,41 +79,38 @@ def test_add_variant_to_order_adds_line_for_new_variant(
def test_add_variant_to_order_allocates_stock_for_new_variant(
- order_with_lines_and_stock, product):
- order = order_with_lines_and_stock
+ order_with_lines, product):
variant = product.variants.get()
stock_before = variant.quantity_allocated
- add_variant_to_order(order, variant, 1)
+ add_variant_to_order(order_with_lines, variant, 1)
variant.refresh_from_db()
assert variant.quantity_allocated == stock_before + 1
def test_add_variant_to_order_edits_line_for_existing_variant(
- order_with_lines_and_stock):
- order = order_with_lines_and_stock
- existing_line = order.lines.first()
+ order_with_lines):
+ existing_line = order_with_lines.lines.first()
variant = existing_line.variant
- lines_before = order.lines.count()
+ lines_before = order_with_lines.lines.count()
line_quantity_before = existing_line.quantity
- add_variant_to_order(order, variant, 1)
+ add_variant_to_order(order_with_lines, variant, 1)
existing_line.refresh_from_db()
- assert order.lines.count() == lines_before
+ assert order_with_lines.lines.count() == lines_before
assert existing_line.product_sku == variant.sku
assert existing_line.quantity == line_quantity_before + 1
def test_add_variant_to_order_allocates_stock_for_existing_variant(
- order_with_lines_and_stock):
- order = order_with_lines_and_stock
- existing_line = order.lines.first()
+ order_with_lines):
+ existing_line = order_with_lines.lines.first()
variant = existing_line.variant
stock_before = variant.quantity_allocated
- add_variant_to_order(order, variant, 1)
+ add_variant_to_order(order_with_lines, variant, 1)
variant.refresh_from_db()
assert variant.quantity_allocated == stock_before + 1
@@ -286,8 +283,7 @@ def test_order_queryset_confirmed(draft_order):
Order.objects.create(status=OrderStatus.UNFULFILLED),
Order.objects.create(status=OrderStatus.PARTIALLY_FULFILLED),
Order.objects.create(status=OrderStatus.FULFILLED),
- Order.objects.create(status=OrderStatus.CANCELED)
- ]
+ Order.objects.create(status=OrderStatus.CANCELED)]
confirmed_orders = Order.objects.confirmed()
@@ -370,7 +366,7 @@ def test_ajax_order_shipping_methods_list_different_country(
assert resp_decoded == {'results': shipping_methods_list}
-def test_update_order_prices(order_with_lines, vatlayer):
+def test_update_order_prices(order_with_lines):
taxes = get_taxes_for_country(Country('DE'))
address = order_with_lines.shipping_address
address.country = 'DE'
@@ -389,5 +385,6 @@ def test_update_order_prices(order_with_lines, vatlayer):
assert line_1.unit_price == price_1
assert line_2.unit_price == price_2
assert order_with_lines.shipping_price == shipping_price
- total = price_1 + price_2 + shipping_price
+ total = (
+ line_1.quantity * price_1 + line_2.quantity * price_2 + shipping_price)
assert order_with_lines.total == total
diff --git a/tests/test_seo_schema.py b/tests/test_seo_schema.py
index 60f79d7cb9..88fa620c38 100644
--- a/tests/test_seo_schema.py
+++ b/tests/test_seo_schema.py
@@ -16,32 +16,29 @@ def test_get_organization(site_settings):
assert result['name'] == example_name
-def test_get_product_data_without_image(order_with_lines_and_stock):
+def test_get_product_data_without_image(order_with_lines):
"""Tested OrderLine Product has no image assigned."""
- order_line = order_with_lines_and_stock.lines.first()
+ line = order_with_lines.lines.first()
organization = get_organization()
- result = get_product_data(order_line, organization)
+ result = get_product_data(line, organization)
assert 'image' not in result['itemOffered']
-def test_get_product_data_with_image(
- order_with_lines_and_stock, product_with_image):
- order = order_with_lines_and_stock
- order_line = order.lines.first()
+def test_get_product_data_with_image(order_with_lines, product_with_image):
+ line = order_with_lines.lines.first()
variant = product_with_image.variants.first()
- order_line.variant = variant
- order_line.product_name = variant.display_product()
- order_line.save()
+ line.variant = variant
+ line.product_name = variant.display_product()
+ line.save()
organization = get_organization()
- result = get_product_data(order_line, organization)
+ result = get_product_data(line, organization)
assert 'image' in result['itemOffered']
assert result['itemOffered']['name'] == variant.display_product()
-def test_get_order_confirmation_markup(order_with_lines_and_stock):
- order = order_with_lines_and_stock
+def test_get_order_confirmation_markup(order_with_lines):
try:
- result = get_order_confirmation_markup(order)
+ result = get_order_confirmation_markup(order_with_lines)
except TypeError:
pytest.fail('Function output is not JSON serializable')