Skip to content

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

Add benchmark to checkout mutations

fowczarekdabda93e2478
diff --git a/tests/api/benchmark/conftest.py b/tests/api/benchmark/conftest.py
index 26f5036cbd..5135de38ce 100644
--- a/tests/api/benchmark/conftest.py
+++ b/tests/api/benchmark/conftest.py
@@ -1,7 +1,11 @@
import pytest
+from saleor.checkout import calculations
+from saleor.checkout.utils import add_variant_to_checkout
from saleor.discount.models import Sale
from saleor.menu.utils import update_menu
+from saleor.payment import ChargeStatus, TransactionKind
+from saleor.payment.models import Payment
from saleor.product.models import Category
@@ -91,3 +95,66 @@ def customer_checkout(customer_user, checkout_with_voucher_percentage_and_shippi
checkout_with_voucher_percentage_and_shipping.user = customer_user
checkout_with_voucher_percentage_and_shipping.save()
return checkout_with_voucher_percentage_and_shipping
+
+
+def checkout_with_variant(checkout, stock):
+ variant = stock.product_variant
+ add_variant_to_checkout(checkout, variant, 1)
+
+ checkout.save()
+ return checkout
+
+
+def checkout_with_shipping_address(checkout_with_variant, address):
+ checkout = checkout_with_variant
+
+ checkout.shipping_address = address.get_copy()
+ checkout.save()
+
+ return checkout
+
+
+def checkout_with_shipping_method(checkout_with_shipping_address, shipping_method):
+ checkout = checkout_with_shipping_address
+
+ checkout.shipping_method = shipping_method
+ checkout.save()
+
+ return checkout
+
+
+def checkout_with_billing_address(checkout_with_shipping_method, address):
+ checkout = checkout_with_shipping_method
+
+ checkout.billing_address = address
+ checkout.save()
+
+ return checkout
+
+
+def checkout_with_charged_payment(checkout_with_billing_address):
+ checkout = checkout_with_billing_address
+
+ taxed_total = calculations.checkout_total(checkout)
+ payment = Payment.objects.create(
+ gateway="Dummy", is_active=True, total=taxed_total.gross.amount, currency="USD"
+ )
+
+ payment.charge_status = ChargeStatus.FULLY_CHARGED
+ payment.captured_amount = payment.total
+ payment.checkout = checkout_with_billing_address
+ payment.save()
+
+ payment.transactions.create(
+ amount=payment.total,
+ kind=TransactionKind.CAPTURE,
+ gateway_response={},
+ is_success=True,
+ )
+
+ return checkout
diff --git a/tests/api/benchmark/test_checkout.py b/tests/api/benchmark/test_checkout.py
deleted file mode 100644
index d4a59f77cd..0000000000
--- a/tests/api/benchmark/test_checkout.py
+++ /dev/null
@@ -1,586 +0,0 @@
-import pytest
-from graphene import Node
-
-from saleor.checkout import calculations
-from saleor.checkout.utils import add_variant_to_checkout
-from saleor.payment import ChargeStatus, TransactionKind
-from saleor.payment.models import Payment
-from tests.api.utils import get_graphql_content
-
-
-def checkout_with_variant(checkout, stock):
- variant = stock.product_variant
- add_variant_to_checkout(checkout, variant, 1)
- checkout.save()
- return checkout
-
-
-def checkout_with_shipping_method(checkout_with_variant, shipping_method):
- checkout = checkout_with_variant
-
- checkout.shipping_method = shipping_method
- checkout.save()
-
- return checkout
-
-
-def checkout_with_billing_address(checkout_with_shipping_method, address):
- checkout = checkout_with_shipping_method
-
- checkout.billing_address = address
- checkout.save()
-
- return checkout
-
-
-def checkout_with_charged_payment(checkout_with_billing_address):
- checkout = checkout_with_billing_address
-
- taxed_total = calculations.checkout_total(checkout)
- payment = Payment.objects.create(
- gateway="Dummy", is_active=True, total=taxed_total.gross.amount, currency="USD"
- )
-
- payment.charge_status = ChargeStatus.FULLY_CHARGED
- payment.captured_amount = payment.total
- payment.checkout = checkout_with_billing_address
- payment.save()
-
- payment.transactions.create(
- amount=payment.total,
- kind=TransactionKind.CAPTURE,
- gateway_response={},
- is_success=True,
- )
-
- return checkout
-
-
[email protected]_queries(autouse=False)
-def test_create_checkout(api_client, graphql_address_data, variant, count_queries):
- query = """
- fragment Price on TaxedMoney {
- gross {
- amount
- localized
- }
- currency
- }
-
- fragment ProductVariant on ProductVariant {
- id
- name
- pricing {
- discountLocalCurrency {
- currency
- gross {
- amount
- localized
- }
- }
- price {
- currency
- gross {
- amount
- localized
- }
- }
- priceUndiscounted {
- currency
- gross {
- amount
- localized
- }
- }
- priceLocalCurrency {
- currency
- gross {
- amount
- localized
- }
- }
- }
- product {
- id
- name
- thumbnail {
- url
- alt
- }
- thumbnail2x: thumbnail(size: 510) {
- url
- }
- }
- }
-
- fragment CheckoutLine on CheckoutLine {
- id
- quantity
- totalPrice {
- ...Price
- }
- variant {
- ...ProductVariant
- }
- quantity
- }
-
- 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
- localized
- }
- }
-
- fragment Checkout on Checkout {
- token
- id
- user {
- email
- }
- totalPrice {
- ...Price
- }
- subtotalPrice {
- ...Price
- }
- billingAddress {
- ...Address
- }
- shippingAddress {
- ...Address
- }
- email
- availableShippingMethods {
- ...ShippingMethod
- }
- shippingMethod {
- ...ShippingMethod
- }
- shippingPrice {
- ...Price
- }
- lines {
- ...CheckoutLine
- }
- }
-
- mutation createCheckout($checkoutInput: CheckoutCreateInput!) {
- checkoutCreate(input: $checkoutInput) {
- errors {
- field
- message
- }
- checkout {
- ...Checkout
- }
- }
- }
- """
- variables = {
- "checkoutInput": {
- "email": "[email protected]",
- "shippingAddress": graphql_address_data,
- "lines": [
- {
- "quantity": 1,
- "variantId": Node.to_global_id("ProductVariant", variant.pk),
- }
- ],
- }
- }
- get_graphql_content(api_client.post_graphql(query, variables))
-
-
[email protected]_queries(autouse=False)
-def test_add_shipping_to_checkout(
- api_client,
- graphql_address_data,
- variant,
- checkout_with_variant,
- shipping_method,
- count_queries,
-):
- query = """
- fragment Price on TaxedMoney {
- gross {
- amount
- localized
- }
- currency
- }
-
- fragment ProductVariant on ProductVariant {
- id
- name
- pricing {
- discountLocalCurrency {
- currency
- gross {
- amount
- localized
- }
- }
- price {
- currency
- gross {
- amount
- localized
- }
- }
- priceUndiscounted {
- currency
- gross {
- amount
- localized
- }
- }
- priceLocalCurrency {
- currency
- gross {
- amount
- localized
- }
- }
- }
- product {
- id
- name
- thumbnail {
- url
- alt
- }
- thumbnail2x: thumbnail(size: 510) {
- url
- }
- }
- }
-
- fragment CheckoutLine on CheckoutLine {
- id
- quantity
- totalPrice {
- ...Price
- }
- variant {
- ...ProductVariant
- }
- quantity
- }
-
- 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
- localized
- }
- }
-
- fragment Checkout on Checkout {
- token
- id
- user {
- email
- }
- totalPrice {
- ...Price
- }
- subtotalPrice {
- ...Price
- }
- billingAddress {
- ...Address
- }
- shippingAddress {
- ...Address
- }
- email
- availableShippingMethods {
- ...ShippingMethod
- }
- shippingMethod {
- ...ShippingMethod
- }
- shippingPrice {
- ...Price
- }
- lines {
- ...CheckoutLine
- }
- }
-
- 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_variant.pk),
- "shippingMethodId": Node.to_global_id("ShippingMethod", shipping_method.pk),
- }
- get_graphql_content(api_client.post_graphql(query, variables))
-
-
[email protected]_queries(autouse=False)
-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
- localized
- }
- currency
- }
-
- fragment ProductVariant on ProductVariant {
- id
- name
- pricing {
- discountLocalCurrency {
- currency
- gross {
- amount
- localized
- }
- }
- price {
- currency
- gross {
- amount
- localized
- }
- }
- priceUndiscounted {
- currency
- gross {
- amount
- localized
- }
- }
- priceLocalCurrency {
- currency
- gross {
- amount
- localized
- }
- }
- }
- product {
- id
- name
- thumbnail {
- url
- alt
- }
- thumbnail2x: thumbnail(size: 510) {
- url
- }
- }
- }
-
- fragment CheckoutLine on CheckoutLine {
- id
- quantity
- totalPrice {
- ...Price
- }
- variant {
- ...ProductVariant
- }
- quantity
- }
-
- 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
- localized
- }
- }
-
- fragment Checkout on Checkout {
- token
- id
- totalPrice {
- ...Price
- }
- subtotalPrice {
- ...Price
- }
- billingAddress {
- ...Address
- }
- shippingAddress {
- ...Address
- }
- email
- availableShippingMethods {
- ...ShippingMethod
- }
- shippingMethod {
- ...ShippingMethod
- }
- shippingPrice {
- ...Price
- }
- lines {
- ...CheckoutLine
- }
- }
-
- 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,
- }
- get_graphql_content(api_client.post_graphql(query, variables))
-
-
[email protected]_queries(autouse=False)
-def test_checkout_payment_charge(
- api_client, graphql_address_data, checkout_with_billing_address, count_queries
-):
- query = """
- mutation createPayment($input: PaymentInput!, $checkoutId: ID!) {
- checkoutPaymentCreate(input: $input, checkoutId: $checkoutId) {
- errors {
- field
- message
- }
- }
- }
- """
-
- variables = {
- "checkoutId": Node.to_global_id("Checkout", checkout_with_billing_address.pk),
- "input": {
- "amount": 1000, # 10.00 USD * 100
- "gateway": "Dummy",
- "token": "charged",
- },
- }
- get_graphql_content(api_client.post_graphql(query, variables))
-
-
[email protected]_queries(autouse=False)
-def test_complete_checkout(api_client, checkout_with_charged_payment, count_queries):
- query = """
- mutation completeCheckout($checkoutId: ID!, $redirectUrl: String) {
- checkoutComplete(checkoutId: $checkoutId, redirectUrl: $redirectUrl) {
- errors {
- field
- message
- }
- order {
- id
- token
- }
- }
- }
- """
-
- variables = {
- "checkoutId": Node.to_global_id("Checkout", checkout_with_charged_payment.pk),
- "redirectUrl": "https://www.example.com",
- }
-
- get_graphql_content(api_client.post_graphql(query, variables))
diff --git a/tests/api/benchmark/test_checkout_mutations.py b/tests/api/benchmark/test_checkout_mutations.py
new file mode 100644
index 0000000000..bdbb072c6d
--- /dev/null
+++ b/tests/api/benchmark/test_checkout_mutations.py
@@ -0,0 +1,991 @@
+import pytest
+from graphene import Node
+
+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
+ }
+
+ 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_as_anonymous(
+ api_client, checkout_with_shipping_address, 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
+ }
+ }
+ }
+
+ fragment CheckoutLine on CheckoutLine {
+ id
+ quantity
+ totalPrice {
+ ...Price
+ }
+ variant {
+ stockQuantity
+ ...ProductVariant
+ }
+ }
+
+ 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 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 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),
+ }
+ response = get_graphql_content(api_client.post_graphql(query, variables))
+ assert not response["data"]["checkoutShippingMethodUpdate"]["errors"]
+
+
[email protected]_queries(autouse=False)
+def test_add_billing_address_to_checkout_as_anonymous(
+ 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
+ }
+ }
+ }
+
+ 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"]
+
+
[email protected]_queries(autouse=False)
+def test_update_checkout_lines_as_anonymous(
+ api_client,
+ graphql_address_data,
+ checkout_with_variant,
+ 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
+ }
+ }
+
+ mutation updateCheckoutLine($checkoutId: ID!, $lines: [CheckoutLineInput]!) {
+ checkoutLinesUpdate(checkoutId: $checkoutId, lines: $lines) {
+ checkout {
+ id
+ lines {
+ ...CheckoutLine
+ }
+ totalPrice {
+ ...Price
+ }
+ subtotalPrice {
+ ...Price
+ }
+ isShippingRequired
+ }
+ errors {
+ field
+ message
+ }
+ }
+ }
+ """
+ variables = {
+ "checkoutId": Node.to_global_id("Checkout", checkout_with_variant.pk),
+ "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,
+ ),
+ },
+ ],
+ }
+ response = get_graphql_content(api_client.post_graphql(query, variables))
+ assert not response["data"]["checkoutLinesUpdate"]["errors"]
+
+
[email protected]_queries(autouse=False)
+def test_checkout_shipping_address_update_as_anonymous(
+ 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
+ }
+ }
+ }
+
+ 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 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
+ }
+ }
+ }
+ """
+ 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"]
+
+
[email protected]_queries(autouse=False)
+def test_checkout_voucher_code_as_anonymous(
+ 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
+ }
+ }
+ }
+ """
+ variables = {
+ "checkoutId": Node.to_global_id("Checkout", checkout_with_billing_address.pk),
+ "promoCode": voucher.code,
+ }
+ response = get_graphql_content(api_client.post_graphql(query, variables))
+ assert not response["data"]["checkoutAddPromoCode"]["errors"]
+
+
[email protected]_queries(autouse=False)
+def test_checkout_payment_charge_as_anonymous(
+ api_client, checkout_with_billing_address, count_queries
+):
+ query = """
+ mutation createPayment($input: PaymentInput!, $checkoutId: ID!) {
+ checkoutPaymentCreate(input: $input, checkoutId: $checkoutId) {
+ errors {
+ field
+ message
+ }
+ }
+ }
+ """
+
+ variables = {
+ "checkoutId": Node.to_global_id("Checkout", checkout_with_billing_address.pk),
+ "input": {
+ "amount": calculations.checkout_total(
+ checkout=checkout_with_billing_address
+ ).gross.amount,
+ "gateway": "Dummy",
+ "token": "charged",
+ },
+ }
+ response = get_graphql_content(api_client.post_graphql(query, variables))
+ assert not response["data"]["checkoutPaymentCreate"]["errors"]
+
+
[email protected]_queries(autouse=False)
+def test_complete_checkout(api_client, checkout_with_charged_payment, count_queries):
+ query = """
+ mutation completeCheckout($checkoutId: ID!) {
+ checkoutComplete(checkoutId: $checkoutId) {
+ errors {
+ field
+ message
+ }
+ order {
+ id
+ token
+ }
+ }
+ }
+ """
+
+ variables = {
+ "checkoutId": Node.to_global_id("Checkout", checkout_with_charged_payment.pk),
+ }
+
+ response = get_graphql_content(api_client.post_graphql(query, variables))
+ assert not response["data"]["checkoutComplete"]["errors"]