Skip to content

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

User type benchmarks (#7573)

Tomasz Szymańskib7ce641e6490
diff --git a/conftest.py b/conftest.py
index 62f7fb73b3..b2bca8e6f7 100644
--- a/conftest.py
+++ b/conftest.py
@@ -3,5 +3,6 @@ pytest_plugins = [
"saleor.plugins.tests.fixtures",
"saleor.graphql.tests.fixtures",
"saleor.graphql.channel.tests.fixtures",
+ "saleor.graphql.account.tests.benchmark.fixtures",
"saleor.graphql.order.tests.benchmark.fixtures",
]
diff --git a/saleor/graphql/account/tests/benchmark/fixtures.py b/saleor/graphql/account/tests/benchmark/fixtures.py
new file mode 100644
index 0000000000..10c9845e1e
--- /dev/null
+++ b/saleor/graphql/account/tests/benchmark/fixtures.py
@@ -0,0 +1,91 @@
+import random
+import uuid
+
+import pytest
+from django.contrib.auth import models as auth_models
+from prices import Money, TaxedMoney
+
+from .....account import CustomerEvents
+from .....account.models import CustomerEvent, User
+from .....giftcard.models import GiftCard
+from .....order.models import Order
+
+ORDER_COUNT_IN_BENCHMARKS = 10
+GIFT_CARDS_PER_USER = 5
+EVENTS_PER_USER = 5
+GROUPS_PER_USER = 5
+
+
+def _prepare_events_for_user(user):
+ events = [
+ CustomerEvent(type=random.choice(CustomerEvents.CHOICES)[0], user=user)
+ for i in range(EVENTS_PER_USER)
+ ]
+ return events
+
+
+def _prepare_gift_cards_for_user(user):
+ gift_cards = [
+ GiftCard(
+ user=user,
+ initial_balance_amount=random.randint(10, 20),
+ current_balance_amount=random.randint(10, 20),
+ code=str(uuid.uuid4())[:16],
+ )
+ for i in range(GIFT_CARDS_PER_USER)
+ ]
+ return gift_cards
+
+
+def _create_permission_groups(user):
+ _groups = [
+ auth_models.Group(name=str(uuid.uuid4())) for i in range(GROUPS_PER_USER)
+ ]
+ groups = auth_models.Group.objects.bulk_create(_groups)
+ user.groups.add(*groups)
+ return groups
+
+
+def users_for_customers_benchmarks(channel_USD, address, shipping_method):
+ users = [
+ User(
+ email=f"jane.doe.{i}@exmaple.com",
+ is_active=True,
+ default_billing_address=address.get_copy(),
+ default_shipping_address=address.get_copy(),
+ first_name=f"Jane_{i}",
+ last_name=f"Doe_{i}",
+ )
+ for i in range(ORDER_COUNT_IN_BENCHMARKS)
+ ]
+ users_for_customers_benchmarks = User.objects.bulk_create(users)
+ for user in users_for_customers_benchmarks:
+ user.addresses.add(user.default_shipping_address, user.default_billing_address)
+
+ orders = [
+ Order(
+ token=str(uuid.uuid4()),
+ channel=channel_USD,
+ billing_address=address.get_copy(),
+ shipping_address=address.get_copy(),
+ shipping_method=shipping_method,
+ user=users_for_customers_benchmarks[i],
+ total=TaxedMoney(net=Money(i, "USD"), gross=Money(i, "USD")),
+ )
+ for i in range(ORDER_COUNT_IN_BENCHMARKS)
+ ]
+ created_orders = Order.objects.bulk_create(orders)
+
+ events = []
+ gift_cards = []
+
+ for user in users_for_customers_benchmarks:
+ gift_cards.extend(_prepare_gift_cards_for_user(user))
+ events.extend(_prepare_events_for_user(user))
+ _create_permission_groups(user)
+
+ CustomerEvent.objects.bulk_create(events)
+ GiftCard.objects.bulk_create(gift_cards)
+
+ return created_orders
diff --git a/saleor/graphql/account/tests/benchmark/test_account.py b/saleor/graphql/account/tests/benchmark/test_account.py
index 9c1fa139ff..b9c6964608 100644
--- a/saleor/graphql/account/tests/benchmark/test_account.py
+++ b/saleor/graphql/account/tests/benchmark/test_account.py
@@ -328,3 +328,33 @@ def test_delete_staff_members(
assert not User.objects.filter(
id__in=[user.id for user in [staff_user1, staff_user2]]
).exists()
+
+
+CUSTOMERS_QUERY = """
+ query accounts {
+ customers(first: 10) {
+ edges {
+ node {
+ events { id }
+ orders(first: 10) { edges { node { id } } }
+ addresses { id }
+ giftCards(first: 10) { edges { node { id } } }
+ permissionGroups { id }
+ }
+ }
+ }
+ }
+"""
+
+
[email protected]_queries(autouse=False)
+def test_customers_query(
+ staff_api_client,
+ permission_manage_users,
+ users_for_customers_benchmarks,
+ count_queries,
+):
+ staff_api_client.user.user_permissions.set([permission_manage_users])
+ content = get_graphql_content(staff_api_client.post_graphql(CUSTOMERS_QUERY))
+ assert content["data"]["customers"] is not None
diff --git a/saleor/graphql/order/tests/benchmark/fixtures.py b/saleor/graphql/order/tests/benchmark/fixtures.py
index 728acd75a2..cc8f2b21c7 100644
--- a/saleor/graphql/order/tests/benchmark/fixtures.py
+++ b/saleor/graphql/order/tests/benchmark/fixtures.py
@@ -66,7 +66,7 @@ def _prepare_lines_for_order(order, variant_with_image):
@pytest.fixture
-def users_for_benchmarks(address):
+def users_for_order_benchmarks(address):
users = [
User(
email=f"john.doe.{i}@exmaple.com",
@@ -86,7 +86,7 @@ def orders_for_benchmarks(
channel_USD,
address,
payment_dummy,
- users_for_benchmarks,
+ users_for_order_benchmarks,
variant_with_image,
shipping_method,
):
@@ -97,7 +97,7 @@ def orders_for_benchmarks(
billing_address=address.get_copy(),
shipping_address=address.get_copy(),
shipping_method=shipping_method,
- user=users_for_benchmarks[i],
+ user=users_for_order_benchmarks[i],
total=TaxedMoney(net=Money(i, "USD"), gross=Money(i, "USD")),
)
for i in range(ORDER_COUNT_IN_BENCHMARKS)