Skip to content

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

Split fixtures for Product module (#16869)

Piotr Zabieglikee0521c550cc
diff --git a/conftest.py b/conftest.py
index 7d49d49d3a..6fda920bb5 100644
--- a/conftest.py
+++ b/conftest.py
@@ -39,6 +39,7 @@ pytest_plugins = [
"saleor.warehouse.tests.fixtures",
"saleor.thumbnail.tests.fixtures",
"saleor.order.tests.fixtures",
+ "saleor.product.tests.fixtures",
]
diff --git a/saleor/graphql/product/tests/queries/test_products_query_with_filter.py b/saleor/graphql/product/tests/queries/test_products_query_with_filter.py
index 3ad077d6e6..d815aa91ae 100644
--- a/saleor/graphql/product/tests/queries/test_products_query_with_filter.py
+++ b/saleor/graphql/product/tests/queries/test_products_query_with_filter.py
@@ -27,31 +27,27 @@ from .....tests.utils import dummy_editorjs
from .....warehouse.models import Allocation, Reservation, Stock, Warehouse
from ....tests.utils import get_graphql_content
-
-def query_products_with_filter():
- query = """
- query ($filter: ProductFilterInput!, $channel: String) {
- products(first:5, filter: $filter, channel: $channel) {
- edges{
- node{
- id
- name
- }
- }
+QUERY_PRODUCTS_WITH_FILTER = """
+ query ($filter: ProductFilterInput!, $channel: String) {
+ products(first:5, filter: $filter, channel: $channel) {
+ edges{
+ node{
+ id
+ name
}
}
- """
- return query
+ }
+ }
+ """
def test_products_query_with_filter_attributes(
- query_products_with_filter,
staff_api_client,
product,
permission_manage_products,
channel_USD,
):
+ # given
product_type = ProductType.objects.create(
name="Custom Type",
slug="custom-type",
@@ -81,8 +77,12 @@ def test_products_query_with_filter_attributes(
}
staff_api_client.user.user_permissions.add(permission_manage_products)
- response = staff_api_client.post_graphql(query_products_with_filter, variables)
+
+ # when
+ response = staff_api_client.post_graphql(QUERY_PRODUCTS_WITH_FILTER, variables)
content = get_graphql_content(response)
+
+ # then
second_product_id = graphene.Node.to_global_id("Product", second_product.id)
products = content["data"]["products"]["edges"]
@@ -109,13 +109,13 @@ def test_products_query_with_filter_numeric_attributes(
gte,
lte,
expected_products_index,
- query_products_with_filter,
staff_api_client,
product,
category,
numeric_attribute,
permission_manage_products,
):
+ # given
product.product_type.product_attributes.add(numeric_attribute)
associate_attribute_values_to_instance(
product,
@@ -181,8 +181,12 @@ def test_products_query_with_filter_numeric_attributes(
}
staff_api_client.user.user_permissions.add(permission_manage_products)
- response = staff_api_client.post_graphql(query_products_with_filter, variables)
+
+ # when
+ response = staff_api_client.post_graphql(QUERY_PRODUCTS_WITH_FILTER, variables)
content = get_graphql_content(response)
+
+ # then
products = content["data"]["products"]["edges"]
assert len(products) == len(expected_products_index)
@@ -204,13 +208,13 @@ def test_products_query_with_filter_numeric_attributes(
def test_products_query_with_filter_boolean_attributes(
filter_value,
expected_products_index,
- query_products_with_filter,
staff_api_client,
product,
category,
boolean_attribute,
permission_manage_products,
):
+ # given
product.product_type.product_attributes.add(boolean_attribute)
associate_attribute_values_to_instance(
@@ -251,8 +255,12 @@ def test_products_query_with_filter_boolean_attributes(
}
staff_api_client.user.user_permissions.add(permission_manage_products)
- response = staff_api_client.post_graphql(query_products_with_filter, variables)
+
+ # when
+ response = staff_api_client.post_graphql(QUERY_PRODUCTS_WITH_FILTER, variables)
content = get_graphql_content(response)
+
+ # then
products = content["data"]["products"]["edges"]
assert len(products) == len(expected_products_index)
@@ -273,13 +281,11 @@ def test_products_query_with_filter_boolean_attributes(
)
def test_products_query_with_filter_non_existing_boolean_attributes(
filter_value,
- query_products_with_filter,
staff_api_client,
product,
permission_manage_products,
):
# given
-
variables = {
"filter": {
"attributes": [{"slug": "non-existing-atr", "boolean": filter_value}]
@@ -289,7 +295,7 @@ def test_products_query_with_filter_non_existing_boolean_attributes(
staff_api_client.user.user_permissions.add(permission_manage_products)
# when
- response = staff_api_client.post_graphql(query_products_with_filter, variables)
+ response = staff_api_client.post_graphql(QUERY_PRODUCTS_WITH_FILTER, variables)
# then
content = get_graphql_content(response)
@@ -306,7 +312,6 @@ def test_products_query_with_filter_non_existing_boolean_attributes(
)
def test_products_query_with_filter_boolean_attributes_not_assigned_to_product(
filter_value,
- query_products_with_filter,
staff_api_client,
product,
category,
@@ -325,7 +330,7 @@ def test_products_query_with_filter_boolean_attributes_not_assigned_to_product(
staff_api_client.user.user_permissions.add(permission_manage_products)
# when
- response = staff_api_client.post_graphql(query_products_with_filter, variables)
+ response = staff_api_client.post_graphql(QUERY_PRODUCTS_WITH_FILTER, variables)
# then
content = get_graphql_content(response)
@@ -334,13 +339,13 @@ def test_products_query_with_filter_boolean_attributes_not_assigned_to_product(
def test_products_query_with_filter_by_attributes_values_and_range(
- query_products_with_filter,
staff_api_client,
product,
category,
numeric_attribute,
permission_manage_products,
):
+ # given
product_attr = get_product_attributes(product).first()
attr_value_1 = get_product_attribute_values(product, product_attr).first()
product.product_type.product_attributes.add(numeric_attribute)
@@ -385,8 +390,12 @@ def test_products_query_with_filter_by_attributes_values_and_range(
}
staff_api_client.user.user_permissions.add(permission_manage_products)
- response = staff_api_client.post_graphql(query_products_with_filter, variables)
+
+ # when
+ response = staff_api_client.post_graphql(QUERY_PRODUCTS_WITH_FILTER, variables)
content = get_graphql_content(response)
+
+ # then
products = content["data"]["products"]["edges"]
assert len(products) == 1
@@ -397,13 +406,13 @@ def test_products_query_with_filter_by_attributes_values_and_range(
def test_products_query_with_filter_swatch_attributes(
- query_products_with_filter,
staff_api_client,
product,
category,
swatch_attribute,
permission_manage_products,
):
+ # given
product.product_type.product_attributes.add(swatch_attribute)
associate_attribute_values_to_instance(
product,
@@ -444,8 +453,12 @@ def test_products_query_with_filter_swatch_attributes(
}
staff_api_client.user.user_permissions.add(permission_manage_products)
- response = staff_api_client.post_graphql(query_products_with_filter, variables)
+
+ # when
+ response = staff_api_client.post_graphql(QUERY_PRODUCTS_WITH_FILTER, variables)
content = get_graphql_content(response)
+
+ # then
second_product_id = graphene.Node.to_global_id("Product", second_product.id)
products = content["data"]["products"]["edges"]
@@ -455,7 +468,6 @@ def test_products_query_with_filter_swatch_attributes(
def test_products_query_with_filter_date_range_date_attributes(
- query_products_with_filter,
staff_api_client,
product_list,
permission_manage_products,
@@ -506,7 +518,7 @@ def test_products_query_with_filter_date_range_date_attributes(
staff_api_client.user.user_permissions.add(permission_manage_products)
# when
- response = staff_api_client.post_graphql(query_products_with_filter, variables)
+ response = staff_api_client.post_graphql(QUERY_PRODUCTS_WITH_FILTER, variables)
# then
content = get_graphql_content(response)
@@ -519,7 +531,6 @@ def test_products_query_with_filter_date_range_date_attributes(
def test_products_query_with_filter_date_range_date_variant_attributes(
- query_products_with_filter,
staff_api_client,
product_list,
permission_manage_products,
@@ -570,7 +581,7 @@ def test_products_query_with_filter_date_range_date_variant_attributes(
staff_api_client.user.user_permissions.add(permission_manage_products)
# when
- response = staff_api_client.post_graphql(query_products_with_filter, variables)
+ response = staff_api_client.post_graphql(QUERY_PRODUCTS_WITH_FILTER, variables)
# then
content = get_graphql_content(response)
@@ -583,7 +594,6 @@ def test_products_query_with_filter_date_range_date_variant_attributes(
def test_products_query_with_filter_date_range_date_time_attributes(
- query_products_with_filter,
staff_api_client,
product_list,
permission_manage_products,
@@ -637,7 +647,7 @@ def test_products_query_with_filter_date_range_date_time_attributes(
staff_api_client.user.user_permissions.add(permission_manage_products)
# when
- response = staff_api_client.post_graphql(query_products_with_filter, variables)
+ response = staff_api_client.post_graphql(QUERY_PRODUCTS_WITH_FILTER, variables)
# then
content = get_graphql_content(response)
@@ -650,7 +660,6 @@ def test_products_query_with_filter_date_range_date_time_attributes(
def test_products_query_with_filter_date_range_date_time_variant_attributes(
- query_products_with_filter,
staff_api_client,
product_list,
permission_manage_products,
@@ -704,7 +713,7 @@ def test_products_query_with_filter_date_range_date_time_variant_attributes(
staff_api_client.user.user_permissions.add(permission_manage_products)
# when
- response = staff_api_client.post_graphql(query_products_with_filter, variables)
+ response = staff_api_client.post_graphql(QUERY_PRODUCTS_WITH_FILTER, variables)
# then
content = get_graphql_content(response)
@@ -717,7 +726,6 @@ def test_products_query_with_filter_date_range_date_time_variant_attributes(
def test_products_query_with_filter_date_time_range_date_time_attributes(
- query_products_with_filter,
staff_api_client,
product_list,
permission_manage_products,
@@ -778,7 +786,7 @@ def test_products_query_with_filter_date_time_range_date_time_attributes(
staff_api_client.user.user_permissions.add(permission_manage_products)
# when
- response = staff_api_client.post_graphql(query_products_with_filter, variables)
+ response = staff_api_client.post_graphql(QUERY_PRODUCTS_WITH_FILTER, variables)
# then
content = get_graphql_content(response)
@@ -791,21 +799,22 @@ def test_products_query_with_filter_date_time_range_date_time_attributes(
def test_products_query_filter_by_non_existing_attribute(
- query_products_with_filter, api_client, product_list, channel_USD
+ api_client, product_list, channel_USD
):
variables = {
"channel": channel_USD.slug,
"filter": {"attributes": [{"slug": "i-do-not-exist", "values": ["red"]}]},
}
- response = api_client.post_graphql(query_products_with_filter, variables)
+ response = api_client.post_graphql(QUERY_PRODUCTS_WITH_FILTER, variables)
content = get_graphql_content(response)
products = content["data"]["products"]["edges"]
assert len(products) == 0
def test_products_query_with_filter_category(
- query_products_with_filter, staff_api_client, product, permission_manage_products
+ staff_api_client, product, permission_manage_products
):
+ # given
category = Category.objects.create(name="Custom", slug="custom")
second_product = product
second_product.id = None
@@ -815,8 +824,12 @@ def test_products_query_with_filter_category(
category_id = graphene.Node.to_global_id("Category", category.id)
variables = {"filter": {"categories": [category_id]}}
+
+ # when
staff_api_client.user.user_permissions.add(permission_manage_products)
- response = staff_api_client.post_graphql(query_products_with_filter, variables)
+ response = staff_api_client.post_graphql(QUERY_PRODUCTS_WITH_FILTER, variables)
+
+ # then
content = get_graphql_content(response)
second_product_id = graphene.Node.to_global_id("Product", second_product.id)
products = content["data"]["products"]["edges"]
@@ -827,8 +840,9 @@ def test_products_query_with_filter_category(
def test_products_query_with_filter_has_category_false(
- query_products_with_filter, staff_api_client, product, permission_manage_products
+ staff_api_client, product, permission_manage_products
):
+ # given
second_product = product
second_product.category = None
second_product.id = None
@@ -837,8 +851,12 @@ def test_products_query_with_filter_has_category_false(
variables = {"filter": {"hasCategory": False}}
staff_api_client.user.user_permissions.add(permission_manage_products)
- response = staff_api_client.post_graphql(query_products_with_filter, variables)
+
+ # when
+ response = staff_api_client.post_graphql(QUERY_PRODUCTS_WITH_FILTER, variables)
content = get_graphql_content(response)
+
+ # then
second_product_id = graphene.Node.to_global_id("Product", second_product.id)
products = content["data"]["products"]["edges"]
@@ -848,11 +866,11 @@ def test_products_query_with_filter_has_category_false(
def test_products_query_with_filter_has_category_true(
- query_products_with_filter,
staff_api_client,
product_without_category,
permission_manage_products,
):
+ # given
category = Category.objects.create(name="Custom", slug="custom")
second_product = product_without_category
second_product.category = category
@@ -862,8 +880,12 @@ def test_products_query_with_filter_has_category_true(
variables = {"filter": {"hasCategory": True}}
staff_api_client.user.user_permissions.add(permission_manage_products)
- response = staff_api_client.post_graphql(query_products_with_filter, variables)
+
+ # when
+ response = staff_api_client.post_graphql(QUERY_PRODUCTS_WITH_FILTER, variables)
content = get_graphql_content(response)
+
+ # then
second_product_id = graphene.Node.to_global_id("Product", second_product.id)
products = content["data"]["products"]["edges"]
@@ -873,12 +895,12 @@ def test_products_query_with_filter_has_category_true(
def test_products_query_with_filter_collection(
- query_products_with_filter,
staff_api_client,
product,
collection,
permission_manage_products,
):
+ # given
second_product = product
second_product.id = None
second_product.slug = "second-product"
@@ -888,8 +910,12 @@ def test_products_query_with_filter_collection(
collection_id = graphene.Node.to_global_id("Collection", collection.id)
variables = {"filter": {"collections": [collection_id]}}
staff_api_client.user.user_permissions.add(permission_manage_products)
- response = staff_api_client.post_graphql(query_products_with_filter, variables)
+
+ # when
+ response = staff_api_client.post_graphql(QUERY_PRODUCTS_WITH_FILTER, variables)
content = get_graphql_content(response)
+
+ # then
second_product_id = graphene.Node.to_global_id("Product", second_product.id)
products = content["data"]["products"]["edges"]
@@ -899,11 +925,11 @@ def test_products_query_with_filter_collection(
def test_products_query_with_filter_category_and_search(
- query_products_with_filter,
staff_api_client,
product,
permission_manage_products,
):
+ # given
category = Category.objects.create(name="Custom", slug="custom")
second_product = product
second_product.id = None
@@ -922,8 +948,12 @@ def test_products_query_with_filter_category_and_search(
category_id = graphene.Node.to_global_id("Category", category.id)
variables = {"filter": {"categories": [category_id], "search": product.name}}
staff_api_client.user.user_permissions.add(permission_manage_products)
- response = staff_api_client.post_graphql(query_products_with_filter, variables)
+
+ # when
+ response = staff_api_client.post_graphql(QUERY_PRODUCTS_WITH_FILTER, variables)
content = get_graphql_content(response)
+
+ # then
product_id = graphene.Node.to_global_id("Product", product.id)
products = content["data"]["products"]["edges"]
@@ -933,7 +963,6 @@ def test_products_query_with_filter_category_and_search(
def test_products_query_with_filter_gift_card_false(
- query_products_with_filter,
staff_api_client,
product,
shippable_gift_card_product,
@@ -944,7 +973,7 @@ def test_products_query_with_filter_gift_card_false(
staff_api_client.user.user_permissions.add(permission_manage_products)
# when
- response = staff_api_client.post_graphql(query_products_with_filter, variables)
+ response = staff_api_client.post_graphql(QUERY_PRODUCTS_WITH_FILTER, variables)
# then
content = get_graphql_content(response)
@@ -957,7 +986,6 @@ def test_products_query_with_filter_gift_card_false(
def test_products_query_with_filter_gift_card_true(
- query_products_with_filter,
staff_api_client,
product,
shippable_gift_card_product,
@@ -968,7 +996,7 @@ def test_products_query_with_filter_gift_card_true(
staff_api_client.user.user_permissions.add(permission_manage_products)
# when
- response = staff_api_client.post_graphql(query_products_with_filter, variables)
+ response = staff_api_client.post_graphql(QUERY_PRODUCTS_WITH_FILTER, variables)
# then
content = get_graphql_content(response)
@@ -990,12 +1018,12 @@ def test_products_query_with_filter_gift_card_true(
)
def test_products_query_with_filter(
products_filter,
- query_products_with_filter,
staff_api_client,
product,
permission_manage_products,
channel_USD,
):
+ # given
assert "Juice1" not in product.name
second_product = product
@@ -1026,8 +1054,12 @@ def test_products_query_with_filter(
second_product.save(update_fields=["search_vector"])
variables = {"filter": products_filter, "channel": channel_USD.slug}
staff_api_client.user.user_permissions.add(permission_manage_products)
- response = staff_api_client.post_graphql(query_products_with_filter, variables)
+
+ # when
+ response = staff_api_client.post_graphql(QUERY_PRODUCTS_WITH_FILTER, variables)
content = get_graphql_content(response)
+
+ # then
second_product_id = graphene.Node.to_global_id("Product", second_product.id)
products = content["data"]["products"]["edges"]
@@ -1037,12 +1069,12 @@ def test_products_query_with_filter(
def test_products_query_with_price_filter_as_staff(
- query_products_with_filter,
staff_api_client,
product_list,
permission_manage_products,
channel_USD,
):
+ # given
product = product_list[0]
product.variants.first().channel_listings.filter().update(price_amount=None)
@@ -1051,20 +1083,24 @@ def test_products_query_with_price_filter_as_staff(
"channel": channel_USD.slug,
}
staff_api_client.user.user_permissions.add(permission_manage_products)
- response = staff_api_client.post_graphql(query_products_with_filter, variables)
+
+ # when
+ response = staff_api_client.post_graphql(QUERY_PRODUCTS_WITH_FILTER, variables)
content = get_graphql_content(response)
+
+ # then
products = content["data"]["products"]["edges"]
assert len(products) == 3
def test_products_query_with_price_filter_as_user(
- query_products_with_filter,
user_api_client,
product_list,
permission_manage_products,
channel_USD,
):
+ # given
product = product_list[0]
product.variants.first().channel_listings.filter().update(price_amount=None)
second_product_id = graphene.Node.to_global_id("Product", product_list[1].id)
@@ -1073,8 +1109,12 @@ def test_products_query_with_price_filter_as_user(
"filter": {"price": {"gte": 9, "lte": 31}},
"channel": channel_USD.slug,
}
- response = user_api_client.post_graphql(query_products_with_filter, variables)
+
+ # when
+ response = user_api_client.post_graphql(QUERY_PRODUCTS_WITH_FILTER, variables)
content = get_graphql_content(response)
+
+ # then
products = content["data"]["products"]["edges"]
assert len(products) == 2
@@ -1085,20 +1125,24 @@ def test_products_query_with_price_filter_as_user(
@pytest.mark.parametrize("is_published", [(True), (False)])
def test_products_query_with_filter_search_by_sku(
is_published,
- query_products_with_filter,
staff_api_client,
product_with_two_variants,
product_with_default_variant,
permission_manage_products,
channel_USD,
):
+ # given
ProductChannelListing.objects.filter(
product=product_with_default_variant, channel=channel_USD
).update(is_published=is_published)
variables = {"filter": {"search": "1234"}}
staff_api_client.user.user_permissions.add(permission_manage_products)
- response = staff_api_client.post_graphql(query_products_with_filter, variables)
+
+ # when
+ response = staff_api_client.post_graphql(QUERY_PRODUCTS_WITH_FILTER, variables)
content = get_graphql_content(response)
+
+ # then
product_id = graphene.Node.to_global_id("Product", product_with_default_variant.id)
products = content["data"]["products"]["edges"]
@@ -1110,7 +1154,6 @@ def test_products_query_with_filter_search_by_sku(
@pytest.mark.parametrize("search_value", ["new", "NEW color", "Color"])
def test_products_query_with_filter_search_by_dropdown_attribute_value(
search_value,
- query_products_with_filter,
staff_api_client,
product_list,
permission_manage_products,
@@ -1141,12 +1184,13 @@ def test_products_query_with_filter_search_by_dropdown_attribute_value(
variables = {"filter": {"search": search_value}, "channel": channel_USD.slug}
- # when
staff_api_client.user.user_permissions.add(permission_manage_products)
- # then
- response = staff_api_client.post_graphql(query_products_with_filter, variables)
+ # when
+ response = staff_api_client.post_graphql(QUERY_PRODUCTS_WITH_FILTER, variables)
content = get_graphql_content(response)
+
+ # then
products = content["data"]["products"]["edges"]
assert len(products) == 1
@@ -1162,7 +1206,6 @@ def test_products_query_with_filter_search_by_dropdown_attribute_value(
)
def test_products_query_with_filter_search_by_multiselect_attribute_value(
search_value,
- query_products_with_filter,
staff_api_client,
product_list,
permission_manage_products,
@@ -1201,13 +1244,13 @@ def test_products_query_with_filter_search_by_multiselect_attribute_value(
product_with_multiselect_attr.save(update_fields=["search_vector"])
variables = {"filter": {"search": search_value}, "channel": channel_USD.slug}
+ staff_api_client.user.user_permissions.add(permission_manage_products)
# when
- staff_api_client.user.user_permissions.add(permission_manage_products)
+ response = staff_api_client.post_graphql(QUERY_PRODUCTS_WITH_FILTER, variables)
+ content = get_graphql_content(response)
# then
- response = staff_api_client.post_graphql(query_products_with_filter, variables)
- content = get_graphql_content(response)
products = content["data"]["products"]["edges"]
assert len(products) == 1
@@ -1220,7 +1263,6 @@ def test_products_query_with_filter_search_by_multiselect_attribute_value(
@pytest.mark.parametrize("search_value", ["rich", "test rich", "RICH text"])
def test_products_query_with_filter_search_by_rich_text_attribute(
search_value,
- query_products_with_filter,
staff_api_client,
product_list,
permission_manage_products,
@@ -1251,12 +1293,13 @@ def test_products_query_with_filter_search_by_rich_text_attribute(
variables = {"filter": {"search": search_value}, "channel": channel_USD.slug}
- # when
staff_api_client.user.user_permissions.add(permission_manage_products)
- # then
- response = staff_api_client.post_graphql(query_products_with_filter, variables)
+ # when
+ response = staff_api_client.post_graphql(QUERY_PRODUCTS_WITH_FILTER, variables)
content = get_graphql_content(response)
+
+ # then
products = content["data"]["products"]["edges"]
assert len(products) == 1
@@ -1269,7 +1312,6 @@ def test_products_query_with_filter_search_by_rich_text_attribute(
@pytest.mark.parametrize("search_value", ["plain", "test plain", "PLAIN text"])
def test_products_query_with_filter_search_by_plain_text_attribute(
search_value,
- query_products_with_filter,
staff_api_client,
product_list,
permission_manage_products,
@@ -1300,12 +1342,13 @@ def test_products_query_with_filter_search_by_plain_text_attribute(
variables = {"filter": {"search": search_value}, "channel": channel_USD.slug}
- # when
staff_api_client.user.user_permissions.add(permission_manage_products)
- # then
- response = staff_api_client.post_graphql(query_products_with_filter, variables)
+ # when
+ response = staff_api_client.post_graphql(QUERY_PRODUCTS_WITH_FILTER, variables)
content = get_graphql_content(response)
+
+ # then
products = content["data"]["products"]["edges"]
assert len(products) == 1
@@ -1318,7 +1361,6 @@ def test_products_query_with_filter_search_by_plain_text_attribute(
@pytest.mark.parametrize("search_value", ["13456", "13456 cm"])
def test_products_query_with_filter_search_by_numeric_attribute_value(
search_value,
- query_products_with_filter,
staff_api_client,
product_list,
permission_manage_products,
@@ -1352,12 +1394,13 @@ def test_products_query_with_filter_search_by_numeric_attribute_value(
variables = {"filter": {"search": search_value}, "channel": channel_USD.slug}
- # when
staff_api_client.user.user_permissions.add(permission_manage_products)
- # then
- response = staff_api_client.post_graphql(query_products_with_filter, variables)
+ # when
+ response = staff_api_client.post_graphql(QUERY_PRODUCTS_WITH_FILTER, variables)
content = get_graphql_content(response)
+
+ # then
products = content["data"]["products"]["edges"]
assert len(products) == 1
@@ -1368,7 +1411,6 @@ def test_products_query_with_filter_search_by_numeric_attribute_value(
def test_products_query_with_filter_search_by_numeric_attribute_value_without_unit(
- query_products_with_filter,
staff_api_client,
product_list,
permission_manage_products,
@@ -1400,12 +1442,13 @@ def test_products_query_with_filter_search_by_numeric_attribute_value_without_un
variables = {"filter": {"search": "13456"}, "channel": channel_USD.slug}
- # when
staff_api_client.user.user_permissions.add(permission_manage_products)
- # then
- response = staff_api_client.post_graphql(query_products_with_filter, variables)
+ # when
+ response = staff_api_client.post_graphql(QUERY_PRODUCTS_WITH_FILTER, variables)
content = get_graphql_content(response)
+
+ # then
products = content["data"]["products"]["edges"]
assert len(products) == 1
@@ -1418,7 +1461,6 @@ def test_products_query_with_filter_search_by_numeric_attribute_value_without_un
@pytest.mark.parametrize("search_value", ["2020", "2020-10-10"])
def test_products_query_with_filter_search_by_date_attribute_value(
search_value,
- query_products_with_filter,
staff_api_client,
product_list,
permission_manage_products,
@@ -1449,12 +1491,13 @@ def test_products_query_with_filter_search_by_date_attribute_value(
variables = {"filter": {"search": search_value}, "channel": channel_USD.slug}
- # when
staff_api_client.user.user_permissions.add(permission_manage_products)
- # then
- response = staff_api_client.post_graphql(query_products_with_filter, variables)
+ # when
+ response = staff_api_client.post_graphql(QUERY_PRODUCTS_WITH_FILTER, variables)
content = get_graphql_content(response)
+
+ # then
products = content["data"]["products"]["edges"]
assert len(products) == 1
@@ -1467,7 +1510,6 @@ def test_products_query_with_filter_search_by_date_attribute_value(
@pytest.mark.parametrize("search_value", ["2020", "2020-10-10", "22:20"])
def test_products_query_with_filter_search_by_date_time_attribute_value(
search_value,
- query_products_with_filter,
staff_api_client,
product_list,
permission_manage_products,
@@ -1500,12 +1542,13 @@ def test_products_query_with_filter_search_by_date_time_attribute_value(
variables = {"filter": {"search": search_value}, "channel": channel_USD.slug}
- # when
staff_api_client.user.user_permissions.add(permission_manage_products)
- # then
- response = staff_api_client.post_graphql(query_products_with_filter, variables)
+ # when
+ response = staff_api_client.post_graphql(QUERY_PRODUCTS_WITH_FILTER, variables)
content = get_graphql_content(response)
+
+ # then
products = content["data"]["products"]["edges"]
assert len(products) == 1
@@ -1516,59 +1559,67 @@ def test_products_query_with_filter_search_by_date_time_attribute_value(
def test_products_query_with_is_published_filter_variants_without_prices(
- query_products_with_filter,
staff_api_client,
variant,
permission_manage_products,
channel_USD,
):
+ # given
ProductVariantChannelListing.objects.filter(
variant__product=variant.product
).update(price_amount=None)
variables = {"channel": channel_USD.slug, "filter": {"isPublished": True}}
+
+ # when
response = staff_api_client.post_graphql(
- query_products_with_filter,
+ QUERY_PRODUCTS_WITH_FILTER,
variables,
permissions=[permission_manage_products],
check_no_permissions=False,
)
content = get_graphql_content(response)
+
+ # then
products = content["data"]["products"]["edges"]
assert len(products) == 0
def test_products_query_with_is_published_filter_one_variant_without_price(
- query_products_with_filter,
staff_api_client,
variant,
permission_manage_products,
channel_USD,
):
+ # given
variant.channel_listings.update(price_amount=None)
variables = {"channel": channel_USD.slug, "filter": {"isPublished": True}}
+
+ # when
response = staff_api_client.post_graphql(
- query_products_with_filter,
+ QUERY_PRODUCTS_WITH_FILTER,
variables,
permissions=[permission_manage_products],
check_no_permissions=False,
)
content = get_graphql_content(response)
+
+ # then
products = content["data"]["products"]["edges"]
assert len(products) == 1
def test_products_query_with_filter_stock_availability_as_staff(
- query_products_with_filter,
staff_api_client,
product_list,
order_line,
permission_manage_products,
channel_USD,
):
+ # given
for product in product_list:
stock = product.variants.first().stocks.first()
Allocation.objects.create(
@@ -1583,15 +1634,18 @@ def test_products_query_with_filter_stock_availability_as_staff(
"channel": channel_USD.slug,
}
staff_api_client.user.user_permissions.add(permission_manage_products)
- response = staff_api_client.post_graphql(query_products_with_filter, variables)
+
+ # when
+ response = staff_api_client.post_graphql(QUERY_PRODUCTS_WITH_FILTER, variables)
content = get_graphql_content(response)
+
+ # then
products = content["data"]["products"]["edges"]
assert len(products) == 3
def test_products_query_with_filter_stock_availability_including_reservations(
- query_products_with_filter,
staff_api_client,
product_list,
order_line,
@@ -1640,7 +1694,7 @@ def test_products_query_with_filter_stock_availability_including_reservations(
staff_api_client.user.user_permissions.add(permission_manage_products)
# when
- response = staff_api_client.post_graphql(query_products_with_filter, variables)
+ response = staff_api_client.post_graphql(QUERY_PRODUCTS_WITH_FILTER, variables)
# then
content = get_graphql_content(response)
@@ -1652,13 +1706,13 @@ def test_products_query_with_filter_stock_availability_including_reservations(
def test_products_query_with_filter_stock_availability_as_user(
- query_products_with_filter,
user_api_client,
product_list,
order_line,
permission_manage_products,
channel_USD,
):
+ # given
for product in product_list:
stock = product.variants.first().stocks.first()
Allocation.objects.create(
@@ -1672,8 +1726,12 @@ def test_products_query_with_filter_stock_availability_as_user(
"filter": {"stockAvailability": "OUT_OF_STOCK"},
"channel": channel_USD.slug,
}
- response = user_api_client.post_graphql(query_products_with_filter, variables)
+
+ # when
+ response = user_api_client.post_graphql(QUERY_PRODUCTS_WITH_FILTER, variables)
content = get_graphql_content(response)
+
+ # then
product_id = graphene.Node.to_global_id("Product", product_list[1].id)
second_product_id = graphene.Node.to_global_id("Product", product_list[2].id)
@@ -1687,20 +1745,24 @@ def test_products_query_with_filter_stock_availability_as_user(
def test_products_query_with_filter_stock_availability_channel_without_shipping_zones(
- query_products_with_filter,
staff_api_client,
product,
permission_manage_products,
channel_USD,
):
+ # given
channel_USD.shipping_zones.clear()
variables = {
"filter": {"stockAvailability": "OUT_OF_STOCK"},
"channel": channel_USD.slug,
}
staff_api_client.user.user_permissions.add(permission_manage_products)
- response = staff_api_client.post_graphql(query_products_with_filter, variables)
+
+ # when
+ response = staff_api_client.post_graphql(QUERY_PRODUCTS_WITH_FILTER, variables)
content = get_graphql_content(response)
+
+ # then
products = content["data"]["products"]["edges"]
product_id = graphene.Node.to_global_id("Product", product.id)
@@ -1709,7 +1771,6 @@ def test_products_query_with_filter_stock_availability_channel_without_shipping_
def test_products_query_with_filter_stock_availability_only_stock_in_cc_warehouse(
- query_products_with_filter,
user_api_client,
product,
order_line,
@@ -1730,7 +1791,7 @@ def test_products_query_with_filter_stock_availability_only_stock_in_cc_warehous
}
# when
- response = user_api_client.post_graphql(query_products_with_filter, variables)
+ response = user_api_client.post_graphql(QUERY_PRODUCTS_WITH_FILTER, variables)
# then
content = get_graphql_content(response)
@@ -1762,13 +1823,13 @@ def test_products_query_with_filter_stocks(
warehouse_indexes,
count,
indexes_of_products_in_result,
- query_products_with_filter,
staff_api_client,
product_with_single_variant,
product_with_two_variants,
warehouse,
channel_USD,
):
+ # given
product1 = product_with_single_variant
product2 = product_with_two_variants
products = [product1, product2]
@@ -1820,10 +1881,14 @@ def test_products_query_with_filter_stocks(
},
"channel": channel_USD.slug,
}
+
+ # when
response = staff_api_client.post_graphql(
- query_products_with_filter, variables, check_no_permissions=False
+ QUERY_PRODUCTS_WITH_FILTER, variables, check_no_permissions=False
)
content = get_graphql_content(response)
+
+ # then
products_data = content["data"]["products"]["edges"]
product_ids = {
@@ -1835,9 +1900,7 @@ def test_products_query_with_filter_stocks(
assert {node["node"]["id"] for node in products_data} == product_ids
-def test_query_products_with_filter_ids(
- api_client, product_list, query_products_with_filter, channel_USD
-):
+def test_query_products_with_filter_ids(api_client, product_list, channel_USD):
# given
product_ids = [
graphene.Node.to_global_id("Product", product.id) for product in product_list
@@ -1848,7 +1911,7 @@ def test_query_products_with_filter_ids(
}
# when
- response = api_client.post_graphql(query_products_with_filter, variables)
+ response = api_client.post_graphql(QUERY_PRODUCTS_WITH_FILTER, variables)
# then
content = get_graphql_content(response)
@@ -1859,17 +1922,21 @@ def test_query_products_with_filter_ids(
def test_products_query_with_filter_has_preordered_variants_false(
- query_products_with_filter,
staff_api_client,
preorder_variant_global_threshold,
product_without_shipping,
permission_manage_products,
):
+ # given
product = product_without_shipping
variables = {"filter": {"hasPreorderedVariants": False}}
staff_api_client.user.user_permissions.add(permission_manage_products)
- response = staff_api_client.post_graphql(query_products_with_filter, variables)
+
+ # when
+ response = staff_api_client.post_graphql(QUERY_PRODUCTS_WITH_FILTER, variables)
content = get_graphql_content(response)
+
+ # then
product_id = graphene.Node.to_global_id("Product", product.id)
products = content["data"]["products"]["edges"]
@@ -1879,17 +1946,21 @@ def test_products_query_with_filter_has_preordered_variants_false(
def test_products_query_with_filter_has_preordered_variants_true(
- query_products_with_filter,
staff_api_client,
preorder_variant_global_threshold,
product_without_shipping,
permission_manage_products,
):
+ # given
product = preorder_variant_global_threshold.product
variables = {"filter": {"hasPreorderedVariants": True}}
staff_api_client.user.user_permissions.add(permission_manage_products)
- response = staff_api_client.post_graphql(query_products_with_filter, variables)
+
+ # when
+ response = staff_api_client.post_graphql(QUERY_PRODUCTS_WITH_FILTER, variables)
content = get_graphql_content(response)
+
+ # then
product_id = graphene.Node.to_global_id("Product", product.id)
products = content["data"]["products"]["edges"]
@@ -1899,11 +1970,11 @@ def test_products_query_with_filter_has_preordered_variants_true(
def test_products_query_with_filter_has_preordered_variants_before_end_date(
- query_products_with_filter,
staff_api_client,
preorder_variant_global_threshold,
permission_manage_products,
):
+ # given
variant = preorder_variant_global_threshold
variant.preorder_end_date = timezone.now() + datetime.timedelta(days=3)
variant.save(update_fields=["preorder_end_date"])
@@ -1911,8 +1982,12 @@ def test_products_query_with_filter_has_preordered_variants_before_end_date(
product = preorder_variant_global_threshold.product
variables = {"filter": {"hasPreorderedVariants": True}}
staff_api_client.user.user_permissions.add(permission_manage_products)
- response = staff_api_client.post_graphql(query_products_with_filter, variables)
+
+ # when
+ response = staff_api_client.post_graphql(QUERY_PRODUCTS_WITH_FILTER, variables)
content = get_graphql_content(response)
+
+ # then
product_id = graphene.Node.to_global_id("Product", product.id)
products = content["data"]["products"]["edges"]
@@ -1922,19 +1997,23 @@ def test_products_query_with_filter_has_preordered_variants_before_end_date(
def test_products_query_with_filter_has_preordered_variants_after_end_date(
- query_products_with_filter,
staff_api_client,
preorder_variant_global_threshold,
permission_manage_products,
):
+ # given
variant = preorder_variant_global_threshold
variant.preorder_end_date = timezone.now() - datetime.timedelta(days=3)
variant.save(update_fields=["preorder_end_date"])
variables = {"filter": {"hasPreorderedVariants": True}}
staff_api_client.user.user_permissions.add(permission_manage_products)
- response = staff_api_client.post_graphql(query_products_with_filter, variables)
+
+ # when
+ response = staff_api_client.post_graphql(QUERY_PRODUCTS_WITH_FILTER, variables)
content = get_graphql_content(response)
+
+ # then
products = content["data"]["products"]["edges"]
assert len(products) == 0
diff --git a/saleor/product/tests/fixtures/__init__.py b/saleor/product/tests/fixtures/__init__.py
new file mode 100644
index 0000000000..290506e78a
--- /dev/null
+++ b/saleor/product/tests/fixtures/__init__.py
@@ -0,0 +1,7 @@
+from .category import * # noqa: F403
+from .collection import * # noqa: F403
+from .digital_content import * # noqa: F403
+from .product import * # noqa: F403
+from .product_media import * # noqa: F403
+from .product_type import * # noqa: F403
+from .variant import * # noqa: F403
diff --git a/saleor/graphql/product/tests/benchmark/conftest.py b/saleor/product/tests/fixtures/category.py
similarity index 70%
rename from saleor/graphql/product/tests/benchmark/conftest.py
rename to saleor/product/tests/fixtures/category.py
index 7b31e1a786..307392fe3e 100644
--- a/saleor/graphql/product/tests/benchmark/conftest.py
+++ b/saleor/product/tests/fixtures/category.py
@@ -1,40 +1,119 @@
+import datetime
+
import pytest
-from .....discount import RewardValueType
-from .....discount.models import Promotion, PromotionRule
-from .....product.models import Category
+from ....attribute.utils import associate_attribute_values_to_instance
+from ...models import Category, Product, ProductChannelListing
+
+
+def category_generator():
+ def create_category(
+ name="Default",
+ slug="default",
+ ):
+ category = Category.objects.create(
+ name=name,
+ slug=slug,
+ )
+ return category
+
+ return create_category
+
+
+def category(category_generator): # pylint: disable=W0613
+ return category_generator()
@pytest.fixture
-def promotion_converted_from_sale_list(channel_USD):
- promotions = Promotion.objects.bulk_create(
- [Promotion(name="Sale1"), Promotion(name="Sale2")]
+def category_with_image(db, image, media_root): # pylint: disable=W0613
+ return Category.objects.create(
+ name="Default2", slug="default2", background_image=image
)
- for promotion in promotions:
- promotion.assign_old_sale_id()
-
- values = [15, 5]
-
- rules = PromotionRule.objects.bulk_create(
- [
- PromotionRule(
- promotion=promotion,
- catalogue_predicate={},
- reward_value_type=RewardValueType.FIXED,
- reward_value=value,
- )
- for promotion, value in zip(promotions, values)
- ]
+
+
+def categories(db):
+ category1 = Category.objects.create(name="Category1", slug="cat1")
+ category2 = Category.objects.create(name="Category2", slug="cat2")
+ return [category1, category2]
+
+
+def category_list():
+ category_1 = Category.objects.create(name="Category 1", slug="category-1")
+ category_2 = Category.objects.create(name="Category 2", slug="category-2")
+ category_3 = Category.objects.create(name="Category 3", slug="category-3")
+ return category_1, category_2, category_3
+
+
+def categories_tree(db, product_type, channel_USD): # pylint: disable=W0613
+ parent = Category.objects.create(name="Parent", slug="parent")
+ parent.children.create(name="Child", slug="child")
+ child = parent.children.first()
+
+ product_attr = product_type.product_attributes.first()
+ attr_value = product_attr.values.first()
+
+ product = Product.objects.create(
+ name="Test product",
+ slug="test-product-10",
+ product_type=product_type,
+ category=child,
)
- PromotionRuleChannel = PromotionRule.channels.through
+ ProductChannelListing.objects.create(
+ product=product,
+ channel=channel_USD,
+ is_published=True,
+ visible_in_listings=True,
+ )
+
+ associate_attribute_values_to_instance(product, {product_attr.pk: [attr_value]})
+ return parent
+
+
+def categories_tree_with_published_products(
+ categories_tree, product, channel_USD, channel_PLN
+):
+ parent = categories_tree
+ parent_product = product
+ parent_product.category = parent
- rules_channels = [
- PromotionRuleChannel(promotionrule_id=rule.id, channel_id=channel_USD.id)
- for rule in rules
- ]
- PromotionRuleChannel.objects.bulk_create(rules_channels)
+ child = parent.children.first()
+ child_product = child.products.first()
- return promotions
+ product_list = [child_product, parent_product]
+
+ ProductChannelListing.objects.filter(product__in=product_list).delete()
+ product_channel_listings = []
+ for product in product_list:
+ product.save()
+ product_channel_listings.append(
+ ProductChannelListing(
+ product=product,
+ channel=channel_USD,
+ published_at=datetime.datetime.now(tz=datetime.UTC),
+ is_published=True,
+ )
+ )
+ product_channel_listings.append(
+ ProductChannelListing(
+ product=product,
+ channel=channel_PLN,
+ published_at=datetime.datetime.now(tz=datetime.UTC),
+ is_published=True,
+ )
+ )
+ ProductChannelListing.objects.bulk_create(product_channel_listings)
+ return parent
+
+
+def non_default_category(db): # pylint: disable=W0613
+ return Category.objects.create(name="Not default", slug="not-default")
@pytest.fixture
@@ -46,7 +125,6 @@ def category_with_products(
product_with_variant_with_two_attributes,
product_with_multiple_values_attributes,
product_without_shipping,
- promotion_converted_from_sale_list,
):
category = categories_tree
child = Category.objects.create(name="TestCategory", slug="test-cat")
diff --git a/saleor/product/tests/fixtures/collection.py b/saleor/product/tests/fixtures/collection.py
new file mode 100644
index 0000000000..167ad69c1e
--- /dev/null
+++ b/saleor/product/tests/fixtures/collection.py
@@ -0,0 +1,148 @@
+import datetime
+
+import pytest
+from django.utils import timezone
+
+from ....tests.utils import dummy_editorjs
+from ...models import Collection, CollectionChannelListing
+
+
+def collection(db):
+ collection = Collection.objects.create(
+ name="Collection",
+ slug="collection",
+ description=dummy_editorjs("Test description."),
+ )
+ return collection
+
+
+def published_collection(db, channel_USD):
+ collection = Collection.objects.create(
+ name="Collection USD",
+ slug="collection-usd",
+ description=dummy_editorjs("Test description."),
+ )
+ CollectionChannelListing.objects.create(
+ channel=channel_USD,
+ collection=collection,
+ is_published=True,
+ published_at=timezone.now(),
+ )
+ return collection
+
+
+def published_collections(db, channel_USD):
+ collections = Collection.objects.bulk_create(
+ [
+ Collection(
+ name="Collection1",
+ slug="coll1",
+ ),
+ Collection(
+ name="Collection2",
+ slug="coll2",
+ ),
+ Collection(
+ name="Collection3",
+ slug="coll3",
+ ),
+ ]
+ )
+ CollectionChannelListing.objects.bulk_create(
+ [
+ CollectionChannelListing(
+ channel=channel_USD,
+ collection=collection,
+ is_published=True,
+ published_at=datetime.datetime(2019, 4, 10, tzinfo=datetime.UTC),
+ )
+ for collection in collections
+ ]
+ )
+
+ return collections
+
+
+def published_collection_PLN(db, channel_PLN):
+ collection = Collection.objects.create(
+ name="Collection PLN",
+ slug="collection-pln",
+ description=dummy_editorjs("Test description."),
+ )
+ CollectionChannelListing.objects.create(
+ channel=channel_PLN,
+ collection=collection,
+ is_published=True,
+ published_at=timezone.now(),
+ )
+ return collection
+
+
+def unpublished_collection(db, channel_USD):
+ collection = Collection.objects.create(
+ name="Unpublished Collection",
+ slug="unpublished-collection",
+ description=dummy_editorjs("Test description."),
+ )
+ CollectionChannelListing.objects.create(
+ channel=channel_USD, collection=collection, is_published=False
+ )
+ return collection
+
+
+def unpublished_collection_PLN(db, channel_PLN):
+ collection = Collection.objects.create(
+ name="Collection",
+ slug="collection",
+ description=dummy_editorjs("Test description."),
+ )
+ CollectionChannelListing.objects.create(
+ channel=channel_PLN, collection=collection, is_published=False
+ )
+ return collection
+
+
+def collection_with_products(db, published_collection, product_list_published):
+ published_collection.products.set(list(product_list_published))
+ return product_list_published
+
+
+def collection_with_image(db, image, media_root, channel_USD):
+ collection = Collection.objects.create(
+ name="Collection",
+ slug="collection",
+ description=dummy_editorjs("Test description."),
+ background_image=image,
+ )
+ CollectionChannelListing.objects.create(
+ channel=channel_USD, collection=collection, is_published=False
+ )
+ return collection
+
+
+def collection_list(db, channel_USD):
+ collections = Collection.objects.bulk_create(
+ [
+ Collection(name="Collection 1", slug="collection-1"),
+ Collection(name="Collection 2", slug="collection-2"),
+ Collection(name="Collection 3", slug="collection-3"),
+ ]
+ )
+ CollectionChannelListing.objects.bulk_create(
+ [
+ CollectionChannelListing(
+ channel=channel_USD, collection=collection, is_published=True
+ )
+ for collection in collections
+ ]
+ )
+ return collections
diff --git a/saleor/product/tests/fixtures/digital_content.py b/saleor/product/tests/fixtures/digital_content.py
new file mode 100644
index 0000000000..e3745853c6
--- /dev/null
+++ b/saleor/product/tests/fixtures/digital_content.py
@@ -0,0 +1,71 @@
+import datetime
+from decimal import Decimal
+
+import pytest
+
+from ....warehouse.models import Stock
+from ... import ProductTypeKind
+from ...models import (
+ DigitalContent,
+ DigitalContentUrl,
+ Product,
+ ProductChannelListing,
+ ProductType,
+ ProductVariant,
+ ProductVariantChannelListing,
+)
+from ..utils import create_image
+
+
+def digital_content(category, media_root, warehouse, channel_USD) -> DigitalContent:
+ product_type = ProductType.objects.create(
+ name="Digital Type",
+ slug="digital-type",
+ kind=ProductTypeKind.NORMAL,
+ has_variants=True,
+ is_shipping_required=False,
+ is_digital=True,
+ )
+ product = Product.objects.create(
+ name="Test digital product",
+ slug="test-digital-product",
+ product_type=product_type,
+ category=category,
+ )
+ ProductChannelListing.objects.create(
+ product=product,
+ channel=channel_USD,
+ is_published=True,
+ visible_in_listings=True,
+ available_for_purchase_at=datetime.datetime(1999, 1, 1, tzinfo=datetime.UTC),
+ )
+ product_variant = ProductVariant.objects.create(product=product, sku="SKU_554")
+ ProductVariantChannelListing.objects.create(
+ variant=product_variant,
+ channel=channel_USD,
+ price_amount=Decimal(10),
+ discounted_price_amount=Decimal(10),
+ cost_price_amount=Decimal(1),
+ currency=channel_USD.currency_code,
+ )
+ Stock.objects.create(
+ product_variant=product_variant,
+ warehouse=warehouse,
+ quantity=5,
+ )
+
+ assert product_variant.is_digital()
+
+ image_file, image_name = create_image()
+ d_content = DigitalContent.objects.create(
+ content_file=image_file,
+ product_variant=product_variant,
+ use_default_settings=True,
+ )
+ return d_content
+
+
+def digital_content_url(digital_content, order_line):
+ return DigitalContentUrl.objects.create(content=digital_content, line=order_line)
diff --git a/saleor/product/tests/fixtures/product.py b/saleor/product/tests/fixtures/product.py
new file mode 100644
index 0000000000..7256e14f03
--- /dev/null
+++ b/saleor/product/tests/fixtures/product.py
@@ -0,0 +1,1135 @@
+import datetime
+import itertools
+import random
+import uuid
+from decimal import Decimal
+from unittest.mock import MagicMock
+
+import pytest
+from django.core.files import File
+from django.db import connection
+
+from ....attribute import AttributeInputType, AttributeType
+from ....attribute.models import Attribute, AttributeValue
+from ....attribute.utils import associate_attribute_values_to_instance
+from ....core.postgres import FlatConcatSearchVector
+from ....warehouse.models import Stock
+from ... import ProductMediaTypes, ProductTypeKind
+from ...models import (
+ Product,
+ ProductChannelListing,
+ ProductMedia,
+ ProductType,
+ ProductVariant,
+ ProductVariantChannelListing,
+)
+from ...search import prepare_product_search_vector_value
+
+
+def product_with_product_attributes(
+ product_type_with_product_attributes, non_default_category
+):
+ return Product.objects.create(
+ name="product_with_product_attributes",
+ slug="product-with-product-attributes",
+ product_type=product_type_with_product_attributes,
+ category=non_default_category,
+ )
+
+
+def product_with_variant_attributes(
+ product_type_with_variant_attributes, non_default_category
+):
+ return Product.objects.create(
+ name="product_with_variant_attributes",
+ slug="product-with-variant-attributes",
+ product_type=product_type_with_variant_attributes,
+ category=non_default_category,
+ )
+
+
+def product(product_type, category, warehouse, channel_USD, default_tax_class):
+ product_attr = product_type.product_attributes.first()
+ product_attr_value = product_attr.values.first()
+
+ product = Product.objects.create(
+ name="Test product",
+ slug="test-product-11",
+ product_type=product_type,
+ category=category,
+ tax_class=default_tax_class,
+ )
+ ProductChannelListing.objects.create(
+ product=product,
+ channel=channel_USD,
+ is_published=True,
+ discounted_price_amount="10.00",
+ currency=channel_USD.currency_code,
+ visible_in_listings=True,
+ available_for_purchase_at=datetime.datetime(1999, 1, 1, tzinfo=datetime.UTC),
+ )
+
+ associate_attribute_values_to_instance(
+ product, {product_attr.pk: [product_attr_value]}
+ )
+
+ variant_attr = product_type.variant_attributes.first()
+ variant_attr_value = variant_attr.values.first()
+
+ variant = ProductVariant.objects.create(product=product, sku="123")
+ ProductVariantChannelListing.objects.create(
+ variant=variant,
+ channel=channel_USD,
+ price_amount=Decimal(10),
+ discounted_price_amount=Decimal(10),
+ cost_price_amount=Decimal(1),
+ currency=channel_USD.currency_code,
+ )
+ Stock.objects.create(warehouse=warehouse, product_variant=variant, quantity=10)
+
+ associate_attribute_values_to_instance(
+ variant, {variant_attr.pk: [variant_attr_value]}
+ )
+
+ return product
+
+
+def product_with_translations(product):
+ product.translations.create(language_code="pl", name="OldProduct PL")
+ product.translations.create(language_code="de", name="OldProduct DE")
+
+ return product
+
+
+def shippable_gift_card_product(
+ shippable_gift_card_product_type, category, warehouse, channel_USD
+):
+ product_type = shippable_gift_card_product_type
+
+ product = Product.objects.create(
+ name="Shippable gift card",
+ slug="shippable-gift-card",
+ product_type=product_type,
+ category=category,
+ )
+ ProductChannelListing.objects.create(
+ product=product,
+ channel=channel_USD,
+ is_published=True,
+ discounted_price_amount="100.00",
+ currency=channel_USD.currency_code,
+ visible_in_listings=True,
+ available_for_purchase_at=datetime.datetime(1999, 1, 1, tzinfo=datetime.UTC),
+ )
+
+ variant = ProductVariant.objects.create(
+ product=product, sku="958", track_inventory=False
+ )
+ ProductVariantChannelListing.objects.create(
+ variant=variant,
+ channel=channel_USD,
+ price_amount=Decimal(100),
+ discounted_price_amount=Decimal(100),
+ cost_price_amount=Decimal(1),
+ currency=channel_USD.currency_code,
+ )
+ Stock.objects.create(warehouse=warehouse, product_variant=variant, quantity=1)
+
+ return product
+
+
+def product_price_0(category, warehouse, channel_USD):
+ product_type = ProductType.objects.create(
+ name="Type with no shipping",
+ slug="no-shipping",
+ has_variants=False,
+ is_shipping_required=False,
+ )
+ product = Product.objects.create(
+ name="Test product",
+ slug="test-product-4",
+ product_type=product_type,
+ category=category,
+ )
+ ProductChannelListing.objects.create(
+ product=product,
+ channel=channel_USD,
+ is_published=True,
+ visible_in_listings=True,
+ available_for_purchase_at=datetime.datetime(1999, 1, 1, tzinfo=datetime.UTC),
+ )
+ variant = ProductVariant.objects.create(product=product, sku="SKU_C")
+ ProductVariantChannelListing.objects.create(
+ variant=variant,
+ channel=channel_USD,
+ price_amount=Decimal(0),
+ discounted_price_amount=Decimal(0),
+ cost_price_amount=Decimal(0),
+ currency=channel_USD.currency_code,
+ )
+ Stock.objects.create(product_variant=variant, warehouse=warehouse, quantity=1)
+ return product
+
+
+def product_in_channel_JPY(product, channel_JPY, warehouse_JPY):
+ ProductChannelListing.objects.create(
+ product=product,
+ channel=channel_JPY,
+ is_published=True,
+ discounted_price_amount="1200",
+ currency=channel_JPY.currency_code,
+ visible_in_listings=True,
+ available_for_purchase_at=datetime.datetime(1999, 1, 1, tzinfo=datetime.UTC),
+ )
+ variant = product.variants.get()
+ ProductVariantChannelListing.objects.create(
+ variant=variant,
+ channel=channel_JPY,
+ price_amount=Decimal(1200),
+ discounted_price_amount=Decimal(1200),
+ cost_price_amount=Decimal(300),
+ currency=channel_JPY.currency_code,
+ )
+ Stock.objects.create(warehouse=warehouse_JPY, product_variant=variant, quantity=10)
+ return product
+
+
+def non_shippable_gift_card_product(
+ non_shippable_gift_card_product_type, category, warehouse, channel_USD
+):
+ product_type = non_shippable_gift_card_product_type
+
+ product = Product.objects.create(
+ name="Non shippable gift card",
+ slug="non-shippable-gift-card",
+ product_type=product_type,
+ category=category,
+ )
+ ProductChannelListing.objects.create(
+ product=product,
+ channel=channel_USD,
+ is_published=True,
+ discounted_price_amount="200.00",
+ currency=channel_USD.currency_code,
+ visible_in_listings=True,
+ available_for_purchase_at=datetime.datetime(1999, 1, 1, tzinfo=datetime.UTC),
+ )
+
+ variant = ProductVariant.objects.create(
+ product=product, sku="785", track_inventory=False
+ )
+ ProductVariantChannelListing.objects.create(
+ variant=variant,
+ channel=channel_USD,
+ price_amount=Decimal(250),
+ discounted_price_amount=Decimal(250),
+ cost_price_amount=Decimal(1),
+ currency=channel_USD.currency_code,
+ )
+ Stock.objects.create(warehouse=warehouse, product_variant=variant, quantity=1)
+
+ return product
+
+
+def product_with_rich_text_attribute(
+ product_type_with_rich_text_attribute, category, warehouse, channel_USD
+):
+ product_attr = product_type_with_rich_text_attribute.product_attributes.first()
+ product_attr_value = product_attr.values.first()
+
+ product = Product.objects.create(
+ name="Test product",
+ slug="test-product-11",
+ product_type=product_type_with_rich_text_attribute,
+ category=category,
+ )
+ ProductChannelListing.objects.create(
+ product=product,
+ channel=channel_USD,
+ is_published=True,
+ discounted_price_amount="10.00",
+ currency=channel_USD.currency_code,
+ visible_in_listings=True,
+ available_for_purchase_at=datetime.datetime(1999, 1, 1, tzinfo=datetime.UTC),
+ )
+
+ associate_attribute_values_to_instance(
+ product, {product_attr.pk: [product_attr_value]}
+ )
+
+ variant_attr = product_type_with_rich_text_attribute.variant_attributes.first()
+ variant_attr_value = variant_attr.values.first()
+
+ variant = ProductVariant.objects.create(product=product, sku="123")
+ ProductVariantChannelListing.objects.create(
+ variant=variant,
+ channel=channel_USD,
+ price_amount=Decimal(10),
+ discounted_price_amount=Decimal(10),
+ cost_price_amount=Decimal(1),
+ currency=channel_USD.currency_code,
+ )
+ Stock.objects.create(warehouse=warehouse, product_variant=variant, quantity=10)
+
+ associate_attribute_values_to_instance(
+ variant, {variant_attr.pk: [variant_attr_value]}
+ )
+ return [product, variant]
+
+
+def product_with_collections(
+ product, published_collection, unpublished_collection, collection
+):
+ product.collections.add(*[published_collection, unpublished_collection, collection])
+ return product
+
+
+def product_available_in_many_channels(product, channel_PLN, channel_USD):
+ ProductChannelListing.objects.create(
+ product=product,
+ channel=channel_PLN,
+ is_published=True,
+ )
+ variant = product.variants.get()
+ ProductVariantChannelListing.objects.create(
+ variant=variant,
+ channel=channel_PLN,
+ price_amount=Decimal(50),
+ discounted_price_amount=Decimal(50),
+ cost_price_amount=Decimal(1),
+ currency=channel_PLN.currency_code,
+ )
+ return product
+
+
+def product_with_single_variant(product_type, category, warehouse, channel_USD):
+ product = Product.objects.create(
+ name="Test product with single variant",
+ slug="test-product-with-single-variant",
+ product_type=product_type,
+ category=category,
+ )
+ ProductChannelListing.objects.create(
+ product=product,
+ channel=channel_USD,
+ is_published=True,
+ visible_in_listings=True,
+ available_for_purchase_at=datetime.datetime(1999, 1, 1, tzinfo=datetime.UTC),
+ )
+ variant = ProductVariant.objects.create(product=product, sku="SKU_SINGLE_VARIANT")
+ ProductVariantChannelListing.objects.create(
+ variant=variant,
+ channel=channel_USD,
+ price_amount=Decimal(1.99),
+ discounted_price_amount=Decimal(10),
+ cost_price_amount=Decimal(1),
+ currency=channel_USD.currency_code,
+ )
+ Stock.objects.create(product_variant=variant, warehouse=warehouse, quantity=101)
+ return product
+
+
+def product_with_two_variants(product_type, category, warehouse, channel_USD):
+ product = Product.objects.create(
+ name="Test product with two variants",
+ slug="test-product-with-two-variant",
+ product_type=product_type,
+ category=category,
+ )
+
+ ProductChannelListing.objects.create(
+ product=product,
+ channel=channel_USD,
+ is_published=True,
+ visible_in_listings=True,
+ available_for_purchase_at=datetime.datetime(1999, 1, 1, tzinfo=datetime.UTC),
+ )
+
+ variants = [
+ ProductVariant(
+ product=product,
+ sku=f"Product variant #{i}",
+ )
+ for i in (1, 2)
+ ]
+ ProductVariant.objects.bulk_create(variants)
+ variants_channel_listing = [
+ ProductVariantChannelListing(
+ variant=variant,
+ channel=channel_USD,
+ price_amount=Decimal(10),
+ discounted_price_amount=Decimal(10),
+ cost_price_amount=Decimal(1),
+ currency=channel_USD.currency_code,
+ )
+ for variant in variants
+ ]
+ ProductVariantChannelListing.objects.bulk_create(variants_channel_listing)
+ Stock.objects.bulk_create(
+ [
+ Stock(
+ warehouse=warehouse,
+ product_variant=variant,
+ quantity=10,
+ )
+ for variant in variants
+ ]
+ )
+ product.search_vector = FlatConcatSearchVector(
+ *prepare_product_search_vector_value(product)
+ )
+ product.save(update_fields=["search_vector"])
+
+ return product
+
+
+def product_with_variant_with_two_attributes(
+ color_attribute, size_attribute, category, warehouse, channel_USD
+):
+ product_type = ProductType.objects.create(
+ name="Type with two variants",
+ slug="two-variants",
+ kind=ProductTypeKind.NORMAL,
+ has_variants=True,
+ is_shipping_required=True,
+ )
+ product_type.variant_attributes.add(color_attribute)
+ product_type.variant_attributes.add(size_attribute)
+
+ product = Product.objects.create(
+ name="Test product with two variants",
+ slug="test-product-with-two-variant",
+ product_type=product_type,
+ category=category,
+ )
+ ProductChannelListing.objects.create(
+ product=product,
+ channel=channel_USD,
+ is_published=True,
+ currency=channel_USD.currency_code,
+ visible_in_listings=True,
+ available_for_purchase_at=datetime.datetime(1999, 1, 1, tzinfo=datetime.UTC),
+ )
+
+ variant = ProductVariant.objects.create(product=product, sku="prodVar1")
+ ProductVariantChannelListing.objects.create(
+ variant=variant,
+ channel=channel_USD,
+ price_amount=Decimal(10),
+ discounted_price_amount=Decimal(10),
+ cost_price_amount=Decimal(1),
+ currency=channel_USD.currency_code,
+ )
+
+ associate_attribute_values_to_instance(
+ variant, {color_attribute.pk: [color_attribute.values.first()]}
+ )
+ associate_attribute_values_to_instance(
+ variant, {size_attribute.pk: [size_attribute.values.first()]}
+ )
+
+ return product
+
+
+def product_with_variant_with_external_media(
+ color_attribute,
+ size_attribute,
+ category,
+ warehouse,
+ channel_USD,
+):
+ product_type = ProductType.objects.create(
+ name="Type with two variants",
+ slug="two-variants",
+ kind=ProductTypeKind.NORMAL,
+ has_variants=True,
+ is_shipping_required=True,
+ )
+ product_type.variant_attributes.add(color_attribute)
+ product_type.variant_attributes.add(size_attribute)
+
+ product = Product.objects.create(
+ name="Test product with two variants",
+ slug="test-product-with-two-variant",
+ product_type=product_type,
+ category=category,
+ )
+ media_obj = ProductMedia.objects.create(
+ product=product,
+ external_url="https://www.youtube.com/watch?v=di8_dJ3Clyo",
+ alt="video_1",
+ type=ProductMediaTypes.VIDEO,
+ oembed_data="{}",
+ )
+ product.media.add(media_obj)
+
+ ProductChannelListing.objects.create(
+ product=product,
+ channel=channel_USD,
+ is_published=True,
+ currency=channel_USD.currency_code,
+ visible_in_listings=True,
+ available_for_purchase_at=datetime.datetime(1999, 1, 1, tzinfo=datetime.UTC),
+ )
+
+ variant = ProductVariant.objects.create(product=product, sku="prodVar1")
+ variant.media.add(media_obj)
+ ProductVariantChannelListing.objects.create(
+ variant=variant,
+ channel=channel_USD,
+ price_amount=Decimal(10),
+ discounted_price_amount=Decimal(10),
+ cost_price_amount=Decimal(1),
+ currency=channel_USD.currency_code,
+ )
+
+ associate_attribute_values_to_instance(
+ variant, {color_attribute.pk: [color_attribute.values.first()]}
+ )
+ associate_attribute_values_to_instance(
+ variant, {size_attribute.pk: [size_attribute.values.first()]}
+ )
+
+ return product
+
+
+def product_with_variant_with_file_attribute(
+ color_attribute, file_attribute, category, warehouse, channel_USD
+):
+ product_type = ProductType.objects.create(
+ name="Type with variant and file attribute",
+ slug="type-with-file-attribute",
+ kind=ProductTypeKind.NORMAL,
+ has_variants=True,
+ is_shipping_required=True,
+ )
+ product_type.variant_attributes.add(file_attribute)
+
+ product = Product.objects.create(
+ name="Test product with variant and file attribute",
+ slug="test-product-with-variant-and-file-attribute",
+ product_type=product_type,
+ category=category,
+ )
+ ProductChannelListing.objects.create(
+ product=product,
+ channel=channel_USD,
+ is_published=True,
+ currency=channel_USD.currency_code,
+ visible_in_listings=True,
+ available_for_purchase_at=datetime.datetime(1999, 1, 1, tzinfo=datetime.UTC),
+ )
+
+ variant = ProductVariant.objects.create(
+ product=product,
+ sku="prodVarTest",
+ )
+ ProductVariantChannelListing.objects.create(
+ variant=variant,
+ channel=channel_USD,
+ price_amount=Decimal(10),
+ discounted_price_amount=Decimal(10),
+ cost_price_amount=Decimal(1),
+ currency=channel_USD.currency_code,
+ )
+
+ associate_attribute_values_to_instance(
+ variant, {file_attribute.pk: [file_attribute.values.first()]}
+ )
+
+ return product
+
+
+def product_with_multiple_values_attributes(product, product_type) -> Product:
+ attribute = Attribute.objects.create(
+ slug="modes",
+ name="Available Modes",
+ input_type=AttributeInputType.MULTISELECT,
+ type=AttributeType.PRODUCT_TYPE,
+ )
+
+ attr_val_1 = AttributeValue.objects.create(
+ attribute=attribute, name="Eco Mode", slug="eco"
+ )
+ attr_val_2 = AttributeValue.objects.create(
+ attribute=attribute, name="Performance Mode", slug="power"
+ )
+
+ product_type.product_attributes.clear()
+ product_type.product_attributes.add(attribute)
+
+ associate_attribute_values_to_instance(
+ product, {attribute.pk: [attr_val_1, attr_val_2]}
+ )
+ return product
+
+
+def product_with_default_variant(
+ product_type_without_variant, category, warehouse, channel_USD
+):
+ product = Product.objects.create(
+ name="Test product",
+ slug="test-product-3",
+ product_type=product_type_without_variant,
+ category=category,
+ )
+ ProductChannelListing.objects.create(
+ product=product,
+ channel=channel_USD,
+ is_published=True,
+ visible_in_listings=True,
+ available_for_purchase_at=datetime.datetime(1999, 1, 1, tzinfo=datetime.UTC),
+ )
+ variant = ProductVariant.objects.create(
+ product=product, sku="1234", track_inventory=True
+ )
+ ProductVariantChannelListing.objects.create(
+ variant=variant,
+ channel=channel_USD,
+ price_amount=Decimal(10),
+ discounted_price_amount=Decimal(10),
+ cost_price_amount=Decimal(1),
+ currency=channel_USD.currency_code,
+ )
+ Stock.objects.create(warehouse=warehouse, product_variant=variant, quantity=100)
+
+ product.search_vector = FlatConcatSearchVector(
+ *prepare_product_search_vector_value(product)
+ )
+ product.save(update_fields=["search_vector"])
+
+ return product
+
+
+def product_without_shipping(category, warehouse, channel_USD):
+ product_type = ProductType.objects.create(
+ name="Type with no shipping",
+ slug="no-shipping",
+ kind=ProductTypeKind.NORMAL,
+ has_variants=False,
+ is_shipping_required=False,
+ )
+ product = Product.objects.create(
+ name="Test product",
+ slug="test-product-4",
+ product_type=product_type,
+ category=category,
+ )
+ ProductChannelListing.objects.create(
+ product=product,
+ channel=channel_USD,
+ is_published=True,
+ visible_in_listings=True,
+ available_for_purchase_at=datetime.datetime(1999, 1, 1, tzinfo=datetime.UTC),
+ )
+ variant = ProductVariant.objects.create(product=product, sku="SKU_E")
+ ProductVariantChannelListing.objects.create(
+ variant=variant,
+ channel=channel_USD,
+ price_amount=Decimal(10),
+ discounted_price_amount=Decimal(10),
+ cost_price_amount=Decimal(1),
+ currency=channel_USD.currency_code,
+ )
+ Stock.objects.create(product_variant=variant, warehouse=warehouse, quantity=1)
+ return product
+
+
+def product_without_category(product):
+ product.category = None
+ product.save()
+ product.channel_listings.all().update(is_published=False)
+ return product
+
+
+def product_list(
+ product_type, category, warehouse, channel_USD, channel_PLN, default_tax_class
+):
+ product_attr = product_type.product_attributes.first()
+ attr_value = product_attr.values.first()
+
+ products = list(
+ Product.objects.bulk_create(
+ [
+ Product(
+ name="Test product 1",
+ slug="test-product-a",
+ description_plaintext="big blue product",
+ category=category,
+ product_type=product_type,
+ tax_class=default_tax_class,
+ ),
+ Product(
+ name="Test product 2",
+ slug="test-product-b",
+ description_plaintext="big orange product",
+ category=category,
+ product_type=product_type,
+ tax_class=default_tax_class,
+ ),
+ Product(
+ name="Test product 3",
+ slug="test-product-c",
+ description_plaintext="small red",
+ category=category,
+ product_type=product_type,
+ tax_class=default_tax_class,
+ ),
+ ]
+ )
+ )
+ ProductChannelListing.objects.bulk_create(
+ [
+ ProductChannelListing(
+ product=products[0],
+ channel=channel_USD,
+ is_published=True,
+ discounted_price_amount=10,
+ currency=channel_USD.currency_code,
+ visible_in_listings=True,
+ available_for_purchase_at=(
+ datetime.datetime(1999, 1, 1, tzinfo=datetime.UTC)
+ ),
+ ),
+ ProductChannelListing(
+ product=products[1],
+ channel=channel_USD,
+ is_published=True,
+ discounted_price_amount=20,
+ currency=channel_USD.currency_code,
+ visible_in_listings=True,
+ available_for_purchase_at=(
+ datetime.datetime(1999, 1, 1, tzinfo=datetime.UTC)
+ ),
+ ),
+ ProductChannelListing(
+ product=products[2],
+ channel=channel_USD,
+ is_published=True,
+ discounted_price_amount=30,
+ currency=channel_USD.currency_code,
+ visible_in_listings=True,
+ available_for_purchase_at=(
+ datetime.datetime(1999, 1, 1, tzinfo=datetime.UTC)
+ ),
+ ),
+ ]
+ )
+ variants = list(
+ ProductVariant.objects.bulk_create(
+ [
+ ProductVariant(
+ product=products[0],
+ sku=str(uuid.uuid4()).replace("-", ""),
+ track_inventory=True,
+ ),
+ ProductVariant(
+ product=products[1],
+ sku=str(uuid.uuid4()).replace("-", ""),
+ track_inventory=True,
+ ),
+ ProductVariant(
+ product=products[2],
+ sku=str(uuid.uuid4()).replace("-", ""),
+ track_inventory=True,
+ ),
+ ]
+ )
+ )
+ ProductVariantChannelListing.objects.bulk_create(
+ [
+ ProductVariantChannelListing(
+ variant=variants[0],
+ channel=channel_USD,
+ cost_price_amount=Decimal(1),
+ price_amount=Decimal(10),
+ discounted_price_amount=Decimal(10),
+ currency=channel_USD.currency_code,
+ ),
+ ProductVariantChannelListing(
+ variant=variants[1],
+ channel=channel_USD,
+ cost_price_amount=Decimal(1),
+ price_amount=Decimal(20),
+ discounted_price_amount=Decimal(20),
+ currency=channel_USD.currency_code,
+ ),
+ ProductVariantChannelListing(
+ variant=variants[2],
+ channel=channel_USD,
+ cost_price_amount=Decimal(1),
+ price_amount=Decimal(30),
+ discounted_price_amount=Decimal(30),
+ currency=channel_USD.currency_code,
+ ),
+ ]
+ )
+ stocks = []
+ for variant in variants:
+ stocks.append(Stock(warehouse=warehouse, product_variant=variant, quantity=100))
+ Stock.objects.bulk_create(stocks)
+
+ for product in products:
+ associate_attribute_values_to_instance(product, {product_attr.pk: [attr_value]})
+ product.search_vector = FlatConcatSearchVector(
+ *prepare_product_search_vector_value(product)
+ )
+
+ Product.objects.bulk_update(products, ["search_vector"])
+
+ return products
+
+
+def product_list_with_variants_many_channel(
+ product_type, category, channel_USD, channel_PLN, default_tax_class
+):
+ products = list(
+ Product.objects.bulk_create(
+ [
+ Product(
+ name="Test product 1",
+ slug="test-product-a",
+ category=category,
+ product_type=product_type,
+ tax_class=default_tax_class,
+ ),
+ Product(
+ name="Test product 2",
+ slug="test-product-b",
+ category=category,
+ product_type=product_type,
+ tax_class=default_tax_class,
+ ),
+ Product(
+ name="Test product 3",
+ slug="test-product-c",
+ category=category,
+ product_type=product_type,
+ tax_class=default_tax_class,
+ ),
+ ]
+ )
+ )
+ ProductChannelListing.objects.bulk_create(
+ [
+ # Channel: USD
+ ProductChannelListing(
+ product=products[0],
+ channel=channel_USD,
+ is_published=True,
+ currency=channel_USD.currency_code,
+ visible_in_listings=True,
+ ),
+ # Channel: PLN
+ ProductChannelListing(
+ product=products[1],
+ channel=channel_PLN,
+ is_published=True,
+ currency=channel_PLN.currency_code,
+ visible_in_listings=True,
+ ),
+ ProductChannelListing(
+ product=products[2],
+ channel=channel_PLN,
+ is_published=True,
+ currency=channel_PLN.currency_code,
+ visible_in_listings=True,
+ ),
+ ]
+ )
+ variants = list(
+ ProductVariant.objects.bulk_create(
+ [
+ ProductVariant(
+ product=products[0],
+ sku=str(uuid.uuid4()).replace("-", ""),
+ track_inventory=True,
+ ),
+ ProductVariant(
+ product=products[1],
+ sku=str(uuid.uuid4()).replace("-", ""),
+ track_inventory=True,
+ ),
+ ProductVariant(
+ product=products[2],
+ sku=str(uuid.uuid4()).replace("-", ""),
+ track_inventory=True,
+ ),
+ ]
+ )
+ )
+ ProductVariantChannelListing.objects.bulk_create(
+ [
+ # Channel: USD
+ ProductVariantChannelListing(
+ variant=variants[0],
+ channel=channel_USD,
+ cost_price_amount=Decimal(1),
+ price_amount=Decimal(10),
+ currency=channel_USD.currency_code,
+ ),
+ # Channel: PLN
+ ProductVariantChannelListing(
+ variant=variants[1],
+ channel=channel_PLN,
+ cost_price_amount=Decimal(1),
+ price_amount=Decimal(20),
+ currency=channel_PLN.currency_code,
+ ),
+ ProductVariantChannelListing(
+ variant=variants[2],
+ channel=channel_PLN,
+ cost_price_amount=Decimal(1),
+ price_amount=Decimal(30),
+ currency=channel_PLN.currency_code,
+ ),
+ ]
+ )
+ return products
+
+
+def product_list_with_many_channels(product_list, channel_PLN):
+ ProductChannelListing.objects.bulk_create(
+ [
+ ProductChannelListing(
+ product=product_list[0],
+ channel=channel_PLN,
+ is_published=True,
+ ),
+ ProductChannelListing(
+ product=product_list[1],
+ channel=channel_PLN,
+ is_published=True,
+ ),
+ ProductChannelListing(
+ product=product_list[2],
+ channel=channel_PLN,
+ is_published=True,
+ ),
+ ]
+ )
+ return product_list
+
+
+def product_list_unpublished(product_list, channel_USD):
+ products = Product.objects.filter(pk__in=[product.pk for product in product_list])
+ ProductChannelListing.objects.filter(
+ product__in=products, channel=channel_USD
+ ).update(is_published=False)
+ return products
+
+
+def product_list_published(product_list, channel_USD):
+ products = Product.objects.filter(pk__in=[product.pk for product in product_list])
+ ProductChannelListing.objects.filter(
+ product__in=products, channel=channel_USD
+ ).update(is_published=True)
+ return products
+
+
+def product_with_image(product, image, media_root):
+ ProductMedia.objects.create(product=product, image=image)
+ return product
+
+
+def product_with_image_list(product, image_list, media_root):
+ ProductMedia.objects.create(product=product, image=image_list[0])
+ ProductMedia.objects.create(product=product, image=image_list[1])
+ return product
+
+
+def product_with_image_list_and_one_null_sort_order(product_with_image_list):
+ """Return a product with mixed sorting order.
+
+ As we allow to have `null` in `sort_order` in database, but our logic
+ covers changing any new `null` values to proper `int` need to execute
+ raw SQL query on database to test behavior of `null` `sort_order`.
+
+ SQL query behavior:
+ Updates one of the product media `sort_order` to `null`.
+ """
+ with connection.cursor() as cursor:
+ cursor.execute(
+ """
+ UPDATE PRODUCT_PRODUCTMEDIA
+ SET SORT_ORDER = NULL
+ WHERE ID IN (
+ SELECT ID FROM PRODUCT_PRODUCTMEDIA
+ WHERE PRODUCT_ID = %s
+ ORDER BY ID
+ LIMIT 1
+ )
+ """,
+ [product_with_image_list.pk],
+ )
+ product_with_image_list.refresh_from_db()
+ return product_with_image_list
+
+
+def unavailable_product(product_type, category, channel_USD, default_tax_class):
+ product = Product.objects.create(
+ name="Test product",
+ slug="test-product-5",
+ product_type=product_type,
+ category=category,
+ tax_class=default_tax_class,
+ )
+ ProductChannelListing.objects.create(
+ product=product,
+ channel=channel_USD,
+ is_published=False,
+ visible_in_listings=False,
+ )
+ return product
+
+
+def unavailable_product_with_variant(
+ product_type, category, warehouse, channel_USD, default_tax_class
+):
+ product = Product.objects.create(
+ name="Test product",
+ slug="test-product-6",
+ product_type=product_type,
+ category=category,
+ tax_class=default_tax_class,
+ )
+ ProductChannelListing.objects.create(
+ product=product,
+ channel=channel_USD,
+ is_published=False,
+ visible_in_listings=False,
+ )
+
+ variant_attr = product_type.variant_attributes.first()
+ variant_attr_value = variant_attr.values.first()
+
+ variant = ProductVariant.objects.create(
+ product=product,
+ sku="123",
+ )
+ ProductVariantChannelListing.objects.create(
+ variant=variant,
+ channel=channel_USD,
+ price_amount=Decimal(10),
+ discounted_price_amount=Decimal(10),
+ cost_price_amount=Decimal(1),
+ currency=channel_USD.currency_code,
+ )
+ Stock.objects.create(product_variant=variant, warehouse=warehouse, quantity=10)
+
+ associate_attribute_values_to_instance(
+ variant, {variant_attr.pk: [variant_attr_value]}
+ )
+ return product
+
+
+def product_with_images(
+ product_type, category, media_root, channel_USD, default_tax_class
+):
+ product = Product.objects.create(
+ name="Test product",
+ slug="test-product-7",
+ product_type=product_type,
+ category=category,
+ tax_class=default_tax_class,
+ )
+ ProductChannelListing.objects.create(
+ product=product,
+ channel=channel_USD,
+ is_published=True,
+ visible_in_listings=True,
+ )
+ file_mock_0 = MagicMock(spec=File, name="FileMock0")
+ file_mock_0.name = "image0.jpg"
+ file_mock_1 = MagicMock(spec=File, name="FileMock1")
+ file_mock_1.name = "image1.jpg"
+ product.media.create(image=file_mock_0)
+ product.media.create(image=file_mock_1)
+ return product
+
+
+def lots_of_products_with_variants(product_type, channel_USD):
+ def chunks(iterable, size):
+ it = iter(iterable)
+ chunk = tuple(itertools.islice(it, size))
+ while chunk:
+ yield chunk
+ chunk = tuple(itertools.islice(it, size))
+
+ variants_per_product = 4
+ products_count = 10000
+ slug_generator = (f"test-slug-{i}" for i in range(products_count))
+
+ for batch in chunks(range(products_count), 500):
+ batch_len = len(batch)
+ variants = []
+ product_listings = []
+ products = [
+ Product(
+ name=i,
+ slug=next(slug_generator),
+ product_type_id=product_type.pk,
+ )
+ for i in range(batch_len)
+ ]
+ for product in Product.objects.bulk_create(products):
+ product_listings.append(
+ ProductChannelListing(
+ channel=channel_USD,
+ product=product,
+ visible_in_listings=True,
+ available_for_purchase_at="2022-01-01",
+ currency=channel_USD.currency_code,
+ )
+ )
+ for x in range(variants_per_product):
+ variant = ProductVariant(name=x, product_id=product.id)
+ variants.append(variant)
+ ProductVariant.objects.bulk_create(variants)
+ variant_listings = []
+ for variant in variants:
+ price = random.randint(1, 100)
+ variant_listings.append(
+ ProductVariantChannelListing(
+ variant=variant,
+ channel=channel_USD,
+ currency=channel_USD.currency_code,
+ price_amount=price,
+ discounted_price_amount=price,
+ )
+ )
+ ProductVariantChannelListing.objects.bulk_create(variant_listings)
+ ProductChannelListing.objects.bulk_create(product_listings)
+ return Product.objects.all()
diff --git a/saleor/product/tests/fixtures/product_media.py b/saleor/product/tests/fixtures/product_media.py
new file mode 100644
index 0000000000..ded9fc98e7
--- /dev/null
+++ b/saleor/product/tests/fixtures/product_media.py
@@ -0,0 +1,15 @@
+import pytest
+
+from ... import ProductMediaTypes
+from ...models import ProductMedia
+
+
+def product_media_image(product, image, media_root):
+ return ProductMedia.objects.create(
+ product=product,
+ image=image,
+ alt="image",
+ type=ProductMediaTypes.IMAGE,
+ oembed_data="{}",
+ )
diff --git a/saleor/product/tests/fixtures/product_type.py b/saleor/product/tests/fixtures/product_type.py
new file mode 100644
index 0000000000..d3f26577a6
--- /dev/null
+++ b/saleor/product/tests/fixtures/product_type.py
@@ -0,0 +1,189 @@
+import pytest
+
+from ....attribute import AttributeType
+from ....attribute.models import Attribute
+from ... import ProductTypeKind
+from ...models import ProductType
+
+
+def product_type_generator(
+ attribute_generator, attribute_value_generator, default_tax_class
+):
+ def create_product_type(
+ name="Default Type",
+ slug="default-type",
+ kind=ProductTypeKind.NORMAL,
+ has_variants=True,
+ is_shipping_required=True,
+ tax_class=default_tax_class,
+ product_attributes=None,
+ variant_attributes=None,
+ ):
+ product_type = ProductType.objects.create(
+ name=name,
+ slug=slug,
+ kind=kind,
+ has_variants=has_variants,
+ is_shipping_required=is_shipping_required,
+ tax_class=tax_class,
+ )
+ if product_attributes is None:
+ product_attribute = attribute_generator(
+ external_reference="colorAttributeExternalReference",
+ slug="color",
+ name="Color",
+ type=AttributeType.PRODUCT_TYPE,
+ filterable_in_storefront=True,
+ filterable_in_dashboard=True,
+ available_in_grid=True,
+ )
+
+ attribute_value_generator(
+ external_reference="colorAttributeValue1ExternalReference",
+ name="Red",
+ slug="red",
+ attribute=product_attribute,
+ )
+
+ product_attributes = [product_attribute]
+ if variant_attributes is None:
+ variant_attribute = attribute_generator(
+ external_reference="sizeAttributeExternalReference",
+ slug="size",
+ name="Size",
+ type=AttributeType.PRODUCT_TYPE,
+ filterable_in_storefront=True,
+ filterable_in_dashboard=True,
+ available_in_grid=True,
+ )
+
+ attribute_value_generator(
+ name="Small",
+ slug="small",
+ attribute=variant_attribute,
+ )
+ variant_attributes = [variant_attribute]
+
+ product_type.product_attributes.add(*product_attributes)
+ product_type.variant_attributes.add(
+ *variant_attributes, through_defaults={"variant_selection": True}
+ )
+ return product_type
+
+ return create_product_type
+
+
+def product_type(product_type_generator):
+ return product_type_generator()
+
+
+def product_type_with_product_attributes(attribute_without_values):
+ product_type = ProductType.objects.create(
+ name="product_type_with_product_attributes",
+ slug="product-type-with-product-attributes",
+ has_variants=False,
+ is_shipping_required=False,
+ weight=0,
+ )
+ product_type.product_attributes.add(attribute_without_values)
+ return product_type
+
+
+def product_type_with_variant_attributes(attribute_without_values):
+ product_type = ProductType.objects.create(
+ name="product_type_with_variant_attributes",
+ slug="product-type-with-variant-attributes",
+ has_variants=False,
+ is_shipping_required=False,
+ weight=0,
+ )
+ product_type.variant_attributes.add(attribute_without_values)
+ return product_type
+
+
+def product_type_with_value_required_attributes(
+ color_attribute, size_attribute, default_tax_class
+):
+ product_type = ProductType.objects.create(
+ name="Default Type",
+ slug="default-type",
+ kind=ProductTypeKind.NORMAL,
+ has_variants=True,
+ is_shipping_required=True,
+ tax_class=default_tax_class,
+ )
+ color_attribute.value_required = True
+ size_attribute.value_required = True
+ Attribute.objects.bulk_update([color_attribute, size_attribute], ["value_required"])
+ product_type.product_attributes.add(color_attribute)
+ product_type.product_attributes.add(size_attribute)
+ return product_type
+
+
+def product_type_list():
+ product_type_1 = ProductType.objects.create(
+ name="Type 1", slug="type-1", kind=ProductTypeKind.NORMAL
+ )
+ product_type_2 = ProductType.objects.create(
+ name="Type 2", slug="type-2", kind=ProductTypeKind.NORMAL
+ )
+ product_type_3 = ProductType.objects.create(
+ name="Type 3", slug="type-3", kind=ProductTypeKind.NORMAL
+ )
+ return product_type_1, product_type_2, product_type_3
+
+
+def non_shippable_gift_card_product_type(db):
+ product_type = ProductType.objects.create(
+ name="Gift card type no shipping",
+ slug="gift-card-type-no-shipping",
+ kind=ProductTypeKind.GIFT_CARD,
+ has_variants=True,
+ is_shipping_required=False,
+ )
+ return product_type
+
+
+def shippable_gift_card_product_type(db):
+ product_type = ProductType.objects.create(
+ name="Gift card type with shipping",
+ slug="gift-card-type-with-shipping",
+ kind=ProductTypeKind.GIFT_CARD,
+ has_variants=True,
+ is_shipping_required=True,
+ )
+ return product_type
+
+
+def product_type_with_rich_text_attribute(rich_text_attribute):
+ product_type = ProductType.objects.create(
+ name="Default Type",
+ slug="default-type",
+ kind=ProductTypeKind.NORMAL,
+ has_variants=True,
+ is_shipping_required=True,
+ )
+ product_type.product_attributes.add(rich_text_attribute)
+ product_type.variant_attributes.add(rich_text_attribute)
+ return product_type
+
+
+def product_type_without_variant():
+ product_type = ProductType.objects.create(
+ name="Type",
+ slug="type",
+ has_variants=False,
+ is_shipping_required=True,
+ kind=ProductTypeKind.NORMAL,
+ )
+ return product_type
diff --git a/saleor/product/tests/fixtures/variant.py b/saleor/product/tests/fixtures/variant.py
new file mode 100644
index 0000000000..dfaea75098
--- /dev/null
+++ b/saleor/product/tests/fixtures/variant.py
@@ -0,0 +1,346 @@
+import datetime
+from decimal import Decimal
+
+import pytest
+from django.utils import timezone
+
+from ....warehouse.models import Stock
+from ... import ProductTypeKind
+from ...models import (
+ Product,
+ ProductChannelListing,
+ ProductMedia,
+ ProductType,
+ ProductVariant,
+ ProductVariantChannelListing,
+ VariantMedia,
+)
+
+
+def variant_without_inventory_tracking(
+ product_type_without_variant, category, warehouse, channel_USD
+):
+ product = Product.objects.create(
+ name="Test product without inventory tracking",
+ slug="test-product-without-tracking",
+ product_type=product_type_without_variant,
+ category=category,
+ )
+ ProductChannelListing.objects.create(
+ product=product,
+ channel=channel_USD,
+ is_published=True,
+ visible_in_listings=True,
+ available_for_purchase_at=datetime.datetime.now(tz=datetime.UTC),
+ )
+ variant = ProductVariant.objects.create(
+ product=product,
+ sku="tracking123",
+ track_inventory=False,
+ )
+ ProductVariantChannelListing.objects.create(
+ variant=variant,
+ channel=channel_USD,
+ price_amount=Decimal(10),
+ discounted_price_amount=Decimal(10),
+ cost_price_amount=Decimal(1),
+ currency=channel_USD.currency_code,
+ )
+ Stock.objects.create(warehouse=warehouse, product_variant=variant, quantity=0)
+ return variant
+
+
+def variant(product, channel_USD) -> ProductVariant:
+ product_variant = ProductVariant.objects.create(
+ product=product, sku="SKU_A", external_reference="SKU_A"
+ )
+ ProductVariantChannelListing.objects.create(
+ variant=product_variant,
+ channel=channel_USD,
+ price_amount=Decimal(10),
+ discounted_price_amount=Decimal(10),
+ cost_price_amount=Decimal(1),
+ currency=channel_USD.currency_code,
+ )
+ return product_variant
+
+
+def variant_with_translations(variant):
+ variant.translations.create(language_code="pl", name="OldVariant PL")
+ variant.translations.create(language_code="de", name="OldVariant DE")
+ return variant
+
+
+def variant_with_image(variant, image_list, media_root):
+ media = ProductMedia.objects.create(product=variant.product, image=image_list[0])
+ VariantMedia.objects.create(variant=variant, media=media)
+ return variant
+
+
+def variant_with_many_stocks(variant, warehouses_with_shipping_zone):
+ warehouses = warehouses_with_shipping_zone
+ Stock.objects.bulk_create(
+ [
+ Stock(warehouse=warehouses[0], product_variant=variant, quantity=4),
+ Stock(warehouse=warehouses[1], product_variant=variant, quantity=3),
+ ]
+ )
+ return variant
+
+
+def variant_on_promotion(
+ product, channel_USD, promotion_rule, warehouse
+) -> ProductVariant:
+ product_variant = ProductVariant.objects.create(
+ product=product, sku="SKU_A", external_reference="SKU_A"
+ )
+ price_amount = Decimal(10)
+ ProductVariantChannelListing.objects.create(
+ variant=product_variant,
+ channel=channel_USD,
+ price_amount=price_amount,
+ discounted_price_amount=price_amount,
+ cost_price_amount=Decimal(1),
+ currency=channel_USD.currency_code,
+ )
+ Stock.objects.create(
+ warehouse=warehouse, product_variant=product_variant, quantity=10
+ )
+
+ promotion_rule.variants.add(product_variant)
+ reward_value = promotion_rule.reward_value
+ discount_amount = price_amount * reward_value / 100
+
+ variant_channel_listing = product_variant.channel_listings.get(channel=channel_USD)
+
+ variant_channel_listing.discounted_price_amount = (
+ variant_channel_listing.price_amount - reward_value
+ )
+ variant_channel_listing.save(update_fields=["discounted_price_amount"])
+
+ variant_channel_listing.variantlistingpromotionrule.create(
+ promotion_rule=promotion_rule,
+ discount_amount=discount_amount,
+ currency=channel_USD.currency_code,
+ )
+
+ return product_variant
+
+
+def preorder_variant_global_threshold(product, channel_USD):
+ product_variant = ProductVariant.objects.create(
+ product=product, sku="SKU_A_P", is_preorder=True, preorder_global_threshold=10
+ )
+ ProductVariantChannelListing.objects.create(
+ variant=product_variant,
+ channel=channel_USD,
+ price_amount=Decimal(10),
+ discounted_price_amount=Decimal(10),
+ cost_price_amount=Decimal(1),
+ currency=channel_USD.currency_code,
+ )
+ return product_variant
+
+
+def preorder_variant_channel_threshold(product, channel_USD):
+ product_variant = ProductVariant.objects.create(
+ product=product, sku="SKU_B_P", is_preorder=True, preorder_global_threshold=None
+ )
+ ProductVariantChannelListing.objects.create(
+ variant=product_variant,
+ channel=channel_USD,
+ price_amount=Decimal(10),
+ discounted_price_amount=Decimal(10),
+ cost_price_amount=Decimal(1),
+ currency=channel_USD.currency_code,
+ preorder_quantity_threshold=10,
+ )
+ return product_variant
+
+
+def preorder_variant_global_and_channel_threshold(product, channel_USD, channel_PLN):
+ product_variant = ProductVariant.objects.create(
+ product=product, sku="SKU_C_P", is_preorder=True, preorder_global_threshold=10
+ )
+ ProductVariantChannelListing.objects.bulk_create(
+ [
+ ProductVariantChannelListing(
+ variant=product_variant,
+ channel=channel_USD,
+ cost_price_amount=Decimal(1),
+ price_amount=Decimal(10),
+ currency=channel_USD.currency_code,
+ preorder_quantity_threshold=8,
+ ),
+ ProductVariantChannelListing(
+ variant=product_variant,
+ channel=channel_PLN,
+ cost_price_amount=Decimal(1),
+ price_amount=Decimal(10),
+ currency=channel_PLN.currency_code,
+ preorder_quantity_threshold=4,
+ ),
+ ]
+ )
+ return product_variant
+
+
+def preorder_variant_with_end_date(product, channel_USD):
+ product_variant = ProductVariant.objects.create(
+ product=product,
+ sku="SKU_D_P",
+ is_preorder=True,
+ preorder_global_threshold=10,
+ preorder_end_date=timezone.now() + datetime.timedelta(days=10),
+ )
+ ProductVariantChannelListing.objects.create(
+ variant=product_variant,
+ channel=channel_USD,
+ price_amount=Decimal(10),
+ discounted_price_amount=Decimal(10),
+ cost_price_amount=Decimal(1),
+ currency=channel_USD.currency_code,
+ )
+ return product_variant
+
+
+def variant_with_many_stocks_different_shipping_zones(
+ variant, warehouses_with_different_shipping_zone
+):
+ warehouses = warehouses_with_different_shipping_zone
+ Stock.objects.bulk_create(
+ [
+ Stock(warehouse=warehouses[0], product_variant=variant, quantity=4),
+ Stock(warehouse=warehouses[1], product_variant=variant, quantity=3),
+ ]
+ )
+ return variant
+
+
+def gift_card_shippable_variant(shippable_gift_card_product, channel_USD, warehouse):
+ product = shippable_gift_card_product
+ product_variant = ProductVariant.objects.create(
+ product=product, sku="SKU_CARD_A", track_inventory=False
+ )
+ ProductVariantChannelListing.objects.create(
+ variant=product_variant,
+ channel=channel_USD,
+ price_amount=Decimal(10),
+ discounted_price_amount=Decimal(10),
+ cost_price_amount=Decimal(1),
+ currency=channel_USD.currency_code,
+ )
+ Stock.objects.create(
+ warehouse=warehouse, product_variant=product_variant, quantity=1
+ )
+ return product_variant
+
+
+def gift_card_non_shippable_variant(
+ non_shippable_gift_card_product, channel_USD, warehouse
+):
+ product = non_shippable_gift_card_product
+ product_variant = ProductVariant.objects.create(
+ product=product, sku="SKU_CARD_B", track_inventory=False
+ )
+ ProductVariantChannelListing.objects.create(
+ variant=product_variant,
+ channel=channel_USD,
+ price_amount=Decimal(10),
+ discounted_price_amount=Decimal(10),
+ cost_price_amount=Decimal(1),
+ currency=channel_USD.currency_code,
+ )
+ Stock.objects.create(
+ warehouse=warehouse, product_variant=product_variant, quantity=1
+ )
+ return product_variant
+
+
+def product_variant_list(product, channel_USD, channel_PLN):
+ variants = list(
+ ProductVariant.objects.bulk_create(
+ [
+ ProductVariant(product=product, sku="1"),
+ ProductVariant(product=product, sku="2"),
+ ProductVariant(product=product, sku="3"),
+ ProductVariant(product=product, sku="4"),
+ ]
+ )
+ )
+ ProductVariantChannelListing.objects.bulk_create(
+ [
+ ProductVariantChannelListing(
+ variant=variants[0],
+ channel=channel_USD,
+ cost_price_amount=Decimal(1),
+ price_amount=Decimal(10),
+ discounted_price_amount=Decimal(10),
+ currency=channel_USD.currency_code,
+ ),
+ ProductVariantChannelListing(
+ variant=variants[1],
+ channel=channel_USD,
+ cost_price_amount=Decimal(1),
+ price_amount=Decimal(10),
+ discounted_price_amount=Decimal(10),
+ currency=channel_USD.currency_code,
+ ),
+ ProductVariantChannelListing(
+ variant=variants[2],
+ channel=channel_PLN,
+ cost_price_amount=Decimal(1),
+ price_amount=Decimal(10),
+ discounted_price_amount=Decimal(10),
+ currency=channel_PLN.currency_code,
+ ),
+ ProductVariantChannelListing(
+ variant=variants[3],
+ channel=channel_USD,
+ cost_price_amount=Decimal(1),
+ price_amount=Decimal(10),
+ discounted_price_amount=Decimal(10),
+ currency=channel_USD.currency_code,
+ ),
+ ]
+ )
+ return variants
+
+
+def variant_with_no_attributes(category, channel_USD):
+ """Create a variant having no attributes, the same for the parent product."""
+ product_type = ProductType.objects.create(
+ name="Test product type",
+ has_variants=True,
+ is_shipping_required=True,
+ kind=ProductTypeKind.NORMAL,
+ )
+ product = Product.objects.create(
+ name="Test product",
+ product_type=product_type,
+ category=category,
+ )
+ variant = ProductVariant.objects.create(product=product, sku="123")
+ ProductVariantChannelListing.objects.create(
+ variant=variant,
+ channel=channel_USD,
+ cost_price_amount=Decimal(1),
+ price_amount=Decimal(10),
+ currency=channel_USD.currency_code,
+ )
+ return variant
diff --git a/saleor/product/tests/test_generate_and_set_variant_name.py b/saleor/product/tests/test_generate_and_set_variant_name.py
index f24b702110..153abf44ad 100644
--- a/saleor/product/tests/test_generate_and_set_variant_name.py
+++ b/saleor/product/tests/test_generate_and_set_variant_name.py
@@ -1,47 +1,17 @@
-from decimal import Decimal
from unittest.mock import MagicMock, Mock
-import pytest
-
from ...attribute import AttributeInputType
from ...attribute.models import AttributeValue
from ...attribute.utils import associate_attribute_values_to_instance
-from ...product import ProductTypeKind
-from ...product.models import ProductVariantChannelListing
-from ..models import Product, ProductType, ProductVariant
+from ..models import ProductType
from ..tasks import _update_variants_names
from ..utils.variants import generate_and_set_variant_name
-def variant_with_no_attributes(category, channel_USD):
- """Create a variant having no attributes, the same for the parent product."""
- product_type = ProductType.objects.create(
- name="Test product type",
- has_variants=True,
- is_shipping_required=True,
- kind=ProductTypeKind.NORMAL,
- )
- product = Product.objects.create(
- name="Test product",
- product_type=product_type,
- category=category,
- )
- variant = ProductVariant.objects.create(product=product, sku="123")
- ProductVariantChannelListing.objects.create(
- variant=variant,
- channel=channel_USD,
- cost_price_amount=Decimal(1),
- price_amount=Decimal(10),
- currency=channel_USD.currency_code,
- )
- return variant
-
-
def test_generate_and_set_variant_name_different_attributes(
variant_with_no_attributes, color_attribute_without_values, size_attribute
):
- """Test variant name generation with a mix of mix of (non-)selection attributes."""
+ """Test variant name generation with a mix of (non-)selection attributes."""
variant = variant_with_no_attributes
color_attribute = color_attribute_without_values
diff --git a/saleor/tests/fixtures.py b/saleor/tests/fixtures.py
index 5636e80fc4..7a03bddc73 100644
--- a/saleor/tests/fixtures.py
+++ b/saleor/tests/fixtures.py
@@ -1,6 +1,4 @@
import datetime
-import itertools
-import random
import uuid
from contextlib import contextmanager
from decimal import Decimal
@@ -13,7 +11,6 @@ import graphene
import pytest
from django.conf import settings
from django.contrib.sites.models import Site
-from django.core.files import File
from django.core.files.uploadedfile import SimpleUploadedFile
from django.db import connection
from django.template.defaultfilters import truncatechars
@@ -43,7 +40,6 @@ from ..checkout.utils import (
from ..core import JobStatus
from ..core.models import EventDelivery, EventDeliveryAttempt, EventPayload
from ..core.payments import PaymentInterface
-from ..core.postgres import FlatConcatSearchVector
from ..core.units import MeasurementUnits
from ..core.utils.editorjs import clean_editor_js
from ..csv.events import ExportEvents
@@ -83,27 +79,13 @@ from ..payment.utils import create_manual_adjustment_events
from ..permission.enums import get_permissions
from ..permission.models import Permission
from ..plugins.manager import get_plugins_manager
-from ..product import ProductMediaTypes, ProductTypeKind
from ..product.models import (
- Category,
CategoryTranslation,
- Collection,
- CollectionChannelListing,
CollectionTranslation,
- DigitalContent,
- DigitalContentUrl,
- Product,
- ProductChannelListing,
- ProductMedia,
ProductTranslation,
- ProductType,
- ProductVariant,
ProductVariantChannelListing,
ProductVariantTranslation,
- VariantMedia,
)
-from ..product.search import prepare_product_search_vector_value
-from ..product.tests.utils import create_image
from ..product.utils.variants import fetch_variants_for_promotion_rules
from ..shipping.models import (
ShippingMethod,
@@ -118,7 +100,6 @@ from ..tax import TaxCalculationStrategy
from ..warehouse.models import (
PreorderReservation,
Reservation,
- Stock,
Warehouse,
)
from ..webhook.event_types import WebhookEventAsyncType
@@ -1189,74 +1170,6 @@ def user_checkouts(request, user_checkout_with_items, user_checkout_with_items_f
raise ValueError("Internal test error")
-def product_type_generator(
- attribute_generator, attribute_value_generator, default_tax_class
-):
- def create_product_type(
- name="Default Type",
- slug="default-type",
- kind=ProductTypeKind.NORMAL,
- has_variants=True,
- is_shipping_required=True,
- tax_class=default_tax_class,
- product_attributes=None,
- variant_attributes=None,
- ):
- product_type = ProductType.objects.create(
- name=name,
- slug=slug,
- kind=kind,
- has_variants=has_variants,
- is_shipping_required=is_shipping_required,
- tax_class=tax_class,
- )
- if product_attributes is None:
- product_attribute = attribute_generator(
- external_reference="colorAttributeExternalReference",
- slug="color",
- name="Color",
- type=AttributeType.PRODUCT_TYPE,
- filterable_in_storefront=True,
- filterable_in_dashboard=True,
- available_in_grid=True,
- )
-
- attribute_value_generator(
- external_reference="colorAttributeValue1ExternalReference",
- name="Red",
- slug="red",
- attribute=product_attribute,
- )
-
- product_attributes = [product_attribute]
- if variant_attributes is None:
- variant_attribute = attribute_generator(
- external_reference="sizeAttributeExternalReference",
- slug="size",
- name="Size",
- type=AttributeType.PRODUCT_TYPE,
- filterable_in_storefront=True,
- filterable_in_dashboard=True,
- available_in_grid=True,
- )
-
- attribute_value_generator(
- name="Small",
- slug="small",
- attribute=variant_attribute,
- )
- variant_attributes = [variant_attribute]
-
- product_type.product_attributes.add(*product_attributes)
- product_type.variant_attributes.add(
- *variant_attributes, through_defaults={"variant_selection": True}
- )
- return product_type
-
- return create_product_type
-
-
@pytest.fixture
def admin_user(db):
"""Return a Django admin user."""
@@ -1732,56 +1645,6 @@ def multiselect_attribute(db, attribute_generator, attribute_values_generator):
return attribute
-def product_type_with_product_attributes(attribute_without_values):
- product_type = ProductType.objects.create(
- name="product_type_with_product_attributes",
- slug="product-type-with-product-attributes",
- has_variants=False,
- is_shipping_required=False,
- weight=0,
- )
- product_type.product_attributes.add(attribute_without_values)
- return product_type
-
-
-def product_type_with_variant_attributes(attribute_without_values):
- product_type = ProductType.objects.create(
- name="product_type_with_variant_attributes",
- slug="product-type-with-variant-attributes",
- has_variants=False,
- is_shipping_required=False,
- weight=0,
- )
- product_type.variant_attributes.add(attribute_without_values)
- return product_type
-
-
-def product_with_product_attributes(
- product_type_with_product_attributes, non_default_category
-):
- return Product.objects.create(
- name="product_with_product_attributes",
- slug="product-with-product-attributes",
- product_type=product_type_with_product_attributes,
- category=non_default_category,
- )
-
-
-def product_with_variant_attributes(
- product_type_with_variant_attributes, non_default_category
-):
- return Product.objects.create(
- name="product_with_variant_attributes",
- slug="product-with-variant-attributes",
- product_type=product_type_with_variant_attributes,
- category=non_default_category,
- )
-
-
@pytest.fixture
def date_attribute(db):
attribute = Attribute.objects.create(
@@ -2260,1673 +2123,141 @@ def page_file_attribute(db):
type=AttributeType.PAGE_TYPE,
input_type=AttributeInputType.FILE,
)
- AttributeValue.objects.create(
- attribute=attribute,
- name="test_file.txt",
- slug="test_filetxt",
- file_url="test_file.txt",
- content_type="text/plain",
- )
- AttributeValue.objects.create(
- attribute=attribute,
- name="test_file.jpeg",
- slug="test_filejpeg",
- file_url="test_file.jpeg",
- content_type="image/jpeg",
- )
- return attribute
-
-
-def product_type_attribute_list() -> list[Attribute]:
- return list(
- Attribute.objects.bulk_create(
- [
- Attribute(
- slug="height", name="Height", type=AttributeType.PRODUCT_TYPE
- ),
- Attribute(
- slug="weight", name="Weight", type=AttributeType.PRODUCT_TYPE
- ),
- Attribute(
- slug="thickness", name="Thickness", type=AttributeType.PRODUCT_TYPE
- ),
- ]
- )
- )
-
-
-def page_type_attribute_list() -> list[Attribute]:
- return list(
- Attribute.objects.bulk_create(
- [
- Attribute(slug="size", name="Size", type=AttributeType.PAGE_TYPE),
- Attribute(slug="font", name="Weight", type=AttributeType.PAGE_TYPE),
- Attribute(
- slug="margin", name="Thickness", type=AttributeType.PAGE_TYPE
- ),
- ]
- )
- )
-
-
-def image():
- img_data = BytesIO()
- image = Image.new("RGB", size=(1, 1))
- image.save(img_data, format="JPEG")
- return SimpleUploadedFile("product.jpg", img_data.getvalue())
-
-
-def icon_image():
- img_data = BytesIO()
- image = Image.new("RGB", size=(1, 1))
- image.save(img_data, format="PNG")
- return SimpleUploadedFile("logo.png", img_data.getvalue())
-
-
-def image_list():
- img_data_1 = BytesIO()
- image_1 = Image.new("RGB", size=(1, 1))
- image_1.save(img_data_1, format="JPEG")
-
- img_data_2 = BytesIO()
- image_2 = Image.new("RGB", size=(1, 1))
- image_2.save(img_data_2, format="JPEG")
- return [
- SimpleUploadedFile("image1.jpg", img_data_1.getvalue()),
- SimpleUploadedFile("image2.jpg", img_data_2.getvalue()),
- ]
-
-
-def category_generator():
- def create_category(
- name="Default",
- slug="default",
- ):
- category = Category.objects.create(
- name=name,
- slug=slug,
- )
- return category
-
- return create_category
-
-
-def category(category_generator): # pylint: disable=W0613
- return category_generator()
-
-
-def category_with_image(db, image, media_root): # pylint: disable=W0613
- return Category.objects.create(
- name="Default2", slug="default2", background_image=image
- )
-
-
-def categories(db):
- category1 = Category.objects.create(name="Category1", slug="cat1")
- category2 = Category.objects.create(name="Category2", slug="cat2")
- return [category1, category2]
-
-
-def category_list():
- category_1 = Category.objects.create(name="Category 1", slug="category-1")
- category_2 = Category.objects.create(name="Category 2", slug="category-2")
- category_3 = Category.objects.create(name="Category 3", slug="category-3")
- return category_1, category_2, category_3
-
-
-def categories_tree(db, product_type, channel_USD): # pylint: disable=W0613
- parent = Category.objects.create(name="Parent", slug="parent")
- parent.children.create(name="Child", slug="child")
- child = parent.children.first()
-
- product_attr = product_type.product_attributes.first()
- attr_value = product_attr.values.first()
-
- product = Product.objects.create(
- name="Test product",
- slug="test-product-10",
- product_type=product_type,
- category=child,
- )
- ProductChannelListing.objects.create(
- product=product,
- channel=channel_USD,
- is_published=True,
- visible_in_listings=True,
- )
-
- associate_attribute_values_to_instance(product, {product_attr.pk: [attr_value]})
- return parent
-
-
-def categories_tree_with_published_products(
- categories_tree, product, channel_USD, channel_PLN
-):
- parent = categories_tree
- parent_product = product
- parent_product.category = parent
-
- child = parent.children.first()
- child_product = child.products.first()
-
- product_list = [child_product, parent_product]
-
- ProductChannelListing.objects.filter(product__in=product_list).delete()
- product_channel_listings = []
- for product in product_list:
- product.save()
- product_channel_listings.append(
- ProductChannelListing(
- product=product,
- channel=channel_USD,
- published_at=datetime.datetime.now(tz=datetime.UTC),
- is_published=True,
- )
- )
- product_channel_listings.append(
- ProductChannelListing(
- product=product,
- channel=channel_PLN,
- published_at=datetime.datetime.now(tz=datetime.UTC),
- is_published=True,
- )
- )
- ProductChannelListing.objects.bulk_create(product_channel_listings)
- return parent
-
-
-def non_default_category(db): # pylint: disable=W0613
- return Category.objects.create(name="Not default", slug="not-default")
-
-
-def permission_manage_discounts():
- return Permission.objects.get(codename="manage_discounts")
-
-
-def permission_manage_gift_card():
- return Permission.objects.get(codename="manage_gift_card")
-
-
-def permission_manage_orders():
- return Permission.objects.get(codename="manage_orders")
-
-
-def permission_manage_orders_import():
- return Permission.objects.get(codename="manage_orders_import")
-
-
-def permission_manage_checkouts():
- return Permission.objects.get(codename="manage_checkouts")
-
-
-def permission_handle_checkouts():
- return Permission.objects.get(codename="handle_checkouts")
-
-
-def permission_manage_plugins():
- return Permission.objects.get(codename="manage_plugins")
-
-
-def permission_manage_apps():
- return Permission.objects.get(codename="manage_apps")
-
-
-def permission_handle_taxes():
- return Permission.objects.get(codename="handle_taxes")
-
-
-def permission_manage_observability():
- return Permission.objects.get(codename="manage_observability")
-
-
-def permission_manage_taxes():
- return Permission.objects.get(codename="manage_taxes")
-
-
-def product_type(product_type_generator):
- return product_type_generator()
-
-
-def product_type_with_value_required_attributes(
- color_attribute, size_attribute, default_tax_class
-):
- product_type = ProductType.objects.create(
- name="Default Type",
- slug="default-type",
- kind=ProductTypeKind.NORMAL,
- has_variants=True,
- is_shipping_required=True,
- tax_class=default_tax_class,
- )
- color_attribute.value_required = True
- size_attribute.value_required = True
- Attribute.objects.bulk_update([color_attribute, size_attribute], ["value_required"])
- product_type.product_attributes.add(color_attribute)
- product_type.product_attributes.add(size_attribute)
- return product_type
-
-
-def product_type_list():
- product_type_1 = ProductType.objects.create(
- name="Type 1", slug="type-1", kind=ProductTypeKind.NORMAL
- )
- product_type_2 = ProductType.objects.create(
- name="Type 2", slug="type-2", kind=ProductTypeKind.NORMAL
- )
- product_type_3 = ProductType.objects.create(
- name="Type 3", slug="type-3", kind=ProductTypeKind.NORMAL
- )
- return product_type_1, product_type_2, product_type_3
-
-
-def non_shippable_gift_card_product_type(db):
- product_type = ProductType.objects.create(
- name="Gift card type no shipping",
- slug="gift-card-type-no-shipping",
- kind=ProductTypeKind.GIFT_CARD,
- has_variants=True,
- is_shipping_required=False,
- )
- return product_type
-
-
-def shippable_gift_card_product_type(db):
- product_type = ProductType.objects.create(
- name="Gift card type with shipping",
- slug="gift-card-type-with-shipping",
- kind=ProductTypeKind.GIFT_CARD,
- has_variants=True,
- is_shipping_required=True,
- )
- return product_type
-
-
-def product_type_with_rich_text_attribute(rich_text_attribute):
- product_type = ProductType.objects.create(
- name="Default Type",
- slug="default-type",
- kind=ProductTypeKind.NORMAL,
- has_variants=True,
- is_shipping_required=True,
- )
- product_type.product_attributes.add(rich_text_attribute)
- product_type.variant_attributes.add(rich_text_attribute)
- return product_type
-
-
-def product_type_without_variant():
- product_type = ProductType.objects.create(
- name="Type",
- slug="type",
- has_variants=False,
- is_shipping_required=True,
- kind=ProductTypeKind.NORMAL,
- )
- return product_type
-
-
-def product(product_type, category, warehouse, channel_USD, default_tax_class):
- product_attr = product_type.product_attributes.first()
- product_attr_value = product_attr.values.first()
-
- product = Product.objects.create(
- name="Test product",
- slug="test-product-11",
- product_type=product_type,
- category=category,
- tax_class=default_tax_class,
- )
- ProductChannelListing.objects.create(
- product=product,
- channel=channel_USD,
- is_published=True,
- discounted_price_amount="10.00",
- currency=channel_USD.currency_code,
- visible_in_listings=True,
- available_for_purchase_at=datetime.datetime(1999, 1, 1, tzinfo=datetime.UTC),
- )
-
- associate_attribute_values_to_instance(
- product, {product_attr.pk: [product_attr_value]}
- )
-
- variant_attr = product_type.variant_attributes.first()
- variant_attr_value = variant_attr.values.first()
-
- variant = ProductVariant.objects.create(product=product, sku="123")
- ProductVariantChannelListing.objects.create(
- variant=variant,
- channel=channel_USD,
- price_amount=Decimal(10),
- discounted_price_amount=Decimal(10),
- cost_price_amount=Decimal(1),
- currency=channel_USD.currency_code,
- )
- Stock.objects.create(warehouse=warehouse, product_variant=variant, quantity=10)
-
- associate_attribute_values_to_instance(
- variant, {variant_attr.pk: [variant_attr_value]}
- )
-
- return product
-
-
-def product_with_translations(product):
- product.translations.create(language_code="pl", name="OldProduct PL")
- product.translations.create(language_code="de", name="OldProduct DE")
-
- return product
-
-
-def shippable_gift_card_product(
- shippable_gift_card_product_type, category, warehouse, channel_USD
-):
- product_type = shippable_gift_card_product_type
-
- product = Product.objects.create(
- name="Shippable gift card",
- slug="shippable-gift-card",
- product_type=product_type,
- category=category,
- )
- ProductChannelListing.objects.create(
- product=product,
- channel=channel_USD,
- is_published=True,
- discounted_price_amount="100.00",
- currency=channel_USD.currency_code,
- visible_in_listings=True,
- available_for_purchase_at=datetime.datetime(1999, 1, 1, tzinfo=datetime.UTC),
- )
-
- variant = ProductVariant.objects.create(
- product=product, sku="958", track_inventory=False
- )
- ProductVariantChannelListing.objects.create(
- variant=variant,
- channel=channel_USD,
- price_amount=Decimal(100),
- discounted_price_amount=Decimal(100),
- cost_price_amount=Decimal(1),
- currency=channel_USD.currency_code,
- )
- Stock.objects.create(warehouse=warehouse, product_variant=variant, quantity=1)
-
- return product
-
-
-def product_price_0(category, warehouse, channel_USD):
- product_type = ProductType.objects.create(
- name="Type with no shipping",
- slug="no-shipping",
- has_variants=False,
- is_shipping_required=False,
- )
- product = Product.objects.create(
- name="Test product",
- slug="test-product-4",
- product_type=product_type,
- category=category,
- )
- ProductChannelListing.objects.create(
- product=product,
- channel=channel_USD,
- is_published=True,
- visible_in_listings=True,
- available_for_purchase_at=datetime.datetime(1999, 1, 1, tzinfo=datetime.UTC),
- )
- variant = ProductVariant.objects.create(product=product, sku="SKU_C")
- ProductVariantChannelListing.objects.create(
- variant=variant,
- channel=channel_USD,
- price_amount=Decimal(0),
- discounted_price_amount=Decimal(0),
- cost_price_amount=Decimal(0),
- currency=channel_USD.currency_code,
- )
- Stock.objects.create(product_variant=variant, warehouse=warehouse, quantity=1)
- return product
-
-
-def product_in_channel_JPY(product, channel_JPY, warehouse_JPY):
- ProductChannelListing.objects.create(
- product=product,
- channel=channel_JPY,
- is_published=True,
- discounted_price_amount="1200",
- currency=channel_JPY.currency_code,
- visible_in_listings=True,
- available_for_purchase_at=datetime.datetime(1999, 1, 1, tzinfo=datetime.UTC),
- )
- variant = product.variants.get()
- ProductVariantChannelListing.objects.create(
- variant=variant,
- channel=channel_JPY,
- price_amount=Decimal(1200),
- discounted_price_amount=Decimal(1200),
- cost_price_amount=Decimal(300),
- currency=channel_JPY.currency_code,
- )
- Stock.objects.create(warehouse=warehouse_JPY, product_variant=variant, quantity=10)
- return product
-
-
-def non_shippable_gift_card_product(
- non_shippable_gift_card_product_type, category, warehouse, channel_USD
-):
- product_type = non_shippable_gift_card_product_type
-
- product = Product.objects.create(
- name="Non shippable gift card",
- slug="non-shippable-gift-card",
- product_type=product_type,
- category=category,
- )
- ProductChannelListing.objects.create(
- product=product,
- channel=channel_USD,
- is_published=True,
- discounted_price_amount="200.00",
- currency=channel_USD.currency_code,
- visible_in_listings=True,
- available_for_purchase_at=datetime.datetime(1999, 1, 1, tzinfo=datetime.UTC),
- )
-
- variant = ProductVariant.objects.create(
- product=product, sku="785", track_inventory=False
- )
- ProductVariantChannelListing.objects.create(
- variant=variant,
- channel=channel_USD,
- price_amount=Decimal(250),
- discounted_price_amount=Decimal(250),
- cost_price_amount=Decimal(1),
- currency=channel_USD.currency_code,
- )
- Stock.objects.create(warehouse=warehouse, product_variant=variant, quantity=1)
-
- return product
-
-
-def product_with_rich_text_attribute(
- product_type_with_rich_text_attribute, category, warehouse, channel_USD
-):
- product_attr = product_type_with_rich_text_attribute.product_attributes.first()
- product_attr_value = product_attr.values.first()
-
- product = Product.objects.create(
- name="Test product",
- slug="test-product-11",
- product_type=product_type_with_rich_text_attribute,
- category=category,
- )
- ProductChannelListing.objects.create(
- product=product,
- channel=channel_USD,
- is_published=True,
- discounted_price_amount="10.00",
- currency=channel_USD.currency_code,
- visible_in_listings=True,
- available_for_purchase_at=datetime.datetime(1999, 1, 1, tzinfo=datetime.UTC),
- )
-
- associate_attribute_values_to_instance(
- product, {product_attr.pk: [product_attr_value]}
- )
-
- variant_attr = product_type_with_rich_text_attribute.variant_attributes.first()
- variant_attr_value = variant_attr.values.first()
-
- variant = ProductVariant.objects.create(product=product, sku="123")
- ProductVariantChannelListing.objects.create(
- variant=variant,
- channel=channel_USD,
- price_amount=Decimal(10),
- discounted_price_amount=Decimal(10),
- cost_price_amount=Decimal(1),
- currency=channel_USD.currency_code,
- )
- Stock.objects.create(warehouse=warehouse, product_variant=variant, quantity=10)
-
- associate_attribute_values_to_instance(
- variant, {variant_attr.pk: [variant_attr_value]}
- )
- return [product, variant]
-
-
-def product_with_collections(
- product, published_collection, unpublished_collection, collection
-):
- product.collections.add(*[published_collection, unpublished_collection, collection])
- return product
-
-
-def product_available_in_many_channels(product, channel_PLN, channel_USD):
- ProductChannelListing.objects.create(
- product=product,
- channel=channel_PLN,
- is_published=True,
- )
- variant = product.variants.get()
- ProductVariantChannelListing.objects.create(
- variant=variant,
- channel=channel_PLN,
- price_amount=Decimal(50),
- discounted_price_amount=Decimal(50),
- cost_price_amount=Decimal(1),
- currency=channel_PLN.currency_code,
- )
- return product
-
-
-def product_with_single_variant(product_type, category, warehouse, channel_USD):
- product = Product.objects.create(
- name="Test product with single variant",
- slug="test-product-with-single-variant",
- product_type=product_type,
- category=category,
- )
- ProductChannelListing.objects.create(
- product=product,
- channel=channel_USD,
- is_published=True,
- visible_in_listings=True,
- available_for_purchase_at=datetime.datetime(1999, 1, 1, tzinfo=datetime.UTC),
- )
- variant = ProductVariant.objects.create(product=product, sku="SKU_SINGLE_VARIANT")
- ProductVariantChannelListing.objects.create(
- variant=variant,
- channel=channel_USD,
- price_amount=Decimal(1.99),
- discounted_price_amount=Decimal(10),
- cost_price_amount=Decimal(1),
- currency=channel_USD.currency_code,
- )
- Stock.objects.create(product_variant=variant, warehouse=warehouse, quantity=101)
- return product
-
-
-def product_with_two_variants(product_type, category, warehouse, channel_USD):
- product = Product.objects.create(
- name="Test product with two variants",
- slug="test-product-with-two-variant",
- product_type=product_type,
- category=category,
- )
-
- ProductChannelListing.objects.create(
- product=product,
- channel=channel_USD,
- is_published=True,
- visible_in_listings=True,
- available_for_purchase_at=datetime.datetime(1999, 1, 1, tzinfo=datetime.UTC),
- )
-
- variants = [
- ProductVariant(
- product=product,
- sku=f"Product variant #{i}",
- )
- for i in (1, 2)
- ]
- ProductVariant.objects.bulk_create(variants)
- variants_channel_listing = [
- ProductVariantChannelListing(
- variant=variant,
- channel=channel_USD,
- price_amount=Decimal(10),
- discounted_price_amount=Decimal(10),
- cost_price_amount=Decimal(1),
- currency=channel_USD.currency_code,
- )
- for variant in variants
- ]
- ProductVariantChannelListing.objects.bulk_create(variants_channel_listing)
- Stock.objects.bulk_create(
- [
- Stock(
- warehouse=warehouse,
- product_variant=variant,
- quantity=10,
- )
- for variant in variants
- ]
- )
- product.search_vector = FlatConcatSearchVector(
- *prepare_product_search_vector_value(product)
- )
- product.save(update_fields=["search_vector"])
-
- return product
-
-
-def product_with_variant_with_two_attributes(
- color_attribute, size_attribute, category, warehouse, channel_USD
-):
- product_type = ProductType.objects.create(
- name="Type with two variants",
- slug="two-variants",
- kind=ProductTypeKind.NORMAL,
- has_variants=True,
- is_shipping_required=True,
- )
- product_type.variant_attributes.add(color_attribute)
- product_type.variant_attributes.add(size_attribute)
-
- product = Product.objects.create(
- name="Test product with two variants",
- slug="test-product-with-two-variant",
- product_type=product_type,
- category=category,
- )
- ProductChannelListing.objects.create(
- product=product,
- channel=channel_USD,
- is_published=True,
- currency=channel_USD.currency_code,
- visible_in_listings=True,
- available_for_purchase_at=datetime.datetime(1999, 1, 1, tzinfo=datetime.UTC),
- )
-
- variant = ProductVariant.objects.create(product=product, sku="prodVar1")
- ProductVariantChannelListing.objects.create(
- variant=variant,
- channel=channel_USD,
- price_amount=Decimal(10),
- discounted_price_amount=Decimal(10),
- cost_price_amount=Decimal(1),
- currency=channel_USD.currency_code,
- )
-
- associate_attribute_values_to_instance(
- variant, {color_attribute.pk: [color_attribute.values.first()]}
- )
- associate_attribute_values_to_instance(
- variant, {size_attribute.pk: [size_attribute.values.first()]}
- )
-
- return product
-
-
-def product_with_variant_with_external_media(
- color_attribute,
- size_attribute,
- category,
- warehouse,
- channel_USD,
-):
- product_type = ProductType.objects.create(
- name="Type with two variants",
- slug="two-variants",
- kind=ProductTypeKind.NORMAL,
- has_variants=True,
- is_shipping_required=True,
- )
- product_type.variant_attributes.add(color_attribute)
- product_type.variant_attributes.add(size_attribute)
-
- product = Product.objects.create(
- name="Test product with two variants",
- slug="test-product-with-two-variant",
- product_type=product_type,
- category=category,
- )
- media_obj = ProductMedia.objects.create(
- product=product,
- external_url="https://www.youtube.com/watch?v=di8_dJ3Clyo",
- alt="video_1",
- type=ProductMediaTypes.VIDEO,
- oembed_data="{}",
- )
- product.media.add(media_obj)
-
- ProductChannelListing.objects.create(
- product=product,
- channel=channel_USD,
- is_published=True,
- currency=channel_USD.currency_code,
- visible_in_listings=True,
- available_for_purchase_at=datetime.datetime(1999, 1, 1, tzinfo=datetime.UTC),
- )
-
- variant = ProductVariant.objects.create(product=product, sku="prodVar1")
- variant.media.add(media_obj)
- ProductVariantChannelListing.objects.create(
- variant=variant,
- channel=channel_USD,
- price_amount=Decimal(10),
- discounted_price_amount=Decimal(10),
- cost_price_amount=Decimal(1),
- currency=channel_USD.currency_code,
- )
-
- associate_attribute_values_to_instance(
- variant, {color_attribute.pk: [color_attribute.values.first()]}
- )
- associate_attribute_values_to_instance(
- variant, {size_attribute.pk: [size_attribute.values.first()]}
- )
-
- return product
-
-
-def product_with_variant_with_file_attribute(
- color_attribute, file_attribute, category, warehouse, channel_USD
-):
- product_type = ProductType.objects.create(
- name="Type with variant and file attribute",
- slug="type-with-file-attribute",
- kind=ProductTypeKind.NORMAL,
- has_variants=True,
- is_shipping_required=True,
- )
- product_type.variant_attributes.add(file_attribute)
-
- product = Product.objects.create(
- name="Test product with variant and file attribute",
- slug="test-product-with-variant-and-file-attribute",
- product_type=product_type,
- category=category,
- )
- ProductChannelListing.objects.create(
- product=product,
- channel=channel_USD,
- is_published=True,
- currency=channel_USD.currency_code,
- visible_in_listings=True,
- available_for_purchase_at=datetime.datetime(1999, 1, 1, tzinfo=datetime.UTC),
- )
-
- variant = ProductVariant.objects.create(
- product=product,
- sku="prodVarTest",
- )
- ProductVariantChannelListing.objects.create(
- variant=variant,
- channel=channel_USD,
- price_amount=Decimal(10),
- discounted_price_amount=Decimal(10),
- cost_price_amount=Decimal(1),
- currency=channel_USD.currency_code,
- )
-
- associate_attribute_values_to_instance(
- variant, {file_attribute.pk: [file_attribute.values.first()]}
- )
-
- return product
-
-
-def product_with_multiple_values_attributes(product, product_type) -> Product:
- attribute = Attribute.objects.create(
- slug="modes",
- name="Available Modes",
- input_type=AttributeInputType.MULTISELECT,
- type=AttributeType.PRODUCT_TYPE,
- )
-
- attr_val_1 = AttributeValue.objects.create(
- attribute=attribute, name="Eco Mode", slug="eco"
- )
- attr_val_2 = AttributeValue.objects.create(
- attribute=attribute, name="Performance Mode", slug="power"
- )
-
- product_type.product_attributes.clear()
- product_type.product_attributes.add(attribute)
-
- associate_attribute_values_to_instance(
- product, {attribute.pk: [attr_val_1, attr_val_2]}
- )
- return product
-
-
-def product_with_default_variant(
- product_type_without_variant, category, warehouse, channel_USD
-):
- product = Product.objects.create(
- name="Test product",
- slug="test-product-3",
- product_type=product_type_without_variant,
- category=category,
- )
- ProductChannelListing.objects.create(
- product=product,
- channel=channel_USD,
- is_published=True,
- visible_in_listings=True,
- available_for_purchase_at=datetime.datetime(1999, 1, 1, tzinfo=datetime.UTC),
- )
- variant = ProductVariant.objects.create(
- product=product, sku="1234", track_inventory=True
- )
- ProductVariantChannelListing.objects.create(
- variant=variant,
- channel=channel_USD,
- price_amount=Decimal(10),
- discounted_price_amount=Decimal(10),
- cost_price_amount=Decimal(1),
- currency=channel_USD.currency_code,
- )
- Stock.objects.create(warehouse=warehouse, product_variant=variant, quantity=100)
-
- product.search_vector = FlatConcatSearchVector(
- *prepare_product_search_vector_value(product)
- )
- product.save(update_fields=["search_vector"])
-
- return product
-
-
-def variant_without_inventory_tracking(
- product_type_without_variant, category, warehouse, channel_USD
-):
- product = Product.objects.create(
- name="Test product without inventory tracking",
- slug="test-product-without-tracking",
- product_type=product_type_without_variant,
- category=category,
- )
- ProductChannelListing.objects.create(
- product=product,
- channel=channel_USD,
- is_published=True,
- visible_in_listings=True,
- available_for_purchase_at=datetime.datetime.now(tz=datetime.UTC),
- )
- variant = ProductVariant.objects.create(
- product=product,
- sku="tracking123",
- track_inventory=False,
- )
- ProductVariantChannelListing.objects.create(
- variant=variant,
- channel=channel_USD,
- price_amount=Decimal(10),
- discounted_price_amount=Decimal(10),
- cost_price_amount=Decimal(1),
- currency=channel_USD.currency_code,
- )
- Stock.objects.create(warehouse=warehouse, product_variant=variant, quantity=0)
- return variant
-
-
-def variant(product, channel_USD) -> ProductVariant:
- product_variant = ProductVariant.objects.create(
- product=product, sku="SKU_A", external_reference="SKU_A"
- )
- ProductVariantChannelListing.objects.create(
- variant=product_variant,
- channel=channel_USD,
- price_amount=Decimal(10),
- discounted_price_amount=Decimal(10),
- cost_price_amount=Decimal(1),
- currency=channel_USD.currency_code,
- )
- return product_variant
-
-
-def variant_with_translations(variant):
- variant.translations.create(language_code="pl", name="OldVariant PL")
- variant.translations.create(language_code="de", name="OldVariant DE")
- return variant
-
-
-def variant_with_image(variant, image_list, media_root):
- media = ProductMedia.objects.create(product=variant.product, image=image_list[0])
- VariantMedia.objects.create(variant=variant, media=media)
- return variant
-
-
-def variant_with_many_stocks(variant, warehouses_with_shipping_zone):
- warehouses = warehouses_with_shipping_zone
- Stock.objects.bulk_create(
- [
- Stock(warehouse=warehouses[0], product_variant=variant, quantity=4),
- Stock(warehouse=warehouses[1], product_variant=variant, quantity=3),
- ]
- )
- return variant
-
-
-def variant_on_promotion(
- product, channel_USD, promotion_rule, warehouse
-) -> ProductVariant:
- product_variant = ProductVariant.objects.create(
- product=product, sku="SKU_A", external_reference="SKU_A"
- )
- price_amount = Decimal(10)
- ProductVariantChannelListing.objects.create(
- variant=product_variant,
- channel=channel_USD,
- price_amount=price_amount,
- discounted_price_amount=price_amount,
- cost_price_amount=Decimal(1),
- currency=channel_USD.currency_code,
- )
- Stock.objects.create(
- warehouse=warehouse, product_variant=product_variant, quantity=10
- )
-
- promotion_rule.variants.add(product_variant)
- reward_value = promotion_rule.reward_value
- discount_amount = price_amount * reward_value / 100
-
- variant_channel_listing = product_variant.channel_listings.get(channel=channel_USD)
-
- variant_channel_listing.discounted_price_amount = (
- variant_channel_listing.price_amount - reward_value
- )
- variant_channel_listing.save(update_fields=["discounted_price_amount"])
-
- variant_channel_listing.variantlistingpromotionrule.create(
- promotion_rule=promotion_rule,
- discount_amount=discount_amount,
- currency=channel_USD.currency_code,
- )
-
- return product_variant
-
-
-def preorder_variant_global_threshold(product, channel_USD):
- product_variant = ProductVariant.objects.create(
- product=product, sku="SKU_A_P", is_preorder=True, preorder_global_threshold=10
- )
- ProductVariantChannelListing.objects.create(
- variant=product_variant,
- channel=channel_USD,
- price_amount=Decimal(10),
- discounted_price_amount=Decimal(10),
- cost_price_amount=Decimal(1),
- currency=channel_USD.currency_code,
- )
- return product_variant
-
-
-def preorder_variant_channel_threshold(product, channel_USD):
- product_variant = ProductVariant.objects.create(
- product=product, sku="SKU_B_P", is_preorder=True, preorder_global_threshold=None
- )
- ProductVariantChannelListing.objects.create(
- variant=product_variant,
- channel=channel_USD,
- price_amount=Decimal(10),
- discounted_price_amount=Decimal(10),
- cost_price_amount=Decimal(1),
- currency=channel_USD.currency_code,
- preorder_quantity_threshold=10,
- )
- return product_variant
-
-
-def preorder_variant_global_and_channel_threshold(product, channel_USD, channel_PLN):
- product_variant = ProductVariant.objects.create(
- product=product, sku="SKU_C_P", is_preorder=True, preorder_global_threshold=10
- )
- ProductVariantChannelListing.objects.bulk_create(
- [
- ProductVariantChannelListing(
- variant=product_variant,
- channel=channel_USD,
- cost_price_amount=Decimal(1),
- price_amount=Decimal(10),
- currency=channel_USD.currency_code,
- preorder_quantity_threshold=8,
- ),
- ProductVariantChannelListing(
- variant=product_variant,
- channel=channel_PLN,
- cost_price_amount=Decimal(1),
- price_amount=Decimal(10),
- currency=channel_PLN.currency_code,
- preorder_quantity_threshold=4,
- ),
- ]
- )
- return product_variant
-
-
-def preorder_variant_with_end_date(product, channel_USD):
- product_variant = ProductVariant.objects.create(
- product=product,
- sku="SKU_D_P",
- is_preorder=True,
- preorder_global_threshold=10,
- preorder_end_date=timezone.now() + datetime.timedelta(days=10),
- )
- ProductVariantChannelListing.objects.create(
- variant=product_variant,
- channel=channel_USD,
- price_amount=Decimal(10),
- discounted_price_amount=Decimal(10),
- cost_price_amount=Decimal(1),
- currency=channel_USD.currency_code,
- )
- return product_variant
-
-
-def variant_with_many_stocks_different_shipping_zones(
- variant, warehouses_with_different_shipping_zone
-):
- warehouses = warehouses_with_different_shipping_zone
- Stock.objects.bulk_create(
- [
- Stock(warehouse=warehouses[0], product_variant=variant, quantity=4),
- Stock(warehouse=warehouses[1], product_variant=variant, quantity=3),
- ]
- )
- return variant
-
-
-def gift_card_shippable_variant(shippable_gift_card_product, channel_USD, warehouse):
- product = shippable_gift_card_product
- product_variant = ProductVariant.objects.create(
- product=product, sku="SKU_CARD_A", track_inventory=False
- )
- ProductVariantChannelListing.objects.create(
- variant=product_variant,
- channel=channel_USD,
- price_amount=Decimal(10),
- discounted_price_amount=Decimal(10),
- cost_price_amount=Decimal(1),
- currency=channel_USD.currency_code,
- )
- Stock.objects.create(
- warehouse=warehouse, product_variant=product_variant, quantity=1
- )
- return product_variant
-
-
-def gift_card_non_shippable_variant(
- non_shippable_gift_card_product, channel_USD, warehouse
-):
- product = non_shippable_gift_card_product
- product_variant = ProductVariant.objects.create(
- product=product, sku="SKU_CARD_B", track_inventory=False
- )
- ProductVariantChannelListing.objects.create(
- variant=product_variant,
- channel=channel_USD,
- price_amount=Decimal(10),
- discounted_price_amount=Decimal(10),
- cost_price_amount=Decimal(1),
- currency=channel_USD.currency_code,
- )
- Stock.objects.create(
- warehouse=warehouse, product_variant=product_variant, quantity=1
- )
- return product_variant
-
-
-def product_variant_list(product, channel_USD, channel_PLN):
- variants = list(
- ProductVariant.objects.bulk_create(
- [
- ProductVariant(product=product, sku="1"),
- ProductVariant(product=product, sku="2"),
- ProductVariant(product=product, sku="3"),
- ProductVariant(product=product, sku="4"),
- ]
- )
- )
- ProductVariantChannelListing.objects.bulk_create(
- [
- ProductVariantChannelListing(
- variant=variants[0],
- channel=channel_USD,
- cost_price_amount=Decimal(1),
- price_amount=Decimal(10),
- discounted_price_amount=Decimal(10),
- currency=channel_USD.currency_code,
- ),
- ProductVariantChannelListing(
- variant=variants[1],
- channel=channel_USD,
- cost_price_amount=Decimal(1),
- price_amount=Decimal(10),
- discounted_price_amount=Decimal(10),
- currency=channel_USD.currency_code,
- ),
- ProductVariantChannelListing(
- variant=variants[2],
- channel=channel_PLN,
- cost_price_amount=Decimal(1),
- price_amount=Decimal(10),
- discounted_price_amount=Decimal(10),
- currency=channel_PLN.currency_code,
- ),
- ProductVariantChannelListing(
- variant=variants[3],
- channel=channel_USD,
- cost_price_amount=Decimal(1),
- price_amount=Decimal(10),
- discounted_price_amount=Decimal(10),
- currency=channel_USD.currency_code,
- ),
- ]
- )
- return variants
-
-
-def product_without_shipping(category, warehouse, channel_USD):
- product_type = ProductType.objects.create(
- name="Type with no shipping",
- slug="no-shipping",
- kind=ProductTypeKind.NORMAL,
- has_variants=False,
- is_shipping_required=False,
- )
- product = Product.objects.create(
- name="Test product",
- slug="test-product-4",
- product_type=product_type,
- category=category,
- )
- ProductChannelListing.objects.create(
- product=product,
- channel=channel_USD,
- is_published=True,
- visible_in_listings=True,
- available_for_purchase_at=datetime.datetime(1999, 1, 1, tzinfo=datetime.UTC),
- )
- variant = ProductVariant.objects.create(product=product, sku="SKU_E")
- ProductVariantChannelListing.objects.create(
- variant=variant,
- channel=channel_USD,
- price_amount=Decimal(10),
- discounted_price_amount=Decimal(10),
- cost_price_amount=Decimal(1),
- currency=channel_USD.currency_code,
- )
- Stock.objects.create(product_variant=variant, warehouse=warehouse, quantity=1)
- return product
-
-
-def product_without_category(product):
- product.category = None
- product.save()
- product.channel_listings.all().update(is_published=False)
- return product
-
-
-def product_list(
- product_type, category, warehouse, channel_USD, channel_PLN, default_tax_class
-):
- product_attr = product_type.product_attributes.first()
- attr_value = product_attr.values.first()
-
- products = list(
- Product.objects.bulk_create(
- [
- Product(
- name="Test product 1",
- slug="test-product-a",
- description_plaintext="big blue product",
- category=category,
- product_type=product_type,
- tax_class=default_tax_class,
- ),
- Product(
- name="Test product 2",
- slug="test-product-b",
- description_plaintext="big orange product",
- category=category,
- product_type=product_type,
- tax_class=default_tax_class,
- ),
- Product(
- name="Test product 3",
- slug="test-product-c",
- description_plaintext="small red",
- category=category,
- product_type=product_type,
- tax_class=default_tax_class,
- ),
- ]
- )
- )
- ProductChannelListing.objects.bulk_create(
- [
- ProductChannelListing(
- product=products[0],
- channel=channel_USD,
- is_published=True,
- discounted_price_amount=10,
- currency=channel_USD.currency_code,
- visible_in_listings=True,
- available_for_purchase_at=(
- datetime.datetime(1999, 1, 1, tzinfo=datetime.UTC)
- ),
- ),
- ProductChannelListing(
- product=products[1],
- channel=channel_USD,
- is_published=True,
- discounted_price_amount=20,
- currency=channel_USD.currency_code,
- visible_in_listings=True,
- available_for_purchase_at=(
- datetime.datetime(1999, 1, 1, tzinfo=datetime.UTC)
- ),
- ),
- ProductChannelListing(
- product=products[2],
- channel=channel_USD,
- is_published=True,
- discounted_price_amount=30,
- currency=channel_USD.currency_code,
- visible_in_listings=True,
- available_for_purchase_at=(
- datetime.datetime(1999, 1, 1, tzinfo=datetime.UTC)
- ),
- ),
- ]
- )
- variants = list(
- ProductVariant.objects.bulk_create(
- [
- ProductVariant(
- product=products[0],
- sku=str(uuid.uuid4()).replace("-", ""),
- track_inventory=True,
- ),
- ProductVariant(
- product=products[1],
- sku=str(uuid.uuid4()).replace("-", ""),
- track_inventory=True,
- ),
- ProductVariant(
- product=products[2],
- sku=str(uuid.uuid4()).replace("-", ""),
- track_inventory=True,
- ),
- ]
- )
- )
- ProductVariantChannelListing.objects.bulk_create(
- [
- ProductVariantChannelListing(
- variant=variants[0],
- channel=channel_USD,
- cost_price_amount=Decimal(1),
- price_amount=Decimal(10),
- discounted_price_amount=Decimal(10),
- currency=channel_USD.currency_code,
- ),
- ProductVariantChannelListing(
- variant=variants[1],
- channel=channel_USD,
- cost_price_amount=Decimal(1),
- price_amount=Decimal(20),
- discounted_price_amount=Decimal(20),
- currency=channel_USD.currency_code,
- ),
- ProductVariantChannelListing(
- variant=variants[2],
- channel=channel_USD,
- cost_price_amount=Decimal(1),
- price_amount=Decimal(30),
- discounted_price_amount=Decimal(30),
- currency=channel_USD.currency_code,
- ),
- ]
+ AttributeValue.objects.create(
+ attribute=attribute,
+ name="test_file.txt",
+ slug="test_filetxt",
+ file_url="test_file.txt",
+ content_type="text/plain",
)
- stocks = []
- for variant in variants:
- stocks.append(Stock(warehouse=warehouse, product_variant=variant, quantity=100))
- Stock.objects.bulk_create(stocks)
-
- for product in products:
- associate_attribute_values_to_instance(product, {product_attr.pk: [attr_value]})
- product.search_vector = FlatConcatSearchVector(
- *prepare_product_search_vector_value(product)
- )
-
- Product.objects.bulk_update(products, ["search_vector"])
-
- return products
+ AttributeValue.objects.create(
+ attribute=attribute,
+ name="test_file.jpeg",
+ slug="test_filejpeg",
+ file_url="test_file.jpeg",
+ content_type="image/jpeg",
+ )
+ return attribute
@pytest.fixture
-def product_list_with_variants_many_channel(
- product_type, category, channel_USD, channel_PLN, default_tax_class
-):
- products = list(
- Product.objects.bulk_create(
+def product_type_attribute_list() -> list[Attribute]:
+ return list(
+ Attribute.objects.bulk_create(
[
- Product(
- name="Test product 1",
- slug="test-product-a",
- category=category,
- product_type=product_type,
- tax_class=default_tax_class,
+ Attribute(
+ slug="height", name="Height", type=AttributeType.PRODUCT_TYPE
),
- Product(
- name="Test product 2",
- slug="test-product-b",
- category=category,
- product_type=product_type,
- tax_class=default_tax_class,
+ Attribute(
+ slug="weight", name="Weight", type=AttributeType.PRODUCT_TYPE
),
- Product(
- name="Test product 3",
- slug="test-product-c",
- category=category,
- product_type=product_type,
- tax_class=default_tax_class,
+ Attribute(
+ slug="thickness", name="Thickness", type=AttributeType.PRODUCT_TYPE
),
]
)
)
- ProductChannelListing.objects.bulk_create(
- [
- # Channel: USD
- ProductChannelListing(
- product=products[0],
- channel=channel_USD,
- is_published=True,
- currency=channel_USD.currency_code,
- visible_in_listings=True,
- ),
- # Channel: PLN
- ProductChannelListing(
- product=products[1],
- channel=channel_PLN,
- is_published=True,
- currency=channel_PLN.currency_code,
- visible_in_listings=True,
- ),
- ProductChannelListing(
- product=products[2],
- channel=channel_PLN,
- is_published=True,
- currency=channel_PLN.currency_code,
- visible_in_listings=True,
- ),
- ]
- )
- variants = list(
- ProductVariant.objects.bulk_create(
+
+
+def page_type_attribute_list() -> list[Attribute]:
+ return list(
+ Attribute.objects.bulk_create(
[
- ProductVariant(
- product=products[0],
- sku=str(uuid.uuid4()).replace("-", ""),
- track_inventory=True,
- ),
- ProductVariant(
- product=products[1],
- sku=str(uuid.uuid4()).replace("-", ""),
- track_inventory=True,
- ),
- ProductVariant(
- product=products[2],
- sku=str(uuid.uuid4()).replace("-", ""),
- track_inventory=True,
+ Attribute(slug="size", name="Size", type=AttributeType.PAGE_TYPE),
+ Attribute(slug="font", name="Weight", type=AttributeType.PAGE_TYPE),
+ Attribute(
+ slug="margin", name="Thickness", type=AttributeType.PAGE_TYPE
),
]
)
)
- ProductVariantChannelListing.objects.bulk_create(
- [
- # Channel: USD
- ProductVariantChannelListing(
- variant=variants[0],
- channel=channel_USD,
- cost_price_amount=Decimal(1),
- price_amount=Decimal(10),
- currency=channel_USD.currency_code,
- ),
- # Channel: PLN
- ProductVariantChannelListing(
- variant=variants[1],
- channel=channel_PLN,
- cost_price_amount=Decimal(1),
- price_amount=Decimal(20),
- currency=channel_PLN.currency_code,
- ),
- ProductVariantChannelListing(
- variant=variants[2],
- channel=channel_PLN,
- cost_price_amount=Decimal(1),
- price_amount=Decimal(30),
- currency=channel_PLN.currency_code,
- ),
- ]
- )
- return products
@pytest.fixture
-def product_list_with_many_channels(product_list, channel_PLN):
- ProductChannelListing.objects.bulk_create(
- [
- ProductChannelListing(
- product=product_list[0],
- channel=channel_PLN,
- is_published=True,
- ),
- ProductChannelListing(
- product=product_list[1],
- channel=channel_PLN,
- is_published=True,
- ),
- ProductChannelListing(
- product=product_list[2],
- channel=channel_PLN,
- is_published=True,
- ),
- ]
- )
- return product_list
+def image():
+ img_data = BytesIO()
+ image = Image.new("RGB", size=(1, 1))
+ image.save(img_data, format="JPEG")
+ return SimpleUploadedFile("product.jpg", img_data.getvalue())
@pytest.fixture
-def product_list_unpublished(product_list, channel_USD):
- products = Product.objects.filter(pk__in=[product.pk for product in product_list])
- ProductChannelListing.objects.filter(
- product__in=products, channel=channel_USD
- ).update(is_published=False)
- return products
+def icon_image():
+ img_data = BytesIO()
+ image = Image.new("RGB", size=(1, 1))
+ image.save(img_data, format="PNG")
+ return SimpleUploadedFile("logo.png", img_data.getvalue())
@pytest.fixture
-def product_list_published(product_list, channel_USD):
- products = Product.objects.filter(pk__in=[product.pk for product in product_list])
- ProductChannelListing.objects.filter(
- product__in=products, channel=channel_USD
- ).update(is_published=True)
- return products
+def image_list():
+ img_data_1 = BytesIO()
+ image_1 = Image.new("RGB", size=(1, 1))
+ image_1.save(img_data_1, format="JPEG")
+
+ img_data_2 = BytesIO()
+ image_2 = Image.new("RGB", size=(1, 1))
+ image_2.save(img_data_2, format="JPEG")
+ return [
+ SimpleUploadedFile("image1.jpg", img_data_1.getvalue()),
+ SimpleUploadedFile("image2.jpg", img_data_2.getvalue()),
+ ]
@pytest.fixture
-def product_with_image(product, image, media_root):
- ProductMedia.objects.create(product=product, image=image)
- return product
+def permission_manage_discounts():
+ return Permission.objects.get(codename="manage_discounts")
@pytest.fixture
-def product_with_image_list(product, image_list, media_root):
- ProductMedia.objects.create(product=product, image=image_list[0])
- ProductMedia.objects.create(product=product, image=image_list[1])
- return product
+def permission_manage_gift_card():
+ return Permission.objects.get(codename="manage_gift_card")
@pytest.fixture
-def product_with_image_list_and_one_null_sort_order(product_with_image_list):
- """Return a product with mixed sorting order.
+def permission_manage_orders():
+ return Permission.objects.get(codename="manage_orders")
- As we allow to have `null` in `sort_order` in database, but our logic
- covers changing any new `null` values to proper `int` need to execute
- raw SQL query on database to test behavior of `null` `sort_order`.
- SQL query behavior:
- Updates one of the product media `sort_order` to `null`.
- """
- with connection.cursor() as cursor:
- cursor.execute(
- """
- UPDATE PRODUCT_PRODUCTMEDIA
- SET SORT_ORDER = NULL
- WHERE ID IN (
- SELECT ID FROM PRODUCT_PRODUCTMEDIA
- WHERE PRODUCT_ID = %s
- ORDER BY ID
- LIMIT 1
- )
- """,
- [product_with_image_list.pk],
- )
- product_with_image_list.refresh_from_db()
- return product_with_image_list
+def permission_manage_orders_import():
+ return Permission.objects.get(codename="manage_orders_import")
@pytest.fixture
-def unavailable_product(product_type, category, channel_USD, default_tax_class):
- product = Product.objects.create(
- name="Test product",
- slug="test-product-5",
- product_type=product_type,
- category=category,
- tax_class=default_tax_class,
- )
- ProductChannelListing.objects.create(
- product=product,
- channel=channel_USD,
- is_published=False,
- visible_in_listings=False,
- )
- return product
+def permission_manage_checkouts():
+ return Permission.objects.get(codename="manage_checkouts")
@pytest.fixture
-def unavailable_product_with_variant(
- product_type, category, warehouse, channel_USD, default_tax_class
-):
- product = Product.objects.create(
- name="Test product",
- slug="test-product-6",
- product_type=product_type,
- category=category,
- tax_class=default_tax_class,
- )
- ProductChannelListing.objects.create(
- product=product,
- channel=channel_USD,
- is_published=False,
- visible_in_listings=False,
- )
+def permission_handle_checkouts():
+ return Permission.objects.get(codename="handle_checkouts")
- variant_attr = product_type.variant_attributes.first()
- variant_attr_value = variant_attr.values.first()
- variant = ProductVariant.objects.create(
- product=product,
- sku="123",
- )
- ProductVariantChannelListing.objects.create(
- variant=variant,
- channel=channel_USD,
- price_amount=Decimal(10),
- discounted_price_amount=Decimal(10),
- cost_price_amount=Decimal(1),
- currency=channel_USD.currency_code,
- )
- Stock.objects.create(product_variant=variant, warehouse=warehouse, quantity=10)
+def permission_manage_plugins():
+ return Permission.objects.get(codename="manage_plugins")
- associate_attribute_values_to_instance(
- variant, {variant_attr.pk: [variant_attr_value]}
- )
- return product
+
+def permission_manage_apps():
+ return Permission.objects.get(codename="manage_apps")
@pytest.fixture
-def product_with_images(
- product_type, category, media_root, channel_USD, default_tax_class
-):
- product = Product.objects.create(
- name="Test product",
- slug="test-product-7",
- product_type=product_type,
- category=category,
- tax_class=default_tax_class,
- )
- ProductChannelListing.objects.create(
- product=product,
- channel=channel_USD,
- is_published=True,
- visible_in_listings=True,
- )
- file_mock_0 = MagicMock(spec=File, name="FileMock0")
- file_mock_0.name = "image0.jpg"
- file_mock_1 = MagicMock(spec=File, name="FileMock1")
- file_mock_1.name = "image1.jpg"
- product.media.create(image=file_mock_0)
- product.media.create(image=file_mock_1)
- return product
+def permission_handle_taxes():
+ return Permission.objects.get(codename="handle_taxes")
+
+
+def permission_manage_observability():
+ return Permission.objects.get(codename="manage_observability")
+
+
+def permission_manage_taxes():
+ return Permission.objects.get(codename="manage_taxes")
@pytest.fixture
@@ -5040,147 +3371,6 @@ def shop_permissions(
]
-def collection(db):
- collection = Collection.objects.create(
- name="Collection",
- slug="collection",
- description=dummy_editorjs("Test description."),
- )
- return collection
-
-
-def published_collection(db, channel_USD):
- collection = Collection.objects.create(
- name="Collection USD",
- slug="collection-usd",
- description=dummy_editorjs("Test description."),
- )
- CollectionChannelListing.objects.create(
- channel=channel_USD,
- collection=collection,
- is_published=True,
- published_at=timezone.now(),
- )
- return collection
-
-
-def published_collections(db, channel_USD):
- collections = Collection.objects.bulk_create(
- [
- Collection(
- name="Collection1",
- slug="coll1",
- ),
- Collection(
- name="Collection2",
- slug="coll2",
- ),
- Collection(
- name="Collection3",
- slug="coll3",
- ),
- ]
- )
- CollectionChannelListing.objects.bulk_create(
- [
- CollectionChannelListing(
- channel=channel_USD,
- collection=collection,
- is_published=True,
- published_at=datetime.datetime(2019, 4, 10, tzinfo=datetime.UTC),
- )
- for collection in collections
- ]
- )
-
- return collections
-
-
-def published_collection_PLN(db, channel_PLN):
- collection = Collection.objects.create(
- name="Collection PLN",
- slug="collection-pln",
- description=dummy_editorjs("Test description."),
- )
- CollectionChannelListing.objects.create(
- channel=channel_PLN,
- collection=collection,
- is_published=True,
- published_at=timezone.now(),
- )
- return collection
-
-
-def unpublished_collection(db, channel_USD):
- collection = Collection.objects.create(
- name="Unpublished Collection",
- slug="unpublished-collection",
- description=dummy_editorjs("Test description."),
- )
- CollectionChannelListing.objects.create(
- channel=channel_USD, collection=collection, is_published=False
- )
- return collection
-
-
-def unpublished_collection_PLN(db, channel_PLN):
- collection = Collection.objects.create(
- name="Collection",
- slug="collection",
- description=dummy_editorjs("Test description."),
- )
- CollectionChannelListing.objects.create(
- channel=channel_PLN, collection=collection, is_published=False
- )
- return collection
-
-
-def collection_with_products(db, published_collection, product_list_published):
- published_collection.products.set(list(product_list_published))
- return product_list_published
-
-
-def collection_with_image(db, image, media_root, channel_USD):
- collection = Collection.objects.create(
- name="Collection",
- slug="collection",
- description=dummy_editorjs("Test description."),
- background_image=image,
- )
- CollectionChannelListing.objects.create(
- channel=channel_USD, collection=collection, is_published=False
- )
- return collection
-
-
-def collection_list(db, channel_USD):
- collections = Collection.objects.bulk_create(
- [
- Collection(name="Collection 1", slug="collection-1"),
- Collection(name="Collection 2", slug="collection-2"),
- Collection(name="Collection 3", slug="collection-3"),
- ]
- )
- CollectionChannelListing.objects.bulk_create(
- [
- CollectionChannelListing(
- channel=channel_USD, collection=collection, is_published=True
- )
- for collection in collections
- ]
- )
- return collections
-
-
@pytest.fixture
def translated_attribute(product):
attribute = product.product_type.product_attributes.first()
@@ -5555,60 +3745,6 @@ def transaction_item(order, transaction_item_generator):
)
-def digital_content(category, media_root, warehouse, channel_USD) -> DigitalContent:
- product_type = ProductType.objects.create(
- name="Digital Type",
- slug="digital-type",
- kind=ProductTypeKind.NORMAL,
- has_variants=True,
- is_shipping_required=False,
- is_digital=True,
- )
- product = Product.objects.create(
- name="Test digital product",
- slug="test-digital-product",
- product_type=product_type,
- category=category,
- )
- ProductChannelListing.objects.create(
- product=product,
- channel=channel_USD,
- is_published=True,
- visible_in_listings=True,
- available_for_purchase_at=datetime.datetime(1999, 1, 1, tzinfo=datetime.UTC),
- )
- product_variant = ProductVariant.objects.create(product=product, sku="SKU_554")
- ProductVariantChannelListing.objects.create(
- variant=product_variant,
- channel=channel_USD,
- price_amount=Decimal(10),
- discounted_price_amount=Decimal(10),
- cost_price_amount=Decimal(1),
- currency=channel_USD.currency_code,
- )
- Stock.objects.create(
- product_variant=product_variant,
- warehouse=warehouse,
- quantity=5,
- )
-
- assert product_variant.is_digital()
-
- image_file, image_name = create_image()
- d_content = DigitalContent.objects.create(
- content_file=image_file,
- product_variant=product_variant,
- use_default_settings=True,
- )
- return d_content
-
-
-def digital_content_url(digital_content, order_line):
- return DigitalContentUrl.objects.create(content=digital_content, line=order_line)
-
-
@pytest.fixture
def media_root(tmpdir, settings):
root = str(tmpdir.mkdir("media"))
@@ -6313,17 +4449,6 @@ def success_gateway_response():
)
-def product_media_image(product, image, media_root):
- return ProductMedia.objects.create(
- product=product,
- image=image,
- alt="image",
- type=ProductMediaTypes.IMAGE,
- oembed_data="{}",
- )
-
-
@pytest.fixture
def transaction_session_response():
return {
@@ -6829,62 +4954,6 @@ def async_subscription_webhooks_with_root_objects(
}
-def lots_of_products_with_variants(product_type, channel_USD):
- def chunks(iterable, size):
- it = iter(iterable)
- chunk = tuple(itertools.islice(it, size))
- while chunk:
- yield chunk
- chunk = tuple(itertools.islice(it, size))
-
- variants_per_product = 4
- products_count = 10000
- slug_generator = (f"test-slug-{i}" for i in range(products_count))
-
- for batch in chunks(range(products_count), 500):
- batch_len = len(batch)
- variants = []
- product_listings = []
- products = [
- Product(
- name=i,
- slug=next(slug_generator),
- product_type_id=product_type.pk,
- )
- for i in range(batch_len)
- ]
- for product in Product.objects.bulk_create(products):
- product_listings.append(
- ProductChannelListing(
- channel=channel_USD,
- product=product,
- visible_in_listings=True,
- available_for_purchase_at="2022-01-01",
- currency=channel_USD.currency_code,
- )
- )
- for x in range(variants_per_product):
- variant = ProductVariant(name=x, product_id=product.id)
- variants.append(variant)
- ProductVariant.objects.bulk_create(variants)
- variant_listings = []
- for variant in variants:
- price = random.randint(1, 100)
- variant_listings.append(
- ProductVariantChannelListing(
- variant=variant,
- channel=channel_USD,
- currency=channel_USD.currency_code,
- price_amount=price,
- discounted_price_amount=price,
- )
- )
- ProductVariantChannelListing.objects.bulk_create(variant_listings)
- ProductChannelListing.objects.bulk_create(product_listings)
- return Product.objects.all()
-
-
@pytest.fixture
def setup_mock_for_cache():
"""Mock cache backend.