Skip to content

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

Tests for replace category pre delete signal with functions and tests update for category model changes

IKarbowiak91e19ec1e06a
diff --git a/tests/api/test_bulk_delete.py b/tests/api/test_bulk_delete.py
index 5a3f1c9bbd..b498bb9b7e 100644
--- a/tests/api/test_bulk_delete.py
+++ b/tests/api/test_bulk_delete.py
@@ -35,6 +35,15 @@ MUTATION_DELETE_ORDER_LINES = """
"""
+MUTATION_CATEGORY_BULK_DELETE = """
+ mutation categoryBulkDelete($ids: [ID]!) {
+ categoryBulkDelete(ids: $ids) {
+ count
+ }
+ }
+"""
+
+
@pytest.fixture
def attribute_value_list(color_attribute):
value_1 = AttributeValue.objects.create(
@@ -167,13 +176,38 @@ def test_delete_attribute_values(
def test_delete_categories(staff_api_client, category_list, permission_manage_products):
- query = """
- mutation categoryBulkDelete($ids: [ID]!) {
- categoryBulkDelete(ids: $ids) {
- count
- }
+ variables = {
+ "ids": [
+ graphene.Node.to_global_id("Category", category.id)
+ for category in category_list
+ ]
}
- """
+ response = staff_api_client.post_graphql(
+ MUTATION_CATEGORY_BULK_DELETE,
+ variables,
+ permissions=[permission_manage_products],
+ )
+ content = get_graphql_content(response)
+
+ assert content["data"]["categoryBulkDelete"]["count"] == 3
+ assert not Category.objects.filter(
+ id__in=[category.id for category in category_list]
+ ).exists()
+
+
+def test_delete_categories_with_subcategories_and_products(
+ staff_api_client, category_list, permission_manage_products, product, category
+):
+ category.parent = category_list[0]
+ child_product = product
+ child_product.category = category
+
+ parent_product = child_product
+ parent_product.id = None
+ parent_product.category = category_list[0]
+
+ parent_product.save()
+ child_product.save()
variables = {
"ids": [
@@ -182,7 +216,9 @@ def test_delete_categories(staff_api_client, category_list, permission_manage_pr
]
}
response = staff_api_client.post_graphql(
- query, variables, permissions=[permission_manage_products]
+ MUTATION_CATEGORY_BULK_DELETE,
+ variables,
+ permissions=[permission_manage_products],
)
content = get_graphql_content(response)
@@ -191,6 +227,12 @@ def test_delete_categories(staff_api_client, category_list, permission_manage_pr
id__in=[category.id for category in category_list]
).exists()
+ for product in [child_product, parent_product]:
+ product.refresh_from_db()
+ assert not product.category
+ assert not product.is_published
+ assert not product.publication_date
+
def test_delete_collections(
staff_api_client, collection_list, permission_manage_products
diff --git a/tests/api/test_category.py b/tests/api/test_category.py
index c255d6035d..ac98b7a1bc 100644
--- a/tests/api/test_category.py
+++ b/tests/api/test_category.py
@@ -350,25 +350,27 @@ def test_category_update_mutation_without_background_image(
assert mock_create_thumbnails.call_count == 0
+MUTATION_CATEGORY_DELETE = """
+ mutation($id: ID!) {
+ categoryDelete(id: $id) {
+ category {
+ name
+ }
+ errors {
+ field
+ message
+ }
+ }
+ }
+"""
+
+
def test_category_delete_mutation(
staff_api_client, category, permission_manage_products
):
- query = """
- mutation($id: ID!) {
- categoryDelete(id: $id) {
- category {
- name
- }
- errors {
- field
- message
- }
- }
- }
- """
variables = {"id": graphene.Node.to_global_id("Category", category.id)}
response = staff_api_client.post_graphql(
- query, variables, permissions=[permission_manage_products]
+ MUTATION_CATEGORY_DELETE, variables, permissions=[permission_manage_products]
)
content = get_graphql_content(response)
data = content["data"]["categoryDelete"]
@@ -377,6 +379,62 @@ def test_category_delete_mutation(
category.refresh_from_db()
+def test_category_delete_mutation_for_categories_tree(
+ staff_api_client,
+ categories_tree_with_published_products,
+ permission_manage_products,
+):
+ parent = categories_tree_with_published_products
+ parent_product = parent.products.first()
+ child_product = parent.children.first().products.first()
+
+ variables = {"id": graphene.Node.to_global_id("Category", parent.id)}
+ response = staff_api_client.post_graphql(
+ MUTATION_CATEGORY_DELETE, variables, permissions=[permission_manage_products]
+ )
+ content = get_graphql_content(response)
+ data = content["data"]["categoryDelete"]
+ assert data["category"]["name"] == parent.name
+ with pytest.raises(parent._meta.model.DoesNotExist):
+ parent.refresh_from_db()
+
+ for product in [child_product, parent_product]:
+ product.refresh_from_db()
+ assert not product.is_published
+ assert not product.publication_date
+
+
+def test_category_delete_mutation_for_children_from_categories_tree(
+ staff_api_client,
+ categories_tree_with_published_products,
+ permission_manage_products,
+):
+ parent = categories_tree_with_published_products
+ child = parent.children.first()
+ parent_product = parent.products.first()
+ child_product = child.products.first()
+
+ variables = {"id": graphene.Node.to_global_id("Category", child.id)}
+ response = staff_api_client.post_graphql(
+ MUTATION_CATEGORY_DELETE, variables, permissions=[permission_manage_products]
+ )
+ content = get_graphql_content(response)
+ data = content["data"]["categoryDelete"]
+ assert data["category"]["name"] == child.name
+ with pytest.raises(child._meta.model.DoesNotExist):
+ child.refresh_from_db()
+
+ parent_product.refresh_from_db()
+ assert parent_product.category
+ assert parent_product.is_published
+ assert parent_product.publication_date
+
+ child_product.refresh_from_db()
+ assert not child_product.category
+ assert not child_product.is_published
+ assert not child_product.publication_date
+
+
LEVELED_CATEGORIES_QUERY = """
query leveled_categories($level: Int) {
categories(level: $level, first: 20) {
diff --git a/tests/api/test_product.py b/tests/api/test_product.py
index a0f1024db9..1f5acdd5a0 100644
--- a/tests/api/test_product.py
+++ b/tests/api/test_product.py
@@ -437,12 +437,14 @@ def test_product_query_search(user_api_client, product_type, category):
price=Money("10.00", "USD"),
product_type=product_type,
category=category,
+ is_published=True,
)
Product.objects.create(
name="Red Paint",
price=Money("10.00", "USD"),
product_type=product_type,
category=category,
+ is_published=True,
)
query = """
diff --git a/tests/api/test_product_minimal_variant_price.py b/tests/api/test_product_minimal_variant_price.py
index 7659698c31..492f344438 100644
--- a/tests/api/test_product_minimal_variant_price.py
+++ b/tests/api/test_product_minimal_variant_price.py
@@ -1,7 +1,6 @@
from unittest.mock import patch
import graphene
-import pytest
from freezegun import freeze_time
from graphql_relay import from_global_id, to_global_id
from prices import Money
@@ -263,12 +262,19 @@ def test_product_variant_delete_updates_minimal_variant_price(
)
-def test_category_delete_also_deletes_products(
- staff_api_client, product, category, permission_manage_products
+@patch(
+ "saleor.graphql.product.mutations.products"
+ ".update_products_minimal_variant_prices_task"
+)
+def test_category_delete_updates_minimal_variant_price(
+ mock_update_products_minimal_variant_prices_task,
+ staff_api_client,
+ categories_tree_with_published_products,
+ permission_manage_products,
):
- # Making sure the products associated with the category are deleted otherwise
- # we need to update their "minimal_variant_price" if the category was on sale.
- category.products.add(product)
+ parent = categories_tree_with_published_products
+ product_list = [parent.children.first().products.first(), parent.products.first()]
+
query = """
mutation CategoryDelete($id: ID!) {
categoryDelete(id: $id) {
@@ -282,7 +288,7 @@ def test_category_delete_also_deletes_products(
}
}
"""
- variables = {"id": to_global_id("Category", category.pk)}
+ variables = {"id": to_global_id("Category", parent.pk)}
response = staff_api_client.post_graphql(
query, variables, permissions=[permission_manage_products]
)
@@ -291,8 +297,14 @@ def test_category_delete_also_deletes_products(
content = get_graphql_content(response)
data = content["data"]["categoryDelete"]
assert data["errors"] == []
- with pytest.raises(product._meta.model.DoesNotExist):
+
+ mock_update_products_minimal_variant_prices_task.delay.assert_called_once_with(
+ product_ids=[p.pk for p in product_list]
+ )
+
+ for product in product_list:
product.refresh_from_db()
+ assert not product.category
@patch(
diff --git a/tests/conftest.py b/tests/conftest.py
index 25ecbcefc6..61ad33ef2d 100644
--- a/tests/conftest.py
+++ b/tests/conftest.py
@@ -1,3 +1,4 @@
+import datetime
import uuid
from decimal import Decimal
from io import BytesIO
@@ -419,12 +420,29 @@ def categories_tree(db, product_type): # pylint: disable=W0613
price=Money(10, "USD"),
product_type=product_type,
category=child,
+ is_published=True,
)
associate_attribute_values_to_instance(product, product_attr, attr_value)
return parent
+def categories_tree_with_published_products(categories_tree, product):
+ parent = categories_tree
+ parent_product = product
+ parent_product.category = parent
+
+ child = parent.children.first()
+ child_product = child.products.first()
+
+ for product in [child_product, parent_product]:
+ product.publication_date = datetime.date.today()
+ product.is_published = True
+ product.save()
+ return parent
+
+
@pytest.fixture
def non_default_category(db): # pylint: disable=W0613
return Category.objects.create(name="Not default", slug="not-default")
@@ -516,6 +534,7 @@ def product_with_two_variants(color_attribute, size_attribute, category):
price=Money("10.00", "USD"),
product_type=product_type,
category=category,
+ is_published=True,
)
variant = ProductVariant.objects.create(
@@ -564,6 +583,7 @@ def product_with_default_variant(product_type_without_variant, category):
price=Money(10, "USD"),
product_type=product_type_without_variant,
category=category,
+ is_published=True,
)
ProductVariant.objects.create(
product=product, sku="1234", track_inventory=True, quantity=100
@@ -606,6 +626,7 @@ def product_without_shipping(category):
price=Money("10.00", "USD"),
product_type=product_type,
category=category,
+ is_published=True,
)
ProductVariant.objects.create(product=product, sku="SKU_B")
return product
@@ -872,6 +893,7 @@ def order_with_lines(order, product_type, category, shipping_zone):
price=Money("10.00", "USD"),
product_type=product_type,
category=category,
+ is_published=True,
)
variant = ProductVariant.objects.create(
product=product,
@@ -898,6 +920,7 @@ def order_with_lines(order, product_type, category, shipping_zone):
price=Money("20.00", "USD"),
product_type=product_type,
category=category,
+ is_published=True,
)
variant = ProductVariant.objects.create(
product=product,
@@ -1155,6 +1178,7 @@ def collection_with_image(db, image, media_root):
slug="collection",
description="Test description",
background_image=image,
+ is_published=True,
)
return collection
@@ -1163,9 +1187,9 @@ def collection_with_image(db, image, media_root):
def collection_list(db):
collections = Collection.objects.bulk_create(
[
- Collection(name="Collection 1"),
- Collection(name="Collection 2"),
- Collection(name="Collection 3"),
+ Collection(name="Collection 1", is_published="True"),
+ Collection(name="Collection 2", is_published="True"),
+ Collection(name="Collection 3", is_published="True"),
]
)
return collections
@@ -1198,15 +1222,30 @@ def unpublished_collection():
@pytest.fixture
def page(db):
- data = {"slug": "test-url", "title": "Test page", "content": "test content"}
+ data = {
+ "slug": "test-url",
+ "title": "Test page",
+ "content": "test content",
+ "is_published": True,
+ }
page = Page.objects.create(**data)
return page
@pytest.fixture
def page_list(db):
- data_1 = {"slug": "test-url", "title": "Test page", "content": "test content"}
- data_2 = {"slug": "test-url-2", "title": "Test page", "content": "test content"}
+ data_1 = {
+ "slug": "test-url",
+ "title": "Test page",
+ "content": "test content",
+ "is_published": True,
+ }
+ data_2 = {
+ "slug": "test-url-2",
+ "title": "Test page",
+ "content": "test content",
+ "is_published": True,
+ }
pages = Page.objects.bulk_create([Page(**data_1), Page(**data_2)])
return pages
@@ -1335,6 +1374,7 @@ def digital_content(category, media_root) -> DigitalContent:
price=Money("10.00", "USD"),
product_type=product_type,
category=category,
+ is_published=True,
)
product_variant = ProductVariant.objects.create(
product=product,
diff --git a/tests/test_elasticsearch.py b/tests/test_elasticsearch.py
index 3718559b71..9ea2399942 100644
--- a/tests/test_elasticsearch.py
+++ b/tests/test_elasticsearch.py
@@ -55,6 +55,7 @@ def indexed_products(product_type, category):
price=Money(10, "USD"),
product_type=product_type,
category=category,
+ is_published=True,
)
return product
diff --git a/tests/test_postgresql_search.py b/tests/test_postgresql_search.py
index 4941c14cb0..e37f502c74 100644
--- a/tests/test_postgresql_search.py
+++ b/tests/test_postgresql_search.py
@@ -31,6 +31,7 @@ def named_products(category, product_type):
price=Money(Decimal(6.6), "USD"),
product_type=product_type,
category=category,
+ is_published=True,
)
return product
diff --git a/tests/test_product.py b/tests/test_product.py
index 7fda8a54ae..b581521fc2 100644
--- a/tests/test_product.py
+++ b/tests/test_product.py
@@ -1054,23 +1054,3 @@ def test_publish_product_with_category(product_type, category):
product.save()
assert product.is_published
-
-
-def test_product_category_pre_delete_signal(categories_tree, product):
- child = categories_tree.children.first()
- child_product = child.products.first()
- product.category = categories_tree
- product.name = "Test parent product"
-
- for product in [child_product, product]:
- product.publication_date = datetime.date.today()
- product.is_published = True
- product.save()
-
- categories_tree.delete()
-
- for product in [child_product, product]:
- product = Product.objects.get(name=product.name)
- assert not product.category
- assert not product.is_published
- assert not product.publication_date
diff --git a/tests/test_product_attributes.py b/tests/test_product_attributes.py
index 2eaaa054c4..6a71fbc86c 100644
--- a/tests/test_product_attributes.py
+++ b/tests/test_product_attributes.py
@@ -9,6 +9,7 @@ from saleor.product.tasks import _update_variants_names
from saleor.product.utils.attributes import (
associate_attribute_values_to_instance,
generate_name_for_variant,
+ unpublished_products_before_category_delete,
)
@@ -136,3 +137,18 @@ def test_associate_attribute_to_product_instance_without_values(product):
# Ensure the values were cleared and no new assignment entry was created
assert new_assignment.pk == old_assignment.pk
assert new_assignment.values.count() == 0
+
+
+def test_unpublished_products_before_category_delete_for_category_tree(
+ categories_tree_with_published_products
+):
+ parent = categories_tree_with_published_products
+ parent_product = parent.products.first()
+ child_product = parent.children.first().products.first()
+
+ unpublished_products_before_category_delete(parent)
+
+ for product in [child_product, parent_product]:
+ product.refresh_from_db()
+ assert not product.is_published
+ assert not product.publication_date
diff --git a/tests/test_product_minimal_variant_price.py b/tests/test_product_minimal_variant_price.py
index 3976e37a8c..1a44cd7b40 100644
--- a/tests/test_product_minimal_variant_price.py
+++ b/tests/test_product_minimal_variant_price.py
@@ -10,6 +10,7 @@ from saleor.product.models import Product, ProductVariant
from saleor.product.tasks import (
update_all_products_minimal_variant_prices_task,
update_products_minimal_variant_prices_of_catalogues,
+ update_products_minimal_variant_prices_task,
)
from saleor.product.utils.variant_prices import update_product_minimal_variant_price
@@ -89,6 +90,24 @@ def test_update_all_products_minimal_variant_prices_task(product_list):
assert product.minimal_variant_price == price_override
+def test_update_products_minimal_variant_prices_task(product_list):
+ price_override = Money("0.01", "USD")
+ for product in product_list:
+ assert product.minimal_variant_price > price_override
+ variant = product.variants.first()
+ variant.price_override = price_override
+ variant.save()
+ # Check that "variant.save()" doesn't update the "minimal_variant_price"
+ assert product.minimal_variant_price > price_override
+
+ update_products_minimal_variant_prices_task.apply(
+ kwargs={"product_ids": [product.pk for product in product_list]}
+ )
+ for product in product_list:
+ product.refresh_from_db()
+ assert product.minimal_variant_price == price_override
+
+
def test_product_objects_create_sets_default_minimal_variant_price(
product_type, category
):