Skip to content

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

Add translation benchmarks

fowczrekb2dd8e11c4c5
diff --git a/saleor/graphql/attribute/tests/benchmark/test_attribute.py b/saleor/graphql/attribute/tests/benchmark/test_attribute.py
index 02af7eef59..8c3637ca2a 100644
--- a/saleor/graphql/attribute/tests/benchmark/test_attribute.py
+++ b/saleor/graphql/attribute/tests/benchmark/test_attribute.py
@@ -1,6 +1,8 @@
import graphene
import pytest
+from .....attribute.models import AttributeTranslation, AttributeValueTranslation
+from ....attribute.utils import associate_attribute_values_to_instance
from ....tests.utils import get_graphql_content
@@ -92,3 +94,85 @@ def test_query_attributes(
content = get_graphql_content(response)
data = content["data"]["attributes"]
assert data
+
+
[email protected]_queries(autouse=False)
+def test_attribute_translation(
+ staff_api_client,
+ size_attribute,
+ weight_attribute,
+ color_attribute,
+ permission_manage_products,
+ count_queries,
+):
+ staff_api_client.user.user_permissions.add(permission_manage_products)
+ query = """
+ {
+ attributes(first: 20) {
+ edges {
+ node {
+ name
+ translation(languageCode: EN) {
+ name
+ }
+ }
+ }
+ }
+ }
+ """
+
+ attributes = [size_attribute, weight_attribute, color_attribute]
+ translations = []
+ for attribute in attributes:
+ translations.append(
+ AttributeTranslation(attribute=attribute, language_code="en")
+ )
+ AttributeTranslation.objects.bulk_create(translations)
+
+ get_graphql_content(staff_api_client.post_graphql(query, {}))
+
+
[email protected]_queries(autouse=False)
+def test_attribute_value_translation(
+ staff_api_client,
+ rich_text_attribute_with_many_values,
+ product,
+ permission_manage_products,
+ count_queries,
+):
+ staff_api_client.user.user_permissions.add(permission_manage_products)
+ query = """
+ query($slug:String){
+ product(slug:$slug){
+ attributes{
+ values{
+ name
+ translation(languageCode:EN){
+ name
+ }
+ }
+ }
+ }
+ }
+ """
+
+ attribute_values = rich_text_attribute_with_many_values.values.all()
+ translations = []
+ for attribute_value in attribute_values:
+ translations.append(
+ AttributeValueTranslation(
+ attribute_value=attribute_value, language_code="en"
+ )
+ )
+ AttributeValueTranslation.objects.bulk_create(translations)
+
+ product.product_type.product_attributes.add(rich_text_attribute_with_many_values)
+ associate_attribute_values_to_instance(
+ product, rich_text_attribute_with_many_values, *attribute_values
+ )
+
+ variables = {"slug": product.slug}
+
+ get_graphql_content(staff_api_client.post_graphql(query, variables))
diff --git a/saleor/graphql/product/tests/benchmark/test_product.py b/saleor/graphql/product/tests/benchmark/test_product.py
index 88fe0b9ff3..f5a0284661 100644
--- a/saleor/graphql/product/tests/benchmark/test_product.py
+++ b/saleor/graphql/product/tests/benchmark/test_product.py
@@ -7,6 +7,7 @@ from graphene import Node
from .....attribute.utils import associate_attribute_values_to_instance
from .....core.taxes import TaxType
from .....plugins.manager import PluginsManager
+from .....product.models import ProductTranslation
from ....tests.utils import get_graphql_content
@@ -670,3 +671,29 @@ def test_filter_products_by_boolean_attributes(
},
}
get_graphql_content(api_client.post_graphql(query, variables))
+
+
[email protected]_queries(autouse=False)
+def test_product_translations(api_client, product_list, channel_USD, count_queries):
+ query = """
+ query($channel: String) {
+ products(channel: $channel, first: 20) {
+ edges {
+ node {
+ name
+ translation(languageCode: EN) {
+ name
+ }
+ }
+ }
+ }
+ }
+ """
+ translations = []
+ for product in product_list:
+ translations.append(ProductTranslation(product=product, language_code="en"))
+ ProductTranslation.objects.bulk_create(translations)
+
+ variables = {"channel": channel_USD.slug}
+ get_graphql_content(api_client.post_graphql(query, variables))
diff --git a/saleor/tests/fixtures.py b/saleor/tests/fixtures.py
index 5d3320d094..dc4dab7c70 100644
--- a/saleor/tests/fixtures.py
+++ b/saleor/tests/fixtures.py
@@ -858,6 +858,26 @@ def rich_text_attribute(db):
return attribute
+def rich_text_attribute_with_many_values(rich_text_attribute):
+ attribute = rich_text_attribute
+ values = []
+ for i in range(5):
+ text = f"Rich text attribute content{i}."
+ values.append(
+ AttributeValue(
+ attribute=attribute,
+ name=truncatechars(
+ clean_editor_js(dummy_editorjs(text), to_string=True), 50
+ ),
+ slug=f"instance_{attribute.id}_{i}",
+ rich_text=dummy_editorjs(text),
+ )
+ )
+ AttributeValue.objects.bulk_create(values)
+ return rich_text_attribute
+
+
@pytest.fixture
def color_attribute_without_values(db): # pylint: disable=W0613
return Attribute.objects.create(