Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
add benchmarks
Damian W605943e0ba3b
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex e5dc539a58..fcd413cd7a 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -30,6 +30,7 @@ All notable, unreleased changes to this project will be documented in this file.- Optimize children field on Category type - #7045 by @IKarbowiak- Added support for querying objects by metadata fields - #6683 by @LeOndaz- Avoid using `get_plugins_manager` method - #7052 by @IKarbowiak+- Add benchmarks for triggered product and variants webhooks - #7061 by @d-wysocki### Breaking- Multichannel MVP: Multicurrency - #6242 by @fowczarek @d-wysocki@@ -65,6 +66,8 @@ All notable, unreleased changes to this project will be documented in this file.- Remove triggering a webhook event `PRODUCT_UPDATED` when calling `ProductVariantCreate` mutation. Use `PRODUCT_VARIANT_CREATED` instead - #6963 by @piotrgrundas- Remove triggering a webhook event `PRODUCT_UPDATED` when calling `ProductVariantChannelListingUpdate` mutation. Use `PRODUCT_VARIANT_UPDATED` instead - #6963 by @piotrgrundas- Refactor listing payment gateways - #7050 by @maarcingebala. Breaking changes in plugin methods: removed `get_payment_gateway` and `get_payment_gateway_for_checkout`; instead `get_payment_gateways` was added.+- Change error class in `CollectionBulkDelete` to `CollectionErrors` - #7061 by @d-wysocki+### Otherdiff --git a/saleor/graphql/product/bulk_mutations/products.py b/saleor/graphql/product/bulk_mutations/products.pyindex 259fbb969e..a3cee95198 100644--- a/saleor/graphql/product/bulk_mutations/products.py+++ b/saleor/graphql/product/bulk_mutations/products.py@@ -21,6 +21,7 @@ from ...core.mutations import BaseMutation, ModelBulkDeleteMutation, ModelMutatifrom ...core.types.common import (BulkProductError,BulkStockError,+ CollectionError,ProductError,StockError,)@@ -67,8 +68,8 @@ class CollectionBulkDelete(ModelBulkDeleteMutation):description = "Deletes collections."model = models.Collectionpermissions = (ProductPermissions.MANAGE_PRODUCTS,)- error_type_class = ProductError- error_type_field = "product_errors"+ error_type_class = CollectionError+ error_type_field = "collection_errors"class ProductBulkDelete(ModelBulkDeleteMutation):@@ -384,7 +385,6 @@ class ProductVariantBulkCreate(BaseMutation):ValidationError(exc.message, exc.code, params={"index": index}))- cleaned_input = Nonevariant_data["product_type"] = product.product_typevariant_data["product"] = productcleaned_input = cls.clean_variant_input(diff --git a/saleor/graphql/product/mutations/products.py b/saleor/graphql/product/mutations/products.pyindex fbe196f748..236a9c0c08 100644--- a/saleor/graphql/product/mutations/products.py+++ b/saleor/graphql/product/mutations/products.py@@ -593,7 +593,6 @@ class ProductCreate(ModelMutation):"""Prefetch related fields that are needed to process the mutation."""# If we are updating an instance and want to update its attributes,# prefetch them.-object_id = data.get("id")if object_id and data.get("attributes"):# Prefetches needed by AttributeAssignmentMixin and@@ -603,7 +602,6 @@ class ProductCreate(ModelMutation):"product_type__attributeproduct",)return cls.get_node_or_error(info, object_id, only_type="Product", qs=qs)-return super().get_instance(info, **data)@classmethoddiff --git a/saleor/graphql/product/tests/benchmark/test_category.py b/saleor/graphql/product/tests/benchmark/test_category.pyindex b168e4b679..4cf652ca32 100644--- a/saleor/graphql/product/tests/benchmark/test_category.py+++ b/saleor/graphql/product/tests/benchmark/test_category.py@@ -157,3 +157,36 @@ def test_categories_children(api_client, categories_with_children, count_queriescontent = get_graphql_content(api_client.post_graphql(query))assert content["data"]["categories"] is not None++[email protected]_queries(autouse=False)+def test_category_delete(+ staff_api_client,+ category_with_products,+ permission_manage_products,+ settings,+ count_queries,+):+ query = """+ mutation($id: ID!) {+ categoryDelete(id: $id) {+ category {+ name+ }+ productErrors {+ field+ message+ }+ }+ }+ """+ settings.PLUGINS = ["saleor.plugins.webhook.plugin.WebhookPlugin"]+ category = category_with_products+ variables = {"id": graphene.Node.to_global_id("Category", category.id)}+ response = staff_api_client.post_graphql(+ query, variables, permissions=[permission_manage_products]+ )+ content = get_graphql_content(response)+ errors = content["data"]["categoryDelete"]["productErrors"]+ assert not errorsdiff --git a/saleor/graphql/product/tests/benchmark/test_collection.py b/saleor/graphql/product/tests/benchmark/test_collection.pyindex 5e86b4dd3c..c73787666c 100644--- a/saleor/graphql/product/tests/benchmark/test_collection.py+++ b/saleor/graphql/product/tests/benchmark/test_collection.py@@ -1,5 +1,6 @@import grapheneimport pytest+from graphql_relay import to_global_idfrom ....tests.utils import get_graphql_content@@ -151,3 +152,224 @@ def test_retrieve_collection_channel_listings(check_no_permissions=False,))++[email protected]_queries(autouse=False)+def test_create_collection(+ settings,+ staff_api_client,+ product_list_with_many_channels,+ permission_manage_products,+ count_queries,+):+ query = """+ mutation createCollection(+ $name: String!, $slug: String,+ $description: JSONString, $products: [ID],+ $backgroundImage: Upload, $backgroundImageAlt: String) {+ collectionCreate(+ input: {+ name: $name,+ slug: $slug,+ description: $description,+ products: $products,+ backgroundImage: $backgroundImage,+ backgroundImageAlt: $backgroundImageAlt}) {+ collection {+ name+ slug+ description+ products {+ totalCount+ }+ backgroundImage{+ alt+ }+ }+ collectionErrors {+ field+ message+ code+ }+ }+ }+ """+ settings.PLUGINS = ["saleor.plugins.webhook.plugin.WebhookPlugin"]+ product_ids = [+ to_global_id("Product", product.pk)+ for product in product_list_with_many_channels+ ]+ name = "test-name"+ slug = "test-slug"+ variables = {+ "name": name,+ "slug": slug,+ "products": product_ids,+ }++ response = staff_api_client.post_graphql(+ query, variables, permissions=[permission_manage_products]+ )+ content = get_graphql_content(response)+ errors = content["data"]["collectionCreate"]["collectionErrors"]+ assert not errors++[email protected]_queries(autouse=False)+def test_delete_collection(+ settings,+ staff_api_client,+ collection_with_products,+ permission_manage_products,+ count_queries,+):+ query = """+ mutation deleteCollection($id: ID!) {+ collectionDelete(id: $id) {+ collection {+ name+ }+ collectionErrors {+ field+ message+ code+ }+ }+ }+ """+ collection = collection_with_products[0].collections.first()+ settings.PLUGINS = ["saleor.plugins.webhook.plugin.WebhookPlugin"]+ collection_id = to_global_id("Collection", collection.id)++ variables = {+ "id": collection_id,+ }++ response = staff_api_client.post_graphql(+ query, variables, permissions=[permission_manage_products]+ )+ content = get_graphql_content(response)+ errors = content["data"]["collectionDelete"]["collectionErrors"]+ assert not errors++[email protected]_queries(autouse=False)+def test_collection_add_products(+ staff_api_client,+ collection,+ product_list,+ permission_manage_products,+ settings,+ count_queries,+):+ query = """+ mutation collectionAddProducts(+ $id: ID!, $products: [ID]!) {+ collectionAddProducts(collectionId: $id, products: $products) {+ collection {+ products {+ totalCount+ }+ }+ collectionErrors {+ field+ message+ code+ }+ }+ }+ """+ settings.PLUGINS = ["saleor.plugins.webhook.plugin.WebhookPlugin"]++ collection_id = to_global_id("Collection", collection.id)+ product_ids = [to_global_id("Product", product.pk) for product in product_list]+ variables = {"id": collection_id, "products": product_ids}+ response = staff_api_client.post_graphql(+ query, variables, permissions=[permission_manage_products]+ )+ content = get_graphql_content(response)+ errors = content["data"]["collectionAddProducts"]["collectionErrors"]+ assert not errors++[email protected]_queries(autouse=False)+def test_remove_products_from_collection(+ staff_api_client,+ collection_with_products,+ permission_manage_products,+ settings,+ count_queries,+):+ query = """+ mutation collectionRemoveProducts(+ $id: ID!, $products: [ID]!) {+ collectionRemoveProducts(collectionId: $id, products: $products) {+ collection {+ products {+ totalCount+ }+ }+ collectionErrors {+ field+ message+ code+ }+ }+ }+ """+ settings.PLUGINS = ["saleor.plugins.webhook.plugin.WebhookPlugin"]++ collection = collection_with_products[0].collections.first()+ collection_id = to_global_id("Collection", collection.id)+ product_ids = [+ to_global_id("Product", product.pk) for product in collection_with_products+ ]+ variables = {"id": collection_id, "products": product_ids}+ response = staff_api_client.post_graphql(+ query, variables, permissions=[permission_manage_products]+ )++ content = get_graphql_content(response)+ errors = content["data"]["collectionRemoveProducts"]["collectionErrors"]+ assert not errors+++def test_collection_bulk_delete(+ staff_api_client,+ collection_list,+ product_list,+ permission_manage_products,+ count_queries,+ settings,+):+ query = """+ mutation collectionBulkDelete($ids: [ID]!) {+ collectionBulkDelete(ids: $ids) {+ count+ collectionErrors {+ field+ message+ code+ }+ }+ }+ """+ for collection in collection_list:+ collection.products.add(*product_list)+ settings.PLUGINS = ["saleor.plugins.webhook.plugin.WebhookPlugin"]+ variables = {+ "ids": [+ graphene.Node.to_global_id("Collection", collection.id)+ for collection in collection_list+ ]+ }+ response = staff_api_client.post_graphql(+ query, variables, permissions=[permission_manage_products]+ )+ content = get_graphql_content(response)++ assert not content["data"]["collectionBulkDelete"]["collectionErrors"]diff --git a/saleor/graphql/product/tests/benchmark/test_product.py b/saleor/graphql/product/tests/benchmark/test_product.pyindex e98d20dca0..75d4d3e396 100644--- a/saleor/graphql/product/tests/benchmark/test_product.py+++ b/saleor/graphql/product/tests/benchmark/test_product.py@@ -1,6 +1,11 @@+import json++import grapheneimport pytestfrom graphene import Node+from .....core.taxes import TaxType+from .....plugins.manager import PluginsManagerfrom ....tests.utils import get_graphql_content@@ -351,3 +356,212 @@ def test_retrive_products_with_product_types_and_attributes("""variables = {"channel": channel_USD.slug}get_graphql_content(api_client.post_graphql(query, variables))++[email protected]_queries(autouse=False)+def test_product_create(+ settings,+ staff_api_client,+ product_type,+ category,+ size_attribute,+ description_json,+ permission_manage_products,+ monkeypatch,+ count_queries,+):+ query = """+ mutation createProduct($input: ProductCreateInput!) {+ productCreate(input: $input) {+ product {+ id+ category {+ name+ }+ description+ chargeTaxes+ taxType {+ taxCode+ description+ }+ name+ slug+ rating+ productType {+ name+ }+ attributes {+ attribute {+ slug+ }+ values {+ slug+ name+ reference+ file {+ url+ contentType+ }+ }+ }+ }+ productErrors {+ field+ code+ message+ attributes+ }+ }+ }+"""+ settings.PLUGINS = ["saleor.plugins.webhook.plugin.WebhookPlugin"]+ description_json = json.dumps(description_json)++ product_type_id = graphene.Node.to_global_id("ProductType", product_type.pk)+ category_id = graphene.Node.to_global_id("Category", category.pk)+ product_name = "test name"+ product_slug = "product-test-slug"+ product_charge_taxes = True+ product_tax_rate = "STANDARD"++ # Mock tax interface with fake response from tax gateway+ monkeypatch.setattr(+ PluginsManager,+ "get_tax_code_from_object_meta",+ lambda self, x: TaxType(description="", code=product_tax_rate),+ )++ # Default attribute defined in product_type fixture+ color_attr = product_type.product_attributes.get(name="Color")+ color_value_slug = color_attr.values.first().slug+ color_attr_id = graphene.Node.to_global_id("Attribute", color_attr.id)++ # Add second attribute+ product_type.product_attributes.add(size_attribute)+ size_attr_id = graphene.Node.to_global_id("Attribute", size_attribute.id)+ non_existent_attr_value = "The cake is a lie"++ # test creating root product+ variables = {+ "input": {+ "productType": product_type_id,+ "category": category_id,+ "name": product_name,+ "slug": product_slug,+ "description": description_json,+ "chargeTaxes": product_charge_taxes,+ "taxCode": product_tax_rate,+ "attributes": [+ {"id": color_attr_id, "values": [color_value_slug]},+ {"id": size_attr_id, "values": [non_existent_attr_value]},+ ],+ }+ }++ response = staff_api_client.post_graphql(+ query, variables, permissions=[permission_manage_products]+ )+ content = get_graphql_content(response)+ assert not content["data"]["productCreate"]["productErrors"]++[email protected]_queries(autouse=False)+def test_update_product(+ settings,+ staff_api_client,+ category,+ non_default_category,+ collection_list,+ product_with_variant_with_two_attributes,+ other_description_json,+ permission_manage_products,+ monkeypatch,+ count_queries,+):+ query = """+ mutation updateProduct($productId: ID!, $input: ProductInput!) {+ productUpdate(id: $productId, input: $input) {+ product {+ category {+ name+ }+ rating+ description+ chargeTaxes+ variants {+ name+ }+ taxType {+ taxCode+ description+ }+ name+ slug+ productType {+ name+ }+ attributes {+ attribute {+ id+ name+ }+ values {+ id+ name+ slug+ reference+ file {+ url+ contentType+ }+ }+ }+ }+ productErrors {+ message+ field+ code+ }+ }+ }+ """+ product = product_with_variant_with_two_attributes+ for collection in collection_list:+ collection.products.add(product)+ other_description_json = json.dumps(other_description_json)+ settings.PLUGINS = ["saleor.plugins.webhook.plugin.WebhookPlugin"]++ product_id = graphene.Node.to_global_id("Product", product.pk)+ category_id = graphene.Node.to_global_id("Category", non_default_category.pk)+ product_name = "updated name"+ product_slug = "updated-product"+ product_charge_taxes = True+ product_tax_rate = "STANDARD"++ # Mock tax interface with fake response from tax gateway+ monkeypatch.setattr(+ PluginsManager,+ "get_tax_code_from_object_meta",+ lambda self, x: TaxType(description="", code=product_tax_rate),+ )++ variables = {+ "productId": product_id,+ "input": {+ "category": category_id,+ "name": product_name,+ "slug": product_slug,+ "description": other_description_json,+ "chargeTaxes": product_charge_taxes,+ "taxCode": product_tax_rate,+ },+ }++ response = staff_api_client.post_graphql(+ query, variables, permissions=[permission_manage_products]+ )+ content = get_graphql_content(response)+ data = content["data"]["productUpdate"]+ assert not data["productErrors"]diff --git a/saleor/graphql/product/tests/benchmark/test_product_variant_channel_listing_update.py b/saleor/graphql/product/tests/benchmark/test_product_variant_channel_listing_update.pynew file mode 100644index 0000000000..5450add997--- /dev/null+++ b/saleor/graphql/product/tests/benchmark/test_product_variant_channel_listing_update.py@@ -0,0 +1,93 @@+import graphene+import pytest++from .....product.models import ProductChannelListing, ProductMedia, VariantMedia+from ....tests.utils import get_graphql_content++[email protected]_queries(autouse=False)+def test_variant_channel_listing_update(+ staff_api_client,+ settings,+ product_with_variant_with_two_attributes,+ permission_manage_products,+ channel_USD,+ channel_PLN,+ image,+ media_root,+ count_queries,+):+ # given+ query = """+ mutation UpdateProductVariantChannelListing(+ $id: ID!,+ $input: [ProductVariantChannelListingAddInput!]!+ ) {+ productVariantChannelListingUpdate(id: $id, input: $input) {+ productChannelListingErrors {+ field+ message+ code+ channels+ }+ variant {+ id+ channelListings {+ channel {+ id+ slug+ currencyCode+ }+ price {+ amount+ currency+ }+ costPrice {+ amount+ currency+ }+ margin+ }+ }+ }+ }+ """+ settings.PLUGINS = ["saleor.plugins.webhook.plugin.WebhookPlugin"]+ product = product_with_variant_with_two_attributes+ ProductChannelListing.objects.create(+ product=product,+ channel=channel_PLN,+ is_published=True,+ )+ variant = product.variants.get()+ product_image = ProductMedia.objects.create(product=product, image=image)+ VariantMedia.objects.create(variant=variant, media=product_image)+ variant_id = graphene.Node.to_global_id("ProductVariant", variant.id)+ channel_usd_id = graphene.Node.to_global_id("Channel", channel_USD.id)+ channel_pln_id = graphene.Node.to_global_id("Channel", channel_PLN.id)+ price = 1+ second_price = 20+ variables = {+ "id": variant_id,+ "input": [+ {"channelId": channel_usd_id, "price": price, "costPrice": price},+ {+ "channelId": channel_pln_id,+ "price": second_price,+ "costPrice": second_price,+ },+ ],+ }++ # when+ response = staff_api_client.post_graphql(+ query,+ variables=variables,+ permissions=(permission_manage_products,),+ )+ content = get_graphql_content(response)++ # then+ data = content["data"]["productVariantChannelListingUpdate"]+ assert not data["productChannelListingErrors"]diff --git a/saleor/graphql/product/tests/benchmark/test_variant.py b/saleor/graphql/product/tests/benchmark/test_variant.pyindex 0b4357273d..7f2fd92128 100644--- a/saleor/graphql/product/tests/benchmark/test_variant.py+++ b/saleor/graphql/product/tests/benchmark/test_variant.py@@ -3,7 +3,7 @@ from uuid import uuid4import grapheneimport pytest-from .....product.models import ProductVariant+from .....product.models import ProductMedia, ProductVariant, VariantMediafrom .....warehouse.models import Stockfrom ....tests.utils import get_graphql_content@@ -170,3 +170,187 @@ def test_product_variant_bulk_create(assert not data["bulkProductErrors"]assert data["count"] == 1assert product_variant_count + 1 == ProductVariant.objects.count()++[email protected]_queries(autouse=False)+def test_product_variant_create(+ staff_api_client,+ permission_manage_products,+ product_type,+ product_available_in_many_channels,+ warehouse,+ settings,+ count_queries,+):+ query = """+ mutation createVariant (+ $productId: ID!,+ $sku: String,+ $stocks: [StockInput!],+ $attributes: [AttributeValueInput]!,+ $weight: WeightScalar,+ $trackInventory: Boolean+ ) {+ productVariantCreate(+ input: {+ product: $productId,+ sku: $sku,+ stocks: $stocks,+ attributes: $attributes,+ trackInventory: $trackInventory,+ weight: $weight+ }+ ) {+ productErrors {+ field+ message+ attributes+ code+ }+ productVariant {+ id+ name+ sku+ attributes {+ attribute {+ slug+ }+ values {+ name+ slug+ reference+ file {+ url+ contentType+ }+ }+ }+ costPrice {+ currency+ amount+ localized+ }+ weight {+ value+ unit+ }+ stocks {+ quantity+ warehouse {+ slug+ }+ }+ }+ }+ }+ """+ settings.PLUGINS = ["saleor.plugins.webhook.plugin.WebhookPlugin"]+ product = product_available_in_many_channels+ product_id = graphene.Node.to_global_id("Product", product.pk)+ sku = "1"+ weight = 10.22+ attribute_id = graphene.Node.to_global_id(+ "Attribute", product_type.variant_attributes.first().pk+ )+ stocks = [+ {+ "warehouse": graphene.Node.to_global_id("Warehouse", warehouse.pk),+ "quantity": 20,+ }+ ]++ variables = {+ "productId": product_id,+ "sku": sku,+ "stocks": stocks,+ "weight": weight,+ "attributes": [+ {"id": attribute_id, "values": ["red"]},+ ],+ "trackInventory": True,+ }+ response = staff_api_client.post_graphql(+ query, variables, permissions=[permission_manage_products]+ )+ content = get_graphql_content(response)["data"]["productVariantCreate"]+ assert not content["productErrors"]++[email protected]_queries(autouse=False)+def test_update_product_variant(+ staff_api_client,+ permission_manage_products,+ product_available_in_many_channels,+ product_type,+ media_root,+ settings,+ image,+ count_queries,+):+ query = """+ mutation VariantUpdate(+ $id: ID!+ $attributes: [AttributeValueInput]+ $sku: String+ $trackInventory: Boolean!+ ) {+ productVariantUpdate(+ id: $id+ input: {+ attributes: $attributes+ sku: $sku+ trackInventory: $trackInventory+ }+ ) {+ errors {+ field+ message+ }+ productVariant {+ id+ attributes {+ attribute {+ id+ name+ slug+ values {+ id+ name+ slug+ __typename+ }+ __typename+ }+ __typename+ }+ }+ }+ }+ """+ settings.PLUGINS = ["saleor.plugins.webhook.plugin.WebhookPlugin"]+ product = product_available_in_many_channels+ variant = product.variants.first()+ product_image = ProductMedia.objects.create(product=product, image=image)+ VariantMedia.objects.create(variant=variant, media=product_image)++ variant_id = graphene.Node.to_global_id("ProductVariant", variant.pk)+ attribute_id = graphene.Node.to_global_id(+ "Attribute", product_type.variant_attributes.first().pk+ )+ variables = {+ "attributes": [+ {"id": attribute_id, "values": ["yellow"]},+ ],+ "id": variant_id,+ "sku": "21599567",+ "trackInventory": True,+ }++ data = get_graphql_content(+ staff_api_client.post_graphql(+ query, variables, permissions=[permission_manage_products]+ )+ )["data"]["productVariantUpdate"]+ assert not data["errors"]diff --git a/saleor/graphql/product/tests/test_variant.py b/saleor/graphql/product/tests/test_variant.pyindex 3a307d0ac8..fdbfcc5f09 100644--- a/saleor/graphql/product/tests/test_variant.py+++ b/saleor/graphql/product/tests/test_variant.py@@ -1050,7 +1050,7 @@ def test_create_variant_invalid_variant_attributes(assert error in errors-def test_create_product_variant_update_with_new_attributes(+def test_product_variant_update_with_new_attributes(staff_api_client, permission_manage_products, product, size_attribute):query = """diff --git a/saleor/graphql/schema.graphql b/saleor/graphql/schema.graphqlindex 6f626e576d..d0832b90d4 100644--- a/saleor/graphql/schema.graphql+++ b/saleor/graphql/schema.graphql@@ -1074,7 +1074,7 @@ type CollectionAddProducts {type CollectionBulkDelete {errors: [Error!]! @deprecated(reason: "Use typed errors with error codes. This field will be removed after 2020-07-31.")count: Int!- productErrors: [ProductError!]!+ collectionErrors: [CollectionError!]!}type CollectionChannelListing implements Node {