Skip to content

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

Split fixtures in discount module (#16878)

Piotr Zabieglika326594adcab
diff --git a/conftest.py b/conftest.py
index ffc2fc400e..b02cedb6e1 100644
--- a/conftest.py
+++ b/conftest.py
@@ -43,6 +43,7 @@ pytest_plugins = [
"saleor.shipping.tests.fixtures",
"saleor.permission.tests.fixtures",
"saleor.giftcard.tests.fixtures",
+ "saleor.discount.tests.fixtures",
]
diff --git a/saleor/discount/tests/fixtures/__init__.py b/saleor/discount/tests/fixtures/__init__.py
new file mode 100644
index 0000000000..6d4c74e59a
--- /dev/null
+++ b/saleor/discount/tests/fixtures/__init__.py
@@ -0,0 +1,4 @@
+from .benchmark import * # noqa: F403
+from .promotion import * # noqa: F403
+from .promotion_rule import * # noqa: F403
+from .voucher import * # noqa: F403
diff --git a/saleor/discount/tests/fixtures/benchmark.py b/saleor/discount/tests/fixtures/benchmark.py
new file mode 100644
index 0000000000..53ce829cb4
--- /dev/null
+++ b/saleor/discount/tests/fixtures/benchmark.py
@@ -0,0 +1,106 @@
+from decimal import Decimal
+
+import graphene
+import pytest
+
+from ....discount import RewardValueType
+from ....discount.models import (
+ Promotion,
+ PromotionRule,
+ PromotionRuleTranslation,
+ PromotionTranslation,
+)
+
+
+def promotion_list_for_benchmark(channel_USD, channel_PLN, product_list):
+ promotions = Promotion.objects.bulk_create(
+ [Promotion(name=f"Promotion-{i}") for i in range(30)]
+ )
+ rules = [
+ PromotionRule(
+ promotion=promotion,
+ catalogue_predicate={},
+ reward_value_type=RewardValueType.PERCENTAGE,
+ reward_value=Decimal("10"),
+ )
+ for promotion in promotions
+ ]
+ for rule, product in zip(rules, product_list):
+ rule.catalogue_predicate = {
+ "productPredicate": {
+ "ids": [graphene.Node.to_global_id("Product", product.id)]
+ }
+ }
+
+ PromotionRule.objects.bulk_create(rules)
+
+ channel_PLN.promotionrule_set.add(*rules)
+ channel_USD.promotionrule_set.add(*rules)
+
+ # TODO: add translations
+ promotion_translations = []
+ for promotion in promotions:
+ promotion_translations.append(
+ PromotionTranslation(
+ language_code="pl",
+ promotion=promotion,
+ name="Polish promotion name",
+ )
+ )
+ PromotionTranslation.objects.bulk_create(promotion_translations)
+
+ promotion_rule_translations = []
+ for rule in rules:
+ promotion_rule_translations.append(
+ PromotionRuleTranslation(
+ language_code="pl",
+ promotion_rule=rule,
+ name="Polish promotion rule name",
+ )
+ )
+ PromotionRuleTranslation.objects.bulk_create(promotion_rule_translations)
+
+ return promotions
+
+
+def promotion_converted_from_sale_list_for_benchmark(channel_USD, channel_PLN):
+ promotions = Promotion.objects.bulk_create(
+ [Promotion(name="Sale1"), Promotion(name="Sale2"), Promotion(name="Sale2")]
+ )
+ for promotion in promotions:
+ promotion.assign_old_sale_id()
+
+ values = [15, 5, 25]
+ usd_rules, pln_rules = [], []
+ for promotion, value in zip(promotions, values):
+ usd_rules.append(
+ PromotionRule(
+ promotion=promotion,
+ catalogue_predicate={},
+ reward_value_type=RewardValueType.FIXED,
+ reward_value=value,
+ )
+ )
+ pln_rules.append(
+ PromotionRule(
+ promotion=promotion,
+ catalogue_predicate={},
+ reward_value_type=RewardValueType.FIXED,
+ reward_value=value * 2,
+ )
+ )
+ PromotionRule.objects.bulk_create(usd_rules + pln_rules)
+ PromotionRuleChannel = PromotionRule.channels.through
+ usd_rules_channels = [
+ PromotionRuleChannel(promotionrule_id=rule.id, channel_id=channel_USD.id)
+ for rule in usd_rules
+ ]
+ pln_rules_channels = [
+ PromotionRuleChannel(promotionrule_id=rule.id, channel_id=channel_PLN.id)
+ for rule in usd_rules
+ ]
+ PromotionRuleChannel.objects.bulk_create(usd_rules_channels + pln_rules_channels)
+
+ return promotions
diff --git a/saleor/discount/tests/fixtures/promotion.py b/saleor/discount/tests/fixtures/promotion.py
new file mode 100644
index 0000000000..51cdfa69ac
--- /dev/null
+++ b/saleor/discount/tests/fixtures/promotion.py
@@ -0,0 +1,335 @@
+import datetime
+from decimal import Decimal
+
+import graphene
+import pytest
+from django.utils import timezone
+
+from ....product.utils.variants import fetch_variants_for_promotion_rules
+from ....tests.utils import dummy_editorjs
+from ... import PromotionType, RewardType, RewardValueType
+from ...models import Promotion, PromotionRule
+
+
+def catalogue_promotion(channel_USD, product, collection):
+ promotion = Promotion.objects.create(
+ name="Promotion",
+ type=PromotionType.CATALOGUE,
+ description=dummy_editorjs("Test description."),
+ end_date=timezone.now() + datetime.timedelta(days=30),
+ )
+ rules = PromotionRule.objects.bulk_create(
+ [
+ PromotionRule(
+ name="Percentage promotion rule",
+ promotion=promotion,
+ description=dummy_editorjs(
+ "Test description for percentage promotion rule."
+ ),
+ catalogue_predicate={
+ "productPredicate": {
+ "ids": [graphene.Node.to_global_id("Product", product.id)]
+ }
+ },
+ reward_value_type=RewardValueType.PERCENTAGE,
+ reward_value=Decimal("10"),
+ ),
+ PromotionRule(
+ name="Fixed promotion rule",
+ promotion=promotion,
+ description=dummy_editorjs(
+ "Test description for fixes promotion rule."
+ ),
+ catalogue_predicate={
+ "collectionPredicate": {
+ "ids": [graphene.Node.to_global_id("Collection", collection.id)]
+ }
+ },
+ reward_value_type=RewardValueType.FIXED,
+ reward_value=Decimal("5"),
+ ),
+ ]
+ )
+ for rule in rules:
+ rule.channels.add(channel_USD)
+ fetch_variants_for_promotion_rules(promotion.rules.all())
+ return promotion
+
+
+def catalogue_promotion_without_rules(db):
+ promotion = Promotion.objects.create(
+ name="Promotion",
+ description=dummy_editorjs("Test description."),
+ end_date=timezone.now() + datetime.timedelta(days=30),
+ type=PromotionType.CATALOGUE,
+ )
+ return promotion
+
+
+def order_promotion_without_rules(db):
+ promotion = Promotion.objects.create(
+ name="Promotion",
+ description=dummy_editorjs("Test description."),
+ end_date=timezone.now() + datetime.timedelta(days=30),
+ type=PromotionType.ORDER,
+ )
+ return promotion
+
+
+def catalogue_promotion_with_single_rule(catalogue_predicate, channel_USD):
+ promotion = Promotion.objects.create(
+ name="Promotion with single rule", type=PromotionType.CATALOGUE
+ )
+ rule = PromotionRule.objects.create(
+ name="Sale rule",
+ promotion=promotion,
+ catalogue_predicate=catalogue_predicate,
+ reward_value_type=RewardValueType.FIXED,
+ reward_value=Decimal(5),
+ )
+ rule.channels.add(channel_USD)
+ return promotion
+
+
+def order_promotion_with_rule(channel_USD):
+ promotion = Promotion.objects.create(
+ name="Promotion with order rule", type=PromotionType.ORDER
+ )
+ rule = PromotionRule.objects.create(
+ name="Promotion rule",
+ promotion=promotion,
+ order_predicate={
+ "discountedObjectPredicate": {"baseSubtotalPrice": {"range": {"gte": 100}}}
+ },
+ reward_value_type=RewardValueType.FIXED,
+ reward_value=Decimal(5),
+ reward_type=RewardType.SUBTOTAL_DISCOUNT,
+ )
+ rule.channels.add(channel_USD)
+ return promotion
+
+
+def promotion_list(channel_USD, product, collection):
+ collection.products.add(product)
+ promotions = Promotion.objects.bulk_create(
+ [
+ Promotion(
+ name="Promotion 1",
+ type=PromotionType.CATALOGUE,
+ description=dummy_editorjs("Promotion 1 description."),
+ start_date=timezone.now() + datetime.timedelta(days=1),
+ end_date=timezone.now() + datetime.timedelta(days=10),
+ ),
+ Promotion(
+ name="Promotion 2",
+ type=PromotionType.CATALOGUE,
+ description=dummy_editorjs("Promotion 2 description."),
+ start_date=timezone.now() + datetime.timedelta(days=5),
+ end_date=timezone.now() + datetime.timedelta(days=20),
+ ),
+ Promotion(
+ name="Promotion 3",
+ type=PromotionType.CATALOGUE,
+ description=dummy_editorjs("TePromotion 3 description."),
+ start_date=timezone.now() + datetime.timedelta(days=15),
+ end_date=timezone.now() + datetime.timedelta(days=30),
+ ),
+ ]
+ )
+ rules = PromotionRule.objects.bulk_create(
+ [
+ PromotionRule(
+ name="Promotion 1 percentage rule",
+ promotion=promotions[0],
+ description=dummy_editorjs(
+ "Test description for promotion 1 percentage rule."
+ ),
+ catalogue_predicate={
+ "productPredicate": {
+ "ids": [graphene.Node.to_global_id("Product", product.id)]
+ }
+ },
+ reward_value_type=RewardValueType.PERCENTAGE,
+ reward_value=Decimal("10"),
+ ),
+ PromotionRule(
+ name="Promotion 1 fixed rule",
+ promotion=promotions[0],
+ description=dummy_editorjs(
+ "Test description for promotion 1 fixed rule."
+ ),
+ catalogue_predicate={
+ "collectionPredicate": {
+ "ids": [graphene.Node.to_global_id("Collection", collection.id)]
+ }
+ },
+ reward_value_type=RewardValueType.FIXED,
+ reward_value=Decimal("5"),
+ ),
+ PromotionRule(
+ name="Promotion 2 percentage rule",
+ promotion=promotions[1],
+ description=dummy_editorjs(
+ "Test description for promotion 2 percentage rule."
+ ),
+ catalogue_predicate={
+ "productPredicate": {
+ "ids": [graphene.Node.to_global_id("Product", product.id)]
+ }
+ },
+ reward_value_type=RewardValueType.PERCENTAGE,
+ reward_value=Decimal("10"),
+ ),
+ PromotionRule(
+ name="Promotion 3 fixed rule",
+ promotion=promotions[2],
+ description=dummy_editorjs(
+ "Test description for promotion 3 fixed rule."
+ ),
+ catalogue_predicate={
+ "collectionPredicate": {
+ "ids": [graphene.Node.to_global_id("Collection", collection.id)]
+ }
+ },
+ reward_value_type=RewardValueType.FIXED,
+ reward_value=Decimal("5"),
+ ),
+ ]
+ )
+ for rule in rules:
+ rule.channels.add(channel_USD)
+ fetch_variants_for_promotion_rules(PromotionRule.objects.all())
+ return promotions
+
+
+def promotion_10_percentage(channel_USD, product_list, product):
+ promotion = Promotion.objects.create(
+ name="Promotion",
+ )
+ product_list.append(product)
+ rule = promotion.rules.create(
+ name="10% promotion rule",
+ catalogue_predicate={
+ "productPredicate": {
+ "ids": [
+ graphene.Node.to_global_id("Product", product.id)
+ for product in product_list
+ ]
+ }
+ },
+ reward_value_type=RewardValueType.PERCENTAGE,
+ reward_value=Decimal("10"),
+ )
+ rule.channels.add(channel_USD)
+ return promotion
+
+
+def promotion_converted_from_sale(catalogue_predicate, channel_USD):
+ promotion = Promotion.objects.create(name="Sale", type=PromotionType.CATALOGUE)
+ promotion.assign_old_sale_id()
+
+ rule = PromotionRule.objects.create(
+ name="Sale rule",
+ promotion=promotion,
+ catalogue_predicate=catalogue_predicate,
+ reward_value_type=RewardValueType.FIXED,
+ reward_value=Decimal(5),
+ old_channel_listing_id=PromotionRule.get_old_channel_listing_ids(1)[0][0],
+ )
+ rule.channels.add(channel_USD)
+ fetch_variants_for_promotion_rules(promotion.rules.all())
+ return promotion
+
+
+def promotion_converted_from_sale_with_many_channels(
+ promotion_converted_from_sale, catalogue_predicate, channel_PLN
+):
+ promotion = promotion_converted_from_sale
+ rule = PromotionRule.objects.create(
+ name="Sale rule 2",
+ promotion=promotion,
+ catalogue_predicate=catalogue_predicate,
+ reward_value_type=RewardValueType.FIXED,
+ reward_value=Decimal(5),
+ old_channel_listing_id=PromotionRule.get_old_channel_listing_ids(1)[0][0],
+ )
+ rule.channels.add(channel_PLN)
+ fetch_variants_for_promotion_rules(promotion.rules.all())
+ return promotion
+
+
+def promotion_converted_from_sale_with_empty_predicate(channel_USD):
+ promotion = Promotion.objects.create(
+ name="Sale with empty predicate", type=PromotionType.CATALOGUE
+ )
+ promotion.assign_old_sale_id()
+ rule = PromotionRule.objects.create(
+ name="Sale with empty predicate rule",
+ promotion=promotion,
+ catalogue_predicate={},
+ reward_value_type=RewardValueType.FIXED,
+ reward_value=Decimal(5),
+ old_channel_listing_id=PromotionRule.get_old_channel_listing_ids(1)[0][0],
+ )
+ rule.channels.add(channel_USD)
+ return promotion
+
+
+def promotion_converted_from_sale_list(channel_USD, product_list, category, collection):
+ promotions = Promotion.objects.bulk_create(
+ [Promotion(name="Sale 1"), Promotion(name="Sale 2"), Promotion(name="Sale 3")]
+ )
+ for promotion in promotions:
+ promotion.assign_old_sale_id()
+ predicates = [
+ {
+ "OR": [
+ {
+ "productPredicate": {
+ "ids": [graphene.Node.to_global_id("Product", product.id)]
+ }
+ },
+ {
+ "variantPredicate": {
+ "ids": [
+ graphene.Node.to_global_id(
+ "Product", product.variants.first().id
+ )
+ ]
+ }
+ },
+ ]
+ }
+ for product in product_list
+ ]
+ collection_id = graphene.Node.to_global_id("Collection", collection.id)
+ category_id = graphene.Node.to_global_id("Category", category.id)
+
+ predicates[0]["OR"].append({"categoryPredicate": {"ids": [category_id]}})
+ predicates[1]["OR"].append({"collectionPredicate": {"ids": [collection_id]}})
+
+ rules = [
+ PromotionRule(
+ promotion=promotion,
+ catalogue_predicate=predicate,
+ reward_value_type=RewardValueType.FIXED,
+ reward_value=Decimal("5"),
+ )
+ for promotion, predicate in zip(promotions, predicates)
+ ]
+ PromotionRule.objects.bulk_create(rules)
+ for rule in rules:
+ rule.channels.add(channel_USD)
+
+ return promotions
diff --git a/saleor/discount/tests/fixtures/promotion_rule.py b/saleor/discount/tests/fixtures/promotion_rule.py
new file mode 100644
index 0000000000..31636e1b2c
--- /dev/null
+++ b/saleor/discount/tests/fixtures/promotion_rule.py
@@ -0,0 +1,97 @@
+from decimal import Decimal
+
+import graphene
+import pytest
+
+from ....tests.utils import dummy_editorjs
+from ... import RewardType, RewardValueType
+from ...interface import VariantPromotionRuleInfo
+from ...models import PromotionRule
+
+
+def promotion_rule(channel_USD, catalogue_promotion, product):
+ rule = PromotionRule.objects.create(
+ name="Promotion rule name",
+ promotion=catalogue_promotion,
+ description=dummy_editorjs("Test description for percentage promotion rule."),
+ catalogue_predicate={
+ "productPredicate": {
+ "ids": [graphene.Node.to_global_id("Product", product.id)]
+ }
+ },
+ reward_value_type=RewardValueType.PERCENTAGE,
+ reward_value=Decimal("25"),
+ )
+ rule.channels.add(channel_USD)
+ return rule
+
+
+def order_promotion_rule(channel_USD, order_promotion_without_rules):
+ rule = PromotionRule.objects.create(
+ name="Order promotion rule",
+ promotion=order_promotion_without_rules,
+ order_predicate={
+ "discountedObjectPredicate": {"baseSubtotalPrice": {"range": {"gte": 20}}}
+ },
+ reward_value_type=RewardValueType.PERCENTAGE,
+ reward_value=Decimal("25"),
+ reward_type=RewardType.SUBTOTAL_DISCOUNT,
+ )
+ rule.channels.add(channel_USD)
+ return rule
+
+
+def gift_promotion_rule(channel_USD, order_promotion_without_rules, product_list):
+ rule = PromotionRule.objects.create(
+ name="Order promotion rule",
+ promotion=order_promotion_without_rules,
+ order_predicate={
+ "discountedObjectPredicate": {"baseSubtotalPrice": {"range": {"gte": 20}}}
+ },
+ reward_type=RewardType.GIFT,
+ )
+ rule.channels.add(channel_USD)
+ rule.gifts.set([product.variants.first() for product in product_list[:2]])
+ return rule
+
+
+def rule_info(
+ promotion_rule,
+ promotion_translation_fr,
+ promotion_rule_translation_fr,
+ variant,
+ channel_USD,
+):
+ variant_channel_listing = variant.channel_listings.get(channel_id=channel_USD.id)
+ listing_promotion_rule = variant_channel_listing.variantlistingpromotionrule.create(
+ promotion_rule=promotion_rule,
+ discount_amount=Decimal("10"),
+ currency=channel_USD.currency_code,
+ )
+ return VariantPromotionRuleInfo(
+ rule=promotion_rule,
+ promotion=promotion_rule.promotion,
+ variant_listing_promotion_rule=listing_promotion_rule,
+ promotion_translation=promotion_translation_fr,
+ rule_translation=promotion_rule_translation_fr,
+ )
+
+
+def catalogue_predicate(product, category, collection, variant):
+ collection_id = graphene.Node.to_global_id("Collection", collection.id)
+ category_id = graphene.Node.to_global_id("Category", category.id)
+ product_id = graphene.Node.to_global_id("Product", product.id)
+ variant_id = graphene.Node.to_global_id("ProductVariant", variant.id)
+ return {
+ "OR": [
+ {"collectionPredicate": {"ids": [collection_id]}},
+ {"categoryPredicate": {"ids": [category_id]}},
+ {"productPredicate": {"ids": [product_id]}},
+ {"variantPredicate": {"ids": [variant_id]}},
+ ]
+ }
diff --git a/saleor/discount/tests/fixtures/voucher.py b/saleor/discount/tests/fixtures/voucher.py
new file mode 100644
index 0000000000..72b71fd192
--- /dev/null
+++ b/saleor/discount/tests/fixtures/voucher.py
@@ -0,0 +1,221 @@
+import pytest
+from django_countries import countries
+from prices import Money
+
+from ... import DiscountValueType, VoucherType
+from ...models import Voucher, VoucherChannelListing, VoucherCode, VoucherCustomer
+
+
+def voucher_without_channel(db):
+ voucher = Voucher.objects.create()
+ VoucherCode.objects.create(code="mirumee", voucher=voucher)
+ return voucher
+
+
+def voucher(voucher_without_channel, channel_USD):
+ VoucherChannelListing.objects.create(
+ voucher=voucher_without_channel,
+ channel=channel_USD,
+ discount=Money(20, channel_USD.currency_code),
+ )
+ return voucher_without_channel
+
+
+def voucher_with_many_codes(voucher):
+ VoucherCode.objects.bulk_create(
+ [
+ VoucherCode(code="Multi1", voucher=voucher),
+ VoucherCode(code="Multi2", voucher=voucher),
+ VoucherCode(code="Multi3", voucher=voucher),
+ VoucherCode(code="Multi4", voucher=voucher),
+ ]
+ )
+ return voucher
+
+
+def voucher_with_many_channels(voucher, channel_PLN):
+ VoucherChannelListing.objects.create(
+ voucher=voucher,
+ channel=channel_PLN,
+ discount=Money(80, channel_PLN.currency_code),
+ )
+ return voucher
+
+
+def voucher_percentage(channel_USD):
+ voucher = Voucher.objects.create(
+ discount_value_type=DiscountValueType.PERCENTAGE,
+ )
+ VoucherCode.objects.create(code="saleor", voucher=voucher)
+ VoucherChannelListing.objects.create(
+ voucher=voucher,
+ channel=channel_USD,
+ discount_value=10,
+ currency=channel_USD.currency_code,
+ )
+ return voucher
+
+
+def voucher_specific_product_type(voucher_percentage, product):
+ voucher_percentage.products.add(product)
+ voucher_percentage.type = VoucherType.SPECIFIC_PRODUCT
+ voucher_percentage.save()
+ return voucher_percentage
+
+
+def voucher_with_high_min_spent_amount(channel_USD):
+ voucher = Voucher.objects.create()
+ VoucherCode.objects.create(code="mirumee", voucher=voucher)
+ VoucherChannelListing.objects.create(
+ voucher=voucher,
+ channel=channel_USD,
+ discount=Money(10, channel_USD.currency_code),
+ min_spent_amount=1_000_000,
+ )
+ return voucher
+
+
+def voucher_shipping_type(channel_USD):
+ voucher = Voucher.objects.create(type=VoucherType.SHIPPING, countries="IS")
+ VoucherCode.objects.create(code="mirumee", voucher=voucher)
+ VoucherChannelListing.objects.create(
+ voucher=voucher,
+ channel=channel_USD,
+ discount=Money(10, channel_USD.currency_code),
+ )
+ return voucher
+
+
+def voucher_free_shipping(voucher_percentage, channel_USD):
+ voucher_percentage.type = VoucherType.SHIPPING
+ voucher_percentage.name = "Free shipping"
+ voucher_percentage.save()
+ voucher_percentage.channel_listings.filter(channel=channel_USD).update(
+ discount_value=100
+ )
+ return voucher_percentage
+
+
+def voucher_customer(voucher, customer_user):
+ email = customer_user.email
+ code = voucher.codes.first()
+ return VoucherCustomer.objects.create(voucher_code=code, customer_email=email)
+
+
+def voucher_multiple_use(voucher_with_many_codes):
+ voucher = voucher_with_many_codes
+ voucher.usage_limit = 3
+ voucher.save(update_fields=["usage_limit"])
+ codes = voucher.codes.all()
+ for code in codes:
+ code.used = 1
+ VoucherCode.objects.bulk_update(codes, ["used"])
+ voucher.refresh_from_db()
+ return voucher
+
+
+def voucher_single_use(voucher_with_many_codes):
+ voucher = voucher_with_many_codes
+ voucher.single_use = True
+ voucher.save(update_fields=["single_use"])
+ return voucher
+
+
+def voucher_with_many_channels_and_countries(voucher_with_many_channels):
+ voucher_with_many_channels.countries = countries
+ voucher_with_many_channels.save(update_fields=["countries"])
+ return voucher_with_many_channels
+
+
+def voucher_list(channel_USD):
+ [voucher_1, voucher_2, voucher_3] = Voucher.objects.bulk_create(
+ [
+ Voucher(),
+ Voucher(),
+ Voucher(),
+ ]
+ )
+
+ VoucherCode.objects.bulk_create(
+ [
+ VoucherCode(code="voucher-1", voucher=voucher_1),
+ VoucherCode(code="voucher-2", voucher=voucher_1),
+ VoucherCode(code="voucher-3", voucher=voucher_2),
+ ]
+ )
+ VoucherChannelListing.objects.bulk_create(
+ [
+ VoucherChannelListing(
+ voucher=voucher_1,
+ channel=channel_USD,
+ discount_value=1,
+ currency=channel_USD.currency_code,
+ ),
+ VoucherChannelListing(
+ voucher=voucher_2,
+ channel=channel_USD,
+ discount_value=2,
+ currency=channel_USD.currency_code,
+ ),
+ VoucherChannelListing(
+ voucher=voucher_3,
+ channel=channel_USD,
+ discount_value=3,
+ currency=channel_USD.currency_code,
+ ),
+ ]
+ )
+ return voucher_1, voucher_2, voucher_3
+
+
+def vouchers_list(channel_USD, channel_PLN):
+ vouchers = Voucher.objects.bulk_create(
+ [
+ Voucher(name="Voucher1"),
+ Voucher(name="Voucher2"),
+ Voucher(name="Voucher3"),
+ ]
+ )
+ VoucherCode.objects.bulk_create(
+ [
+ VoucherCode(code="Voucher1", voucher=vouchers[0]),
+ VoucherCode(code="Voucher2", voucher=vouchers[1]),
+ VoucherCode(code="Voucher3", voucher=vouchers[2]),
+ ]
+ )
+ values = [15, 5, 25]
+ voucher_channel_listings = []
+ for voucher, value in zip(vouchers, values):
+ voucher_channel_listings.append(
+ VoucherChannelListing(
+ voucher=voucher,
+ channel=channel_USD,
+ discount_value=value,
+ currency=channel_USD.currency_code,
+ )
+ )
+ voucher_channel_listings.append(
+ VoucherChannelListing(
+ voucher=voucher,
+ channel=channel_PLN,
+ discount_value=value * 2,
+ currency=channel_PLN.currency_code,
+ )
+ )
+ VoucherChannelListing.objects.bulk_create(voucher_channel_listings)
+ return vouchers
diff --git a/saleor/discount/tests/test_rounding_issue.py b/saleor/discount/tests/test_rounding_issue.py
index 8908a29a74..71c24f2f5f 100644
--- a/saleor/discount/tests/test_rounding_issue.py
+++ b/saleor/discount/tests/test_rounding_issue.py
@@ -1,6 +1,5 @@
from decimal import Decimal
-import graphene
import pytest
from ...checkout.fetch import fetch_checkout_info, fetch_checkout_lines
@@ -10,36 +9,12 @@ from ...plugins.manager import get_plugins_manager
from ...product.models import Product, ProductVariantChannelListing
from ...product.utils.variant_prices import update_discounted_prices_for_promotion
from ...product.utils.variants import fetch_variants_for_promotion_rules
-from .. import DiscountValueType, RewardValueType
-from ..models import Promotion
+from .. import DiscountValueType
from ..utils.checkout import (
create_or_update_discount_objects_from_promotion_for_checkout,
)
-def promotion_10_percentage(channel_USD, product_list, product):
- promotion = Promotion.objects.create(
- name="Promotion",
- )
- product_list.append(product)
- rule = promotion.rules.create(
- name="10% promotion rule",
- catalogue_predicate={
- "productPredicate": {
- "ids": [
- graphene.Node.to_global_id("Product", product.id)
- for product in product_list
- ]
- }
- },
- reward_value_type=RewardValueType.PERCENTAGE,
- reward_value=Decimal("10"),
- )
- rule.channels.add(channel_USD)
- return promotion
-
-
@pytest.mark.parametrize(
("variant_prices", "expected_discounts"),
[
diff --git a/saleor/graphql/discount/tests/benchmark/test_promotions.py b/saleor/graphql/discount/tests/benchmark/test_promotions.py
index 7fd1ed5fb5..329b3687bc 100644
--- a/saleor/graphql/discount/tests/benchmark/test_promotions.py
+++ b/saleor/graphql/discount/tests/benchmark/test_promotions.py
@@ -1,70 +1,7 @@
-from decimal import Decimal
-
-import graphene
import pytest
-from .....discount import RewardValueType
-from .....discount.models import (
- Promotion,
- PromotionRule,
- PromotionRuleTranslation,
- PromotionTranslation,
-)
from ....tests.utils import get_graphql_content
-
-def promotion_list(channel_USD, channel_PLN, product_list):
- promotions = Promotion.objects.bulk_create(
- [Promotion(name=f"Promotion-{i}") for i in range(30)]
- )
- rules = [
- PromotionRule(
- promotion=promotion,
- catalogue_predicate={},
- reward_value_type=RewardValueType.PERCENTAGE,
- reward_value=Decimal("10"),
- )
- for promotion in promotions
- ]
- for rule, product in zip(rules, product_list):
- rule.catalogue_predicate = {
- "productPredicate": {
- "ids": [graphene.Node.to_global_id("Product", product.id)]
- }
- }
-
- PromotionRule.objects.bulk_create(rules)
-
- channel_PLN.promotionrule_set.add(*rules)
- channel_USD.promotionrule_set.add(*rules)
-
- # TODO: add translations
- promotion_translations = []
- for promotion in promotions:
- promotion_translations.append(
- PromotionTranslation(
- language_code="pl",
- promotion=promotion,
- name="Polish promotion name",
- )
- )
- PromotionTranslation.objects.bulk_create(promotion_translations)
-
- promotion_rule_translations = []
- for rule in rules:
- promotion_rule_translations.append(
- PromotionRuleTranslation(
- language_code="pl",
- promotion_rule=rule,
- name="Polish promotion rule name",
- )
- )
- PromotionRuleTranslation.objects.bulk_create(promotion_rule_translations)
-
- return promotions
-
-
PROMOTIONS_QUERY = """
query {
promotions(first: 10) {
@@ -106,7 +43,7 @@ query {
@pytest.mark.count_queries(autouse=False)
def test_promotions_querytest_promotions_query(
staff_api_client,
- promotion_list,
+ promotion_list_for_benchmark,
permission_group_manage_discounts,
count_queries,
):
diff --git a/saleor/graphql/discount/tests/benchmark/test_sales.py b/saleor/graphql/discount/tests/benchmark/test_sales.py
index 28601168ca..58a9c02b11 100644
--- a/saleor/graphql/discount/tests/benchmark/test_sales.py
+++ b/saleor/graphql/discount/tests/benchmark/test_sales.py
@@ -1,52 +1,7 @@
import pytest
-from .....discount import RewardValueType
-from .....discount.models import Promotion, PromotionRule
from ....tests.utils import get_graphql_content
-
-def promotion_converted_from_sale_list(channel_USD, channel_PLN):
- promotions = Promotion.objects.bulk_create(
- [Promotion(name="Sale1"), Promotion(name="Sale2"), Promotion(name="Sale2")]
- )
- for promotion in promotions:
- promotion.assign_old_sale_id()
-
- values = [15, 5, 25]
- usd_rules, pln_rules = [], []
- for promotion, value in zip(promotions, values):
- usd_rules.append(
- PromotionRule(
- promotion=promotion,
- catalogue_predicate={},
- reward_value_type=RewardValueType.FIXED,
- reward_value=value,
- )
- )
- pln_rules.append(
- PromotionRule(
- promotion=promotion,
- catalogue_predicate={},
- reward_value_type=RewardValueType.FIXED,
- reward_value=value * 2,
- )
- )
- PromotionRule.objects.bulk_create(usd_rules + pln_rules)
- PromotionRuleChannel = PromotionRule.channels.through
- usd_rules_channels = [
- PromotionRuleChannel(promotionrule_id=rule.id, channel_id=channel_USD.id)
- for rule in usd_rules
- ]
- pln_rules_channels = [
- PromotionRuleChannel(promotionrule_id=rule.id, channel_id=channel_PLN.id)
- for rule in usd_rules
- ]
- PromotionRuleChannel.objects.bulk_create(usd_rules_channels + pln_rules_channels)
-
- return promotions
-
-
SALES_QUERY = """
query GetSales($channel: String){
sales(last: 10, channel: $channel) {
@@ -110,7 +65,7 @@ query GetSales($channel: String){
@pytest.mark.count_queries(autouse=False)
def test_sales_query_with_channel_slug(
staff_api_client,
- promotion_converted_from_sale_list,
+ promotion_converted_from_sale_list_for_benchmark,
channel_USD,
permission_manage_discounts,
count_queries,
@@ -130,7 +85,7 @@ def test_sales_query_with_channel_slug(
@pytest.mark.count_queries(autouse=False)
def test_sales_query_without_channel_slug(
staff_api_client,
- promotion_converted_from_sale_list,
+ promotion_converted_from_sale_list_for_benchmark,
permission_manage_discounts,
count_queries,
):
diff --git a/saleor/graphql/discount/tests/benchmark/test_vouchers.py b/saleor/graphql/discount/tests/benchmark/test_vouchers.py
index 5858f4f47a..cd2aa55f13 100644
--- a/saleor/graphql/discount/tests/benchmark/test_vouchers.py
+++ b/saleor/graphql/discount/tests/benchmark/test_vouchers.py
@@ -1,48 +1,7 @@
import pytest
-from .....discount.models import Voucher, VoucherChannelListing, VoucherCode
from ....tests.utils import get_graphql_content
-
-def vouchers_list(channel_USD, channel_PLN):
- vouchers = Voucher.objects.bulk_create(
- [
- Voucher(name="Voucher1"),
- Voucher(name="Voucher2"),
- Voucher(name="Voucher3"),
- ]
- )
- VoucherCode.objects.bulk_create(
- [
- VoucherCode(code="Voucher1", voucher=vouchers[0]),
- VoucherCode(code="Voucher2", voucher=vouchers[1]),
- VoucherCode(code="Voucher3", voucher=vouchers[2]),
- ]
- )
- values = [15, 5, 25]
- voucher_channel_listings = []
- for voucher, value in zip(vouchers, values):
- voucher_channel_listings.append(
- VoucherChannelListing(
- voucher=voucher,
- channel=channel_USD,
- discount_value=value,
- currency=channel_USD.currency_code,
- )
- )
- voucher_channel_listings.append(
- VoucherChannelListing(
- voucher=voucher,
- channel=channel_PLN,
- discount_value=value * 2,
- currency=channel_PLN.currency_code,
- )
- )
- VoucherChannelListing.objects.bulk_create(voucher_channel_listings)
- return vouchers
-
-
VOUCHERS_QUERY = """
query GetVouchers($channel: String){
vouchers(last: 10, channel: $channel) {
diff --git a/saleor/graphql/discount/tests/mutations/test_sale_bulk_delete.py b/saleor/graphql/discount/tests/mutations/test_sale_bulk_delete.py
index fc8e3705b3..32b8eccec5 100644
--- a/saleor/graphql/discount/tests/mutations/test_sale_bulk_delete.py
+++ b/saleor/graphql/discount/tests/mutations/test_sale_bulk_delete.py
@@ -1,66 +1,12 @@
-from decimal import Decimal
from unittest import mock
import graphene
-import pytest
-from .....discount import RewardValueType
from .....discount.error_codes import DiscountErrorCode
from .....discount.models import Promotion, PromotionRule
from .....product.models import ProductChannelListing, ProductVariant
from ....tests.utils import get_graphql_content
-
-def promotion_converted_from_sale_list(channel_USD, product_list, category, collection):
- promotions = Promotion.objects.bulk_create(
- [Promotion(name="Sale 1"), Promotion(name="Sale 2"), Promotion(name="Sale 3")]
- )
- for promotion in promotions:
- promotion.assign_old_sale_id()
- predicates = [
- {
- "OR": [
- {
- "productPredicate": {
- "ids": [graphene.Node.to_global_id("Product", product.id)]
- }
- },
- {
- "variantPredicate": {
- "ids": [
- graphene.Node.to_global_id(
- "Product", product.variants.first().id
- )
- ]
- }
- },
- ]
- }
- for product in product_list
- ]
- collection_id = graphene.Node.to_global_id("Collection", collection.id)
- category_id = graphene.Node.to_global_id("Category", category.id)
-
- predicates[0]["OR"].append({"categoryPredicate": {"ids": [category_id]}})
- predicates[1]["OR"].append({"collectionPredicate": {"ids": [collection_id]}})
-
- rules = [
- PromotionRule(
- promotion=promotion,
- catalogue_predicate=predicate,
- reward_value_type=RewardValueType.FIXED,
- reward_value=Decimal("5"),
- )
- for promotion, predicate in zip(promotions, predicates)
- ]
- PromotionRule.objects.bulk_create(rules)
- for rule in rules:
- rule.channels.add(channel_USD)
-
- return promotions
-
-
SALE_BULK_DELETE_MUTATION = """
mutation saleBulkDelete($ids: [ID!]!) {
saleBulkDelete(ids: $ids) {
diff --git a/saleor/graphql/discount/tests/mutations/test_voucher_bulk_delete.py b/saleor/graphql/discount/tests/mutations/test_voucher_bulk_delete.py
index 2fb922c6bb..d286f74373 100644
--- a/saleor/graphql/discount/tests/mutations/test_voucher_bulk_delete.py
+++ b/saleor/graphql/discount/tests/mutations/test_voucher_bulk_delete.py
@@ -1,54 +1,10 @@
from unittest import mock
import graphene
-import pytest
-from .....discount.models import Voucher, VoucherChannelListing, VoucherCode
+from .....discount.models import Voucher
from ....tests.utils import get_graphql_content
-
-def voucher_list(channel_USD):
- [voucher_1, voucher_2, voucher_3] = Voucher.objects.bulk_create(
- [
- Voucher(),
- Voucher(),
- Voucher(),
- ]
- )
-
- VoucherCode.objects.bulk_create(
- [
- VoucherCode(code="voucher-1", voucher=voucher_1),
- VoucherCode(code="voucher-2", voucher=voucher_1),
- VoucherCode(code="voucher-3", voucher=voucher_2),
- ]
- )
- VoucherChannelListing.objects.bulk_create(
- [
- VoucherChannelListing(
- voucher=voucher_1,
- channel=channel_USD,
- discount_value=1,
- currency=channel_USD.currency_code,
- ),
- VoucherChannelListing(
- voucher=voucher_2,
- channel=channel_USD,
- discount_value=2,
- currency=channel_USD.currency_code,
- ),
- VoucherChannelListing(
- voucher=voucher_3,
- channel=channel_USD,
- discount_value=3,
- currency=channel_USD.currency_code,
- ),
- ]
- )
- return voucher_1, voucher_2, voucher_3
-
-
BULK_DELETE_VOUCHERS_MUTATION = """
mutation voucherBulkDelete($ids: [ID!]!) {
voucherBulkDelete(ids: $ids) {
diff --git a/saleor/graphql/discount/tests/queries/test_vouchers.py b/saleor/graphql/discount/tests/queries/test_vouchers.py
index 74c9fd2878..f36d7ac6c8 100644
--- a/saleor/graphql/discount/tests/queries/test_vouchers.py
+++ b/saleor/graphql/discount/tests/queries/test_vouchers.py
@@ -1,8 +1,5 @@
from unittest.mock import ANY
-import pytest
-from django_countries import countries
-
from ....tests.utils import get_graphql_content
VOUCHERS_QUERY = """
@@ -44,13 +41,6 @@ VOUCHERS_QUERY = """
"""
-def voucher_with_many_channels_and_countries(voucher_with_many_channels):
- voucher_with_many_channels.countries = countries
- voucher_with_many_channels.save(update_fields=["countries"])
- return voucher_with_many_channels
-
-
def test_voucher_query(
staff_api_client,
voucher,
diff --git a/saleor/tests/fixtures.py b/saleor/tests/fixtures.py
index aa397b9d06..49e1cd78fc 100644
--- a/saleor/tests/fixtures.py
+++ b/saleor/tests/fixtures.py
@@ -46,24 +46,16 @@ from ..discount import (
DiscountType,
DiscountValueType,
PromotionEvents,
- PromotionType,
RewardType,
RewardValueType,
- VoucherType,
)
-from ..discount.interface import VariantPromotionRuleInfo
from ..discount.models import (
CheckoutDiscount,
CheckoutLineDiscount,
Promotion,
PromotionEvent,
- PromotionRule,
PromotionRuleTranslation,
PromotionTranslation,
- Voucher,
- VoucherChannelListing,
- VoucherCode,
- VoucherCustomer,
VoucherTranslation,
)
from ..payment import ChargeStatus, TransactionKind
@@ -80,7 +72,6 @@ from ..product.models import (
ProductVariantChannelListing,
ProductVariantTranslation,
)
-from ..product.utils.variants import fetch_variants_for_promotion_rules
from ..tax import TaxCalculationStrategy
from ..warehouse.models import (
PreorderReservation,
@@ -1867,133 +1858,6 @@ def image_list():
]
-def voucher_without_channel(db):
- voucher = Voucher.objects.create()
- VoucherCode.objects.create(code="mirumee", voucher=voucher)
- return voucher
-
-
-def voucher(voucher_without_channel, channel_USD):
- VoucherChannelListing.objects.create(
- voucher=voucher_without_channel,
- channel=channel_USD,
- discount=Money(20, channel_USD.currency_code),
- )
- return voucher_without_channel
-
-
-def voucher_with_many_codes(voucher):
- VoucherCode.objects.bulk_create(
- [
- VoucherCode(code="Multi1", voucher=voucher),
- VoucherCode(code="Multi2", voucher=voucher),
- VoucherCode(code="Multi3", voucher=voucher),
- VoucherCode(code="Multi4", voucher=voucher),
- ]
- )
- return voucher
-
-
-def voucher_with_many_channels(voucher, channel_PLN):
- VoucherChannelListing.objects.create(
- voucher=voucher,
- channel=channel_PLN,
- discount=Money(80, channel_PLN.currency_code),
- )
- return voucher
-
-
-def voucher_percentage(channel_USD):
- voucher = Voucher.objects.create(
- discount_value_type=DiscountValueType.PERCENTAGE,
- )
- VoucherCode.objects.create(code="saleor", voucher=voucher)
- VoucherChannelListing.objects.create(
- voucher=voucher,
- channel=channel_USD,
- discount_value=10,
- currency=channel_USD.currency_code,
- )
- return voucher
-
-
-def voucher_specific_product_type(voucher_percentage, product):
- voucher_percentage.products.add(product)
- voucher_percentage.type = VoucherType.SPECIFIC_PRODUCT
- voucher_percentage.save()
- return voucher_percentage
-
-
-def voucher_with_high_min_spent_amount(channel_USD):
- voucher = Voucher.objects.create()
- VoucherCode.objects.create(code="mirumee", voucher=voucher)
- VoucherChannelListing.objects.create(
- voucher=voucher,
- channel=channel_USD,
- discount=Money(10, channel_USD.currency_code),
- min_spent_amount=1_000_000,
- )
- return voucher
-
-
-def voucher_shipping_type(channel_USD):
- voucher = Voucher.objects.create(type=VoucherType.SHIPPING, countries="IS")
- VoucherCode.objects.create(code="mirumee", voucher=voucher)
- VoucherChannelListing.objects.create(
- voucher=voucher,
- channel=channel_USD,
- discount=Money(10, channel_USD.currency_code),
- )
- return voucher
-
-
-def voucher_free_shipping(voucher_percentage, channel_USD):
- voucher_percentage.type = VoucherType.SHIPPING
- voucher_percentage.name = "Free shipping"
- voucher_percentage.save()
- voucher_percentage.channel_listings.filter(channel=channel_USD).update(
- discount_value=100
- )
- return voucher_percentage
-
-
-def voucher_customer(voucher, customer_user):
- email = customer_user.email
- code = voucher.codes.first()
- return VoucherCustomer.objects.create(voucher_code=code, customer_email=email)
-
-
-def voucher_multiple_use(voucher_with_many_codes):
- voucher = voucher_with_many_codes
- voucher.usage_limit = 3
- voucher.save(update_fields=["usage_limit"])
- codes = voucher.codes.all()
- for code in codes:
- code.used = 1
- VoucherCode.objects.bulk_update(codes, ["used"])
- voucher.refresh_from_db()
- return voucher
-
-
-def voucher_single_use(voucher_with_many_codes):
- voucher = voucher_with_many_codes
- voucher.single_use = True
- voucher.save(update_fields=["single_use"])
- return voucher
-
-
@pytest.fixture
def checkout_line_with_reservation_in_many_stocks(
customer_user, variant_with_many_stocks, checkout
@@ -2239,345 +2103,6 @@ def dummy_webhook_app_payment_data(dummy_payment_data, payment_app):
return dummy_payment_data
-def catalogue_promotion(channel_USD, product, collection):
- promotion = Promotion.objects.create(
- name="Promotion",
- type=PromotionType.CATALOGUE,
- description=dummy_editorjs("Test description."),
- end_date=timezone.now() + datetime.timedelta(days=30),
- )
- rules = PromotionRule.objects.bulk_create(
- [
- PromotionRule(
- name="Percentage promotion rule",
- promotion=promotion,
- description=dummy_editorjs(
- "Test description for percentage promotion rule."
- ),
- catalogue_predicate={
- "productPredicate": {
- "ids": [graphene.Node.to_global_id("Product", product.id)]
- }
- },
- reward_value_type=RewardValueType.PERCENTAGE,
- reward_value=Decimal("10"),
- ),
- PromotionRule(
- name="Fixed promotion rule",
- promotion=promotion,
- description=dummy_editorjs(
- "Test description for fixes promotion rule."
- ),
- catalogue_predicate={
- "collectionPredicate": {
- "ids": [graphene.Node.to_global_id("Collection", collection.id)]
- }
- },
- reward_value_type=RewardValueType.FIXED,
- reward_value=Decimal("5"),
- ),
- ]
- )
- for rule in rules:
- rule.channels.add(channel_USD)
- fetch_variants_for_promotion_rules(promotion.rules.all())
- return promotion
-
-
-def catalogue_promotion_without_rules(db):
- promotion = Promotion.objects.create(
- name="Promotion",
- description=dummy_editorjs("Test description."),
- end_date=timezone.now() + datetime.timedelta(days=30),
- type=PromotionType.CATALOGUE,
- )
- return promotion
-
-
-def order_promotion_without_rules(db):
- promotion = Promotion.objects.create(
- name="Promotion",
- description=dummy_editorjs("Test description."),
- end_date=timezone.now() + datetime.timedelta(days=30),
- type=PromotionType.ORDER,
- )
- return promotion
-
-
-def catalogue_promotion_with_single_rule(catalogue_predicate, channel_USD):
- promotion = Promotion.objects.create(
- name="Promotion with single rule", type=PromotionType.CATALOGUE
- )
- rule = PromotionRule.objects.create(
- name="Sale rule",
- promotion=promotion,
- catalogue_predicate=catalogue_predicate,
- reward_value_type=RewardValueType.FIXED,
- reward_value=Decimal(5),
- )
- rule.channels.add(channel_USD)
- return promotion
-
-
-def order_promotion_with_rule(channel_USD):
- promotion = Promotion.objects.create(
- name="Promotion with order rule", type=PromotionType.ORDER
- )
- rule = PromotionRule.objects.create(
- name="Promotion rule",
- promotion=promotion,
- order_predicate={
- "discountedObjectPredicate": {"baseSubtotalPrice": {"range": {"gte": 100}}}
- },
- reward_value_type=RewardValueType.FIXED,
- reward_value=Decimal(5),
- reward_type=RewardType.SUBTOTAL_DISCOUNT,
- )
- rule.channels.add(channel_USD)
- return promotion
-
-
-def promotion_list(channel_USD, product, collection):
- collection.products.add(product)
- promotions = Promotion.objects.bulk_create(
- [
- Promotion(
- name="Promotion 1",
- type=PromotionType.CATALOGUE,
- description=dummy_editorjs("Promotion 1 description."),
- start_date=timezone.now() + datetime.timedelta(days=1),
- end_date=timezone.now() + datetime.timedelta(days=10),
- ),
- Promotion(
- name="Promotion 2",
- type=PromotionType.CATALOGUE,
- description=dummy_editorjs("Promotion 2 description."),
- start_date=timezone.now() + datetime.timedelta(days=5),
- end_date=timezone.now() + datetime.timedelta(days=20),
- ),
- Promotion(
- name="Promotion 3",
- type=PromotionType.CATALOGUE,
- description=dummy_editorjs("TePromotion 3 description."),
- start_date=timezone.now() + datetime.timedelta(days=15),
- end_date=timezone.now() + datetime.timedelta(days=30),
- ),
- ]
- )
- rules = PromotionRule.objects.bulk_create(
- [
- PromotionRule(
- name="Promotion 1 percentage rule",
- promotion=promotions[0],
- description=dummy_editorjs(
- "Test description for promotion 1 percentage rule."
- ),
- catalogue_predicate={
- "productPredicate": {
- "ids": [graphene.Node.to_global_id("Product", product.id)]
- }
- },
- reward_value_type=RewardValueType.PERCENTAGE,
- reward_value=Decimal("10"),
- ),
- PromotionRule(
- name="Promotion 1 fixed rule",
- promotion=promotions[0],
- description=dummy_editorjs(
- "Test description for promotion 1 fixed rule."
- ),
- catalogue_predicate={
- "collectionPredicate": {
- "ids": [graphene.Node.to_global_id("Collection", collection.id)]
- }
- },
- reward_value_type=RewardValueType.FIXED,
- reward_value=Decimal("5"),
- ),
- PromotionRule(
- name="Promotion 2 percentage rule",
- promotion=promotions[1],
- description=dummy_editorjs(
- "Test description for promotion 2 percentage rule."
- ),
- catalogue_predicate={
- "productPredicate": {
- "ids": [graphene.Node.to_global_id("Product", product.id)]
- }
- },
- reward_value_type=RewardValueType.PERCENTAGE,
- reward_value=Decimal("10"),
- ),
- PromotionRule(
- name="Promotion 3 fixed rule",
- promotion=promotions[2],
- description=dummy_editorjs(
- "Test description for promotion 3 fixed rule."
- ),
- catalogue_predicate={
- "collectionPredicate": {
- "ids": [graphene.Node.to_global_id("Collection", collection.id)]
- }
- },
- reward_value_type=RewardValueType.FIXED,
- reward_value=Decimal("5"),
- ),
- ]
- )
- for rule in rules:
- rule.channels.add(channel_USD)
- fetch_variants_for_promotion_rules(PromotionRule.objects.all())
- return promotions
-
-
-def promotion_rule(channel_USD, catalogue_promotion, product):
- rule = PromotionRule.objects.create(
- name="Promotion rule name",
- promotion=catalogue_promotion,
- description=dummy_editorjs("Test description for percentage promotion rule."),
- catalogue_predicate={
- "productPredicate": {
- "ids": [graphene.Node.to_global_id("Product", product.id)]
- }
- },
- reward_value_type=RewardValueType.PERCENTAGE,
- reward_value=Decimal("25"),
- )
- rule.channels.add(channel_USD)
- return rule
-
-
-def order_promotion_rule(channel_USD, order_promotion_without_rules):
- rule = PromotionRule.objects.create(
- name="Order promotion rule",
- promotion=order_promotion_without_rules,
- order_predicate={
- "discountedObjectPredicate": {"baseSubtotalPrice": {"range": {"gte": 20}}}
- },
- reward_value_type=RewardValueType.PERCENTAGE,
- reward_value=Decimal("25"),
- reward_type=RewardType.SUBTOTAL_DISCOUNT,
- )
- rule.channels.add(channel_USD)
- return rule
-
-
-def gift_promotion_rule(channel_USD, order_promotion_without_rules, product_list):
- rule = PromotionRule.objects.create(
- name="Order promotion rule",
- promotion=order_promotion_without_rules,
- order_predicate={
- "discountedObjectPredicate": {"baseSubtotalPrice": {"range": {"gte": 20}}}
- },
- reward_type=RewardType.GIFT,
- )
- rule.channels.add(channel_USD)
- rule.gifts.set([product.variants.first() for product in product_list[:2]])
- return rule
-
-
-def rule_info(
- promotion_rule,
- promotion_translation_fr,
- promotion_rule_translation_fr,
- variant,
- channel_USD,
-):
- variant_channel_listing = variant.channel_listings.get(channel_id=channel_USD.id)
- listing_promotion_rule = variant_channel_listing.variantlistingpromotionrule.create(
- promotion_rule=promotion_rule,
- discount_amount=Decimal("10"),
- currency=channel_USD.currency_code,
- )
- return VariantPromotionRuleInfo(
- rule=promotion_rule,
- promotion=promotion_rule.promotion,
- variant_listing_promotion_rule=listing_promotion_rule,
- promotion_translation=promotion_translation_fr,
- rule_translation=promotion_rule_translation_fr,
- )
-
-
-def catalogue_predicate(product, category, collection, variant):
- collection_id = graphene.Node.to_global_id("Collection", collection.id)
- category_id = graphene.Node.to_global_id("Category", category.id)
- product_id = graphene.Node.to_global_id("Product", product.id)
- variant_id = graphene.Node.to_global_id("ProductVariant", variant.id)
- return {
- "OR": [
- {"collectionPredicate": {"ids": [collection_id]}},
- {"categoryPredicate": {"ids": [category_id]}},
- {"productPredicate": {"ids": [product_id]}},
- {"variantPredicate": {"ids": [variant_id]}},
- ]
- }
-
-
-def promotion_converted_from_sale(catalogue_predicate, channel_USD):
- promotion = Promotion.objects.create(name="Sale", type=PromotionType.CATALOGUE)
- promotion.assign_old_sale_id()
-
- rule = PromotionRule.objects.create(
- name="Sale rule",
- promotion=promotion,
- catalogue_predicate=catalogue_predicate,
- reward_value_type=RewardValueType.FIXED,
- reward_value=Decimal(5),
- old_channel_listing_id=PromotionRule.get_old_channel_listing_ids(1)[0][0],
- )
- rule.channels.add(channel_USD)
- fetch_variants_for_promotion_rules(promotion.rules.all())
- return promotion
-
-
-def promotion_converted_from_sale_with_many_channels(
- promotion_converted_from_sale, catalogue_predicate, channel_PLN
-):
- promotion = promotion_converted_from_sale
- rule = PromotionRule.objects.create(
- name="Sale rule 2",
- promotion=promotion,
- catalogue_predicate=catalogue_predicate,
- reward_value_type=RewardValueType.FIXED,
- reward_value=Decimal(5),
- old_channel_listing_id=PromotionRule.get_old_channel_listing_ids(1)[0][0],
- )
- rule.channels.add(channel_PLN)
- fetch_variants_for_promotion_rules(promotion.rules.all())
- return promotion
-
-
-def promotion_converted_from_sale_with_empty_predicate(channel_USD):
- promotion = Promotion.objects.create(
- name="Sale with empty predicate", type=PromotionType.CATALOGUE
- )
- promotion.assign_old_sale_id()
- rule = PromotionRule.objects.create(
- name="Sale with empty predicate rule",
- promotion=promotion,
- catalogue_predicate={},
- reward_value_type=RewardValueType.FIXED,
- reward_value=Decimal(5),
- old_channel_listing_id=PromotionRule.get_old_channel_listing_ids(1)[0][0],
- )
- rule.channels.add(channel_USD)
- return promotion
-
-
@pytest.fixture
def promotion_events(catalogue_promotion, staff_user):
promotion = catalogue_promotion