Skip to content

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

Add db query counting for getting variants and product details

NyanKiyoshi7f6dc33baef5
diff --git a/tests/api/test_bulk_delete.py b/tests/api/test_bulk_delete.py
index 27fe8db267..0c192b49fa 100644
--- a/tests/api/test_bulk_delete.py
+++ b/tests/api/test_bulk_delete.py
@@ -81,14 +81,6 @@ def product_type_list():
return product_type_1, product_type_2, product_type_3
-def product_variant_list(product):
- product_variant_1 = ProductVariant.objects.create(product=product, sku="1")
- product_variant_2 = ProductVariant.objects.create(product=product, sku="2")
- product_variant_3 = ProductVariant.objects.create(product=product, sku="3")
- return product_variant_1, product_variant_2, product_variant_3
-
-
@pytest.fixture
def sale_list():
sale_1 = Sale.objects.create(name="Sale 1", value=5)
diff --git a/tests/api/test_checkout_bench.py b/tests/api/test_checkout_bench.py
index 86813643ef..e3394c9376 100644
--- a/tests/api/test_checkout_bench.py
+++ b/tests/api/test_checkout_bench.py
@@ -69,10 +69,8 @@ def test_create_checkout(api_client, graphql_address_data, variant, count_querie
gross {
amount
localized
- __typename
}
currency
- __typename
}
fragment ProductVariant on ProductVariant {
@@ -82,7 +80,6 @@ def test_create_checkout(api_client, graphql_address_data, variant, count_querie
amount
currency
localized
- __typename
}
product {
id
@@ -90,15 +87,11 @@ def test_create_checkout(api_client, graphql_address_data, variant, count_querie
thumbnail {
url
alt
- __typename
}
thumbnail2x: thumbnail(size: 510) {
url
- __typename
}
- __typename
}
- __typename
}
fragment CheckoutLine on CheckoutLine {
@@ -106,15 +99,12 @@ def test_create_checkout(api_client, graphql_address_data, variant, count_querie
quantity
totalPrice {
...Price
- __typename
}
variant {
stockQuantity
...ProductVariant
- __typename
}
quantity
- __typename
}
fragment Address on Address {
@@ -129,11 +119,9 @@ def test_create_checkout(api_client, graphql_address_data, variant, count_querie
country {
code
country
- __typename
}
countryArea
phone
- __typename
}
fragment ShippingMethod on ShippingMethod {
@@ -143,9 +131,7 @@ def test_create_checkout(api_client, graphql_address_data, variant, count_querie
currency
amount
localized
- __typename
}
- __typename
}
fragment Checkout on Checkout {
@@ -154,42 +140,32 @@ def test_create_checkout(api_client, graphql_address_data, variant, count_querie
id
user {
email
- __typename
}
totalPrice {
...Price
- __typename
}
subtotalPrice {
...Price
- __typename
}
billingAddress {
...Address
- __typename
}
shippingAddress {
...Address
- __typename
}
email
availableShippingMethods {
...ShippingMethod
- __typename
}
shippingMethod {
...ShippingMethod
- __typename
}
shippingPrice {
...Price
- __typename
}
lines {
...CheckoutLine
- __typename
}
- __typename
}
mutation createCheckout($checkoutInput: CheckoutCreateInput!) {
@@ -197,13 +173,10 @@ def test_create_checkout(api_client, graphql_address_data, variant, count_querie
errors {
field
message
- __typename
}
checkout {
...Checkout
- __typename
}
- __typename
}
}
"""
@@ -237,10 +210,8 @@ def test_add_shipping_to_checkout(
gross {
amount
localized
- __typename
}
currency
- __typename
}
fragment ProductVariant on ProductVariant {
@@ -250,7 +221,6 @@ def test_add_shipping_to_checkout(
amount
currency
localized
- __typename
}
product {
id
@@ -258,15 +228,11 @@ def test_add_shipping_to_checkout(
thumbnail {
url
alt
- __typename
}
thumbnail2x: thumbnail(size: 510) {
url
- __typename
}
- __typename
}
- __typename
}
fragment CheckoutLine on CheckoutLine {
@@ -274,15 +240,12 @@ def test_add_shipping_to_checkout(
quantity
totalPrice {
...Price
- __typename
}
variant {
stockQuantity
...ProductVariant
- __typename
}
quantity
- __typename
}
fragment Address on Address {
@@ -297,11 +260,9 @@ def test_add_shipping_to_checkout(
country {
code
country
- __typename
}
countryArea
phone
- __typename
}
fragment ShippingMethod on ShippingMethod {
@@ -311,9 +272,7 @@ def test_add_shipping_to_checkout(
currency
amount
localized
- __typename
}
- __typename
}
fragment Checkout on Checkout {
@@ -322,42 +281,32 @@ def test_add_shipping_to_checkout(
id
user {
email
- __typename
}
totalPrice {
...Price
- __typename
}
subtotalPrice {
...Price
- __typename
}
billingAddress {
...Address
- __typename
}
shippingAddress {
...Address
- __typename
}
email
availableShippingMethods {
...ShippingMethod
- __typename
}
shippingMethod {
...ShippingMethod
- __typename
}
shippingPrice {
...Price
- __typename
}
lines {
...CheckoutLine
- __typename
}
- __typename
}
mutation updateCheckoutShippingOptions(
@@ -371,13 +320,10 @@ def test_add_shipping_to_checkout(
errors {
field
message
- __typename
}
checkout {
...Checkout
- __typename
}
- __typename
}
}
"""
@@ -398,10 +344,8 @@ def test_add_billing_address_to_checkout(
gross {
amount
localized
- __typename
}
currency
- __typename
}
fragment ProductVariant on ProductVariant {
@@ -411,7 +355,6 @@ def test_add_billing_address_to_checkout(
amount
currency
localized
- __typename
}
product {
id
@@ -419,15 +362,11 @@ def test_add_billing_address_to_checkout(
thumbnail {
url
alt
- __typename
}
thumbnail2x: thumbnail(size: 510) {
url
- __typename
}
- __typename
}
- __typename
}
fragment CheckoutLine on CheckoutLine {
@@ -435,15 +374,12 @@ def test_add_billing_address_to_checkout(
quantity
totalPrice {
...Price
- __typename
}
variant {
stockQuantity
...ProductVariant
- __typename
}
quantity
- __typename
}
fragment Address on Address {
@@ -458,11 +394,9 @@ def test_add_billing_address_to_checkout(
country {
code
country
- __typename
}
countryArea
phone
- __typename
}
fragment ShippingMethod on ShippingMethod {
@@ -472,9 +406,7 @@ def test_add_billing_address_to_checkout(
currency
amount
localized
- __typename
}
- __typename
}
fragment Checkout on Checkout {
@@ -483,42 +415,32 @@ def test_add_billing_address_to_checkout(
id
user {
email
- __typename
}
totalPrice {
...Price
- __typename
}
subtotalPrice {
...Price
- __typename
}
billingAddress {
...Address
- __typename
}
shippingAddress {
...Address
- __typename
}
email
availableShippingMethods {
...ShippingMethod
- __typename
}
shippingMethod {
...ShippingMethod
- __typename
}
shippingPrice {
...Price
- __typename
}
lines {
...CheckoutLine
- __typename
}
- __typename
}
mutation updateCheckoutBillingAddress(
@@ -532,13 +454,10 @@ def test_add_billing_address_to_checkout(
errors {
field
message
- __typename
}
checkout {
...Checkout
- __typename
}
- __typename
}
}
"""
@@ -560,9 +479,7 @@ def test_checkout_payment_charge(
errors {
field
message
- __typename
}
- __typename
}
}
"""
@@ -588,14 +505,11 @@ def test_complete_checkout(api_client, checkout_with_charged_payment, count_quer
errors {
field
message
- __typename
}
order {
id
token
- __typename
}
- __typename
}
}
"""
diff --git a/tests/api/test_homepage_bench.py b/tests/api/test_homepage_bench.py
index 71170b68ea..35de04c1ba 100644
--- a/tests/api/test_homepage_bench.py
+++ b/tests/api/test_homepage_bench.py
@@ -13,23 +13,18 @@ def test_retrieve_main_menu(api_client, count_queries):
category {
id
name
- __typename
}
url
collection {
id
name
- __typename
}
page {
slug
- __typename
}
parent {
id
- __typename
}
- __typename
}
query MainMenu {
@@ -43,17 +38,11 @@ def test_retrieve_main_menu(api_client, count_queries):
...MainMenuSubItem
children {
...MainMenuSubItem
- __typename
}
- __typename
}
- __typename
}
- __typename
}
- __typename
}
- __typename
}
}
"""
@@ -71,19 +60,15 @@ def test_retrieve_secondary_menu(api_client, count_queries):
category {
id
name
- __typename
}
url
collection {
id
name
- __typename
}
page {
slug
- __typename
}
- __typename
}
query SecondaryMenu {
@@ -94,15 +79,10 @@ def test_retrieve_secondary_menu(api_client, count_queries):
...SecondaryMenuSubItem
children {
...SecondaryMenuSubItem
- __typename
}
- __typename
}
- __typename
}
- __typename
}
- __typename
}
}
"""
@@ -118,22 +98,17 @@ def test_retrieve_shop(api_client, count_queries):
defaultCountry {
code
country
- __typename
}
countries {
country
code
- __typename
}
geolocalization {
country {
code
country
- __typename
}
- __typename
}
- __typename
}
}
"""
@@ -153,12 +128,9 @@ def test_retrieve_product_list(api_client, count_queries):
id
backgroundImage {
url
- __typename
}
name
- __typename
}
- __typename
}
categories(level: 0, first: 4) {
edges {
@@ -167,13 +139,9 @@ def test_retrieve_product_list(api_client, count_queries):
name
backgroundImage {
url
- __typename
}
- __typename
}
- __typename
}
- __typename
}
}
"""
diff --git a/tests/api/test_payment_bench.py b/tests/api/test_payment_bench.py
index c1458d7052..c76c90fe90 100644
--- a/tests/api/test_payment_bench.py
+++ b/tests/api/test_payment_bench.py
@@ -1,16 +1,15 @@
-import pytest
from django.conf import settings
from tests.api.utils import get_graphql_content
[email protected]_queries(autouse=False)
-def test_get_payment_token(count_queries, api_client):
+def test_get_payment_token(api_client, django_assert_num_queries):
+ """This test ensures getting a gateway does not generate any queries."""
query = """
query getPaymentToken($gateway: GatewaysEnum!) {
paymentClientToken(gateway: $gateway)
}
"""
variables = {"gateway": settings.DUMMY.upper()}
- get_graphql_content(api_client.post_graphql(query, variables))
+ with django_assert_num_queries(0):
+ get_graphql_content(api_client.post_graphql(query, variables))
diff --git a/tests/api/test_product_bench.py b/tests/api/test_product_bench.py
new file mode 100644
index 0000000000..d17d60ab8e
--- /dev/null
+++ b/tests/api/test_product_bench.py
@@ -0,0 +1,93 @@
+import pytest
+from graphene import Node
+
+from tests.api.utils import get_graphql_content
+
+
[email protected]_queries(autouse=False)
+def test_product_details(product, api_client, count_queries):
+ query = """
+ fragment BasicProductFields on Product {
+ id
+ name
+ thumbnail {
+ url
+ alt
+ }
+ thumbnail2x: thumbnail(size: 510) {
+ url
+ }
+ }
+
+ fragment ProductVariantFields on ProductVariant {
+ id
+ sku
+ name
+ stockQuantity
+ isAvailable
+ price {
+ currency
+ amount
+ localized
+ }
+ attributes {
+ attribute {
+ id
+ name
+ }
+ value {
+ id
+ name
+ value: name
+ }
+ }
+ }
+
+ query ProductDetails($id: ID!) {
+ product(id: $id) {
+ ...BasicProductFields
+ description
+ category {
+ id
+ name
+ products(first: 4) {
+ edges {
+ node {
+ ...BasicProductFields
+ category {
+ id
+ name
+ }
+ price {
+ amount
+ currency
+ localized
+ }
+ }
+ }
+ }
+ }
+ price {
+ amount
+ currency
+ localized
+ }
+ images {
+ id
+ url
+ }
+ variants {
+ ...ProductVariantFields
+ }
+ seoDescription
+ seoTitle
+ availability {
+ available
+ }
+ }
+ }
+ """
+
+ variables = {"id": Node.to_global_id("Product", product.pk)}
+ get_graphql_content(api_client.post_graphql(query, variables))
diff --git a/tests/api/test_variant_bench.py b/tests/api/test_variant_bench.py
new file mode 100644
index 0000000000..52d7653211
--- /dev/null
+++ b/tests/api/test_variant_bench.py
@@ -0,0 +1,68 @@
+import pytest
+from graphene import Node
+
+from tests.api.utils import get_graphql_content
+
+
[email protected]_queries(autouse=False)
+def test_retrieve_variant_list(product_variant_list, api_client, count_queries):
+ query = """
+ fragment BasicProductFields on Product {
+ id
+ name
+ thumbnail {
+ url
+ alt
+ }
+ thumbnail2x: thumbnail(size: 510) {
+ url
+ }
+ }
+
+ fragment ProductVariantFields on ProductVariant {
+ id
+ sku
+ name
+ stockQuantity
+ isAvailable
+ price {
+ currency
+ amount
+ localized
+ }
+ attributes {
+ attribute {
+ id
+ name
+ }
+ value {
+ id
+ name
+ value: name
+ }
+ }
+ }
+
+ query VariantList($ids: [ID!]) {
+ productVariants(ids: $ids, first: 100) {
+ edges {
+ node {
+ ...ProductVariantFields
+ stockQuantity
+ product {
+ ...BasicProductFields
+ }
+ }
+ }
+ }
+ }
+ """
+
+ variables = {
+ "ids": [
+ Node.to_global_id("ProductVariant", variant.pk)
+ for variant in product_variant_list
+ ]
+ }
+ get_graphql_content(api_client.post_graphql(query, variables))
diff --git a/tests/conftest.py b/tests/conftest.py
index 17127f7b1a..8b380782fa 100644
--- a/tests/conftest.py
+++ b/tests/conftest.py
@@ -430,6 +430,19 @@ def variant(product):
return product_variant
+def product_variant_list(product):
+ return list(
+ ProductVariant.objects.bulk_create(
+ [
+ ProductVariant(product=product, sku="1"),
+ ProductVariant(product=product, sku="2"),
+ ProductVariant(product=product, sku="3"),
+ ]
+ )
+ )
+
+
@pytest.fixture
def product_without_shipping(category):
product_type = ProductType.objects.create(