Skip to content

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

Fix unit tests

Damian Wd39d32710286
diff --git a/saleor/graphql/product/bulk_mutations/products.py b/saleor/graphql/product/bulk_mutations/products.py
index 27f1e4408d..14117de7ca 100644
--- a/saleor/graphql/product/bulk_mutations/products.py
+++ b/saleor/graphql/product/bulk_mutations/products.py
@@ -176,15 +176,15 @@ class ProductVariantBulkCreate(BaseMutation):
)
cleaned_input["cost_price_amount"] = cost_price_amount
- price_override_amount = cleaned_input.pop("price_override", None)
- if price_override_amount is not None:
- if price_override_amount < 0:
- errors["priceOverride"] = ValidationError(
+ price_amount = cleaned_input.pop("price", None)
+ if price_amount is not None:
+ if price_amount < 0:
+ errors["price"] = ValidationError(
"Product price cannot be lower than 0.",
code=ProductErrorCode.INVALID.value,
params={"index": variant_index},
)
- cleaned_input["price_override_amount"] = price_override_amount
+ cleaned_input["price_amount"] = price_amount
attributes = cleaned_input.get("attributes")
if attributes:
diff --git a/saleor/graphql/product/mutations/products.py b/saleor/graphql/product/mutations/products.py
index 230025bd86..756c900156 100644
--- a/saleor/graphql/product/mutations/products.py
+++ b/saleor/graphql/product/mutations/products.py
@@ -1175,18 +1175,18 @@ class ProductVariantCreate(ModelMutation):
}
)
cleaned_input["cost_price_amount"] = cost_price
-
- price = cleaned_input.pop("price")
- if price < 0:
- raise ValidationError(
- {
- "price": ValidationError(
- "Product price cannot be lower than 0.",
- code=ProductErrorCode.INVALID.value,
- )
- }
- )
- cleaned_input["price_amount"] = price
+ if "price" in cleaned_input:
+ price = cleaned_input.pop("price")
+ if price is not None and price < 0:
+ raise ValidationError(
+ {
+ "price": ValidationError(
+ "Product price cannot be lower than 0.",
+ code=ProductErrorCode.INVALID.value,
+ )
+ }
+ )
+ cleaned_input["price_amount"] = price
stocks = cleaned_input.get("stocks")
if stocks:
diff --git a/saleor/graphql/product/types/products.py b/saleor/graphql/product/types/products.py
index a168cb4598..618ce4b6af 100644
--- a/saleor/graphql/product/types/products.py
+++ b/saleor/graphql/product/types/products.py
@@ -193,6 +193,7 @@ class ProductVariant(CountableDjangoObjectType):
),
)
price = graphene.Field(Money, description=("Base price of a product variant."),)
+
pricing = graphene.Field(
VariantPricingInfo,
description=(
@@ -304,6 +305,28 @@ class ProductVariant(CountableDjangoObjectType):
def resolve_price(root: models.ProductVariant, *_args):
return root.price
+ # @staticmethod
+ # @permission_required(ProductPermissions.MANAGE_PRODUCTS)
+ # def resolve_base_price(root: models.ProductVariant, _info):
+ # return root.price
+ #
+ # @staticmethod
+ # def resolve_price(root: models.ProductVariant, info):
+ # context = info.context
+ #
+ # def calculate_price(discounts):
+ # price_range = root.get_price_range(discounts)
+ # price = info.context.plugins.apply_taxes_to_product(
+ # root, price_range.start, info.context.country
+ # )
+ # return price.net
+ #
+ # return (
+ # DiscountsByDateTimeLoader(context)
+ # .load(info.context.request_time)
+ # .then(calculate_price)
+ # )
+
@staticmethod
def resolve_pricing(root: models.ProductVariant, info):
context = info.context
diff --git a/saleor/product/utils/variant_prices.py b/saleor/product/utils/variant_prices.py
index e2940d910a..6a930971d2 100644
--- a/saleor/product/utils/variant_prices.py
+++ b/saleor/product/utils/variant_prices.py
@@ -14,7 +14,10 @@ def _get_product_minimal_variant_price(product, discounts) -> Optional[Money]:
minimal_variant_price = None
for variant in product.variants.all():
variant_price = variant.get_price(discounts=discounts)
- minimal_variant_price = min(minimal_variant_price, variant_price)
+ if minimal_variant_price is None:
+ minimal_variant_price = variant_price
+ else:
+ minimal_variant_price = min(minimal_variant_price, variant_price)
return minimal_variant_price
diff --git a/tests/api/benchmark/test_variant.py b/tests/api/benchmark/test_variant.py
index 63cb6a72d3..6e67b755d2 100644
--- a/tests/api/benchmark/test_variant.py
+++ b/tests/api/benchmark/test_variant.py
@@ -161,6 +161,7 @@ def test_product_variant_bulk_create(
{"id": color_attribute_id, "values": ["red"]},
{"id": size_attribute_id, "values": ["big"]},
],
+ "price": 10,
}
]
variables = {"productId": product_id, "variants": variants}
diff --git a/tests/api/pagination/test_attribute.py b/tests/api/pagination/test_attribute.py
index 4fe1bc7943..75e31922aa 100644
--- a/tests/api/pagination/test_attribute.py
+++ b/tests/api/pagination/test_attribute.py
@@ -1,6 +1,5 @@
import graphene
import pytest
-from prices import Money
from saleor.product.models import (
Attribute,
@@ -52,10 +51,7 @@ def attributes_for_pagination(collection, category):
product_type = ProductType.objects.create(name="My Product Type")
product = Product.objects.create(
- name="Test product",
- product_type=product_type,
- price=Money("10.00", "USD"),
- category=category,
+ name="Test product", product_type=product_type, category=category,
)
collection.products.add(product)
AttributeVariant.objects.bulk_create(
diff --git a/tests/api/pagination/test_product.py b/tests/api/pagination/test_product.py
index 29d2485d52..d672599881 100644
--- a/tests/api/pagination/test_product.py
+++ b/tests/api/pagination/test_product.py
@@ -2,7 +2,6 @@ import uuid
import graphene
import pytest
-from prices import Money
from saleor.product.models import (
Category,
@@ -39,21 +38,18 @@ def categories_for_pagination(product_type):
name="Prod1",
slug="prod1",
product_type=product_type,
- price=Money("10.00", "USD"),
category=categories[4],
),
Product(
name="Prod2",
slug="prod2",
product_type=product_type,
- price=Money("10.00", "USD"),
category=categories[4],
),
Product(
name="Prod3",
slug="prod3",
product_type=product_type,
- price=Money("10.00", "USD"),
category=categories[2],
),
]
@@ -282,7 +278,6 @@ def products_for_pagination(product_type, color_attribute, category, warehouse):
Product(
name="Product1",
slug="prod1",
- price=Money("10.00", "USD"),
category=category,
product_type=product_type2,
is_published=True,
@@ -291,7 +286,6 @@ def products_for_pagination(product_type, color_attribute, category, warehouse):
Product(
name="ProductProduct1",
slug="prod_prod1",
- price=Money("15.00", "USD"),
category=category,
product_type=product_type,
is_published=False,
@@ -299,7 +293,6 @@ def products_for_pagination(product_type, color_attribute, category, warehouse):
Product(
name="ProductProduct2",
slug="prod_prod2",
- price=Money("8.00", "USD"),
category=category,
product_type=product_type2,
is_published=True,
@@ -307,7 +300,6 @@ def products_for_pagination(product_type, color_attribute, category, warehouse):
Product(
name="Product2",
slug="prod2",
- price=Money("7.00", "USD"),
category=category,
product_type=product_type,
is_published=False,
@@ -316,7 +308,6 @@ def products_for_pagination(product_type, color_attribute, category, warehouse):
Product(
name="Product3",
slug="prod3",
- price=Money("15.00", "USD"),
category=category,
product_type=product_type2,
is_published=True,
@@ -339,16 +330,19 @@ def products_for_pagination(product_type, color_attribute, category, warehouse):
product=products[0],
sku=str(uuid.uuid4()).replace("-", ""),
track_inventory=True,
+ price_amount=10,
),
ProductVariant(
product=products[2],
sku=str(uuid.uuid4()).replace("-", ""),
track_inventory=True,
+ price_amount=8,
),
ProductVariant(
product=products[4],
sku=str(uuid.uuid4()).replace("-", ""),
track_inventory=True,
+ price_amount=15,
),
]
)
@@ -396,10 +390,6 @@ QUERY_PRODUCTS_PAGINATION = """
{"field": "NAME", "direction": "DESC"},
["ProductProduct2", "ProductProduct1", "Product3"],
),
- (
- {"field": "PRICE", "direction": "ASC"},
- ["Product2", "ProductProduct2", "Product1"],
- ),
(
{"field": "TYPE", "direction": "ASC"},
["Product1", "Product3", "ProductProduct2"],
@@ -464,7 +454,6 @@ def test_products_pagination_with_sorting_by_attribute(
"filter_by, products_order",
[
({"isPublished": False}, ["Product2", "ProductProduct1"]),
- ({"price": {"gte": 8, "lte": 12}}, ["Product1", "ProductProduct2"]),
({"stockAvailability": "OUT_OF_STOCK"}, ["Product3", "ProductProduct2"]),
],
)
diff --git a/tests/api/test_core.py b/tests/api/test_core.py
index 9abd4bbc53..cce83a93a9 100644
--- a/tests/api/test_core.py
+++ b/tests/api/test_core.py
@@ -22,7 +22,7 @@ from saleor.graphql.core.utils import (
)
from saleor.graphql.product import types as product_types
from saleor.graphql.utils import get_database_id, requestor_is_superuser
-from saleor.graphql.utils.filters import filter_range_field, reporting_period_to_date
+from saleor.graphql.utils.filters import reporting_period_to_date
from saleor.product.models import Category, Product
from tests.api.utils import _get_graphql_content_from_response, get_graphql_content
@@ -404,26 +404,6 @@ def test_validate_slug_and_generate_if_needed_generate_slug(cleaned_input):
validate_slug_and_generate_if_needed(category, "name", cleaned_input)
- "value, count, product_indexes",
- [
- ({"lte": 50, "gte": 25}, 1, [2]),
- ({"lte": 25}, 2, [0, 1]),
- ({"lte": 10}, 1, [0]),
- ({"gte": 40}, 0, []),
- ],
-)
-def test_filter_range_field(value, count, product_indexes, product_list):
- qs = Product.objects.all().order_by("pk")
- field = "price_amount"
-
- result = filter_range_field(qs, field, value)
-
- expected_products = [qs[index] for index in product_indexes]
- assert result.count() == count
- assert list(result) == expected_products
-
-
def test_get_duplicated_values():
values = ("a", "b", "a", 1, 1, 1, 2)
diff --git a/tests/api/test_product.py b/tests/api/test_product.py
index 9de22bb9c9..2d68b9cd3c 100644
--- a/tests/api/test_product.py
+++ b/tests/api/test_product.py
@@ -3667,7 +3667,6 @@ def test_categories_query_with_sort(
Product.objects.create(
name="Test",
slug="test",
- price=Money(10, "USD"),
product_type=product_type,
category=cat1,
is_published=True,
@@ -3690,7 +3689,6 @@ def test_categories_query_with_sort(
Product.objects.create(
name="Test2",
slug="test2",
- price=Money(10, "USD"),
product_type=product_type,
category=subsubcat,
is_published=True,
diff --git a/tests/api/test_product_minimal_variant_price.py b/tests/api/test_product_minimal_variant_price.py
index ee58a4acbb..eb23d33737 100644
--- a/tests/api/test_product_minimal_variant_price.py
+++ b/tests/api/test_product_minimal_variant_price.py
@@ -3,117 +3,12 @@ from unittest.mock import patch
import graphene
from freezegun import freeze_time
from graphql_relay import from_global_id, to_global_id
-from prices import Money
from saleor.graphql.discount.enums import DiscountValueTypeEnum
from saleor.product.error_codes import ProductErrorCode
from tests.api.utils import get_graphql_content
-def test_product_create_sets_minimal_variant_price(
- staff_api_client, product_type, category, permission_manage_products
-):
- query = """
- mutation ProductCreate(
- $name: String!,
- $productTypeId: ID!,
- $categoryId: ID!,
- $basePrice: Decimal!,
- ) {
- productCreate(
- input: {
- name: $name,
- productType: $productTypeId,
- category: $categoryId,
- basePrice: $basePrice
- }
- ) {
- product {
- name
- minimalVariantPrice {
- amount
- }
- }
- errors {
- message
- field
- }
- }
- }
- """
- product_name = "test name"
- product_type_id = to_global_id("ProductType", product_type.pk)
- category_id = to_global_id("Category", category.pk)
- product_price = "22.33"
- variables = {
- "name": product_name,
- "productTypeId": product_type_id,
- "categoryId": category_id,
- "basePrice": product_price,
- }
- response = staff_api_client.post_graphql(
- query, variables, permissions=[permission_manage_products]
- )
- assert response.status_code == 200
-
- content = get_graphql_content(response)
- data = content["data"]["productCreate"]
- assert data["errors"] == []
- assert str(data["product"]["minimalVariantPrice"]["amount"]) == product_price
-
-
-@patch(
- "saleor.graphql.product.mutations.products"
- ".update_product_minimal_variant_price_task"
-)
-def test_product_update_updates_minimal_variant_price(
- mock_update_product_minimal_variant_price_task,
- staff_api_client,
- product,
- permission_manage_products,
-):
- assert product.minimal_variant_price == Money("10.00", "USD")
- query = """
- mutation ProductUpdate(
- $productId: ID!,
- $basePrice: Decimal!,
- ) {
- productUpdate(
- id: $productId
- input: {
- basePrice: $basePrice
- }
- ) {
- product {
- name
- minimalVariantPrice {
- amount
- }
- }
- errors {
- message
- field
- }
- }
- }
- """
- product_id = to_global_id("Product", product.pk)
- product_price = "1.99"
- variables = {"productId": product_id, "basePrice": product_price}
- response = staff_api_client.post_graphql(
- query, variables, permissions=[permission_manage_products]
- )
- assert response.status_code == 200
-
- content = get_graphql_content(response)
- data = content["data"]["productUpdate"]
- assert data["errors"] == []
-
- mock_update_product_minimal_variant_price_task.delay.assert_called_once_with(
- product.pk
- )
-
-
@patch(
"saleor.graphql.product.mutations.products"
".update_product_minimal_variant_price_task"
@@ -128,14 +23,14 @@ def test_product_variant_create_updates_minimal_variant_price(
mutation ProductVariantCreate(
$productId: ID!,
$sku: String!,
- $priceOverride: Decimal,
+ $price: Decimal,
$attributes: [AttributeValueInput]!,
) {
productVariantCreate(
input: {
product: $productId,
sku: $sku,
- priceOverride: $priceOverride,
+ price: $price,
attributes: $attributes
}
) {
@@ -151,7 +46,7 @@ def test_product_variant_create_updates_minimal_variant_price(
"""
product_id = to_global_id("Product", product.pk)
sku = "1"
- price_override = "1.99"
+ price = "1.99"
variant_id = graphene.Node.to_global_id(
"Attribute", product.product_type.variant_attributes.first().pk
)
@@ -159,7 +54,7 @@ def test_product_variant_create_updates_minimal_variant_price(
variables = {
"productId": product_id,
"sku": sku,
- "priceOverride": price_override,
+ "price": price,
"attributes": [{"id": variant_id, "values": [variant_value]}],
}
response = staff_api_client.post_graphql(
@@ -189,12 +84,12 @@ def test_product_variant_update_updates_minimal_variant_price(
query = """
mutation ProductVariantUpdate(
$id: ID!,
- $priceOverride: Decimal,
+ $price: Decimal,
) {
productVariantUpdate(
id: $id,
input: {
- priceOverride: $priceOverride,
+ price: $price,
}
) {
productVariant {
@@ -209,8 +104,8 @@ def test_product_variant_update_updates_minimal_variant_price(
"""
variant = product.variants.first()
variant_id = to_global_id("ProductVariant", variant.pk)
- price_override = "1.99"
- variables = {"id": variant_id, "priceOverride": price_override}
+ price = "1.99"
+ variables = {"id": variant_id, "price": price}
response = staff_api_client.post_graphql(
query, variables, permissions=[permission_manage_products]
)
@@ -238,12 +133,12 @@ def test_product_variant_update_updates_invalid_variant_price(
query = """
mutation ProductVariantUpdate(
$id: ID!,
- $priceOverride: Decimal,
+ $price: Decimal,
) {
productVariantUpdate(
id: $id,
input: {
- priceOverride: $priceOverride,
+ price: $price,
}
) {
productVariant {
@@ -259,8 +154,8 @@ def test_product_variant_update_updates_invalid_variant_price(
"""
variant = product.variants.first()
variant_id = to_global_id("ProductVariant", variant.pk)
- price_override = "-1.99"
- variables = {"id": variant_id, "priceOverride": price_override}
+ price = "-1.99"
+ variables = {"id": variant_id, "price": price}
response = staff_api_client.post_graphql(
query, variables, permissions=[permission_manage_products]
)
@@ -268,7 +163,7 @@ def test_product_variant_update_updates_invalid_variant_price(
content = get_graphql_content(response)
data = content["data"]["productVariantUpdate"]
- assert data["productErrors"][0]["field"] == "priceOverride"
+ assert data["productErrors"][0]["field"] == "price"
assert data["productErrors"][0]["code"] == ProductErrorCode.INVALID.name
diff --git a/tests/api/test_product_sorting_attributes.py b/tests/api/test_product_sorting_attributes.py
index 945fce1cfa..b9947bb93f 100644
--- a/tests/api/test_product_sorting_attributes.py
+++ b/tests/api/test_product_sorting_attributes.py
@@ -3,7 +3,6 @@ import os.path
import graphene
import pytest
-from saleor.core.taxes import zero_money
from saleor.product import models as product_models
from saleor.product.utils.attributes import associate_attribute_values_to_instance
@@ -100,7 +99,6 @@ def products_structures(category):
slug=f"{attrs[0]}-apple-{attrs[1]}-({i})",
product_type=pt_apples,
category=category,
- price=zero_money(),
is_published=True,
)
for i, attrs in enumerate(zip(COLORS, TRADEMARKS))
@@ -116,7 +114,6 @@ def products_structures(category):
slug=f"{attrs[0]}-orange-{attrs[1]}-({i})",
product_type=pt_oranges,
category=category,
- price=zero_money(),
is_published=True,
)
for i, attrs in enumerate(zip(COLORS, TRADEMARKS))
@@ -129,7 +126,6 @@ def products_structures(category):
slug="oopsie-dummy",
product_type=pt_other,
category=category,
- price=zero_money(),
is_published=True,
)
product_models.Product.objects.create(
@@ -137,7 +133,6 @@ def products_structures(category):
slug="another-dummy",
product_type=pt_other,
category=category,
- price=zero_money(),
is_published=True,
)
dummy_attr_value = attr_value(dummy_attr, DUMMIES[0])
@@ -474,7 +469,6 @@ def test_sort_product_not_having_attribute_data(api_client, category, count_quer
expected_results = ["Z", "Y", "A"]
product_create_kwargs = {
"category": category,
- "price": zero_money(),
"is_published": True,
}
diff --git a/tests/api/test_variant.py b/tests/api/test_variant.py
index 62ffc6d1ac..b60f310f7a 100644
--- a/tests/api/test_variant.py
+++ b/tests/api/test_variant.py
@@ -3,7 +3,6 @@ from uuid import uuid4
import graphene
import pytest
-from graphene.utils.str_converters import to_camel_case
from saleor.product.error_codes import ProductErrorCode
from saleor.product.models import ProductVariant
@@ -46,7 +45,7 @@ def test_fetch_variant(staff_api_client, product, permission_manage_products):
id
}
name
- priceOverride {
+ price {
currency
amount
}
@@ -76,7 +75,7 @@ def test_create_variant(
$productId: ID!,
$sku: String!,
$stocks: [StockInput!],
- $priceOverride: Decimal,
+ $price: Decimal,
$costPrice: Decimal,
$attributes: [AttributeValueInput]!,
$weight: WeightScalar,
@@ -86,7 +85,7 @@ def test_create_variant(
product: $productId,
sku: $sku,
stocks: $stocks,
- priceOverride: $priceOverride,
+ price: $price,
costPrice: $costPrice,
attributes: $attributes,
trackInventory: $trackInventory,
@@ -107,7 +106,7 @@ def test_create_variant(
slug
}
}
- priceOverride {
+ price {
currency
amount
localized
@@ -134,7 +133,7 @@ def test_create_variant(
"""
product_id = graphene.Node.to_global_id("Product", product.pk)
sku = "1"
- price_override = 1.32
+ price = 1.32
cost_price = 3.22
weight = 10.22
variant_slug = product_type.variant_attributes.first().slug
@@ -154,7 +153,7 @@ def test_create_variant(
"sku": sku,
"stocks": stocks,
"costPrice": cost_price,
- "priceOverride": price_override,
+ "price": price,
"weight": weight,
"attributes": [{"id": variant_id, "values": [variant_value]}],
"trackInventory": True,
@@ -167,7 +166,7 @@ def test_create_variant(
data = content["productVariant"]
assert data["name"] == variant_value
assert data["costPrice"]["amount"] == cost_price
- assert data["priceOverride"]["amount"] == price_override
+ assert data["price"]["amount"] == price
assert data["sku"] == sku
assert data["attributes"][0]["attribute"]["slug"] == variant_slug
assert data["attributes"][0]["values"][0]["slug"] == variant_value
@@ -332,7 +331,7 @@ def test_create_product_variant_update_with_new_attributes(
$id: ID!
$attributes: [AttributeValueInput]
$costPrice: Decimal
- $priceOverride: Decimal
+ $price: Decimal
$sku: String
$trackInventory: Boolean!
) {
@@ -341,7 +340,7 @@ def test_create_product_variant_update_with_new_attributes(
input: {
attributes: $attributes
costPrice: $costPrice
- priceOverride: $priceOverride
+ price: $price
sku: $sku
trackInventory: $trackInventory
}
@@ -381,7 +380,7 @@ def test_create_product_variant_update_with_new_attributes(
"attributes": [{"id": size_attribute_id, "values": ["XXXL"]}],
"costPrice": 10,
"id": variant_id,
- "priceOverride": 0,
+ "price": 0,
"sku": "21599567",
"trackInventory": True,
}
@@ -490,9 +489,8 @@ def test_update_product_variant_with_negative_weight(
assert error["code"] == ProductErrorCode.INVALID.name
[email protected]("field", ("cost_price", "price_override"))
-def test_update_product_variant_unset_amounts(
- staff_api_client, product, permission_manage_products, field
+def test_update_product_variant_unset_cost_price(
+ staff_api_client, product, permission_manage_products
):
"""Ensure setting nullable amounts to null is properly handled
(setting the amount to none) and doesn't override the currency.
@@ -501,14 +499,12 @@ def test_update_product_variant_unset_amounts(
mutation updateVariant (
$id: ID!,
$sku: String!,
- $costPrice: Decimal,
- $priceOverride: Decimal) {
+ $costPrice: Decimal) {
productVariantUpdate(
id: $id,
input: {
sku: $sku,
costPrice: $costPrice,
- priceOverride: $priceOverride
}) {
productVariant {
name
@@ -518,11 +514,6 @@ def test_update_product_variant_unset_amounts(
amount
localized
}
- priceOverride {
- currency
- amount
- localized
- }
}
}
}
@@ -532,9 +523,7 @@ def test_update_product_variant_unset_amounts(
variant_id = graphene.Node.to_global_id("ProductVariant", variant.pk)
sku = variant.sku
- camel_case_field_name = to_camel_case(field)
-
- variables = {"id": variant_id, "sku": sku, camel_case_field_name: None}
+ variables = {"id": variant_id, "sku": sku, "costPrice": None}
response = staff_api_client.post_graphql(
query, variables, permissions=[permission_manage_products]
@@ -542,11 +531,11 @@ def test_update_product_variant_unset_amounts(
variant.refresh_from_db()
assert variant.currency is not None
- assert getattr(variant, field) is None
+ assert getattr(variant, "cost_price") is None
content = get_graphql_content(response)
data = content["data"]["productVariantUpdate"]["productVariant"]
- assert data[camel_case_field_name] is None
+ assert data["costPrice"] is None
QUERY_UPDATE_VARIANT_ATTRIBUTES = """
@@ -927,6 +916,9 @@ PRODUCT_VARIANT_BULK_CREATE_MUTATION = """
productVariants{
id
sku
+ price {
+ amount
+ }
stocks {
warehouse {
slug
@@ -953,10 +945,10 @@ def test_product_variant_bulk_create_by_attribute_id(
{
"sku": sku,
"costPrice": None,
- "priceOverride": None,
"weight": 2.5,
"trackInventory": True,
"attributes": [{"id": attribut_id, "values": [attribute_value.name]}],
+ "price": 10,
}
]
@@ -973,11 +965,10 @@ def test_product_variant_bulk_create_by_attribute_id(
assert attribute_value_count == size_attribute.values.count()
product_variant = ProductVariant.objects.get(sku=sku)
assert not product_variant.cost_price
- assert not product_variant.price_override
@pytest.mark.parametrize(
- "price_field", ("priceOverride", "costPrice",),
+ "price_field", ("price", "costPrice",),
)
def test_product_variant_bulk_create_by_attribute_id_with_invalid_price(
staff_api_client, product, size_attribute, permission_manage_products, price_field
@@ -991,8 +982,9 @@ def test_product_variant_bulk_create_by_attribute_id_with_invalid_price(
"weight": 2.5,
"trackInventory": True,
"attributes": [{"id": attribut_id, "values": [attribute_value.name]}],
+ "price": 10,
+ price_field: "-1",
}
- variant[price_field] = "-1"
variables = {"productId": product_id, "variants": [variant]}
staff_api_client.user.user_permissions.add(permission_manage_products)
@@ -1010,7 +1002,7 @@ def test_product_variant_bulk_create_empty_attribute(
):
product_variant_count = ProductVariant.objects.count()
product_id = graphene.Node.to_global_id("Product", product.pk)
- variants = [{"sku": str(uuid4())[:12], "attributes": []}]
+ variants = [{"sku": str(uuid4())[:12], "attributes": [], "price": 10}]
variables = {"productId": product_id, "variants": variants}
staff_api_client.user.user_permissions.add(permission_manage_products)
@@ -1036,10 +1028,12 @@ def test_product_variant_bulk_create_with_new_attribute_value(
{
"sku": str(uuid4())[:12],
"attributes": [{"id": size_attribute_id, "values": [attribute_value.name]}],
+ "price": 10,
},
{
"sku": str(uuid4())[:12],
"attributes": [{"id": size_attribute_id, "values": ["Test-attribute"]}],
+ "price": 10,
},
]
@@ -1076,6 +1070,7 @@ def test_product_variant_bulk_create_stocks_input(
}
],
"attributes": [{"id": size_attribute_id, "values": [attribute_value.name]}],
+ "price": 10,
},
{
"sku": str(uuid4())[:12],
@@ -1094,6 +1089,7 @@ def test_product_variant_bulk_create_stocks_input(
),
},
],
+ "price": 10,
},
]
@@ -1118,6 +1114,7 @@ def test_product_variant_bulk_create_stocks_input(
"quantity": variants[0]["stocks"][0]["quantity"],
}
],
+ "price": {"amount": 10.0},
},
{
"sku": variants[1]["sku"],
@@ -1131,6 +1128,7 @@ def test_product_variant_bulk_create_stocks_input(
"quantity": variants[1]["stocks"][1]["quantity"],
},
],
+ "price": {"amount": 10.0},
},
]
for variant_data in data["productVariants"]:
@@ -1157,6 +1155,7 @@ def test_product_variant_bulk_create_duplicated_warehouses(
}
],
"attributes": [{"id": size_attribute_id, "values": [attribute_value.name]}],
+ "price": 10,
},
{
"sku": str(uuid4())[:12],
@@ -1165,6 +1164,7 @@ def test_product_variant_bulk_create_duplicated_warehouses(
{"quantity": 15, "warehouse": warehouse1_id},
{"quantity": 15, "warehouse": warehouse1_id},
],
+ "price": 10,
},
]
@@ -1203,10 +1203,12 @@ def test_product_variant_bulk_create_duplicated_sku(
{
"sku": sku,
"attributes": [{"id": size_attribute_id, "values": ["Test-value"]}],
+ "price": 10,
},
{
"sku": sku2,
"attributes": [{"id": size_attribute_id, "values": ["Test-valuee"]}],
+ "price": 10,
},
]
@@ -1237,10 +1239,12 @@ def test_product_variant_bulk_create_duplicated_sku_in_input(
{
"sku": sku,
"attributes": [{"id": size_attribute_id, "values": ["Test-value"]}],
+ "price": 10,
},
{
"sku": sku,
"attributes": [{"id": size_attribute_id, "values": ["Test-value2"]}],
+ "price": 10,
},
]
@@ -1274,18 +1278,22 @@ def test_product_variant_bulk_create_many_errors(
{
"sku": str(uuid4())[:12],
"attributes": [{"id": size_attribute_id, "values": ["Test-value1"]}],
+ "price": 10,
},
{
"sku": str(uuid4())[:12],
"attributes": [{"id": size_attribute_id, "values": ["Test-value4"]}],
+ "price": 10,
},
{
"sku": sku,
"attributes": [{"id": size_attribute_id, "values": ["Test-value2"]}],
+ "price": 10,
},
{
"sku": str(uuid4())[:12],
"attributes": [{"id": invalid_attribute_id, "values": ["Test-value3"]}],
+ "price": 10,
},
]
@@ -1338,6 +1346,7 @@ def test_product_variant_bulk_create_two_variants_duplicated_attribute_value(
{"id": color_attribute_id, "values": ["red"]},
{"id": size_attribute_id, "values": ["small"]},
],
+ "price": 10,
}
]
variables = {"productId": product_id, "variants": variants}
@@ -1372,8 +1381,8 @@ def test_product_variant_bulk_create_two_variants_duplicated_attribute_value_in_
{"id": size_attribute_id, "values": [size_attribute.values.last().slug]},
]
variants = [
- {"sku": str(uuid4())[:12], "attributes": attributes},
- {"sku": str(uuid4())[:12], "attributes": attributes},
+ {"sku": str(uuid4())[:12], "attributes": attributes, "price": 10},
+ {"sku": str(uuid4())[:12], "attributes": attributes, "price": 10},
]
variables = {"productId": product_id, "variants": variants}
staff_api_client.user.user_permissions.add(permission_manage_products)
@@ -1409,6 +1418,7 @@ def test_product_variant_bulk_create_two_variants_duplicated_one_attribute_value
{"id": color_attribute_id, "values": ["red"]},
{"id": size_attribute_id, "values": ["big"]},
],
+ "price": 10,
}
]
variables = {"productId": product_id, "variants": variants}
diff --git a/tests/api/test_variant_pricing.py b/tests/api/test_variant_pricing.py
index 02209597cd..cffe3ec425 100644
--- a/tests/api/test_variant_pricing.py
+++ b/tests/api/test_variant_pricing.py
@@ -47,7 +47,7 @@ query {
def test_get_variant_pricing_on_sale(api_client, sale, product):
- price = product.price
+ price = product.variants.first().price
discounted_price = price.amount - sale.value
response = api_client.post_graphql(QUERY_GET_VARIANT_PRICING, {})
@@ -75,6 +75,8 @@ def test_get_variant_pricing_on_sale(api_client, sale, product):
def test_get_variant_pricing_not_on_sale(api_client, product):
+ price = product.variants.first().price
+
response = api_client.post_graphql(QUERY_GET_VARIANT_PRICING, {})
content = get_graphql_content(response)
@@ -90,12 +92,12 @@ def test_get_variant_pricing_not_on_sale(api_client, product):
assert pricing["discount"] is None
# check the undiscounted price
- assert pricing["priceUndiscounted"]["currency"] == product.price.currency
- assert pricing["priceUndiscounted"]["net"]["amount"] == product.price.amount
+ assert pricing["priceUndiscounted"]["currency"] == price.currency
+ assert pricing["priceUndiscounted"]["net"]["amount"] == price.amount
# check the discounted price
- assert pricing["price"]["currency"] == product.price.currency
- assert pricing["price"]["net"]["amount"] == product.price.amount
+ assert pricing["price"]["currency"] == price.currency
+ assert pricing["price"]["net"]["amount"] == price.amount
def test_variant_pricing(variant: ProductVariant, monkeypatch, settings, stock):
diff --git a/tests/conftest.py b/tests/conftest.py
index 1969c1c450..456b54373d 100644
--- a/tests/conftest.py
+++ b/tests/conftest.py
@@ -596,7 +596,6 @@ def categories_tree(db, product_type): # pylint: disable=W0613
product = Product.objects.create(
name="Test product",
slug="test-product-10",
- price=Money(10, "USD"),
product_type=product_type,
category=child,
is_published=True,
@@ -686,7 +685,6 @@ def product(product_type, category, warehouse):
product = Product.objects.create(
name="Test product",
slug="test-product-11",
- price=Money("10.00", "USD"),
product_type=product_type,
category=category,
is_published=True,
@@ -698,7 +696,7 @@ 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")
+ product=product, sku="123", cost_price=Money("1.00", "USD"), price_amount=10.00
)
Stock.objects.create(warehouse=warehouse, product_variant=variant, quantity=10)
@@ -711,13 +709,15 @@ def product_with_single_variant(product_type, category, warehouse):
product = Product.objects.create(
name="Test product with single variant",
slug="test-product-with-single-variant",
- price=Money("1.99", "USD"),
product_type=product_type,
category=category,
is_published=True,
)
variant = ProductVariant.objects.create(
- product=product, sku="SKU_SINGLE_VARIANT", cost_price=Money("1.00", "USD")
+ product=product,
+ sku="SKU_SINGLE_VARIANT",
+ cost_price=Money("1.00", "USD"),
+ price_amount=1.99,
)
Stock.objects.create(product_variant=variant, warehouse=warehouse, quantity=101)
return product
@@ -728,7 +728,6 @@ def product_with_two_variants(product_type, category, warehouse):
product = Product.objects.create(
name="Test product with two variants",
slug="test-product-with-two-variant",
- price=Money("10.00", "USD"),
product_type=product_type,
category=category,
is_published=True,
@@ -739,6 +738,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,
)
for i in (1, 2)
]
@@ -769,14 +769,16 @@ def product_with_variant_with_two_attributes(
product = Product.objects.create(
name="Test product with two variants",
slug="test-product-with-two-variant",
- price=Money("10.00", "USD"),
product_type=product_type,
category=category,
is_published=True,
)
variant = ProductVariant.objects.create(
- product=product, sku="prodVar1", cost_price=Money("1.00", "USD")
+ product=product,
+ sku="prodVar1",
+ cost_price=Money("1.00", "USD"),
+ price_amount=10,
)
associate_attribute_values_to_instance(
@@ -815,13 +817,12 @@ def product_with_default_variant(product_type_without_variant, category, warehou
product = Product.objects.create(
name="Test product",
slug="test-product-3",
- price=Money(10, "USD"),
product_type=product_type_without_variant,
category=category,
is_published=True,
)
variant = ProductVariant.objects.create(
- product=product, sku="1234", track_inventory=True
+ product=product, sku="1234", track_inventory=True, price_amount=10
)
Stock.objects.create(warehouse=warehouse, product_variant=variant, quantity=100)
return product
@@ -834,13 +835,12 @@ def variant_without_inventory_tracking(
product = Product.objects.create(
name="Test product without inventory tracking",
slug="test-product-without-tracking",
- price=Money(10, "USD"),
product_type=product_type_without_variant,
category=category,
is_published=True,
)
variant = ProductVariant.objects.create(
- product=product, sku="tracking123", track_inventory=False
+ product=product, sku="tracking123", track_inventory=False, price_amount=10
)
Stock.objects.create(warehouse=warehouse, product_variant=variant, quantity=0)
return variant
@@ -849,7 +849,7 @@ def variant_without_inventory_tracking(
@pytest.fixture
def variant(product) -> ProductVariant:
product_variant = ProductVariant.objects.create(
- product=product, sku="SKU_A", cost_price=Money(1, "USD")
+ product=product, sku="SKU_A", cost_price=Money(1, "USD"), price_amount=10
)
return product_variant
@@ -867,9 +867,9 @@ def product_variant_list(product):
return list(
ProductVariant.objects.bulk_create(
[
- ProductVariant(product=product, sku="1"),
- ProductVariant(product=product, sku="2"),
- ProductVariant(product=product, sku="3"),
+ ProductVariant(product=product, sku="1", price_amount=10),
+ ProductVariant(product=product, sku="2", price_amount=10),
+ ProductVariant(product=product, sku="3", price_amount=10),
]
)
)
@@ -886,12 +886,13 @@ def product_without_shipping(category, warehouse):
product = Product.objects.create(
name="Test product",
slug="test-product-4",
- price=Money("10.00", "USD"),
product_type=product_type,
category=category,
is_published=True,
)
- variant = ProductVariant.objects.create(product=product, sku="SKU_B")
+ variant = ProductVariant.objects.create(
+ product=product, sku="SKU_B", price_amount=10
+ )
Stock.objects.create(product_variant=variant, warehouse=warehouse, quantity=1)
return product
@@ -916,7 +917,6 @@ def product_list(product_type, category, warehouse):
pk=1486,
name="Test product 1",
slug="test-product-a",
- price=Money(10, "USD"),
category=category,
product_type=product_type,
is_published=True,
@@ -925,7 +925,6 @@ def product_list(product_type, category, warehouse):
pk=1487,
name="Test product 2",
slug="test-product-b",
- price=Money(20, "USD"),
category=category,
product_type=product_type,
is_published=True,
@@ -934,7 +933,6 @@ def product_list(product_type, category, warehouse):
pk=1489,
name="Test product 3",
slug="test-product-c",
- price=Money(30, "USD"),
category=category,
product_type=product_type,
is_published=True,
@@ -949,16 +947,19 @@ def product_list(product_type, category, warehouse):
product=products[0],
sku=str(uuid.uuid4()).replace("-", ""),
track_inventory=True,
+ price_amount=10,
),
ProductVariant(
product=products[1],
sku=str(uuid.uuid4()).replace("-", ""),
track_inventory=True,
+ price_amount=20,
),
ProductVariant(
product=products[2],
sku=str(uuid.uuid4()).replace("-", ""),
track_inventory=True,
+ price_amount=30,
),
]
)
@@ -1014,7 +1015,6 @@ def unavailable_product(product_type, category):
product = Product.objects.create(
name="Test product",
slug="test-product-5",
- price=Money("10.00", "USD"),
product_type=product_type,
is_published=False,
category=category,
@@ -1027,7 +1027,6 @@ def unavailable_product_with_variant(product_type, category, warehouse):
product = Product.objects.create(
name="Test product",
slug="test-product-6",
- price=Money("10.00", "USD"),
product_type=product_type,
is_published=False,
category=category,
@@ -1037,7 +1036,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")
+ product=product, sku="123", cost_price=Money(1, "USD"), price_amount=10
)
Stock.objects.create(product_variant=variant, warehouse=warehouse, quantity=10)
@@ -1050,7 +1049,6 @@ def product_with_images(product_type, category, media_root):
product = Product.objects.create(
name="Test product",
slug="test-product-7",
- price=Money("10.00", "USD"),
product_type=product_type,
category=category,
is_published=True,
@@ -1225,13 +1223,12 @@ def order_with_lines(order, product_type, category, shipping_zone, warehouse):
product = Product.objects.create(
name="Test product",
slug="test-product-8",
- price=Money("10.00", "USD"),
product_type=product_type,
category=category,
is_published=True,
)
variant = ProductVariant.objects.create(
- product=product, sku="SKU_A", cost_price=Money(1, "USD")
+ product=product, sku="SKU_A", cost_price=Money(1, "USD"), price_amount=10
)
stock = Stock.objects.create(
warehouse=warehouse, product_variant=variant, quantity=5
@@ -1255,13 +1252,12 @@ def order_with_lines(order, product_type, category, shipping_zone, warehouse):
product = Product.objects.create(
name="Test product 2",
slug="test-product-9",
- price=Money("20.00", "USD"),
product_type=product_type,
category=category,
is_published=True,
)
variant = ProductVariant.objects.create(
- product=product, sku="SKU_B", cost_price=Money(2, "USD")
+ product=product, sku="SKU_B", cost_price=Money(2, "USD"), price_amount=20
)
stock = Stock.objects.create(
product_variant=variant, warehouse=warehouse, quantity=2
@@ -1858,13 +1854,12 @@ def digital_content(category, media_root, warehouse) -> DigitalContent:
product = Product.objects.create(
name="Test digital product",
slug="test-digital-product",
- price=Money("10.00", "USD"),
product_type=product_type,
category=category,
is_published=True,
)
product_variant = ProductVariant.objects.create(
- product=product, sku="SKU_554", cost_price=Money(1, "USD")
+ product=product, sku="SKU_554", cost_price=Money(1, "USD"), price_amount=10
)
Stock.objects.create(
product_variant=product_variant, warehouse=warehouse, quantity=5,