Skip to content

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

Refactor fragments in chekout mutation benchmarks

fowczrekc5f3e26bc161
diff --git a/tests/api/benchmark/test_checkout_mutations.py b/tests/api/benchmark/test_checkout_mutations.py
index bdbb072c6d..ebc6073821 100644
--- a/tests/api/benchmark/test_checkout_mutations.py
+++ b/tests/api/benchmark/test_checkout_mutations.py
@@ -5,216 +5,22 @@ from saleor.checkout import calculations
from saleor.checkout.models import Checkout
from tests.api.utils import get_graphql_content
-
[email protected]_queries(autouse=False)
-def test_create_checkout_as_anonymous(
- api_client,
- graphql_address_data,
- stock,
- product_with_default_variant,
- product_with_single_variant,
- product_with_two_variants,
- count_queries,
-):
- query = """
- fragment Price on TaxedMoney {
- gross {
- amount
- currency
- }
- net {
- amount
- currency
- }
- }
-
- fragment ProductVariant on ProductVariant {
- id
- name
- pricing {
- onSale
- priceUndiscounted {
- ...Price
- }
- price {
- ...Price
- }
- }
- product {
- id
- name
- thumbnail {
- url
- alt
- }
- thumbnail2x: thumbnail(size: 510) {
- url
- }
- }
- }
-
- fragment CheckoutLine on CheckoutLine {
- id
- quantity
- totalPrice {
- ...Price
- }
- variant {
- stockQuantity
- ...ProductVariant
- }
- quantity
- }
-
- fragment Address on Address {
- id
- firstName
- lastName
- companyName
- streetAddress1
- streetAddress2
- city
- postalCode
- country {
- code
- country
- }
- countryArea
- phone
- isDefaultBillingAddress
- isDefaultShippingAddress
- }
-
- fragment ShippingMethod on ShippingMethod {
- id
- name
- price {
- currency
- amount
- }
- }
-
- fragment Checkout on Checkout {
- availablePaymentGateways {
- name
- config {
- field
- value
- }
- }
- token
- id
- totalPrice {
- ...Price
- }
- subtotalPrice {
- ...Price
- }
- billingAddress {
- ...Address
- }
- shippingAddress {
- ...Address
- }
- email
- availableShippingMethods {
- ...ShippingMethod
- }
- shippingMethod {
- ...ShippingMethod
- }
- shippingPrice {
- ...Price
- }
- lines {
- ...CheckoutLine
- }
- isShippingRequired
- discount {
- currency
- amount
- }
- discountName
- translatedDiscountName
- voucherCode
+FRAGMENT_PRICE = """
+ fragment Price on TaxedMoney {
+ gross {
+ amount
+ currency
}
-
- mutation CreateCheckout($checkoutInput: CheckoutCreateInput!) {
- checkoutCreate(input: $checkoutInput) {
- errors {
- field
- message
- }
- checkout {
- ...Checkout
- }
- }
+ net {
+ amount
+ currency
}
- """
-
- checkout_counts = Checkout.objects.count()
- variables = {
- "checkoutInput": {
- "email": "[email protected]",
- "shippingAddress": graphql_address_data,
- "lines": [
- {
- "quantity": 1,
- "variantId": Node.to_global_id(
- "ProductVariant", stock.product_variant.pk
- ),
- },
- {
- "quantity": 2,
- "variantId": Node.to_global_id(
- "ProductVariant",
- product_with_default_variant.variants.first().pk,
- ),
- },
- {
- "quantity": 10,
- "variantId": Node.to_global_id(
- "ProductVariant",
- product_with_single_variant.variants.first().pk,
- ),
- },
- {
- "quantity": 3,
- "variantId": Node.to_global_id(
- "ProductVariant", product_with_two_variants.variants.first().pk,
- ),
- },
- {
- "quantity": 2,
- "variantId": Node.to_global_id(
- "ProductVariant", product_with_two_variants.variants.last().pk,
- ),
- },
- ],
- }
}
- get_graphql_content(api_client.post_graphql(query, variables))
- assert checkout_counts + 1 == Checkout.objects.count()
-
-
[email protected]_queries(autouse=False)
-def test_add_shipping_to_checkout_as_anonymous(
- api_client, checkout_with_shipping_address, shipping_method, count_queries,
-):
- query = """
- fragment Price on TaxedMoney {
- gross {
- amount
- currency
- }
- net {
- amount
- currency
- }
- }
+"""
+FRAGMENT_PRODUCT_VARIANT = (
+ FRAGMENT_PRICE
+ + """
fragment ProductVariant on ProductVariant {
id
name
@@ -239,7 +45,12 @@ def test_add_shipping_to_checkout_as_anonymous(
}
}
}
+ """
+)
+FRAGMENT_CHECKOUT_LINE = (
+ FRAGMENT_PRODUCT_VARIANT
+ + """
fragment CheckoutLine on CheckoutLine {
id
quantity
@@ -250,34 +61,50 @@ def test_add_shipping_to_checkout_as_anonymous(
stockQuantity
...ProductVariant
}
+ quantity
}
+ """
+)
+
+FRAGMENT_ADDRESS = """
+ fragment Address on Address {
+ id
+ firstName
+ lastName
+ companyName
+ streetAddress1
+ streetAddress2
+ city
+ postalCode
+ country {
+ code
+ country
+ }
+ countryArea
+ phone
+ isDefaultBillingAddress
+ isDefaultShippingAddress
+ }
+"""
- fragment Address on Address {
- id
- firstName
- lastName
- companyName
- streetAddress1
- streetAddress2
- city
- postalCode
- country {
- code
- country
- }
- countryArea
- phone
- }
- fragment ShippingMethod on ShippingMethod {
- id
- name
- price {
- currency
- amount
- }
- }
+FRAGMENT_SHIPPING_METHOD = """
+ fragment ShippingMethod on ShippingMethod {
+ id
+ name
+ price {
+ currency
+ amount
+ }
+ }
+"""
+
+FRAGMENT_CHECKOUT = (
+ FRAGMENT_CHECKOUT_LINE
+ + FRAGMENT_ADDRESS
+ + FRAGMENT_SHIPPING_METHOD
+ + """
fragment Checkout on Checkout {
availablePaymentGateways {
name
@@ -322,23 +149,108 @@ def test_add_shipping_to_checkout_as_anonymous(
translatedDiscountName
voucherCode
}
+ """
+)
- mutation updateCheckoutShippingOptions(
- $checkoutId: ID!, $shippingMethodId: ID!
- ) {
- checkoutShippingMethodUpdate(
- checkoutId: $checkoutId, shippingMethodId: $shippingMethodId
- ) {
- errors {
- field
- message
- }
- checkout {
- ...Checkout
+
[email protected]_queries(autouse=False)
+def test_create_checkout(
+ api_client,
+ graphql_address_data,
+ stock,
+ product_with_default_variant,
+ product_with_single_variant,
+ product_with_two_variants,
+ count_queries,
+):
+ query = (
+ FRAGMENT_CHECKOUT
+ + """
+ mutation CreateCheckout($checkoutInput: CheckoutCreateInput!) {
+ checkoutCreate(input: $checkoutInput) {
+ errors {
+ field
+ message
+ }
+ checkout {
+ ...Checkout
+ }
+ }
}
- }
+ """
+ )
+
+ checkout_counts = Checkout.objects.count()
+ variables = {
+ "checkoutInput": {
+ "email": "[email protected]",
+ "shippingAddress": graphql_address_data,
+ "lines": [
+ {
+ "quantity": 1,
+ "variantId": Node.to_global_id(
+ "ProductVariant", stock.product_variant.pk
+ ),
+ },
+ {
+ "quantity": 2,
+ "variantId": Node.to_global_id(
+ "ProductVariant",
+ product_with_default_variant.variants.first().pk,
+ ),
+ },
+ {
+ "quantity": 10,
+ "variantId": Node.to_global_id(
+ "ProductVariant",
+ product_with_single_variant.variants.first().pk,
+ ),
+ },
+ {
+ "quantity": 3,
+ "variantId": Node.to_global_id(
+ "ProductVariant", product_with_two_variants.variants.first().pk,
+ ),
+ },
+ {
+ "quantity": 2,
+ "variantId": Node.to_global_id(
+ "ProductVariant", product_with_two_variants.variants.last().pk,
+ ),
+ },
+ ],
}
- """
+ }
+ get_graphql_content(api_client.post_graphql(query, variables))
+ assert checkout_counts + 1 == Checkout.objects.count()
+
+
[email protected]_queries(autouse=False)
+def test_add_shipping_to_checkout(
+ api_client, checkout_with_shipping_address, shipping_method, count_queries,
+):
+ query = (
+ FRAGMENT_CHECKOUT
+ + """
+ mutation updateCheckoutShippingOptions(
+ $checkoutId: ID!, $shippingMethodId: ID!
+ ) {
+ checkoutShippingMethodUpdate(
+ checkoutId: $checkoutId, shippingMethodId: $shippingMethodId
+ ) {
+ errors {
+ field
+ message
+ }
+ checkout {
+ ...Checkout
+ }
+ }
+ }
+ """
+ )
variables = {
"checkoutId": Node.to_global_id("Checkout", checkout_with_shipping_address.pk),
"shippingMethodId": Node.to_global_id("ShippingMethod", shipping_method.pk),
@@ -349,161 +261,41 @@ def test_add_shipping_to_checkout_as_anonymous(
@pytest.mark.django_db
@pytest.mark.count_queries(autouse=False)
-def test_add_billing_address_to_checkout_as_anonymous(
+def test_add_billing_address_to_checkout(
api_client, graphql_address_data, checkout_with_shipping_method, count_queries
):
- query = """
- fragment Price on TaxedMoney {
- gross {
- amount
- currency
- }
- net {
- amount
- currency
- }
- }
-
- fragment ProductVariant on ProductVariant {
- id
- name
- pricing {
- onSale
- priceUndiscounted {
- ...Price
- }
- price {
- ...Price
- }
- }
- product {
- id
- name
- thumbnail {
- url
- alt
- }
- thumbnail2x: thumbnail(size: 510) {
- url
+ query = (
+ FRAGMENT_CHECKOUT
+ + """
+ mutation UpdateCheckoutBillingAddress(
+ $checkoutId: ID!, $billingAddress: AddressInput!
+ ) {
+ checkoutBillingAddressUpdate(
+ checkoutId: $checkoutId, billingAddress: $billingAddress
+ ) {
+ errors {
+ field
+ message
+ }
+ checkout {
+ ...Checkout
+ }
+ }
}
- }
- }
-
- fragment CheckoutLine on CheckoutLine {
- id
- quantity
- totalPrice {
- ...Price
- }
- variant {
- stockQuantity
- ...ProductVariant
- }
- quantity
- }
-
- fragment Address on Address {
- id
- firstName
- lastName
- companyName
- streetAddress1
- streetAddress2
- city
- postalCode
- country {
- code
- country
- }
- countryArea
- phone
- isDefaultBillingAddress
- isDefaultShippingAddress
- }
-
- fragment ShippingMethod on ShippingMethod {
- id
- name
- price {
- currency
- amount
- }
- }
-
- fragment Checkout on Checkout {
- availablePaymentGateways {
- name
- config {
- field
- value
- }
- }
- token
- id
- totalPrice {
- ...Price
- }
- subtotalPrice {
- ...Price
- }
- billingAddress {
- ...Address
- }
- shippingAddress {
- ...Address
- }
- email
- availableShippingMethods {
- ...ShippingMethod
- }
- shippingMethod {
- ...ShippingMethod
- }
- shippingPrice {
- ...Price
- }
- lines {
- ...CheckoutLine
- }
- isShippingRequired
- discount {
- currency
- amount
- }
- discountName
- translatedDiscountName
- voucherCode
- }
-
- mutation UpdateCheckoutBillingAddress(
- $checkoutId: ID!, $billingAddress: AddressInput!
- ) {
- checkoutBillingAddressUpdate(
- checkoutId: $checkoutId, billingAddress: $billingAddress
- ) {
- errors {
- field
- message
- }
- checkout {
- ...Checkout
- }
- }
- }
- """
- variables = {
- "checkoutId": Node.to_global_id("Checkout", checkout_with_shipping_method.pk),
- "billingAddress": graphql_address_data,
- }
- response = get_graphql_content(api_client.post_graphql(query, variables))
- assert not response["data"]["checkoutBillingAddressUpdate"]["errors"]
+ """
+ )
+ variables = {
+ "checkoutId": Node.to_global_id("Checkout", checkout_with_shipping_method.pk),
+ "billingAddress": graphql_address_data,
+ }
+ response = get_graphql_content(api_client.post_graphql(query, variables))
+ assert not response["data"]["checkoutBillingAddressUpdate"]["errors"]
@pytest.mark.django_db
@pytest.mark.count_queries(autouse=False)
-def test_update_checkout_lines_as_anonymous(
+def test_update_checkout_lines(
api_client,
- graphql_address_data,
checkout_with_variant,
stock,
product_with_default_variant,
@@ -511,77 +303,32 @@ def test_update_checkout_lines_as_anonymous(
product_with_two_variants,
count_queries,
):
- query = """
- fragment Price on TaxedMoney {
- gross {
- amount
- currency
- }
- net {
- amount
- currency
- }
- }
-
- fragment ProductVariant on ProductVariant {
- id
- name
- pricing {
- onSale
- priceUndiscounted {
- ...Price
- }
- price {
- ...Price
- }
- }
- product {
- id
- name
- thumbnail {
- url
- alt
- }
- thumbnail2x: thumbnail(size: 510) {
- url
- }
- }
- }
-
- fragment CheckoutLine on CheckoutLine {
- id
- quantity
- totalPrice {
- ...Price
- }
- variant {
- stockQuantity
- ...ProductVariant
- }
- }
-
- mutation updateCheckoutLine($checkoutId: ID!, $lines: [CheckoutLineInput]!) {
- checkoutLinesUpdate(checkoutId: $checkoutId, lines: $lines) {
- checkout {
- id
- lines {
- ...CheckoutLine
+ query = (
+ FRAGMENT_CHECKOUT_LINE
+ + """
+ mutation updateCheckoutLine($checkoutId: ID!, $lines: [CheckoutLineInput]!){
+ checkoutLinesUpdate(checkoutId: $checkoutId, lines: $lines) {
+ checkout {
+ id
+ lines {
+ ...CheckoutLine
+ }
+ totalPrice {
+ ...Price
+ }
+ subtotalPrice {
+ ...Price
+ }
+ isShippingRequired
+ }
+ errors {
+ field
+ message
+ }
}
- totalPrice {
- ...Price
- }
- subtotalPrice {
- ...Price
- }
- isShippingRequired
- }
- errors {
- field
- message
}
- }
- }
- """
+ """
+ )
variables = {
"checkoutId": Node.to_global_id("Checkout", checkout_with_variant.pk),
"lines": [
@@ -623,310 +370,92 @@ def test_update_checkout_lines_as_anonymous(
@pytest.mark.django_db
@pytest.mark.count_queries(autouse=False)
-def test_checkout_shipping_address_update_as_anonymous(
+def test_checkout_shipping_address_update(
api_client, graphql_address_data, checkout_with_variant, count_queries
):
- query = """
- fragment Price on TaxedMoney {
- gross {
- amount
- currency
- }
- net {
- amount
- currency
- }
- }
-
- fragment ProductVariant on ProductVariant {
- id
- name
- pricing {
- onSale
- priceUndiscounted {
- ...Price
- }
- price {
- ...Price
- }
- }
- product {
- id
- name
- thumbnail {
- url
- alt
- }
- thumbnail2x: thumbnail(size: 510) {
- url
+ query = (
+ FRAGMENT_CHECKOUT
+ + """
+ mutation UpdateCheckoutShippingAddress(
+ $checkoutId: ID!, $shippingAddress: AddressInput!
+ ) {
+ checkoutShippingAddressUpdate(
+ checkoutId: $checkoutId, shippingAddress: $shippingAddress
+ ) {
+ errors {
+ field
+ message
+ }
+ checkout {
+ ...Checkout
+ }
+ }
}
- }
- }
-
- fragment CheckoutLine on CheckoutLine {
- id
- quantity
- totalPrice {
- ...Price
- }
- variant {
- stockQuantity
- ...ProductVariant
- }
- quantity
- }
-
- fragment Address on Address {
- id
- firstName
- lastName
- companyName
- streetAddress1
- streetAddress2
- city
- postalCode
- country {
- code
- country
- }
- countryArea
- phone
- isDefaultBillingAddress
- isDefaultShippingAddress
- }
-
- fragment ShippingMethod on ShippingMethod {
- id
- name
- price {
- currency
- amount
- }
- }
+ """
+ )
+ variables = {
+ "checkoutId": Node.to_global_id("Checkout", checkout_with_variant.pk),
+ "shippingAddress": graphql_address_data,
+ }
+ response = get_graphql_content(api_client.post_graphql(query, variables))
+ assert not response["data"]["checkoutShippingAddressUpdate"]["errors"]
- fragment Checkout on Checkout {
- availablePaymentGateways {
- name
- config {
- field
- value
- }
- }
- token
- id
- totalPrice {
- ...Price
- }
- subtotalPrice {
- ...Price
- }
- billingAddress {
- ...Address
- }
- shippingAddress {
- ...Address
- }
- email
- availableShippingMethods {
- ...ShippingMethod
- }
- shippingMethod {
- ...ShippingMethod
- }
- shippingPrice {
- ...Price
- }
- lines {
- ...CheckoutLine
- }
- isShippingRequired
- discount {
- currency
- amount
- }
- discountName
- translatedDiscountName
- voucherCode
- }
- mutation UpdateCheckoutShippingAddress(
- $checkoutId: ID!, $shippingAddress: AddressInput!, $email: String!
- ) {
- checkoutShippingAddressUpdate(
- checkoutId: $checkoutId, shippingAddress: $shippingAddress
- ) {
- errors {
- field
- message
- }
- checkout {
- ...Checkout
- }
- }
- checkoutEmailUpdate(checkoutId: $checkoutId, email: $email) {
- checkout {
- ...Checkout
- }
- errors {
- field
- message
[email protected]_queries(autouse=False)
+def test_checkout_email_update(api_client, checkout_with_variant, count_queries):
+ query = (
+ FRAGMENT_CHECKOUT
+ + """
+ mutation UpdateCheckoutEmail(
+ $checkoutId: ID!, $email: String!
+ ) {
+ checkoutEmailUpdate(checkoutId: $checkoutId, email: $email) {
+ checkout {
+ ...Checkout
+ }
+ errors {
+ field
+ message
+ }
+ }
}
- }
- }
- """
+ """
+ )
variables = {
"checkoutId": Node.to_global_id("Checkout", checkout_with_variant.pk),
"email": "[email protected]",
- "shippingAddress": graphql_address_data,
}
response = get_graphql_content(api_client.post_graphql(query, variables))
- assert not response["data"]["checkoutShippingAddressUpdate"]["errors"]
+ assert not response["data"]["checkoutEmailUpdate"]["errors"]
@pytest.mark.django_db
@pytest.mark.count_queries(autouse=False)
-def test_checkout_voucher_code_as_anonymous(
+def test_checkout_voucher_code(
api_client, checkout_with_billing_address, voucher, count_queries
):
- query = """
- fragment Price on TaxedMoney {
- gross {
- amount
- currency
- }
- net {
- amount
- currency
- }
- }
-
- fragment ProductVariant on ProductVariant {
- id
- name
- pricing {
- onSale
- priceUndiscounted {
- ...Price
- }
- price {
- ...Price
- }
- }
- product {
- id
- name
- thumbnail {
- url
- alt
- }
- thumbnail2x: thumbnail(size: 510) {
- url
- }
- }
- }
-
- fragment CheckoutLine on CheckoutLine {
- id
- quantity
- totalPrice {
- ...Price
- }
- variant {
- stockQuantity
- ...ProductVariant
- }
- quantity
- }
-
- fragment Address on Address {
- id
- firstName
- lastName
- companyName
- streetAddress1
- streetAddress2
- city
- postalCode
- country {
- code
- country
- }
- countryArea
- phone
- isDefaultBillingAddress
- isDefaultShippingAddress
- }
-
- fragment ShippingMethod on ShippingMethod {
- id
- name
- price {
- currency
- amount
- }
- }
-
- fragment Checkout on Checkout {
- availablePaymentGateways {
- name
- config {
- field
- }
- }
- token
- id
- totalPrice {
- ...Price
- }
- subtotalPrice {
- ...Price
- }
- billingAddress {
- ...Address
- }
- shippingAddress {
- ...Address
- }
- email
- availableShippingMethods {
- ...ShippingMethod
- }
- shippingMethod {
- ...ShippingMethod
- }
- shippingPrice {
- ...Price
- }
- lines {
- ...CheckoutLine
- }
- isShippingRequired
- discount {
- currency
- amount
- }
- discountName
- translatedDiscountName
- voucherCode
- }
-
- mutation AddCheckoutPromoCode($checkoutId: ID!, $promoCode: String!) {
- checkoutAddPromoCode(checkoutId: $checkoutId, promoCode: $promoCode) {
- checkout {
- ...Checkout
- }
- errors {
- field
- message
- }
- checkoutErrors {
- field
- message
- code
+ query = (
+ FRAGMENT_CHECKOUT
+ + """
+ mutation AddCheckoutPromoCode($checkoutId: ID!, $promoCode: String!) {
+ checkoutAddPromoCode(checkoutId: $checkoutId, promoCode: $promoCode) {
+ checkout {
+ ...Checkout
+ }
+ errors {
+ field
+ message
+ }
+ checkoutErrors {
+ field
+ message
+ code
+ }
+ }
}
- }
- }
- """
+ """
+ )
variables = {
"checkoutId": Node.to_global_id("Checkout", checkout_with_billing_address.pk),
"promoCode": voucher.code,
@@ -937,7 +466,7 @@ def test_checkout_voucher_code_as_anonymous(
@pytest.mark.django_db
@pytest.mark.count_queries(autouse=False)
-def test_checkout_payment_charge_as_anonymous(
+def test_checkout_payment_charge(
api_client, checkout_with_billing_address, count_queries
):
query = """