Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Factories in fixtures (#13153)
Krzysztof Waliczek17d84e4afb9c
diff --git a/saleor/attribute/tests/test_utils.py b/saleor/attribute/tests/test_utils.pyindex d13b09fba5..5e8f6cdaa7 100644--- a/saleor/attribute/tests/test_utils.py+++ b/saleor/attribute/tests/test_utils.py@@ -47,13 +47,19 @@ def test_associate_attribute_to_product_instance_without_values(product):assert new_assignment.values.count() == 0-def test_associate_attribute_to_product_instance_multiple_values(product):+def test_associate_attribute_to_product_instance_multiple_values(+ product, attribute_value_generator+):"""Ensure multiple values in proper order are assigned."""old_assignment = product.attributes.first()assert old_assignment is not None, "The product doesn't have attribute-values"assert old_assignment.values.count() == 1attribute = old_assignment.attribute+ attribute_value_generator(+ attribute=attribute,+ slug="attr-value2",+ )values = attribute.values.all()# Assign new values@@ -91,10 +97,16 @@ def test_associate_attribute_to_page_instance_multiple_values(page):) == [(values[1].pk, 0), (values[0].pk, 1)]-def test_associate_attribute_to_variant_instance_multiple_values(variant):+def test_associate_attribute_to_variant_instance_multiple_values(+ variant, attribute_value_generator+):"""Ensure multiple values in proper order are assigned."""attribute = variant.product.product_type.variant_attributes.first()+ attribute_value_generator(+ attribute=attribute,+ slug="attr-value2",+ )values = attribute.values.all()new_assignment = associate_attribute_values_to_instance(diff --git a/saleor/core/tests/test_search_tasks.py b/saleor/core/tests/test_search_tasks.pyindex 0750688cf8..83b9b1cbbf 100644--- a/saleor/core/tests/test_search_tasks.py+++ b/saleor/core/tests/test_search_tasks.py@@ -1,3 +1,4 @@+from ...core.postgres import FlatConcatSearchVectorfrom ...core.search_tasks import (set_order_search_document_values,set_user_search_document_values,@@ -21,10 +22,10 @@ def test_set_user_search_document_values(customer_user, customer_user2):def test_set_order_search_document_values_already_present(- order_with_search_vector_value,+ order_generator,):# given- order = order_with_search_vector_value+ order = order_generator(search_vector_class=FlatConcatSearchVector)order.refresh_from_db()vector = order.search_vectordiff --git a/saleor/graphql/order/tests/queries/test_order_with_filter.py b/saleor/graphql/order/tests/queries/test_order_with_filter.pyindex a7ffc15774..2194d27fe2 100644--- a/saleor/graphql/order/tests/queries/test_order_with_filter.py+++ b/saleor/graphql/order/tests/queries/test_order_with_filter.py@@ -414,20 +414,17 @@ def test_order_query_with_filter_status(staff_api_client,payment_dummy,permission_group_manage_orders,- order,+ order_generator,channel_USD,):- order.status = status- order.save()-- Order.objects.create(channel=channel_USD)+ order1 = order_generator(status=status)variables = {"filter": orders_filter}permission_group_manage_orders.user_set.add(staff_api_client.user)response = staff_api_client.post_graphql(orders_query_with_filter, variables)content = get_graphql_content(response)orders = content["data"]["orders"]["edges"]- order_id = graphene.Node.to_global_id("Order", order.pk)+ order_id = graphene.Node.to_global_id("Order", order1.pk)orders_ids_from_response = [o["node"]["id"] for o in orders]assert len(orders) == count@@ -768,11 +765,11 @@ def test_orders_query_with_filter_search_by_global_payment_id(def test_orders_query_with_filter_search_by_number(orders_query_with_filter,- order_with_search_vector_value,+ order_generator,staff_api_client,permission_group_manage_orders,):- order = order_with_search_vector_value+ order = order_generator(search_vector_class=FlatConcatSearchVector)variables = {"filter": {"search": order.number}}permission_group_manage_orders.user_set.add(staff_api_client.user)response = staff_api_client.post_graphql(orders_query_with_filter, variables)@@ -782,11 +779,11 @@ def test_orders_query_with_filter_search_by_number(def test_orders_query_with_filter_search_by_number_with_hash(orders_query_with_filter,- order_with_search_vector_value,+ order_generator,staff_api_client,permission_group_manage_orders,):- order = order_with_search_vector_value+ order = order_generator(search_vector_class=FlatConcatSearchVector)variables = {"filter": {"search": f"#{order.number}"}}permission_group_manage_orders.user_set.add(staff_api_client.user)response = staff_api_client.post_graphql(orders_query_with_filter, variables)@@ -1302,13 +1299,18 @@ def test_order_query_with_filter_by_multiple_checkout_tokens(orders_query_with_filter,staff_api_client,permission_group_manage_orders,+ order_generator,orders_from_checkout,- order_from_checkout_JPY,orders,checkout,checkout_JPY,):# given+ order_from_checkout_JPY = order_generator(+ status=OrderStatus.CANCELED,+ channel=checkout_JPY.channel,+ checkout_token=checkout_JPY.token,+ )permission_group_manage_orders.user_set.add(staff_api_client.user)variables = {"filter": {diff --git a/saleor/graphql/order/tests/queries/test_pagination.py b/saleor/graphql/order/tests/queries/test_pagination.pyindex 2d3b1fe1ed..dc6008abc6 100644--- a/saleor/graphql/order/tests/queries/test_pagination.py+++ b/saleor/graphql/order/tests/queries/test_pagination.py@@ -570,9 +570,9 @@ def test_draft_orders_query_pagination_with_filter_search(def test_orders_query_pagination_with_filter_search_by_number(- order_with_search_vector_value, staff_api_client, permission_group_manage_orders+ order_generator, staff_api_client, permission_group_manage_orders):- order = order_with_search_vector_value+ order = order_generator(search_vector_class=FlatConcatSearchVector)page_size = 2variables = {"first": page_size, "after": None, "filter": {"search": order.number}}permission_group_manage_orders.user_set.add(staff_api_client.user)diff --git a/saleor/graphql/product/tests/mutations/test_product_type_create.py b/saleor/graphql/product/tests/mutations/test_product_type_create.pyindex e79823dc5d..b9961f70c8 100644--- a/saleor/graphql/product/tests/mutations/test_product_type_create.py+++ b/saleor/graphql/product/tests/mutations/test_product_type_create.py@@ -264,16 +264,6 @@ def test_create_product_type_with_rich_text_attribute("dateTime": None,}},- {- "node": {- "name": "Blue",- "richText": None,- "plainText": None,- "boolean": None,- "date": None,- "dateTime": None,- }- },]},},diff --git a/saleor/graphql/product/tests/mutations/test_product_update.py b/saleor/graphql/product/tests/mutations/test_product_update.pyindex 3e9fa00e82..1fb2ef553b 100644--- a/saleor/graphql/product/tests/mutations/test_product_update.py+++ b/saleor/graphql/product/tests/mutations/test_product_update.py@@ -2332,7 +2332,6 @@ def test_update_product_with_dropdown_attribute_non_existing_value(def test_update_product_with_dropdown_attribute_existing_value(updated_webhook_mock,staff_api_client,- color_attribute,product,product_type,permission_manage_products,@@ -2342,14 +2341,15 @@ def test_update_product_with_dropdown_attribute_existing_value(query = MUTATION_UPDATE_PRODUCTproduct_id = graphene.Node.to_global_id("Product", product.pk)+ attribute = product_type.product_attributes.first()- attribute_id = graphene.Node.to_global_id("Attribute", color_attribute.pk)- attribute_value = color_attribute.values.model.objects.first()+ attribute_id = graphene.Node.to_global_id("Attribute", attribute.pk)+ attribute_value = attribute.values.model.objects.first()attribute_value_id = graphene.Node.to_global_id("AttributeValue", attribute_value.pk)- attribute_value_name = color_attribute.values.model.objects.first().name- product_type.product_attributes.add(color_attribute)+ attribute_value_name = attribute.values.model.objects.first().name+ product_type.product_attributes.add(attribute)variables = {"productId": product_id,@@ -2384,7 +2384,6 @@ def test_update_product_with_dropdown_attribute_existing_value(def test_update_product_with_dropdown_attribute_existing_value_passed_as_new_value(updated_webhook_mock,staff_api_client,- color_attribute,product,product_type,permission_manage_products,@@ -2394,13 +2393,14 @@ def test_update_product_with_dropdown_attribute_existing_value_passed_as_new_valquery = MUTATION_UPDATE_PRODUCTproduct_id = graphene.Node.to_global_id("Product", product.pk)- attribute_id = graphene.Node.to_global_id("Attribute", color_attribute.pk)- attribute_value = color_attribute.values.model.objects.first()+ attribute = product_type.product_attributes.first()+ attribute_id = graphene.Node.to_global_id("Attribute", attribute.pk)+ attribute_value = attribute.values.model.objects.first()attribute_value_id = graphene.Node.to_global_id("AttributeValue", attribute_value.pk)- attribute_value_name = color_attribute.values.model.objects.first().name- product_type.product_attributes.add(color_attribute)+ attribute_value_name = attribute.values.model.objects.first().name+ product_type.product_attributes.add(attribute)value_count = AttributeValue.objects.count()diff --git a/saleor/graphql/translations/tests/test_translations.py b/saleor/graphql/translations/tests/test_translations.pyindex f793662ef5..2f7362ff4b 100644--- a/saleor/graphql/translations/tests/test_translations.py+++ b/saleor/graphql/translations/tests/test_translations.py@@ -410,7 +410,13 @@ def test_attribute_translation(user_api_client, color_attribute):assert attribute["translation"]["language"]["code"] == "PL"-def test_attribute_value_translation(user_api_client, pink_attribute_value):+def test_attribute_value_translation(user_api_client, attribute_value_generator):+ # given+ pink_attribute_value = attribute_value_generator(+ slug="pink",+ name="Pink",+ value="#FF69B4",+ )pink_attribute_value.translations.create(language_code="pl", name="Różowy", rich_text=dummy_editorjs("Pink"))@@ -442,11 +448,14 @@ def test_attribute_value_translation(user_api_client, pink_attribute_value):attribute_value_id = graphene.Node.to_global_id("AttributeValue", pink_attribute_value.id)++ # whenresponse = user_api_client.post_graphql(query, {"attributeValueId": attribute_value_id})data = get_graphql_content(response)["data"]+ # thenattribute_value = data["attributes"]["edges"][0]["node"]["choices"]["edges"][-1]["node"]diff --git a/saleor/tests/fixtures.py b/saleor/tests/fixtures.pyindex f0c3f099ea..d3800e07e3 100644--- a/saleor/tests/fixtures.py+++ b/saleor/tests/fixtures.py@@ -1023,19 +1023,10 @@ def orders_from_checkout(customer_user, checkout):@pytest.fixture-def order_from_checkout_JPY(customer_user, checkout_JPY):- return Order.objects.create(- user=customer_user,- status=OrderStatus.CANCELED,- channel=checkout_JPY.channel,- checkout_token=checkout_JPY.token,- )---def order(customer_user, channel_USD):+def order_generator(customer_user, channel_USD):address = customer_user.default_billing_address.get_copy()- order = Order.objects.create(++ def create_order(billing_address=address,channel=channel_USD,currency=channel_USD.currency_code,@@ -1046,31 +1037,38 @@ def order(customer_user, channel_USD):should_refresh_prices=False,metadata={"key": "value"},private_metadata={"secret_key": "secret_value"},- )- return order+ checkout_token="",+ status=OrderStatus.UNFULFILLED,+ search_vector_class=None,+ ):+ order = Order.objects.create(+ billing_address=billing_address,+ channel=channel,+ currency=currency,+ shipping_address=shipping_address,+ user_email=user_email,+ user=user,+ origin=origin,+ should_refresh_prices=should_refresh_prices,+ metadata=metadata,+ private_metadata=private_metadata,+ checkout_token=checkout_token,+ status=status,+ )+ if search_vector_class:+ search_vector = search_vector_class(+ *prepare_order_search_vector_value(order)+ )+ order.search_vector = search_vector+ order.save(update_fields=["search_vector"])+ return order--def order_with_search_vector_value(order):- order.search_vector = FlatConcatSearchVector(- *prepare_order_search_vector_value(order)- )- order.save(update_fields=["search_vector"])- return order+ return create_order@pytest.fixture-def order_JPY(customer_user, channel_JPY):- address = customer_user.default_billing_address.get_copy()- return Order.objects.create(- billing_address=address,- channel=channel_JPY,- currency=channel_JPY.currency_code,- shipping_address=address,- user_email=customer_user.email,- user=customer_user,- origin=OrderOrigin.CHECKOUT,- )+def order(order_generator):+ return order_generator()@pytest.fixture@@ -1080,6 +1078,74 @@ def order_unconfirmed(order):return order+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.fixturedef admin_user(db):"""Return a Django admin user."""@@ -1376,8 +1442,103 @@ def shipping_method_channel_PLN(shipping_zone, channel_PLN):@pytest.fixture-def color_attribute(db):- attribute = Attribute.objects.create(+def attribute_generator():+ def create_attribute(+ external_reference="attributeExtRef",+ slug="attr",+ name="Attr",+ type=AttributeType.PRODUCT_TYPE,+ filterable_in_storefront=True,+ filterable_in_dashboard=True,+ available_in_grid=True,+ ):+ attribute, _ = Attribute.objects.get_or_create(+ external_reference=external_reference,+ slug=slug,+ name=name,+ type=type,+ filterable_in_storefront=filterable_in_storefront,+ filterable_in_dashboard=filterable_in_dashboard,+ available_in_grid=available_in_grid,+ )++ return attribute++ return create_attribute+++def attribute_value_generator(attribute_generator):+ def create_attribute_value(+ attribute=None,+ external_reference=None,+ name="Attr Value",+ slug="attr-value",+ value="",+ ):+ if attribute is None:+ attribute = attribute_generator()+ attribute_value, _ = AttributeValue.objects.get_or_create(+ attribute=attribute,+ external_reference=external_reference,+ name=name,+ slug=slug,+ value=value,+ )++ return attribute_value++ return create_attribute_value+++def attribute_values_generator(attribute_generator):+ def create_attribute_values(+ external_references=None,+ names=None,+ slugs=None,+ attribute=None,+ values=None,+ ):+ if attribute is None:+ attribute = attribute_generator()++ if slugs is None:+ slugs = ["attr-value"]++ if external_references is None:+ external_references = [None] * len(slugs)++ if names is None:+ names = [""] * len(slugs)++ if values is None:+ values = [""] * len(slugs)++ AttributeValue.objects.bulk_create(+ [+ AttributeValue(+ attribute=attribute,+ external_reference=ext_ref,+ name=name,+ slug=slug,+ value=value,+ )+ for slug, name, ext_ref, value in zip(+ slugs, names, external_references, values+ )+ ],+ ignore_conflicts=True,+ )++ return list(AttributeValue.objects.filter(slug__in=slugs))++ return create_attribute_values+++def color_attribute(db, attribute_generator, attribute_values_generator):+ attribute = attribute_generator(external_reference="colorAttributeExternalReference",slug="color",name="Color",@@ -1386,18 +1547,19 @@ def color_attribute(db):filterable_in_dashboard=True,available_in_grid=True,)- AttributeValue.objects.create(- external_reference="colorAttributeValue1ExternalReference",- attribute=attribute,- name="Red",- slug="red",- )- AttributeValue.objects.create(- external_reference="colorAttributeValue2ExternalReference",+ external_references = [+ "colorAttributeValue1ExternalReference",+ "colorAttributeValue2ExternalReference",+ ]+ slugs = ["red", "blue"]+ names = ["Red", "Blue"]+ attribute_values_generator(attribute=attribute,- name="Blue",- slug="blue",+ external_references=external_references,+ names=names,+ slugs=slugs,)+return attribute@@ -1711,8 +1873,10 @@ def pink_attribute_value(color_attribute): # pylint: disable=W0613@pytest.fixture-def size_attribute(db): # pylint: disable=W0613- attribute = Attribute.objects.create(+def size_attribute(+ db, attribute_generator, attribute_values_generator+): # pylint: disable=W0613+ attribute = attribute_generator(external_reference="sizeAttributeExternalReference",slug="size",name="Size",@@ -1721,8 +1885,15 @@ def size_attribute(db): # pylint: disable=W0613filterable_in_dashboard=True,available_in_grid=True,)- AttributeValue.objects.create(attribute=attribute, name="Small", slug="small")- AttributeValue.objects.create(attribute=attribute, name="Big", slug="big")++ slugs = ["small", "big"]+ names = ["Small", "Big"]+ attribute_values_generator(+ attribute=attribute,+ names=names,+ slugs=slugs,+ )+return attribute@@ -2039,8 +2210,23 @@ def image_list():@pytest.fixture-def category(db): # pylint: disable=W0613- return Category.objects.create(name="Default", slug="default")+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@@ -2189,20 +2375,8 @@ def permission_manage_taxes():@pytest.fixture-def product_type(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,- )- product_type.product_attributes.add(color_attribute)- product_type.variant_attributes.add(- size_attribute, through_defaults={"variant_selection": True}- )- return product_type+def product_type(product_type_generator):+ return product_type_generator()@pytest.fixture@@ -3749,7 +3923,11 @@ def gift_card_shippable_order_line(order, gift_card_shippable_variant, warehouse@pytest.fixture-def order_line_JPY(order_JPY, product_in_channel_JPY):+def order_line_JPY(order_generator, channel_JPY, product_in_channel_JPY):+ order_JPY = order_generator(+ channel=channel_JPY,+ currency=channel_JPY.currency_code,+ )product = product_in_channel_JPYvariant = product_in_channel_JPY.variants.get()channel = order_JPY.channel