Skip to content

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

Update "test_ProductUpdate_updates_minimal_variant_price"

Mateusz Dereniowski239ef8611e5d
diff --git a/.isort.cfg b/.isort.cfg
index fcfb25ad82..45830349a6 100644
--- a/.isort.cfg
+++ b/.isort.cfg
@@ -1,2 +1,2 @@
[settings]
-known_third_party = PIL,babel,bleach,bootstrap4,braintree,bs4,captcha,celery,dataclasses,dj_database_url,dj_email_url,django,django_babel,django_cache_url,django_countries,django_elasticsearch_dsl,django_filters,django_measurement,django_prices,django_prices_openexchangerates,django_prices_vatlayer,draftjs_sanitizer,elasticsearch_dsl,faker,freezegun,geolite2,google_measurement_protocol,graphene,graphene_django,graphene_django_optimizer,graphql,graphql_jwt,graphql_relay,html5lib,html_to_draftjs,i18naddress,impersonate,markdown,measurement,mptt,phonenumber_field,phonenumbers,prices,promise,pytest,razorpay,requests,sentry_sdk,six,social_core,storages,stripe,templated_email,tests,text_unidecode,versatileimagefield
+known_third_party = PIL,babel,bleach,bootstrap4,braintree,bs4,captcha,celery,dataclasses,dj_database_url,dj_email_url,django,django_babel,django_cache_url,django_countries,django_elasticsearch_dsl,django_filters,django_measurement,django_prices,django_prices_openexchangerates,django_prices_vatlayer,draftjs_sanitizer,elasticsearch_dsl,faker,freezegun,geolite2,google_measurement_protocol,graphene,graphene_django,graphene_django_optimizer,graphql,graphql_jwt,graphql_relay,html5lib,html_to_draftjs,i18naddress,impersonate,markdown,measurement,mptt,phonenumber_field,phonenumbers,prices,promise,pytest,razorpay,requests,sentry_sdk,six,social_core,storages,stripe,templated_email,text_unidecode,versatileimagefield
diff --git a/saleor/graphql/product/mutations/products.py b/saleor/graphql/product/mutations/products.py
index 611c2058a4..b57ecf18fa 100644
--- a/saleor/graphql/product/mutations/products.py
+++ b/saleor/graphql/product/mutations/products.py
@@ -499,8 +499,9 @@ class ProductCreate(ModelMutation):
price = data.get("base_price", data.get("price"))
if price is not None:
cleaned_input["price"] = price
- # Set the default "minimal_variant_price" to the "price"
- cleaned_input["minimal_variant_price"] = price
+ if instance.minimal_variant_price is None:
+ # Set the default "minimal_variant_price" to the "price"
+ cleaned_input["minimal_variant_price"] = price
# FIXME tax_rate logic should be dropped after we remove tax_rate from input
tax_rate = cleaned_input.pop("tax_rate", "")
@@ -604,6 +605,8 @@ class ProductUpdate(ProductCreate):
update_fields.append("sku")
if update_fields:
variant.save(update_fields=update_fields)
+ # Recalculate the "minimal variant price"
+ update_product_minimal_variant_price_task.delay(instance.pk)
class ProductDelete(ModelDeleteMutation):
diff --git a/tests/api/test_product_minimal_variant_price.py b/tests/api/test_product_minimal_variant_price.py
index d03b75e6c7..80b16dbde6 100644
--- a/tests/api/test_product_minimal_variant_price.py
+++ b/tests/api/test_product_minimal_variant_price.py
@@ -3,12 +3,13 @@ from unittest.mock import patch
import pytest
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 tests.api.utils import get_graphql_content
-def test_product_create_sets_minimal_variant_price(
+def test_ProductCreate_sets_minimal_variant_price(
staff_api_client, product_type, category, permission_manage_products
):
query = """
@@ -60,21 +61,25 @@ def test_product_create_sets_minimal_variant_price(
assert str(data["product"]["minimalVariantPrice"]["amount"]) == product_price
-def test_product_update_sets_minimal_variant_price(
- staff_api_client, product_type, category, permission_manage_products
+@patch(
+ "saleor.graphql.product.mutations.products"
+ ".update_product_minimal_variant_price_task"
+)
+def test_ProductUpdate_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 ProductCreate(
- $name: String!,
- $productTypeId: ID!,
- $categoryId: ID!,
+ mutation ProductUpdate(
+ $productId: ID!,
$basePrice: Decimal!,
) {
- productCreate(
+ productUpdate(
+ id: $productId
input: {
- name: $name,
- productType: $productTypeId,
- category: $categoryId,
basePrice: $basePrice
}
) {
@@ -91,32 +96,28 @@ def test_product_update_sets_minimal_variant_price(
}
}
"""
- 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,
- }
+ 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"]["productCreate"]
+ data = content["data"]["productUpdate"]
assert data["errors"] == []
- assert str(data["product"]["minimalVariantPrice"]["amount"]) == product_price
+
+ 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"
)
-def test_product_variant_create_updates_minimal_variant_price(
+def test_ProductVariantCreate_updates_minimal_variant_price(
mock_update_product_minimal_variant_price_task,
staff_api_client,
product,