Skip to content

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

Split fixtures in the GiftCard module (#16879)

Filip Owczareka5eeafa42f71
diff --git a/conftest.py b/conftest.py
index 0ff9c29f1b..ffc2fc400e 100644
--- a/conftest.py
+++ b/conftest.py
@@ -30,7 +30,6 @@ pytest_plugins = [
"saleor.graphql.tests.fixtures",
"saleor.graphql.checkout.tests.benchmark.fixtures",
"saleor.graphql.account.tests.benchmark.fixtures",
- "saleor.graphql.giftcard.tests.benchmark.fixtures",
"saleor.webhook.tests.fixtures",
"saleor.tax.tests.fixtures",
"saleor.channel.tests.fixtures",
@@ -43,6 +42,7 @@ pytest_plugins = [
"saleor.site.tests.fixtures",
"saleor.shipping.tests.fixtures",
"saleor.permission.tests.fixtures",
+ "saleor.giftcard.tests.fixtures",
]
diff --git a/saleor/giftcard/tests/fixtures/__init__.py b/saleor/giftcard/tests/fixtures/__init__.py
new file mode 100644
index 0000000000..1ddd4dfa7a
--- /dev/null
+++ b/saleor/giftcard/tests/fixtures/__init__.py
@@ -0,0 +1,3 @@
+from .giftcard import * # noqa: F403
+from .giftcard_event import * # noqa: F403
+from .giftcard_tag import * # noqa: F403
diff --git a/saleor/giftcard/tests/fixtures/giftcard.py b/saleor/giftcard/tests/fixtures/giftcard.py
new file mode 100644
index 0000000000..af05f3f5ce
--- /dev/null
+++ b/saleor/giftcard/tests/fixtures/giftcard.py
@@ -0,0 +1,147 @@
+import datetime
+import secrets
+
+import pytest
+
+from ....core.prices import Money
+from ....graphql.giftcard.tests.benchmark import GIFT_CARD_COUNT_IN_BENCHMARKS
+from ... import GiftCardEvents
+from ...models import GiftCard, GiftCardEvent, GiftCardTag
+
+
+def gift_card(customer_user):
+ gift_card = GiftCard.objects.create(
+ code="never_expiry",
+ created_by=customer_user,
+ created_by_email=customer_user.email,
+ initial_balance=Money(10, "USD"),
+ current_balance=Money(10, "USD"),
+ )
+ tag, _ = GiftCardTag.objects.get_or_create(name="test-tag")
+ gift_card.tags.add(tag)
+ return gift_card
+
+
+def gift_card_with_metadata(customer_user):
+ return GiftCard.objects.create(
+ code="card_with_meta",
+ created_by=customer_user,
+ created_by_email=customer_user.email,
+ initial_balance=Money(10, "USD"),
+ current_balance=Money(10, "USD"),
+ metadata={"test": "value"},
+ )
+
+
+def gift_card_expiry_date(customer_user):
+ gift_card = GiftCard.objects.create(
+ code="expiry_date",
+ created_by=customer_user,
+ created_by_email=customer_user.email,
+ initial_balance=Money(20, "USD"),
+ current_balance=Money(20, "USD"),
+ expiry_date=datetime.datetime.now(tz=datetime.UTC).date()
+ + datetime.timedelta(days=100),
+ )
+ tag = GiftCardTag.objects.create(name="another-tag")
+ gift_card.tags.add(tag)
+ return gift_card
+
+
+def gift_card_used(staff_user, customer_user):
+ gift_card = GiftCard.objects.create(
+ code="giftcard_used",
+ created_by=staff_user,
+ used_by=customer_user,
+ created_by_email=staff_user.email,
+ used_by_email=customer_user.email,
+ initial_balance=Money(100, "USD"),
+ current_balance=Money(80, "USD"),
+ )
+ tag = GiftCardTag.objects.create(name="tag")
+ gift_card.tags.add(tag)
+ return gift_card
+
+
+def gift_card_created_by_staff(staff_user):
+ gift_card = GiftCard.objects.create(
+ code="created_by_staff",
+ created_by=staff_user,
+ created_by_email=staff_user.email,
+ initial_balance=Money(10, "USD"),
+ current_balance=Money(10, "USD"),
+ )
+ tag, _ = GiftCardTag.objects.get_or_create(name="test-tag")
+ gift_card.tags.add(tag)
+ return gift_card
+
+
+def gift_card_list():
+ gift_cards = list(
+ GiftCard.objects.bulk_create(
+ [
+ GiftCard(
+ code="code-test-1",
+ initial_balance=Money(10, "USD"),
+ current_balance=Money(10, "USD"),
+ ),
+ GiftCard(
+ code="code-test-2",
+ initial_balance=Money(10, "USD"),
+ current_balance=Money(10, "USD"),
+ ),
+ GiftCard(
+ code="code-test-3",
+ initial_balance=Money(10, "USD"),
+ current_balance=Money(10, "USD"),
+ ),
+ ]
+ )
+ )
+ return gift_cards
+
+
+def gift_cards_for_benchmarks(
+ staff_user,
+ gift_card,
+ customer_user,
+):
+ gift_cards = [
+ GiftCard(
+ code=secrets.token_hex(8),
+ created_by=customer_user,
+ created_by_email=customer_user.email,
+ initial_balance=Money(10, "USD"),
+ current_balance=Money(10, "USD"),
+ )
+ for _ in range(GIFT_CARD_COUNT_IN_BENCHMARKS)
+ ]
+ created_gift_cards = GiftCard.objects.bulk_create(gift_cards)
+ tag = GiftCardTag.objects.create(name="benchmark-test-tag")
+ tag.gift_cards.add(*created_gift_cards)
+
+ parameters = {
+ "message": "test message",
+ "email": "[email protected]",
+ }
+ events = []
+ for gift_card in gift_cards:
+ events.append(
+ GiftCardEvent(
+ user=staff_user,
+ gift_card=gift_card,
+ type=GiftCardEvents.ISSUED,
+ parameters=parameters,
+ )
+ )
+
+ GiftCardEvent.objects.bulk_create(events)
+
+ return created_gift_cards
diff --git a/saleor/giftcard/tests/fixtures/giftcard_event.py b/saleor/giftcard/tests/fixtures/giftcard_event.py
new file mode 100644
index 0000000000..c31061b34a
--- /dev/null
+++ b/saleor/giftcard/tests/fixtures/giftcard_event.py
@@ -0,0 +1,35 @@
+import datetime
+
+import pytest
+from django.utils import timezone
+
+from ... import GiftCardEvents
+from ...models import GiftCardEvent
+
+
+def gift_card_event(gift_card, order, app, staff_user):
+ parameters = {
+ "message": "test message",
+ "email": "[email protected]",
+ "tags": ["test tag"],
+ "old_tags": ["test old tag"],
+ "balance": {
+ "currency": "USD",
+ "initial_balance": 10,
+ "old_initial_balance": 20,
+ "current_balance": 10,
+ "old_current_balance": 5,
+ },
+ "expiry_date": datetime.date(2050, 1, 1),
+ "old_expiry_date": datetime.date(2010, 1, 1),
+ }
+ return GiftCardEvent.objects.create(
+ user=staff_user,
+ app=app,
+ gift_card=gift_card,
+ order=order,
+ type=GiftCardEvents.UPDATED,
+ parameters=parameters,
+ date=timezone.now() + datetime.timedelta(days=10),
+ )
diff --git a/saleor/giftcard/tests/fixtures/giftcard_tag.py b/saleor/giftcard/tests/fixtures/giftcard_tag.py
new file mode 100644
index 0000000000..d6727a8bec
--- /dev/null
+++ b/saleor/giftcard/tests/fixtures/giftcard_tag.py
@@ -0,0 +1,9 @@
+import pytest
+
+from ...models import GiftCardTag
+
+
+def gift_card_tag_list(db):
+ tags = [GiftCardTag(name=f"test-tag-{i}") for i in range(5)]
+ return GiftCardTag.objects.bulk_create(tags)
diff --git a/saleor/graphql/giftcard/tests/benchmark/__init__.py b/saleor/graphql/giftcard/tests/benchmark/__init__.py
index e69de29bb2..44b316ed04 100644
--- a/saleor/graphql/giftcard/tests/benchmark/__init__.py
+++ b/saleor/graphql/giftcard/tests/benchmark/__init__.py
@@ -0,0 +1 @@
+GIFT_CARD_COUNT_IN_BENCHMARKS = 20
diff --git a/saleor/graphql/giftcard/tests/benchmark/fixtures.py b/saleor/graphql/giftcard/tests/benchmark/fixtures.py
deleted file mode 100644
index 54eb5e78b9..0000000000
--- a/saleor/graphql/giftcard/tests/benchmark/fixtures.py
+++ /dev/null
@@ -1,49 +0,0 @@
-import secrets
-
-import pytest
-from prices import Money
-
-from .....giftcard import GiftCardEvents
-from .....giftcard.models import GiftCard, GiftCardEvent, GiftCardTag
-
-GIFT_CARD_COUNT_IN_BENCHMARKS = 20
-
-
-def gift_cards_for_benchmarks(
- staff_user,
- gift_card,
- customer_user,
-):
- gift_cards = [
- GiftCard(
- code=secrets.token_hex(8),
- created_by=customer_user,
- created_by_email=customer_user.email,
- initial_balance=Money(10, "USD"),
- current_balance=Money(10, "USD"),
- )
- for _ in range(GIFT_CARD_COUNT_IN_BENCHMARKS)
- ]
- created_gift_cards = GiftCard.objects.bulk_create(gift_cards)
- tag = GiftCardTag.objects.create(name="benchmark-test-tag")
- tag.gift_cards.add(*created_gift_cards)
-
- parameters = {
- "message": "test message",
- "email": "[email protected]",
- }
- events = []
- for gift_card in gift_cards:
- events.append(
- GiftCardEvent(
- user=staff_user,
- gift_card=gift_card,
- type=GiftCardEvents.ISSUED,
- parameters=parameters,
- )
- )
-
- GiftCardEvent.objects.bulk_create(events)
-
- return created_gift_cards
diff --git a/saleor/tests/fixtures.py b/saleor/tests/fixtures.py
index b55d104b93..024811154a 100644
--- a/saleor/tests/fixtures.py
+++ b/saleor/tests/fixtures.py
@@ -66,8 +66,6 @@ from ..discount.models import (
VoucherCustomer,
VoucherTranslation,
)
-from ..giftcard import GiftCardEvents
-from ..giftcard.models import GiftCard, GiftCardEvent, GiftCardTag
from ..payment import ChargeStatus, TransactionKind
from ..payment.interface import AddressData, GatewayConfig, GatewayResponse, PaymentData
from ..payment.models import Payment, TransactionEvent, TransactionItem
@@ -2101,138 +2099,6 @@ def checkout_line_with_reserved_preorder_item(
return checkout_line
-def gift_card_tag_list(db):
- tags = [GiftCardTag(name=f"test-tag-{i}") for i in range(5)]
- return GiftCardTag.objects.bulk_create(tags)
-
-
-def gift_card(customer_user):
- gift_card = GiftCard.objects.create(
- code="never_expiry",
- created_by=customer_user,
- created_by_email=customer_user.email,
- initial_balance=Money(10, "USD"),
- current_balance=Money(10, "USD"),
- )
- tag, _ = GiftCardTag.objects.get_or_create(name="test-tag")
- gift_card.tags.add(tag)
- return gift_card
-
-
-def gift_card_with_metadata(customer_user):
- return GiftCard.objects.create(
- code="card_with_meta",
- created_by=customer_user,
- created_by_email=customer_user.email,
- initial_balance=Money(10, "USD"),
- current_balance=Money(10, "USD"),
- metadata={"test": "value"},
- )
-
-
-def gift_card_expiry_date(customer_user):
- gift_card = GiftCard.objects.create(
- code="expiry_date",
- created_by=customer_user,
- created_by_email=customer_user.email,
- initial_balance=Money(20, "USD"),
- current_balance=Money(20, "USD"),
- expiry_date=datetime.datetime.now(tz=datetime.UTC).date()
- + datetime.timedelta(days=100),
- )
- tag = GiftCardTag.objects.create(name="another-tag")
- gift_card.tags.add(tag)
- return gift_card
-
-
-def gift_card_used(staff_user, customer_user):
- gift_card = GiftCard.objects.create(
- code="giftcard_used",
- created_by=staff_user,
- used_by=customer_user,
- created_by_email=staff_user.email,
- used_by_email=customer_user.email,
- initial_balance=Money(100, "USD"),
- current_balance=Money(80, "USD"),
- )
- tag = GiftCardTag.objects.create(name="tag")
- gift_card.tags.add(tag)
- return gift_card
-
-
-def gift_card_created_by_staff(staff_user):
- gift_card = GiftCard.objects.create(
- code="created_by_staff",
- created_by=staff_user,
- created_by_email=staff_user.email,
- initial_balance=Money(10, "USD"),
- current_balance=Money(10, "USD"),
- )
- tag, _ = GiftCardTag.objects.get_or_create(name="test-tag")
- gift_card.tags.add(tag)
- return gift_card
-
-
-def gift_card_event(gift_card, order, app, staff_user):
- parameters = {
- "message": "test message",
- "email": "[email protected]",
- "tags": ["test tag"],
- "old_tags": ["test old tag"],
- "balance": {
- "currency": "USD",
- "initial_balance": 10,
- "old_initial_balance": 20,
- "current_balance": 10,
- "old_current_balance": 5,
- },
- "expiry_date": datetime.date(2050, 1, 1),
- "old_expiry_date": datetime.date(2010, 1, 1),
- }
- return GiftCardEvent.objects.create(
- user=staff_user,
- app=app,
- gift_card=gift_card,
- order=order,
- type=GiftCardEvents.UPDATED,
- parameters=parameters,
- date=timezone.now() + datetime.timedelta(days=10),
- )
-
-
-def gift_card_list():
- gift_cards = list(
- GiftCard.objects.bulk_create(
- [
- GiftCard(
- code="code-test-1",
- initial_balance=Money(10, "USD"),
- current_balance=Money(10, "USD"),
- ),
- GiftCard(
- code="code-test-2",
- initial_balance=Money(10, "USD"),
- current_balance=Money(10, "USD"),
- ),
- GiftCard(
- code="code-test-3",
- initial_balance=Money(10, "USD"),
- current_balance=Money(10, "USD"),
- ),
- ]
- )
- )
- return gift_cards
-
-
@pytest.fixture
def payment_txn_preauth(order_with_lines, payment_dummy):
order = order_with_lines