Skip to content

Commit

Permalink
Split fixtures in discount module (saleor#16878)
Browse files Browse the repository at this point in the history
  • Loading branch information
zedzior authored Oct 14, 2024
1 parent 210c3a0 commit a326594
Show file tree
Hide file tree
Showing 14 changed files with 769 additions and 762 deletions.
1 change: 1 addition & 0 deletions conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
"saleor.shipping.tests.fixtures",
"saleor.permission.tests.fixtures",
"saleor.giftcard.tests.fixtures",
"saleor.discount.tests.fixtures",
]


Expand Down
4 changes: 4 additions & 0 deletions saleor/discount/tests/fixtures/__init__.py
Original file line number Diff line number Diff line change
@@ -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
106 changes: 106 additions & 0 deletions saleor/discount/tests/fixtures/benchmark.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
from decimal import Decimal

import graphene
import pytest

from ....discount import RewardValueType
from ....discount.models import (
Promotion,
PromotionRule,
PromotionRuleTranslation,
PromotionTranslation,
)


@pytest.fixture
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


@pytest.fixture
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
Loading

0 comments on commit a326594

Please sign in to comment.