Skip to content

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

Make cost_price less than price

oldPadavan9de14d8c4655
diff --git a/saleor/core/utils/random_data.py b/saleor/core/utils/random_data.py
index da4d53a173..7f38b78d1a 100644
--- a/saleor/core/utils/random_data.py
+++ b/saleor/core/utils/random_data.py
@@ -2,6 +2,7 @@ import itertools
import os
import random
import unicodedata
+from decimal import Decimal
from django.conf import settings
from django.contrib.auth.models import Group, Permission
@@ -301,10 +302,12 @@ def create_variant(product, **kwargs):
defaults = {
'product': product,
'quantity': fake.random_int(1, 50),
- 'cost_price': fake.money(),
'quantity_allocated': fake.random_int(1, 50)}
defaults.update(kwargs)
variant = ProductVariant(**defaults)
+ if 'cost_price' not in kwargs:
+ variant.cost_price = (variant.base_price * Decimal(
+ fake.random_int(10, 99) / 100)).quantize()
if variant.attributes:
variant.name = get_name_from_attributes(variant)
variant.save()
diff --git a/tests/test_core.py b/tests/test_core.py
index db017903e5..8f3dd82a4c 100644
--- a/tests/test_core.py
+++ b/tests/test_core.py
@@ -4,6 +4,7 @@ from unittest.mock import Mock, patch
import pytest
from django.conf import settings
+from django.db.models import Case, F, When
from django.shortcuts import reverse
from django.urls import translate_url
from prices import Money
@@ -16,7 +17,7 @@ from saleor.core.utils import (
from saleor.core.utils.text import get_cleaner, strip_html
from saleor.discount.models import Sale, Voucher
from saleor.order.models import Order
-from saleor.product.models import Product, ProductImage
+from saleor.product.models import Product, ProductImage, ProductVariant
from saleor.shipping.models import ShippingMethod
type_schema = {
@@ -145,6 +146,11 @@ def test_create_products_by_type(
how_many=how_many, create_images=True)
assert Product.objects.all().count() == how_many
assert mock_create_thumbnails.called
+ assert ProductVariant.objects.annotate(
+ base_price=Case(
+ When(price_override__lt=0, then='price_override'),
+ default='product__price')).\
+ filter(base_price__lt=F('cost_price')).count() == 0
def test_create_fake_order(db, monkeypatch, product_image):