Skip to content

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

Fix tests

Pacu26807b9d71bb1
diff --git a/saleor/dashboard/product/views.py b/saleor/dashboard/product/views.py
index 4900529998..a24c71ba88 100644
--- a/saleor/dashboard/product/views.py
+++ b/saleor/dashboard/product/views.py
@@ -176,8 +176,7 @@ def product_create(request, type_pk):
@staff_member_required
@permission_required('product.view_product')
def product_detail(request, pk):
- products = Product.objects.prefetch_related(
- 'variants__stock', 'images').all()
+ products = Product.objects.prefetch_related('variants', 'images').all()
product = get_object_or_404(products, pk=pk)
variants = product.variants.all()
images = product.images.all()
@@ -375,17 +374,13 @@ def variant_edit(request, product_pk, variant_pk):
@permission_required('product.view_product')
def variant_details(request, product_pk, variant_pk):
product = get_object_or_404(Product, pk=product_pk)
- qs = product.variants.prefetch_related(
- 'stock__location')
- variant = get_object_or_404(qs, pk=variant_pk)
+ variant = get_object_or_404(product.variants.all(), pk=variant_pk)
# If the product type of this product assumes no variants, redirect to
# product details page that has special UI for products without variants.
-
if not product.product_type.has_variants:
return redirect('dashboard:product-detail', pk=product.pk)
- stock = variant.stock.all()
images = variant.images.all()
costs_data = get_variant_costs_data(variant)
if costs_data.costs:
@@ -396,9 +391,8 @@ def variant_details(request, product_pk, variant_pk):
costs = {}
ctx = {
- 'images': images, 'product': product, 'stock': stock,
- 'variant': variant, 'costs': costs, 'margins': costs_data.margins,
- 'is_empty': not stock.exists()}
+ 'images': images, 'product': product,
+ 'variant': variant, 'costs': costs, 'margins': costs_data.margins}
return TemplateResponse(
request,
'dashboard/product/product_variant/detail.html',
diff --git a/saleor/order/models.py b/saleor/order/models.py
index d102410f72..b8497ba931 100644
--- a/saleor/order/models.py
+++ b/saleor/order/models.py
@@ -181,7 +181,8 @@ class OrderLine(models.Model):
Product, blank=True, null=True, related_name='+',
on_delete=models.SET_NULL)
variant = models.ForeignKey(
- ProductVariant, related_name='stock', on_delete=models.CASCADE)
+ ProductVariant, related_name='stock', on_delete=models.SET_NULL,
+ blank=True, null=True)
product_name = models.CharField(max_length=128)
product_sku = models.CharField(max_length=32)
is_shipping_required = models.BooleanField()
diff --git a/saleor/order/utils.py b/saleor/order/utils.py
index bd33390248..94207c4708 100644
--- a/saleor/order/utils.py
+++ b/saleor/order/utils.py
@@ -144,7 +144,7 @@ def add_variant_to_order(
order, variant, total_quantity) if add_to_existing else total_quantity
price = variant.get_price_per_item(discounts)
while quantity_left > 0:
- quantity_available = variant.quantity_available()
+ quantity_available = variant.quantity_available
if not quantity_available:
raise InsufficientStock(variant)
quantity = (
@@ -177,20 +177,18 @@ def add_variant_to_existing_lines(order, variant, total_quantity):
Returns quantity that could not be fulfilled with existing lines.
"""
# order descending by lines' stock available quantity
- lines = order.lines.filter(
- product=variant.product, product_sku=variant.sku,
- stock__isnull=False).order_by(
- F('stock__quantity_allocated') - F('stock__quantity'))
+ lines = order.lines.filter(variant=variant).order_by(
+ F('variant__quantity_allocated') - F('variant__quantity'))
quantity_left = total_quantity
for line in lines:
quantity = (
- line.stock.quantity_available
- if quantity_left > line.stock.quantity_available
+ line.variant.quantity_available
+ if quantity_left > line.variant.quantity_available
else quantity_left)
line.quantity += quantity
line.save()
- allocate_stock(line.stock, quantity)
+ allocate_stock(line.variant, quantity)
quantity_left -= quantity
if quantity_left == 0:
break
@@ -204,7 +202,7 @@ def merge_duplicates_into_order_line(line):
"""
lines = line.order.lines.filter(
product=line.product, product_name=line.product_name,
- product_sku=line.product_sku, stock=line.stock,
+ product_sku=line.product_sku, variant=line.variant,
is_shipping_required=line.is_shipping_required)
if lines.count() > 1:
line.quantity = sum([line.quantity for line in lines])
@@ -224,11 +222,11 @@ def change_order_line_quantity(line, new_quantity):
def restock_order_lines(order):
"""Return ordered products to corresponding stocks."""
for line in order:
- if line.stock:
+ if line.variant:
if line.quantity_unfulfilled > 0:
- deallocate_stock(line.stock, line.quantity_unfulfilled)
+ deallocate_stock(line.variant, line.quantity_unfulfilled)
if line.quantity_fulfilled > 0:
- increase_stock(line.stock, line.quantity_fulfilled)
+ increase_stock(line.variant, line.quantity_fulfilled)
line.quantity_fulfilled = 0
line.save(update_fields=['quantity_fulfilled'])
diff --git a/templates/dashboard/product/_stock_table.html b/templates/dashboard/product/_stock_table.html
deleted file mode 100644
index 96b9450999..0000000000
--- a/templates/dashboard/product/_stock_table.html
+++ /dev/null
@@ -1,37 +0,0 @@
-{% load i18n %}
-{% load prices_i18n %}
-{% load utils %}
-
-<table class="bordered highlight responsive data-table">
- <thead>
- <tr>
- <th class="wide">{% trans "Location" %}</th>
- <th>{% trans "Quantity" %}</th>
- <th>{% trans "Allocated" %}</th>
- <th class="right-align">{% trans "Cost" %}</th>
- <th class="right-align">{% trans "Gross margin" %}</th>
- </tr>
- </thead>
- <tbody>
- {% for stock_item in stock %}
- <tr class="{% if stock_item.quantity_available == 0 %}danger{% endif %}" data-action-go="{% url "dashboard:variant-stock-details" product_pk=product.pk variant_pk=variant.pk stock_pk=stock_item.pk %}">
- <td class="wide">{{ stock_item.location }}</td>
- <td>{{ stock_item.quantity }}</td>
- <td>{{ stock_item.quantity_allocated }}</td>
- <td class="right-align">{% if stock_item.cost_price %}{{ stock_item.cost_price|amount }}{% else %}-{% endif %}</td>
- <td class="right-align">
- {% margin_for_variant stock=stock_item as margin %}
- {% if margin %}
- {{ margin }}%
- {% else %}
- -
- {% endif %}
- </td>
- </tr>
- {% empty %}
- <tr>
- <td colspan="4">{% trans "No stock records found" context "Dashboard variant details page" %}</td>
- </tr>
- {% endfor %}
- </tbody>
-</table>
diff --git a/templates/dashboard/product/detail.html b/templates/dashboard/product/detail.html
index 01d7107a5a..e1209e2b65 100644
--- a/templates/dashboard/product/detail.html
+++ b/templates/dashboard/product/detail.html
@@ -135,21 +135,6 @@
</div>
</div>
{% endif %}
- {% if no_variants and only_variant %}
- <div class="card">
- <div class="card-content">
- <span class="card-title">{% trans "Stock" context "Dashboard variant details view" %}</span>
- </div>
- <div class="data-table-header-action">
- <a class="btn-flat waves-effect" href="{% url "dashboard:variant-stock-add" product_pk=product.pk variant_pk=only_variant.pk %}">
- {% trans "Add" context "Dashboard action" %}
- </a>
- </div>
- <div class="data-table-container">
- {% include "dashboard/product/_stock_table.html" with product=product variant=only_variant stock=stock %}
- </div>
- </div>
- {% endif %}
</div>
{% if perms.product.edit_product %}
<div class="col s12 l3">
diff --git a/templates/dashboard/product/product_variant/detail.html b/templates/dashboard/product/product_variant/detail.html
index d2369bec64..3c94caa066 100644
--- a/templates/dashboard/product/product_variant/detail.html
+++ b/templates/dashboard/product/product_variant/detail.html
@@ -100,23 +100,6 @@
</div>
{% endif %}
</div>
- {% if perms.product.view_stock %}
- <div class="card">
- <div class="card-content">
- <span class="card-title">{% trans "Stock" context "Dashboard variant details view" %}</span>
- </div>
- {% if perms.product.edit_stock %}
- <div class="data-table-header-action">
- <a class="btn-flat waves-effect" href="{% url "dashboard:variant-stock-add" product_pk=product.pk variant_pk=variant.pk %}">
- {% trans "Add" context "Dashboard action" %}
- </a>
- </div>
- {% endif %}
- <div class="data-table-container">
- {% include "dashboard/product/_stock_table.html" with product=product variant=variant stock=stock %}
- </div>
- </div>
- {% endif %}
</div>
<div class="col m12 l3">
<div class="card">
diff --git a/templates/dashboard/product/stock/detail.html b/templates/dashboard/product/stock/detail.html
deleted file mode 100644
index 911820c7c9..0000000000
--- a/templates/dashboard/product/stock/detail.html
+++ /dev/null
@@ -1,76 +0,0 @@
-{% extends "dashboard/base.html" %}
-
-{% load i18n %}
-{% load prices_i18n %}
-{% load staticfiles %}
-
-{% block title %}
- {{ stock.location }} - {{ block.super }}
-{% endblock %}
-
-{% block body_class %}body-products{% endblock %}
-
-{% block menu_products_class %}active{% endblock %}
-
-{% block breadcrumbs %}
- <ul class="breadcrumbs breadcrumbs--history">
- <li>
- <a href="{% url "dashboard:product-list" %}" class="breadcrumb">
- {% trans "Products" context "Product form breadcrumbs" %}
- </a>
- </li>
- <li class="back-mobile">
- <a href="{% url "dashboard:product-list" %}">
- <svg data-src="{% static "dashboard/images/arrow-left.svg" %}" fill="#fff" width="20px" height="20px" />
- </a>
- </li>
- <li class="hide-on-med-and-down">
- <a href="{% url "dashboard:product-detail" pk=product.pk %}" class="breadcrumbs--ellipsed-item">
- {{ product }}
- </a>
- </li>
- <li>
- <a href="{% url 'dashboard:variant-details' product_pk=product.pk variant_pk=variant.pk %}" class="breadcrumbs--ellipsed-item">
- {{ variant }}
- </a>
- </li>
- <li>
- <span class="breadcrumbs--ellipsed-item">
- {{ stock.location }}
- </span>
- </li>
- </ul>
-{% endblock %}
-
-{% block header_extra %}
-{% endblock %}
-
-{% block content %}
- <div class="row">
- <div class="col m12 l9">
- <div class="card">
- <div class="card-content">
- <p class="card-title">{{ variant }}</p>
- <p></p>
- <div class="row">
- <div class="col s12">
- <p class="text-padding">{% trans "Location" context "Dashboard stock details view" %}: {{ stock.location }}</p>
- <p class="text-padding">{% trans "Quantity" context "Dashboard stock details view" %}: {{ stock.quantity }}</p>
- <p class="text-padding">{% trans "Allocated" context "Dashboard stock details view" %}: {{ stock.quantity_allocated }}</p>
- </div>
- </div>
- </div>
- {% if perms.product.edit_stock %}
- <div class="card-action">
- <a class="btn-flat waves-effect" href="{% url "dashboard:variant-stock-update" product_pk=product.pk variant_pk=variant.pk stock_pk=stock.pk %}">
- {% trans "Edit stock" context "Dashboard action" %}
- </a>
- <a href="#base-modal" class="btn-flat waves-effect modal-trigger-custom" data-href="{% url 'dashboard:variant-stock-delete' product_pk=product.pk variant_pk=variant.pk stock_pk=stock.pk %}">
- {% trans "Remove stock" context "Dashboard action" %}
- </a>
- </div>
- {% endif %}
- </div>
- </div>
- </div>
-{% endblock %}
diff --git a/templates/dashboard/product/stock/form.html b/templates/dashboard/product/stock/form.html
deleted file mode 100644
index 2b86a0c9a4..0000000000
--- a/templates/dashboard/product/stock/form.html
+++ /dev/null
@@ -1,113 +0,0 @@
-{% extends "dashboard/base.html" %}
-{% load i18n %}
-{% load materializecss %}
-{% load staticfiles %}
-
-{% block title %}
- {% if stock.pk %}
- {% blocktrans trimmed context "Dashboard stock" %}
- Edit - Stock {{ variant }}
- {% endblocktrans %}
- {% else %}
- {% trans "Add stock" context "Stock form page title" %}
- {% endif %}
- - {{ block.super }}
-{% endblock %}
-
-{% block body_class %}body-store body-products{% endblock %}
-
-{% block menu_products_class %}active{% endblock %}
-
-{% block header_menu %}
- {% url "dashboard:variant-details" product_pk=product.pk variant_pk=variant.pk as url %}
- {% include "dashboard/includes/_back_link.html" with url=url %}
-{% endblock %}
-
-{% block breadcrumbs %}
- <ul class="breadcrumbs breadcrumbs--history">
- <li>
- <a href="{% url "dashboard:product-list" %}" class="breadcrumb">
- {% trans "Products" context "Dashboard products list" %}
- </a>
- </li>
- <li class="back-mobile">
- <a href="{% url "dashboard:product-detail" pk=product.pk %}">
- <svg data-src="{% static "dashboard/images/chevron-left.svg" %}" />
- </a>
- </li>
- <li>
- <a href="{% url "dashboard:product-detail" pk=product.pk %}" class="breadcrumbs--ellipsed-item">
- {{ product }}
- </a>
- </li>
- {% if stock.pk %}
- <li>
- <a href="{% url "dashboard:variant-details" product_pk=product.pk variant_pk=variant.pk %}" class="breadcrumbs--ellipsed-item">
- {% blocktrans trimmed with variant=stock.variant context "Dashboard stock" %}
- Stock {{ variant }}
- {% endblocktrans %}
- </a>
- </li>
- <li>
- <span class="breadcrumbs--ellipsed-item">
- {% trans "Edit" context "Stock form breadcrumbs" %}
- </span>
- </li>
- {% else %}
- <li>
- <span class="hide-on-med-and-down breadcrumbs--ellipsed-item">
- {% trans "Add stock" context "Stock form breadcrumbs" %}
- </span>
- <span class="hide-on-large-only breadcrumbs--ellipsed-item">
- {% blocktrans trimmed context "Stock form breadcrumbs" %}
- Add stock for {{ product }}
- {% endblocktrans %}
- </span>
- </li>
- {% endif %}
- </ul>
-{% endblock %}
-
-{% block menu_catalogue_class %} active{% endblock %}
-
-{% block content %}
- <div class="row">
- <div class="col s12 l9">
- <div class="card">
- <form method="post" id="form-stock" enctype="multipart/form-data">
- <div class="card-content">
- {% csrf_token %}
- <div class="row">
- <div class="col s12 m4">
- <div class="row">
- {{ form.quantity|materializecss }}
- </div>
- <div class="row">
- {{ form.cost_price|materializecss }}
- </div>
- </div>
- </div>
- </div>
- <div class="card-action right-align">
- {% if stock.pk %}
- <a href="{% url "dashboard:variant-stock-details" product_pk=product.pk variant_pk=variant.pk stock_pk=stock.pk %}" class="btn-flat waves-effect">
- {% trans "Cancel" context "Dashboard cancel action" %}
- </a>
- <button type="submit" class="btn waves-effect">
- {% trans "Save" context "Dashboard update action" %}
- </button>
- {% else %}
- <a href="{% url "dashboard:variant-details" product_pk=product.pk variant_pk=variant.pk %}" class="btn-flat waves-effect">
- {% trans "Cancel" context "Dashboard cancel action" %}
- </a>
- <button type="submit" class="btn waves-effect">
- {% trans "Create" context "Dashboard create action" %}
- </button>
- {% endif %}
- </div>
- </form>
- </div>
- </div>
- <div class="col s12 m3 help-box"></div>
- </div>
-{% endblock %}
diff --git a/templates/dashboard/product/stock/modal/confirm_delete.html b/templates/dashboard/product/stock/modal/confirm_delete.html
deleted file mode 100644
index cd6885927b..0000000000
--- a/templates/dashboard/product/stock/modal/confirm_delete.html
+++ /dev/null
@@ -1,28 +0,0 @@
-{% extends "dashboard/base_modal.html" %}
-
-{% load i18n %}
-{% load prices_i18n %}
-
-{% block form_class %}{% endblock %}
-
-{% block action_url %}
-{% url 'dashboard:variant-stock-delete' product_pk=product.pk variant_pk=variant.pk stock_pk=stock.pk %}
-{% endblock %}
-
-{% block title %}
-{% blocktrans context "Dashboard confirmation modal" %}
- Remove stock {{ stock }}
-{% endblocktrans %}
-{% endblock title %}
-
-{% block content %}
- <div class="col s12">
- {% blocktrans context "Dashboard confirmation modal" %}
- Are you sure you want to remove stock <strong>{{ stock }}</strong>?
- {% endblocktrans %}
- </div>
-{% endblock content %}
-
-{% block primary_action %}
-{% trans "Remove" context "Dashboard delete action" %}
-{% endblock %}
diff --git a/tests/conftest.py b/tests/conftest.py
index 124330f0e4..0b3a134fd3 100644
--- a/tests/conftest.py
+++ b/tests/conftest.py
@@ -21,7 +21,8 @@ from saleor.dashboard.order.utils import fulfill_order_line
from saleor.discount.models import Sale, Voucher
from saleor.menu.models import Menu, MenuItem
from saleor.order import OrderStatus
-from saleor.order.models import Order, OrderLine
+from saleor.order.models import Order
+from saleor.menu.models import Menu, MenuItem
from saleor.order.utils import recalculate_order
from saleor.page.models import Page
from saleor.product.models import (
@@ -268,7 +269,7 @@ def product(product_type, default_category):
variant = ProductVariant.objects.create(
product=product, sku='123', attributes=variant_attributes,
- cost_price=Money('1.00', 'USD'), quantity=5, quantity_allocated=5)
+ cost_price=Money('1.00', 'USD'), quantity=10, quantity_allocated=1)
return product
diff --git a/tests/dashboard/test_order.py b/tests/dashboard/test_order.py
index 3462086500..1b65f04f5b 100644
--- a/tests/dashboard/test_order.py
+++ b/tests/dashboard/test_order.py
@@ -408,7 +408,7 @@ def test_view_cancel_order_line(admin_client, draft_order):
lines_before_count = lines_before.count()
line = lines_before.first()
line_quantity = line.quantity
- quantity_allocated_before = line.stock.quantity_allocated
+ quantity_allocated_before = line.variant.quantity_allocated
product = line.product
url = reverse(
@@ -426,8 +426,8 @@ def test_view_cancel_order_line(admin_client, draft_order):
lines_after = Order.objects.get().lines.all()
assert lines_before_count - 1 == lines_after.count()
# check stock deallocation
- line.refresh_from_db()
- assert line.quantity_allocated == (
+ line.variant.refresh_from_db()
+ assert line.variant.quantity_allocated == (
quantity_allocated_before - line_quantity)
url = reverse(
'dashboard:orderline-cancel', kwargs={
@@ -466,7 +466,7 @@ def test_view_change_order_line_quantity(admin_client, draft_order):
# order should have the same lines
assert lines_before_quantity_change_count == lines_after.count()
# stock allocation should be 2 now
- assert line.quantity_allocated == 2
+ assert line.variant.quantity_allocated == 2
line.refresh_from_db()
# source line quantity should be decreased to 2
assert line.quantity == 2
@@ -492,7 +492,7 @@ def test_dashboard_change_quantity_form(request_cart_with_item, order):
for line in cart.lines.all():
add_variant_to_order(order, line.variant, line.quantity)
order_line = order.lines.get()
-
+ quantity_before = order_line.variant.quantity_allocated
# Check max quantity validation
form = ChangeQuantityForm({'quantity': 9999}, instance=order_line)
assert not form.is_valid()
@@ -502,33 +502,37 @@ def test_dashboard_change_quantity_form(request_cart_with_item, order):
# Check minimum quantity validation
form = ChangeQuantityForm({'quantity': 0}, instance=order_line)
assert not form.is_valid()
- assert order.lines.get().stock.quantity_allocated == 1
+ assert order.lines.get().variant.quantity_allocated == quantity_before
+ assert 'quantity' in form.errors
# Check available quantity validation
form = ChangeQuantityForm({'quantity': 20}, instance=order_line)
assert not form.is_valid()
- assert order.lines.get().stock.quantity_allocated == 1
- assert form.errors['quantity'] == ['Only 5 remaining in stock.']
+ assert order.lines.get().variant.quantity_allocated == quantity_before
+ assert 'quantity' in form.errors
# Save same quantity
- form = ChangeQuantityForm({'quantity': 1}, instance=order_line)
+ form = ChangeQuantityForm(
+ {'quantity': 1}, instance=order_line)
assert form.is_valid()
form.save()
- order_line.stock.refresh_from_db()
- assert order.lines.get().stock.quantity_allocated == 1
+ order_line.variant.refresh_from_db()
+ assert order_line.variant.quantity_allocated == quantity_before
# Increase quantity
- form = ChangeQuantityForm({'quantity': 2}, instance=order_line)
+ form = ChangeQuantityForm(
+ {'quantity': 2}, instance=order_line)
assert form.is_valid()
form.save()
- order_line.stock.refresh_from_db()
- assert order.lines.get().stock.quantity_allocated == 2
+ order_line.variant.refresh_from_db()
+ assert order_line.variant.quantity_allocated == quantity_before + 1
# Decrease quantity
form = ChangeQuantityForm({'quantity': 1}, instance=order_line)
assert form.is_valid()
form.save()
- assert order.lines.get().stock.quantity_allocated == 1
+ order_line.variant.refresh_from_db()
+ assert order_line.variant.quantity_allocated == quantity_before
def test_ordered_item_change_quantity(transactional_db, order_with_lines):
@@ -599,55 +603,18 @@ def test_add_variant_to_existing_lines_one_line(
lines = order.lines.filter(product_sku='SKU_A')
variant_lines_before = lines.count()
line = lines.first()
+ line_quantity_before = line.quantity
variant = ProductVariant.objects.get(sku='SKU_A')
- quantity_left = add_variant_to_existing_lines(line.order, variant, 2)
+ quantity_added = 2
+ quantity_left = add_variant_to_existing_lines(
+ line.order, variant, quantity_added)
lines_after = order.lines.filter(product_sku='SKU_A').count()
line.refresh_from_db()
assert quantity_left == 0
assert lines_after == variant_lines_before
- assert line.quantity == 4
-
-
-def test_add_variant_to_existing_lines_multiple_lines(
- order_with_lines_and_stock):
- order = order_with_lines_and_stock
- lines = order.lines.filter(product_sku='SKU_A').all()
- variant_lines_before = lines.count()
- line_1 = lines[0]
- line_2 = lines[1]
- variant = ProductVariant.objects.get(sku='SKU_A')
-
- quantity_left = add_variant_to_existing_lines(line_1.order, variant, 4)
-
- lines_after = order.lines.filter(product_sku='SKU_A').count()
- line_1.refresh_from_db()
- line_2.refresh_from_db()
- assert quantity_left == 0
- assert lines_after == variant_lines_before
- assert line_1.quantity == 4
- assert line_2.quantity == 5
-
-
-def test_add_variant_to_existing_lines_multiple_lines_with_rest(
- order_with_lines_and_stock):
- order = order_with_lines_and_stock
- lines = order.lines.filter(product_sku='SKU_A').all()
- variant_lines_before = lines.count()
- line_1 = lines[0]
- line_2 = lines[1]
- variant = ProductVariant.objects.get(sku='SKU_A')
-
- quantity_left = add_variant_to_existing_lines(line_1.order, variant, 7)
-
- lines_after = order.lines.filter(product_sku='SKU_A').count()
- line_1.refresh_from_db()
- line_2.refresh_from_db()
- assert quantity_left == 2
- assert lines_after == variant_lines_before
- assert line_1.quantity == 5
- assert line_2.quantity == 5
+ assert line.quantity == line_quantity_before + quantity_added
def test_view_add_variant_to_order(
@@ -656,9 +623,12 @@ def test_view_add_variant_to_order(
order = order_with_lines_and_stock
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})
- data = {'variant': variant.pk, 'quantity': 2}
+ data = {'variant': variant.pk, 'quantity': added_quantity}
response = admin_client.post(url, data)
@@ -666,7 +636,7 @@ def test_view_add_variant_to_order(
assert response.status_code == 302
assert get_redirect_location(response) == reverse(
'dashboard:order-details', kwargs={'order_pk': order.pk})
- assert line.quantity == 4
+ assert line.quantity == line_quantity_before + added_quantity
@patch('saleor.dashboard.order.forms.send_note_confirmation')
@@ -683,13 +653,13 @@ def test_fulfill_order_line(order_with_lines):
order = order_with_lines
line = order.lines.first()
quantity_fulfilled_before = line.quantity_fulfilled
- stock = line.stock
- stock_quantity_after = stock.quantity - line.quantity
+ variant = line.variant
+ stock_quantity_after = variant.quantity - line.quantity
fulfill_order_line(line, line.quantity)
- stock.refresh_from_db()
- assert stock.quantity == stock_quantity_after
+ variant.refresh_from_db()
+ assert variant.quantity == stock_quantity_after
assert line.quantity_fulfilled == quantity_fulfilled_before + line.quantity
diff --git a/tests/test_cart.py b/tests/test_cart.py
index 56ae42f41f..c5da3d6747 100644
--- a/tests/test_cart.py
+++ b/tests/test_cart.py
@@ -378,7 +378,8 @@ def test_contains_unavailable_variants():
def test_remove_unavailable_variants(cart, product):
variant = product.variants.get()
cart.add(variant, 1)
- variant.stock.update(quantity=0)
+ variant.quantity = 0
+ variant.save()
utils.remove_unavailable_variants(cart)
assert len(cart) == 0
@@ -443,31 +444,15 @@ def test_form_when_variant_does_not_exist():
assert not form.is_valid()
-def test_add_to_cart_form_when_empty_stock():
- cart_lines = []
- cart = Mock(
- add=lambda variant, quantity: cart_lines.append(Mock()),
- get_line=Mock(return_value=Mock(quantity=1)))
-
- form = forms.AddToCartForm(data={'quantity': 1}, cart=cart, product=Mock())
- exception_mock = InsufficientStock(
- Mock(get_stock_quantity=Mock(return_value=1)))
- product_variant = Mock(check_quantity=Mock(side_effect=exception_mock))
- form.get_variant = Mock(return_value=product_variant)
- assert not form.is_valid()
-
-
-def test_add_to_cart_form_when_insufficient_stock():
+def test_add_to_cart_form_when_insufficient_stock(product):
+ variant = product.variants.first()
cart_lines = []
cart = Mock(
add=lambda variant, quantity: cart_lines.append(variant),
- get_line=Mock(return_value=Mock(quantity=1)))
+ get_line=Mock(return_value=Mock(quantity=49)))
form = forms.AddToCartForm(data={'quantity': 1}, cart=cart, product=Mock())
- exception_mock = InsufficientStock(
- Mock(get_stock_quantity=Mock(return_value=4)))
- product_variant = Mock(check_quantity=Mock(side_effect=exception_mock))
- form.get_variant = Mock(return_value=product_variant)
+ form.get_variant = Mock(return_value=variant)
assert not form.is_valid()
diff --git a/tests/test_order.py b/tests/test_order.py
index 36a1cbb4dc..3b9820e5cf 100644
--- a/tests/test_order.py
+++ b/tests/test_order.py
@@ -83,13 +83,13 @@ def test_add_variant_to_order_allocates_stock_for_existing_variant(
order = order_with_lines
existing_line = order.lines.first()
variant = existing_line.product.variants.get()
- stock = existing_line.stock
- stock_before = stock.quantity_allocated
+ variant = existing_line.variant
+ stock_before = variant.quantity_allocated
add_variant_to_order(order, variant, 1)
- stock.refresh_from_db()
- assert stock.quantity_allocated == stock_before + 1
+ variant.refresh_from_db()
+ assert variant.quantity_allocated == stock_before + 1
def test_view_connect_order_with_user_authorized_user(
@@ -148,21 +148,21 @@ def test_restock_order_lines(order_with_lines):
order = order_with_lines
line_1 = order.lines.first()
line_2 = order.lines.last()
- stock_1_quantity_allocated_before = line_1.stock.quantity_allocated
- stock_2_quantity_allocated_before = line_2.stock.quantity_allocated
- stock_1_quantity_before = line_1.stock.quantity
- stock_2_quantity_before = line_2.stock.quantity
+ stock_1_quantity_allocated_before = line_1.variant.quantity_allocated
+ stock_2_quantity_allocated_before = line_2.variant.quantity_allocated
+ stock_1_quantity_before = line_1.variant.quantity
+ stock_2_quantity_before = line_2.variant.quantity
restock_order_lines(order)
- line_1.stock.refresh_from_db()
- line_2.stock.refresh_from_db()
- assert line_1.stock.quantity_allocated == (
+ line_1.variant.refresh_from_db()
+ line_2.variant.refresh_from_db()
+ assert line_1.variant.quantity_allocated == (
stock_1_quantity_allocated_before - line_1.quantity)
- assert line_2.stock.quantity_allocated == (
+ assert line_2.variant.quantity_allocated == (
stock_2_quantity_allocated_before - line_2.quantity)
- assert line_1.stock.quantity == stock_1_quantity_before
- assert line_2.stock.quantity == stock_2_quantity_before
+ assert line_1.variant.quantity == stock_1_quantity_before
+ assert line_2.variant.quantity == stock_2_quantity_before
assert line_1.quantity_fulfilled == 0
assert line_2.quantity_fulfilled == 0
@@ -170,21 +170,21 @@ def test_restock_order_lines(order_with_lines):
def test_restock_fulfilled_order_lines(fulfilled_order):
line_1 = fulfilled_order.lines.first()
line_2 = fulfilled_order.lines.last()
- stock_1_quantity_allocated_before = line_1.stock.quantity_allocated
- stock_2_quantity_allocated_before = line_2.stock.quantity_allocated
- stock_1_quantity_before = line_1.stock.quantity
- stock_2_quantity_before = line_2.stock.quantity
+ stock_1_quantity_allocated_before = line_1.variant.quantity_allocated
+ stock_2_quantity_allocated_before = line_2.variant.quantity_allocated
+ stock_1_quantity_before = line_1.variant.quantity
+ stock_2_quantity_before = line_2.variant.quantity
restock_order_lines(fulfilled_order)
- line_1.stock.refresh_from_db()
- line_2.stock.refresh_from_db()
- assert line_1.stock.quantity_allocated == (
+ line_1.variant.refresh_from_db()
+ line_2.variant.refresh_from_db()
+ assert line_1.variant.quantity_allocated == (
stock_1_quantity_allocated_before)
- assert line_2.stock.quantity_allocated == (
+ assert line_2.variant.quantity_allocated == (
stock_2_quantity_allocated_before)
- assert line_1.stock.quantity == stock_1_quantity_before + line_1.quantity
- assert line_2.stock.quantity == stock_2_quantity_before + line_2.quantity
+ assert line_1.variant.quantity == stock_1_quantity_before + line_1.quantity
+ assert line_2.variant.quantity == stock_2_quantity_before + line_2.quantity
def test_restock_fulfillment_lines(fulfilled_order):
diff --git a/tests/test_product.py b/tests/test_product.py
index 5ebf011aa0..4ed75332e2 100644
--- a/tests/test_product.py
+++ b/tests/test_product.py
@@ -22,7 +22,7 @@ from .utils import filter_products_by_attribute
(increase_stock, 150, 80),
(decrease_stock, 50, 30),
(deallocate_stock, 100, 30),
- (allocate_stock, 10, 1)))
+ (allocate_stock, 100, 130)))
def test_stock_utils(
product, func, expected_quanitty, expected_quant_allocated):
variant = product.variants.first()