Skip to content

Commit

Permalink
Refactor flaky tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
zedzior committed Jan 29, 2024
1 parent 999ba4d commit 401356a
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 14 deletions.
9 changes: 5 additions & 4 deletions saleor/graphql/discount/dataloaders.py
Original file line number Diff line number Diff line change
Expand Up @@ -457,13 +457,14 @@ class GiftsByPromotionRuleIDLoader(DataLoader):

def batch_load(self, keys):
PromotionRuleGift = PromotionRule.gifts.through
rule_gifts = PromotionRuleGift.objects.using(
self.database_connection_name
).filter(promotionrule_id__in=keys)
rule_gifts = (
PromotionRuleGift.objects.using(self.database_connection_name)
.filter(promotionrule_id__in=keys)
.order_by("pk")
)
gifts = (
ProductVariant.objects.using(self.database_connection_name)
.filter(Exists(rule_gifts.filter(productvariant_id=OuterRef("id"))))
.order_by("created_at")
.in_bulk()
)
rule_to_gifts_map = defaultdict(list)
Expand Down
34 changes: 24 additions & 10 deletions saleor/graphql/discount/tests/queries/test_promotion.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

import graphene

from .....discount import PromotionEvents, RewardValueType
from .....discount import PromotionEvents, RewardType, RewardValueType
from .....tests.utils import dummy_editorjs
from ....tests.utils import assert_no_permission, get_graphql_content

Expand Down Expand Up @@ -31,8 +31,6 @@
cataloguePredicate
orderPredicate
rewardType
giftIds
giftsLimit
}
}
}
Expand Down Expand Up @@ -61,11 +59,6 @@ def _assert_promotion_data(promotion, content_data):
"cataloguePredicate": rule.catalogue_predicate,
"orderPredicate": rule.order_predicate,
"rewardType": rule.reward_type.upper() if rule.reward_type else None,
"giftIds": [
graphene.Node.to_global_id("ProductVariant", gift.pk)
for gift in rule.gifts.order_by("created_at").all()
],
"giftsLimit": 1,
}
assert rule_data in promotion_data["rules"]

Expand Down Expand Up @@ -184,6 +177,18 @@ def test_query_order_promotion_with_gift_rule(
permission_group_manage_discounts,
):
# given
query = """
query Promotion($id: ID!) {
promotion(id: $id) {
id
rules {
rewardType
giftIds
giftsLimit
}
}
}
"""
promotion = order_promotion_without_rules
permission_group_manage_discounts.user_set.add(staff_api_client.user)
promotion_id = graphene.Node.to_global_id("Promotion", promotion.id)
Expand All @@ -192,11 +197,20 @@ def test_query_order_promotion_with_gift_rule(
variables = {"id": promotion_id}

# when
response = staff_api_client.post_graphql(QUERY_PROMOTION_BY_ID, variables)
response = staff_api_client.post_graphql(query, variables)

# then
content = get_graphql_content(response)
_assert_promotion_data(promotion, content)
rule = content["data"]["promotion"]["rules"][0]
rule_db = promotion.rules.first()
assert set(rule["giftIds"]) == set(
[
graphene.Node.to_global_id("ProductVariant", gift.pk)
for gift in rule_db.gifts.all()
]
)
assert rule["giftsLimit"] == 1
assert rule["rewardType"] == RewardType.GIFT.upper()


def test_query_promotion_translation(
Expand Down

0 comments on commit 401356a

Please sign in to comment.