Skip to content

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

Fix unit tests

Damian Wad54476a210e
diff --git a/saleor/discount/tests/test_discounts.py b/saleor/discount/tests/test_discounts.py
index 4e9ebe0977..d5b1bc45f3 100644
--- a/saleor/discount/tests/test_discounts.py
+++ b/saleor/discount/tests/test_discounts.py
@@ -1,4 +1,5 @@
from datetime import timedelta
+from decimal import Decimal
import pytest
from django.utils import timezone
@@ -145,7 +146,7 @@ def test_sale_applies_to_correct_products(product_type, category):
category=category,
)
variant = ProductVariant.objects.create(
- product=product, sku="firstvar", price_amount=10
+ product=product, sku="firstvar", price_amount=Decimal(10)
)
product2 = Product.objects.create(
name="Second product",
@@ -155,7 +156,7 @@ def test_sale_applies_to_correct_products(product_type, category):
category=category,
)
sec_variant = ProductVariant.objects.create(
- product=product2, sku="secvar", pk=111, price_amount=10
+ product=product2, sku="secvar", pk=111, price_amount=Decimal(10)
)
sale = Sale(name="Test sale", value=3, type=DiscountValueType.FIXED)
discount = DiscountInfo(
diff --git a/saleor/graphql/product/tests/test_product.py b/saleor/graphql/product/tests/test_product.py
index 9779892630..9002f335b5 100644
--- a/saleor/graphql/product/tests/test_product.py
+++ b/saleor/graphql/product/tests/test_product.py
@@ -459,6 +459,7 @@ def test_products_query_with_filter_collection(
{"minimalPrice": {"gte": 1.0, "lte": 2.0}},
{"isPublished": False},
{"search": "Juice1"},
+ {"price": {"gte": 1.0, "lte": 2.0}},
],
)
def test_products_query_with_filter(
@@ -470,16 +471,19 @@ def test_products_query_with_filter(
):
assert product.is_published is True
assert "Juice1" not in product.name
- product.save()
second_product = product
second_product.id = None
second_product.name = "Apple Juice1"
second_product.slug = "apple-juice1"
- second_product.minimal_variant_price = Money("1.99", "USD")
second_product.is_published = products_filter.get("isPublished", True)
second_product.save()
-
+ second_product.variants.create(
+ product=second_product,
+ sku=second_product.slug,
+ cost_price=Money("1.00", "USD"),
+ price_amount=Decimal(1.99),
+ )
variables = {"filter": products_filter}
staff_api_client.user.user_permissions.add(permission_manage_products)
response = staff_api_client.post_graphql(query_products_with_filter, variables)
@@ -807,6 +811,7 @@ def test_sort_products(user_api_client, product):
query = SORT_PRODUCTS_QUERY
+ # Test sorting by PRICE, ascending
asc_price_query = query % {"sort_by_product_order": "{field: PRICE, direction:ASC}"}
response = user_api_client.post_graphql(asc_price_query)
content = get_graphql_content(response)
@@ -3150,14 +3155,14 @@ def test_product_variants_no_ids_list(user_api_client, variant):
@pytest.mark.parametrize(
- "variant_override, api_variant_price", [(200, 200), (0, 0)],
+ "variant_price_amount, api_variant_price", [(200, 200), (0, 0)],
)
def test_product_variant_price(
- variant_override, api_variant_price, user_api_client, variant, stock
+ variant_price_amount, api_variant_price, user_api_client, variant, stock
):
# Set price override on variant that is different than product price
product = variant.product
- product.variants.update(price_amount=variant_override, currency="USD")
+ product.variants.update(price_amount=variant_price_amount, currency="USD")
# Drop other variants
# product.variants.exclude(id=variant.pk).delete()
diff --git a/saleor/graphql/product/tests/test_product_pagination.py b/saleor/graphql/product/tests/test_product_pagination.py
index 8934847085..874449ab02 100644
--- a/saleor/graphql/product/tests/test_product_pagination.py
+++ b/saleor/graphql/product/tests/test_product_pagination.py
@@ -1,4 +1,5 @@
import uuid
+from decimal import Decimal
import graphene
import pytest
@@ -329,19 +330,31 @@ def products_for_pagination(product_type, color_attribute, category, warehouse):
product=products[0],
sku=str(uuid.uuid4()).replace("-", ""),
track_inventory=True,
- price_amount=10,
+ price_amount=Decimal(10),
+ ),
+ ProductVariant(
+ product=products[1],
+ sku=str(uuid.uuid4()).replace("-", ""),
+ track_inventory=True,
+ price_amount=Decimal(15),
),
ProductVariant(
product=products[2],
sku=str(uuid.uuid4()).replace("-", ""),
track_inventory=True,
- price_amount=8,
+ price_amount=Decimal(8),
+ ),
+ ProductVariant(
+ product=products[3],
+ sku=str(uuid.uuid4()).replace("-", ""),
+ track_inventory=True,
+ price_amount=Decimal(7),
),
ProductVariant(
product=products[4],
sku=str(uuid.uuid4()).replace("-", ""),
track_inventory=True,
- price_amount=15,
+ price_amount=Decimal(15),
),
]
)
@@ -393,6 +406,10 @@ QUERY_PRODUCTS_PAGINATION = """
{"field": "TYPE", "direction": "ASC"},
["Product1", "Product3", "ProductProduct2"],
),
+ (
+ {"field": "PRICE", "direction": "ASC"},
+ ["Product2", "ProductProduct2", "Product1"],
+ ),
(
{"field": "PUBLISHED", "direction": "ASC"},
["Product2", "ProductProduct1", "Product1"],
@@ -453,7 +470,8 @@ def test_products_pagination_with_sorting_by_attribute(
"filter_by, products_order",
[
({"isPublished": False}, ["Product2", "ProductProduct1"]),
- ({"stockAvailability": "OUT_OF_STOCK"}, ["Product3", "ProductProduct2"]),
+ ({"price": {"gte": 8, "lte": 12}}, ["Product1", "ProductProduct2"]),
+ ({"stockAvailability": "OUT_OF_STOCK"}, ["ProductProduct1", "ProductProduct2"]),
],
)
def test_products_pagination_with_filtering(
@@ -526,7 +544,7 @@ def test_products_pagination_with_filtering_by_stocks(
staff_api_client, permission_manage_products, products_for_pagination, warehouse
):
page_size = 2
- products_order = ["Product3", "ProductProduct2"]
+ products_order = ["ProductProduct1", "ProductProduct2"]
warehouse_id = graphene.Node.to_global_id("Warehouse", warehouse.id)
filter_by = {"stocks": {"warehouseIds": [warehouse_id], "quantity": {"lte": 10}}}
diff --git a/saleor/graphql/product/tests/test_product_sorting_attributes.py b/saleor/graphql/product/tests/test_product_sorting_attributes.py
index ab382e9873..629b7e8218 100644
--- a/saleor/graphql/product/tests/test_product_sorting_attributes.py
+++ b/saleor/graphql/product/tests/test_product_sorting_attributes.py
@@ -1,4 +1,5 @@
import os.path
+from decimal import Decimal
import graphene
import pytest
@@ -104,7 +105,10 @@ def products_structures(category):
]
)
)
-
+ for product_apple in apples:
+ product_models.ProductVariant.objects.create(
+ product=product_apple, sku=product_apple.slug, price_amount=Decimal(10)
+ )
oranges = list(
product_models.Product.objects.bulk_create(
[
@@ -119,7 +123,10 @@ def products_structures(category):
]
)
)
-
+ for product_orange in oranges:
+ product_models.ProductVariant.objects.create(
+ product=product_orange, sku=product_orange.slug, price_amount=Decimal(10)
+ )
dummy = product_models.Product.objects.create(
name="Oopsie Dummy",
slug="oopsie-dummy",
@@ -127,13 +134,19 @@ def products_structures(category):
category=category,
is_published=True,
)
- product_models.Product.objects.create(
+ product_models.ProductVariant.objects.create(
+ product=dummy, sku=dummy.slug, price_amount=Decimal(10)
+ )
+ other_dummy = product_models.Product.objects.create(
name="Another Dummy but first in ASC and has no attribute value",
slug="another-dummy",
product_type=pt_other,
category=category,
is_published=True,
)
+ product_models.ProductVariant.objects.create(
+ product=other_dummy, sku=other_dummy.slug, price_amount=Decimal(10)
+ )
dummy_attr_value = attr_value(dummy_attr, DUMMIES[0])
associate_attribute_values_to_instance(dummy, dummy_attr, *dummy_attr_value)
diff --git a/saleor/product/migrations/0118_populate_product_variant_price.py b/saleor/product/migrations/0118_populate_product_variant_price.py
index 9eadb30b16..0788091415 100644
--- a/saleor/product/migrations/0118_populate_product_variant_price.py
+++ b/saleor/product/migrations/0118_populate_product_variant_price.py
@@ -37,4 +37,12 @@ class Migration(migrations.Migration):
migrations.RemoveField(
model_name="productvariant", name="price_override_amount",
),
+ migrations.RemoveField(model_name="product", name="price_amount",),
+ migrations.AlterField(
+ model_name="product",
+ name="minimal_variant_price_amount",
+ field=models.DecimalField(
+ blank=True, decimal_places=2, max_digits=12, null=True
+ ),
+ ),
]
diff --git a/saleor/product/migrations/0119_auto_20200527_0402.py b/saleor/product/migrations/0119_auto_20200527_0402.py
deleted file mode 100644
index 7552f85f35..0000000000
--- a/saleor/product/migrations/0119_auto_20200527_0402.py
+++ /dev/null
@@ -1,21 +0,0 @@
-# Generated by Django 3.0.6 on 2020-05-27 09:02
-
-from django.db import migrations, models
-
-
-class Migration(migrations.Migration):
-
- dependencies = [
- ("product", "0118_populate_product_variant_price"),
- ]
-
- operations = [
- migrations.RemoveField(model_name="product", name="price_amount",),
- migrations.AlterField(
- model_name="product",
- name="minimal_variant_price_amount",
- field=models.DecimalField(
- blank=True, decimal_places=2, max_digits=12, null=True
- ),
- ),
- ]
diff --git a/saleor/product/models.py b/saleor/product/models.py
index e8414ba153..1ad18bd438 100644
--- a/saleor/product/models.py
+++ b/saleor/product/models.py
@@ -126,6 +126,15 @@ class ProductsQueryset(PublishedQuerySet):
)
return qs
+ def published_with_variants(self):
+ published = self.published()
+ return published.filter(variants__isnull=False)
+
+ def visible_to_user(self, user):
+ if self.user_has_access_to_all(user):
+ return self.all()
+ return self.published_with_variants()
+
def sort_by_attribute(
self, attribute_pk: Union[int, str], descending: bool = False
):
diff --git a/saleor/product/tests/test_product.py b/saleor/product/tests/test_product.py
index c369f6a1d0..1e7cb544f8 100644
--- a/saleor/product/tests/test_product.py
+++ b/saleor/product/tests/test_product.py
@@ -31,7 +31,9 @@ def test_filtering_by_attribute(db, color_attribute, category, settings):
product_type=product_type_a,
category=category,
)
- models.ProductVariant.objects.create(product=product_a, sku="1234", price_amount=10)
+ models.ProductVariant.objects.create(
+ product=product_a, sku="1234", price_amount=Decimal(10)
+ )
product_b = models.Product.objects.create(
name="Test product b",
slug="test-product-b",
@@ -39,7 +41,7 @@ def test_filtering_by_attribute(db, color_attribute, category, settings):
category=category,
)
variant_b = models.ProductVariant.objects.create(
- product=product_b, sku="12345", price_amount=10
+ product=product_b, sku="12345", price_amount=Decimal(10)
)
color = color_attribute.values.first()
color_2 = color_attribute.values.last()
@@ -102,7 +104,7 @@ def test_product_get_price_do_not_charge_taxes(product_type, category, discount_
product = models.Product.objects.create(
product_type=product_type, category=category, charge_taxes=False,
)
- variant = product.variants.create(price_amount=10)
+ variant = product.variants.create(price_amount=Decimal(10))
price = variant.get_price(discounts=[discount_info])
diff --git a/saleor/product/tests/test_product_attributes.py b/saleor/product/tests/test_product_attributes.py
index b409f66656..a42513e78f 100644
--- a/saleor/product/tests/test_product_attributes.py
+++ b/saleor/product/tests/test_product_attributes.py
@@ -1,3 +1,4 @@
+from decimal import Decimal
from unittest.mock import MagicMock, Mock
import pytest
@@ -20,7 +21,9 @@ def variant_with_no_attributes(category):
product = Product.objects.create(
name="Test product", product_type=product_type, category=category,
)
- variant = ProductVariant.objects.create(product=product, sku="123", price_amount=10)
+ variant = ProductVariant.objects.create(
+ product=product, sku="123", price_amount=Decimal(10)
+ )
return variant
diff --git a/saleor/product/tests/test_product_availability.py b/saleor/product/tests/test_product_availability.py
index ef32208b74..1632a9bf57 100644
--- a/saleor/product/tests/test_product_availability.py
+++ b/saleor/product/tests/test_product_availability.py
@@ -1,4 +1,5 @@
import datetime
+from decimal import Decimal
from unittest.mock import Mock
import pytest
@@ -29,8 +30,8 @@ def test_product_availability_status(unavailable_product, warehouse):
status = get_product_availability_status(product, "US")
assert status == ProductAvailabilityStatus.VARIANTS_MISSSING
- variant_1 = product.variants.create(sku="test-1", price_amount=10)
- variant_2 = product.variants.create(sku="test-2", price_amount=10)
+ variant_1 = product.variants.create(sku="test-1", price_amount=Decimal(10))
+ variant_2 = product.variants.create(sku="test-2", price_amount=Decimal(10))
# create empty stock records
stock_1 = Stock.objects.create(
product_variant=variant_1, warehouse=warehouse, quantity=0
@@ -67,7 +68,7 @@ def test_variant_is_out_of_stock_when_product_is_unavalable(
product = unavailable_product
product.product_type.has_variants = True
- variant = product.variants.create(sku="test", price_amount=10)
+ variant = product.variants.create(sku="test", price_amount=Decimal(10))
Stock.objects.create(product_variant=variant, warehouse=warehouse, quantity=0)
status = get_variant_availability_status(variant, "US")
@@ -177,3 +178,28 @@ def test_available_products_only_available(product_list):
available_products = models.Product.objects.published()
assert available_products.count() == 2
assert all([product.is_visible for product in available_products])
+
+
+def test_available_products_with_variants(product_list):
+ product = product_list[0]
+ product.variants.all().delete()
+
+ available_products = models.Product.objects.published_with_variants()
+ assert available_products.count() == 2
+
+
+def test_visible_to_customer_user(customer_user, product_list):
+ product = product_list[0]
+ product.variants.all().delete()
+
+ available_products = models.Product.objects.visible_to_user(customer_user)
+ assert available_products.count() == 2
+
+
+def test_visible_to_staff_user(customer_user, product_list, permission_manage_products):
+ product = product_list[0]
+ product.variants.all().delete()
+ customer_user.user_permissions.add(permission_manage_products)
+
+ available_products = models.Product.objects.visible_to_user(customer_user)
+ assert available_products.count() == 3
diff --git a/saleor/product/utils/availability.py b/saleor/product/utils/availability.py
index fed6c5ef11..d5e5f9970b 100644
--- a/saleor/product/utils/availability.py
+++ b/saleor/product/utils/availability.py
@@ -27,8 +27,8 @@ if TYPE_CHECKING:
@dataclass
class ProductAvailability:
on_sale: bool
- price_range: TaxedMoneyRange
- price_range_undiscounted: TaxedMoneyRange
+ price_range: Optional[TaxedMoneyRange]
+ price_range_undiscounted: Optional[TaxedMoneyRange]
discount: Optional[TaxedMoney]
price_range_local_currency: Optional[TaxedMoneyRange]
discount_local_currency: Optional[TaxedMoneyRange]
@@ -136,7 +136,7 @@ def get_product_price_range(
variants: Iterable[ProductVariant],
collections: Iterable[Collection],
discounts: Iterable[DiscountInfo]
-) -> MoneyRange:
+) -> Optional[MoneyRange]:
with opentracing.global_tracer().start_active_span("get_product_price_range"):
if variants:
prices = [
@@ -150,7 +150,7 @@ def get_product_price_range(
]
return MoneyRange(min(prices), max(prices))
- return MoneyRange(zero_money(), zero_money())
+ return None
def get_product_availability(
@@ -166,38 +166,49 @@ def get_product_availability(
with opentracing.global_tracer().start_active_span("get_product_availability"):
if not plugins:
plugins = get_plugins_manager()
+
+ discounted = None
discounted_net_range = get_product_price_range(
product=product,
variants=variants,
collections=collections,
discounts=discounts,
)
+ if discounted_net_range is not None:
+ discounted = TaxedMoneyRange(
+ start=plugins.apply_taxes_to_product(
+ product, discounted_net_range.start, country
+ ),
+ stop=plugins.apply_taxes_to_product(
+ product, discounted_net_range.stop, country
+ ),
+ )
+
+ undiscounted = None
undiscounted_net_range = get_product_price_range(
product=product, variants=variants, collections=collections, discounts=[]
)
- discounted = TaxedMoneyRange(
- start=plugins.apply_taxes_to_product(
- product, discounted_net_range.start, country
- ),
- stop=plugins.apply_taxes_to_product(
- product, discounted_net_range.stop, country
- ),
- )
- undiscounted = TaxedMoneyRange(
- start=plugins.apply_taxes_to_product(
- product, undiscounted_net_range.start, country
- ),
- stop=plugins.apply_taxes_to_product(
- product, undiscounted_net_range.stop, country
- ),
- )
-
- discount = _get_total_discount_from_range(undiscounted, discounted)
- price_range_local, discount_local_currency = _get_product_price_range(
- discounted, undiscounted, local_currency
- )
+ if undiscounted_net_range is not None:
+ undiscounted = TaxedMoneyRange(
+ start=plugins.apply_taxes_to_product(
+ product, undiscounted_net_range.start, country
+ ),
+ stop=plugins.apply_taxes_to_product(
+ product, undiscounted_net_range.stop, country
+ ),
+ )
+
+ discount = None
+ price_range_local = None
+ discount_local_currency = None
+ if undiscounted_net_range is not None and discounted_net_range is not None:
+ discount = _get_total_discount_from_range(undiscounted, discounted)
+ price_range_local, discount_local_currency = _get_product_price_range(
+ discounted, undiscounted, local_currency
+ )
is_on_sale = product.is_visible and discount is not None
+
return ProductAvailability(
on_sale=is_on_sale,
price_range=discounted,
diff --git a/saleor/tests/fixtures.py b/saleor/tests/fixtures.py
index 5f1d75de05..57931e5163 100644
--- a/saleor/tests/fixtures.py
+++ b/saleor/tests/fixtures.py
@@ -697,7 +697,10 @@ def product(product_type, category, warehouse):
variant_attr_value = variant_attr.values.first()
variant = ProductVariant.objects.create(
- product=product, sku="123", cost_price=Money("1.00", "USD"), price_amount=10.00
+ product=product,
+ sku="123",
+ cost_price=Money("1.00", "USD"),
+ price_amount=Decimal(10),
)
Stock.objects.create(warehouse=warehouse, product_variant=variant, quantity=10)
@@ -739,7 +742,7 @@ def product_with_two_variants(product_type, category, warehouse):
product=product,
sku=f"Product variant #{i}",
cost_price=Money("1.00", "USD"),
- price_amount=10,
+ price_amount=Decimal(10),
)
for i in (1, 2)
]
@@ -779,7 +782,7 @@ def product_with_variant_with_two_attributes(
product=product,
sku="prodVar1",
cost_price=Money("1.00", "USD"),
- price_amount=10,
+ price_amount=Decimal(10),
)
associate_attribute_values_to_instance(
@@ -823,7 +826,7 @@ def product_with_default_variant(product_type_without_variant, category, warehou
is_published=True,
)
variant = ProductVariant.objects.create(
- product=product, sku="1234", track_inventory=True, price_amount=10
+ product=product, sku="1234", track_inventory=True, price_amount=Decimal(10)
)
Stock.objects.create(warehouse=warehouse, product_variant=variant, quantity=100)
return product
@@ -841,7 +844,10 @@ def variant_without_inventory_tracking(
is_published=True,
)
variant = ProductVariant.objects.create(
- product=product, sku="tracking123", track_inventory=False, price_amount=10
+ product=product,
+ sku="tracking123",
+ track_inventory=False,
+ price_amount=Decimal(10),
)
Stock.objects.create(warehouse=warehouse, product_variant=variant, quantity=0)
return variant
@@ -853,7 +859,7 @@ def variant(product) -> ProductVariant:
product=product,
sku="SKU_A",
cost_price=Money(1, "USD"),
- price_amount=10,
+ price_amount=Decimal(10),
price=Money(10, "USD"),
)
return product_variant
@@ -872,9 +878,9 @@ def product_variant_list(product):
return list(
ProductVariant.objects.bulk_create(
[
- ProductVariant(product=product, sku="1", price_amount=10),
- ProductVariant(product=product, sku="2", price_amount=10),
- ProductVariant(product=product, sku="3", price_amount=10),
+ ProductVariant(product=product, sku="1", price_amount=Decimal(10)),
+ ProductVariant(product=product, sku="2", price_amount=Decimal(10)),
+ ProductVariant(product=product, sku="3", price_amount=Decimal(10)),
]
)
)
@@ -896,7 +902,7 @@ def product_without_shipping(category, warehouse):
is_published=True,
)
variant = ProductVariant.objects.create(
- product=product, sku="SKU_B", price_amount=10
+ product=product, sku="SKU_B", price_amount=Decimal(10)
)
Stock.objects.create(product_variant=variant, warehouse=warehouse, quantity=1)
return product
@@ -952,19 +958,19 @@ def product_list(product_type, category, warehouse):
product=products[0],
sku=str(uuid.uuid4()).replace("-", ""),
track_inventory=True,
- price_amount=10,
+ price_amount=Decimal(10),
),
ProductVariant(
product=products[1],
sku=str(uuid.uuid4()).replace("-", ""),
track_inventory=True,
- price_amount=20,
+ price_amount=Decimal(20),
),
ProductVariant(
product=products[2],
sku=str(uuid.uuid4()).replace("-", ""),
track_inventory=True,
- price_amount=30,
+ price_amount=Decimal(30),
),
]
)
@@ -1041,7 +1047,7 @@ def unavailable_product_with_variant(product_type, category, warehouse):
variant_attr_value = variant_attr.values.first()
variant = ProductVariant.objects.create(
- product=product, sku="123", cost_price=Money(1, "USD"), price_amount=10
+ product=product, sku="123", cost_price=Money(1, "USD"), price_amount=Decimal(10)
)
Stock.objects.create(product_variant=variant, warehouse=warehouse, quantity=10)
@@ -1233,7 +1239,10 @@ def order_with_lines(order, product_type, category, shipping_zone, warehouse):
is_published=True,
)
variant = ProductVariant.objects.create(
- product=product, sku="SKU_A", cost_price=Money(1, "USD"), price_amount=10
+ product=product,
+ sku="SKU_A",
+ cost_price=Money(1, "USD"),
+ price_amount=Decimal(10),
)
stock = Stock.objects.create(
warehouse=warehouse, product_variant=variant, quantity=5
@@ -1864,7 +1873,10 @@ def digital_content(category, media_root, warehouse) -> DigitalContent:
is_published=True,
)
product_variant = ProductVariant.objects.create(
- product=product, sku="SKU_554", cost_price=Money(1, "USD"), price_amount=10
+ product=product,
+ sku="SKU_554",
+ cost_price=Money(1, "USD"),
+ price_amount=Decimal(10),
)
Stock.objects.create(
product_variant=product_variant, warehouse=warehouse, quantity=5,