Skip to content

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

Drop flush_post_commit_hooks (#16848)

Marcin Gębala58c061b4462b
diff --git a/saleor/account/tests/test_utils.py b/saleor/account/tests/test_utils.py
index 065379400f..0d358d2d63 100644
--- a/saleor/account/tests/test_utils.py
+++ b/saleor/account/tests/test_utils.py
@@ -5,7 +5,6 @@ from django.test import override_settings
from ...checkout import AddressType
from ...plugins.manager import get_plugins_manager
-from ...tests.utils import flush_post_commit_hooks
from ..models import Address, User
from ..utils import (
get_user_groups_permissions,
@@ -251,12 +250,13 @@ def test_send_user_event_no_webhook_sent(
mock_customer_created_webhook,
mock_customer_updated_webhook,
customer_user,
+ django_capture_on_commit_callbacks,
):
# when
- send_user_event(customer_user, False, False)
+ with django_capture_on_commit_callbacks(execute=True):
+ send_user_event(customer_user, False, False)
# then
- flush_post_commit_hooks()
mock_staff_created_webhook.assert_not_called()
mock_staff_updated_webhook.assert_not_called()
mock_customer_created_webhook.assert_not_called()
@@ -273,12 +273,13 @@ def test_send_user_event_customer_created_event(
mock_customer_created_webhook,
mock_customer_updated_webhook,
customer_user,
+ django_capture_on_commit_callbacks,
):
# when
- send_user_event(customer_user, True, True)
+ with django_capture_on_commit_callbacks(execute=True):
+ send_user_event(customer_user, True, True)
# then
- flush_post_commit_hooks()
mock_customer_created_webhook.assert_called_once_with(customer_user)
mock_customer_updated_webhook.assert_not_called()
mock_staff_created_webhook.assert_not_called()
@@ -295,12 +296,13 @@ def test_send_user_event_customer_updated_event(
mock_customer_created_webhook,
mock_customer_updated_webhook,
customer_user,
+ django_capture_on_commit_callbacks,
):
# when
- send_user_event(customer_user, False, True)
+ with django_capture_on_commit_callbacks(execute=True):
+ send_user_event(customer_user, False, True)
# then
- flush_post_commit_hooks()
mock_customer_updated_webhook.assert_called_once_with(customer_user)
mock_customer_created_webhook.assert_not_called()
mock_staff_created_webhook.assert_not_called()
@@ -317,12 +319,13 @@ def test_send_user_event_staff_created_event(
mock_customer_created_webhook,
mock_customer_updated_webhook,
staff_user,
+ django_capture_on_commit_callbacks,
):
# when
- send_user_event(staff_user, True, True)
+ with django_capture_on_commit_callbacks(execute=True):
+ send_user_event(staff_user, True, True)
# then
- flush_post_commit_hooks()
mock_staff_created_webhook.assert_called_once_with(staff_user)
mock_staff_updated_webhook.assert_not_called()
mock_customer_created_webhook.assert_not_called()
@@ -339,12 +342,13 @@ def test_send_user_event_staff_updated_event(
mock_customer_created_webhook,
mock_customer_updated_webhook,
staff_user,
+ django_capture_on_commit_callbacks,
):
# when
- send_user_event(staff_user, False, True)
+ with django_capture_on_commit_callbacks(execute=True):
+ send_user_event(staff_user, False, True)
# then
- flush_post_commit_hooks()
mock_staff_updated_webhook.assert_called_once_with(staff_user)
mock_staff_created_webhook.assert_not_called()
mock_customer_created_webhook.assert_not_called()
diff --git a/saleor/checkout/tests/test_checkout_complete.py b/saleor/checkout/tests/test_checkout_complete.py
index 832f54fdbf..03b94f30c0 100644
--- a/saleor/checkout/tests/test_checkout_complete.py
+++ b/saleor/checkout/tests/test_checkout_complete.py
@@ -28,7 +28,6 @@ from ...payment.interface import GatewayResponse
from ...payment.models import Payment
from ...plugins.manager import get_plugins_manager
from ...product.models import ProductTranslation, ProductVariantTranslation
-from ...tests.utils import flush_post_commit_hooks
from .. import calculations
from ..complete_checkout import (
_complete_checkout_fail_handler,
@@ -54,6 +53,7 @@ def test_create_order_captured_payment_creates_expected_events(
payment_txn_captured,
channel_USD,
site_settings,
+ django_capture_on_commit_callbacks,
):
checkout = checkout_with_item
checkout_user = customer_user
@@ -76,20 +76,20 @@ def test_create_order_captured_payment_creates_expected_events(
manager = get_plugins_manager(allow_replica=False)
lines, _ = fetch_checkout_lines(checkout)
checkout_info = fetch_checkout_info(checkout, lines, manager)
- order = _create_order(
- checkout_info=checkout_info,
- checkout_lines=lines,
- order_data=_prepare_order_data(
- manager=manager,
+ with django_capture_on_commit_callbacks(execute=True):
+ order = _create_order(
checkout_info=checkout_info,
- lines=lines,
- prices_entered_with_tax=True,
- ),
- user=customer_user,
- app=None,
- manager=manager,
- )
- flush_post_commit_hooks()
+ checkout_lines=lines,
+ order_data=_prepare_order_data(
+ manager=manager,
+ checkout_info=checkout_info,
+ lines=lines,
+ prices_entered_with_tax=True,
+ ),
+ user=customer_user,
+ app=None,
+ manager=manager,
+ )
(
order_placed_event,
@@ -216,6 +216,7 @@ def test_create_order_captured_payment_creates_expected_events_anonymous_user(
payment_txn_captured,
channel_USD,
site_settings,
+ django_capture_on_commit_callbacks,
):
checkout = checkout_with_item
checkout_user = None
@@ -239,20 +240,20 @@ def test_create_order_captured_payment_creates_expected_events_anonymous_user(
manager = get_plugins_manager(allow_replica=False)
lines, _ = fetch_checkout_lines(checkout)
checkout_info = fetch_checkout_info(checkout, lines, manager)
- order = _create_order(
- checkout_info=checkout_info,
- checkout_lines=lines,
- order_data=_prepare_order_data(
- manager=manager,
+ with django_capture_on_commit_callbacks(execute=True):
+ order = _create_order(
checkout_info=checkout_info,
- lines=lines,
- prices_entered_with_tax=True,
- ),
- user=None,
- app=None,
- manager=manager,
- )
- flush_post_commit_hooks()
+ checkout_lines=lines,
+ order_data=_prepare_order_data(
+ manager=manager,
+ checkout_info=checkout_info,
+ lines=lines,
+ prices_entered_with_tax=True,
+ ),
+ user=None,
+ app=None,
+ manager=manager,
+ )
(
order_placed_event,
@@ -375,6 +376,7 @@ def test_create_order_preauth_payment_creates_expected_events(
payment_txn_preauth,
channel_USD,
site_settings,
+ django_capture_on_commit_callbacks,
):
checkout = checkout_with_item
checkout_user = customer_user
@@ -397,20 +399,20 @@ def test_create_order_preauth_payment_creates_expected_events(
manager = get_plugins_manager(allow_replica=False)
lines, _ = fetch_checkout_lines(checkout)
checkout_info = fetch_checkout_info(checkout, lines, manager)
- order = _create_order(
- checkout_info=checkout_info,
- checkout_lines=lines,
- order_data=_prepare_order_data(
- manager=manager,
+ with django_capture_on_commit_callbacks(execute=True):
+ order = _create_order(
checkout_info=checkout_info,
- lines=lines,
- prices_entered_with_tax=True,
- ),
- user=customer_user,
- app=None,
- manager=manager,
- )
- flush_post_commit_hooks()
+ checkout_lines=lines,
+ order_data=_prepare_order_data(
+ manager=manager,
+ checkout_info=checkout_info,
+ lines=lines,
+ prices_entered_with_tax=True,
+ ),
+ user=customer_user,
+ app=None,
+ manager=manager,
+ )
(
order_placed_event,
@@ -489,6 +491,7 @@ def test_create_order_preauth_payment_creates_expected_events_anonymous_user(
payment_txn_preauth,
channel_USD,
site_settings,
+ django_capture_on_commit_callbacks,
):
checkout = checkout_with_item
checkout_user = None
@@ -512,20 +515,20 @@ def test_create_order_preauth_payment_creates_expected_events_anonymous_user(
manager = get_plugins_manager(allow_replica=False)
lines, _ = fetch_checkout_lines(checkout)
checkout_info = fetch_checkout_info(checkout, lines, manager)
- order = _create_order(
- checkout_info=checkout_info,
- checkout_lines=lines,
- order_data=_prepare_order_data(
- manager=manager,
+ with django_capture_on_commit_callbacks(execute=True):
+ order = _create_order(
checkout_info=checkout_info,
- lines=lines,
- prices_entered_with_tax=True,
- ),
- user=None,
- app=None,
- manager=manager,
- )
- flush_post_commit_hooks()
+ checkout_lines=lines,
+ order_data=_prepare_order_data(
+ manager=manager,
+ checkout_info=checkout_info,
+ lines=lines,
+ prices_entered_with_tax=True,
+ ),
+ user=None,
+ app=None,
+ manager=manager,
+ )
(
order_placed_event,
@@ -845,6 +848,7 @@ def test_create_order_gift_card_bought(
shipping_method,
is_anonymous_user,
non_shippable_gift_card_product,
+ django_capture_on_commit_callbacks,
):
# given
checkout_user = None if is_anonymous_user else customer_user
@@ -892,24 +896,23 @@ def test_create_order_gift_card_bought(
total_gross = subtotal.gross + shipping_price.gross - checkout.discount
# when
- order = _create_order(
- checkout_info=checkout_info,
- checkout_lines=lines,
- order_data=_prepare_order_data(
- manager=manager,
+ with django_capture_on_commit_callbacks(execute=True):
+ order = _create_order(
checkout_info=checkout_info,
- lines=lines,
- prices_entered_with_tax=True,
- ),
- user=customer_user if not is_anonymous_user else None,
- app=None,
- manager=manager,
- )
+ checkout_lines=lines,
+ order_data=_prepare_order_data(
+ manager=manager,
+ checkout_info=checkout_info,
+ lines=lines,
+ prices_entered_with_tax=True,
+ ),
+ user=customer_user if not is_anonymous_user else None,
+ app=None,
+ manager=manager,
+ )
# then
- flush_post_commit_hooks()
assert order.total.gross == total_gross
- flush_post_commit_hooks()
gift_card = GiftCard.objects.get()
assert (
gift_card.initial_balance
@@ -918,7 +921,6 @@ def test_create_order_gift_card_bought(
).unit_price_gross
)
assert GiftCardEvent.objects.filter(gift_card=gift_card, type=GiftCardEvents.BOUGHT)
- flush_post_commit_hooks()
send_notification_mock.assert_called_once_with(
checkout_user,
None,
@@ -939,6 +941,7 @@ def test_create_order_gift_card_bought_order_not_captured_gift_cards_not_sent(
customer_user,
shipping_method,
is_anonymous_user,
+ django_capture_on_commit_callbacks,
):
"""Check that digital gift cards are not issued if the payment is not captured."""
# given
@@ -971,23 +974,22 @@ def test_create_order_gift_card_bought_order_not_captured_gift_cards_not_sent(
total_gross = subtotal.gross + shipping_price.gross - checkout.discount
# when
- order = _create_order(
- checkout_info=checkout_info,
- checkout_lines=lines,
- order_data=_prepare_order_data(
- manager=manager,
+ with django_capture_on_commit_callbacks(execute=True):
+ order = _create_order(
checkout_info=checkout_info,
- lines=lines,
- prices_entered_with_tax=True,
- ),
- user=customer_user if not is_anonymous_user else None,
- app=None,
- manager=manager,
- )
+ checkout_lines=lines,
+ order_data=_prepare_order_data(
+ manager=manager,
+ checkout_info=checkout_info,
+ lines=lines,
+ prices_entered_with_tax=True,
+ ),
+ user=customer_user if not is_anonymous_user else None,
+ app=None,
+ manager=manager,
+ )
# then
- flush_post_commit_hooks()
- flush_post_commit_hooks()
assert order.total.gross == total_gross
assert not GiftCard.objects.exists()
send_notification_mock.assert_not_called()
@@ -1223,6 +1225,7 @@ def test_complete_checkout_0_total_with_transaction_for_mark_as_paid(
checkout_with_item_total_0,
customer_user,
app,
+ django_capture_on_commit_callbacks,
):
# given
checkout = checkout_with_item_total_0
@@ -1243,19 +1246,18 @@ def test_complete_checkout_0_total_with_transaction_for_mark_as_paid(
checkout_info = fetch_checkout_info(checkout, lines, manager)
# when
- order, _, _ = complete_checkout(
- checkout_info=checkout_info,
- manager=manager,
- lines=lines,
- payment_data={},
- store_source=False,
- user=customer_user,
- app=app,
- )
+ with django_capture_on_commit_callbacks(execute=True):
+ order, _, _ = complete_checkout(
+ checkout_info=checkout_info,
+ manager=manager,
+ lines=lines,
+ payment_data={},
+ store_source=False,
+ user=customer_user,
+ app=app,
+ )
# then
- flush_post_commit_hooks()
-
assert order
assert order.authorize_status == OrderAuthorizeStatus.FULL
assert order.charge_status == OrderChargeStatus.FULL
@@ -1269,6 +1271,7 @@ def test_complete_checkout_0_total_captured_payment_creates_expected_events(
channel_USD,
app,
site_settings,
+ django_capture_on_commit_callbacks,
):
checkout = checkout_with_item_total_0
checkout_user = customer_user
@@ -1291,17 +1294,17 @@ def test_complete_checkout_0_total_captured_payment_creates_expected_events(
manager = get_plugins_manager(allow_replica=False)
lines, _ = fetch_checkout_lines(checkout)
checkout_info = fetch_checkout_info(checkout, lines, manager)
- order, action_required, action_data = complete_checkout(
- checkout_info=checkout_info,
- lines=lines,
- manager=manager,
- payment_data={},
- store_source=False,
- user=customer_user,
- app=app,
- )
+ with django_capture_on_commit_callbacks(execute=True):
+ order, action_required, action_data = complete_checkout(
+ checkout_info=checkout_info,
+ lines=lines,
+ manager=manager,
+ payment_data={},
+ store_source=False,
+ user=customer_user,
+ app=app,
+ )
- flush_post_commit_hooks()
(
order_marked_as_paid,
order_placed_event,
diff --git a/saleor/checkout/tests/test_order_from_checkout.py b/saleor/checkout/tests/test_order_from_checkout.py
index bef6917707..6db16b4d92 100644
--- a/saleor/checkout/tests/test_order_from_checkout.py
+++ b/saleor/checkout/tests/test_order_from_checkout.py
@@ -14,7 +14,6 @@ from ...giftcard import GiftCardEvents
from ...giftcard.models import GiftCard, GiftCardEvent
from ...plugins.manager import get_plugins_manager
from ...product.models import ProductTranslation, ProductVariantTranslation
-from ...tests.utils import flush_post_commit_hooks
from .. import calculations
from ..complete_checkout import create_order_from_checkout
from ..fetch import fetch_checkout_info, fetch_checkout_lines
@@ -291,6 +290,7 @@ def test_create_order_gift_card_bought(
non_shippable_gift_card_product,
app,
payment_txn_captured,
+ django_capture_on_commit_callbacks,
):
# given
checkout_user = None if is_anonymous_user else customer_user
@@ -328,17 +328,16 @@ def test_create_order_gift_card_bought(
payment.save(update_fields=["checkout", "captured_amount", "total"])
# when
- order = create_order_from_checkout(
- checkout_info=checkout_info,
- manager=manager,
- user=None,
- app=app,
- )
+ with django_capture_on_commit_callbacks(execute=True):
+ order = create_order_from_checkout(
+ checkout_info=checkout_info,
+ manager=manager,
+ user=None,
+ app=app,
+ )
# then
- flush_post_commit_hooks()
assert order.total.gross == total_gross
- flush_post_commit_hooks()
gift_card = GiftCard.objects.get()
assert (
gift_card.initial_balance
@@ -347,7 +346,6 @@ def test_create_order_gift_card_bought(
).unit_price_gross
)
assert GiftCardEvent.objects.filter(gift_card=gift_card, type=GiftCardEvents.BOUGHT)
- flush_post_commit_hooks()
send_notification_mock.assert_called_once_with(
None,
app,
diff --git a/saleor/giftcard/tests/test_utils.py b/saleor/giftcard/tests/test_utils.py
index c3a6eb64d4..b6b0ed6285 100644
--- a/saleor/giftcard/tests/test_utils.py
+++ b/saleor/giftcard/tests/test_utils.py
@@ -17,7 +17,6 @@ from ...core.utils.promo_code import InvalidPromoCode
from ...order.models import OrderLine
from ...plugins.manager import get_plugins_manager
from ...site import GiftCardSettingsExpiryType
-from ...tests.utils import flush_post_commit_hooks
from ...webhook.event_types import WebhookEventAsyncType
from ...webhook.payloads import generate_meta, generate_requestor
from .. import GiftCardEvents, GiftCardLineData, events
@@ -197,6 +196,7 @@ def test_gift_cards_create(
gift_card_non_shippable_order_line,
site_settings,
staff_user,
+ django_capture_on_commit_callbacks,
):
# given
manager = get_plugins_manager(allow_replica=False)
@@ -229,9 +229,10 @@ def test_gift_cards_create(
]
# when
- gift_cards = gift_cards_create(
- order, lines_data, site_settings, staff_user, None, manager
- )
+ with django_capture_on_commit_callbacks(execute=True):
+ gift_cards = gift_cards_create(
+ order, lines_data, site_settings, staff_user, None, manager
+ )
# then
assert len(gift_cards) == len(lines_data)
@@ -275,8 +276,6 @@ def test_gift_cards_create(
"expiry_date": None,
}
- flush_post_commit_hooks()
-
send_notification_mock.assert_called_once_with(
staff_user,
None,
@@ -297,6 +296,7 @@ def test_gift_cards_create_expiry_date_set(
gift_card_non_shippable_order_line,
site_settings,
staff_user,
+ django_capture_on_commit_callbacks,
):
# given
manager = get_plugins_manager(allow_replica=False)
@@ -328,9 +328,10 @@ def test_gift_cards_create_expiry_date_set(
]
# when
- gift_cards = gift_cards_create(
- order, lines_data, site_settings, staff_user, None, manager
- )
+ with django_capture_on_commit_callbacks(execute=True):
+ gift_cards = gift_cards_create(
+ order, lines_data, site_settings, staff_user, None, manager
+ )
# then
assert len(gift_cards) == len(lines_data)
@@ -352,8 +353,6 @@ def test_gift_cards_create_expiry_date_set(
"expiry_date": gift_card.expiry_date.isoformat(),
}
- flush_post_commit_hooks()
-
send_notification_mock.assert_called_once_with(
staff_user,
None,
@@ -373,6 +372,7 @@ def test_gift_cards_create_multiple_quantity(
gift_card_non_shippable_order_line,
site_settings,
staff_user,
+ django_capture_on_commit_callbacks,
):
# given
manager = get_plugins_manager(allow_replica=False)
@@ -394,12 +394,12 @@ def test_gift_cards_create_multiple_quantity(
]
# when
- gift_cards = gift_cards_create(
- order, lines_data, site_settings, staff_user, None, manager
- )
+ with django_capture_on_commit_callbacks(execute=True):
+ gift_cards = gift_cards_create(
+ order, lines_data, site_settings, staff_user, None, manager
+ )
# then
- flush_post_commit_hooks()
assert len(gift_cards) == quantity
price = gift_card_non_shippable_order_line.unit_price_gross
for gift_card in gift_cards:
@@ -427,6 +427,7 @@ def test_gift_cards_create_trigger_webhook(
gift_card_non_shippable_order_line,
site_settings,
staff_user,
+ django_capture_on_commit_callbacks,
):
# given
mocked_get_webhooks_for_event.return_value = [any_webhook]
@@ -461,12 +462,12 @@ def test_gift_cards_create_trigger_webhook(
]
# when
- gift_cards = gift_cards_create(
- order, lines_data, site_settings, staff_user, None, manager
- )
+ with django_capture_on_commit_callbacks(execute=True):
+ gift_cards = gift_cards_create(
+ order, lines_data, site_settings, staff_user, None, manager
+ )
# then
- flush_post_commit_hooks()
assert len(gift_cards) == len(lines_data)
gift_card = gift_cards[-1]
@@ -647,6 +648,7 @@ def test_fulfill_gift_card_lines(
gift_card_non_shippable_order_line,
gift_card_shippable_order_line,
site_settings,
+ django_capture_on_commit_callbacks,
):
# given
manager = get_plugins_manager(allow_replica=False)
@@ -663,14 +665,14 @@ def test_fulfill_gift_card_lines(
)
# when
- fulfillments = fulfill_gift_card_lines(
- lines, staff_user, None, order, site_settings, manager
- )
+ with django_capture_on_commit_callbacks(execute=True):
+ fulfillments = fulfill_gift_card_lines(
+ lines, staff_user, None, order, site_settings, manager
+ )
# then
assert len(fulfillments) == 1
assert fulfillments[0].lines.count() == len(lines)
- flush_post_commit_hooks()
gift_cards = GiftCard.objects.all()
assert gift_cards.count() == sum([line.quantity for line in lines])
shippable_gift_cards = gift_cards.filter(
diff --git a/saleor/graphql/checkout/tests/mutations/test_checkout_complete_with_payment.py b/saleor/graphql/checkout/tests/mutations/test_checkout_complete_with_payment.py
index 74cfde1323..3d934a408c 100644
--- a/saleor/graphql/checkout/tests/mutations/test_checkout_complete_with_payment.py
+++ b/saleor/graphql/checkout/tests/mutations/test_checkout_complete_with_payment.py
@@ -30,7 +30,6 @@ from .....payment.gateways.dummy_credit_card import TOKEN_VALIDATION_MAPPING
from .....payment.interface import GatewayResponse
from .....payment.model_helpers import get_subtotal
from .....plugins.manager import PluginsManager, get_plugins_manager
-from .....tests.utils import flush_post_commit_hooks
from .....warehouse.models import Reservation, Stock, WarehouseClickAndCollectOption
from .....warehouse.tests.utils import get_available_quantity_for_stock
from ....core.utils import to_global_id_or_none
@@ -654,6 +653,7 @@ def test_checkout_complete_gift_card_bought(
address,
shipping_method,
payment_txn_captured,
+ django_capture_on_commit_callbacks,
):
# given
checkout = checkout_with_gift_card_items
@@ -693,19 +693,18 @@ def test_checkout_complete_gift_card_bought(
variables = {"id": to_global_id_or_none(checkout), "redirectUrl": redirect_url}
# when
- response = user_api_client.post_graphql(MUTATION_CHECKOUT_COMPLETE, variables)
+ with django_capture_on_commit_callbacks(execute=True):
+ response = user_api_client.post_graphql(MUTATION_CHECKOUT_COMPLETE, variables)
# then
content = get_graphql_content(response)
data = content["data"]["checkoutComplete"]
assert not data["errors"]
- flush_post_commit_hooks()
assert Order.objects.count() == orders_count + 1
order = Order.objects.first()
assert order.status == OrderStatus.PARTIALLY_FULFILLED
- flush_post_commit_hooks()
gift_card = GiftCard.objects.get()
assert GiftCardEvent.objects.filter(gift_card=gift_card, type=GiftCardEvents.BOUGHT)
send_notification_mock.assert_called_once_with(
diff --git a/saleor/graphql/checkout/tests/mutations/test_checkout_complete_with_transactions.py b/saleor/graphql/checkout/tests/mutations/test_checkout_complete_with_transactions.py
index 1476a237d0..7c24354477 100644
--- a/saleor/graphql/checkout/tests/mutations/test_checkout_complete_with_transactions.py
+++ b/saleor/graphql/checkout/tests/mutations/test_checkout_complete_with_transactions.py
@@ -30,7 +30,6 @@ from .....payment.model_helpers import get_subtotal
from .....payment.transaction_item_calculations import recalculate_transaction_amounts
from .....plugins.manager import PluginsManager, get_plugins_manager
from .....product.models import VariantChannelListingPromotionRule
-from .....tests.utils import flush_post_commit_hooks
from .....warehouse.models import Reservation, Stock, WarehouseClickAndCollectOption
from .....warehouse.tests.utils import get_available_quantity_for_stock
from ....core.utils import to_global_id_or_none
@@ -1622,6 +1621,7 @@ def test_checkout_complete_gift_card_bought(
shipping_method,
transaction_events_generator,
transaction_item_generator,
+ django_capture_on_commit_callbacks,
):
# given
checkout = prepare_checkout_for_test(
@@ -1649,19 +1649,18 @@ def test_checkout_complete_gift_card_bought(
variables = {"id": to_global_id_or_none(checkout), "redirectUrl": redirect_url}
# when
- response = user_api_client.post_graphql(MUTATION_CHECKOUT_COMPLETE, variables)
+ with django_capture_on_commit_callbacks(execute=True):
+ response = user_api_client.post_graphql(MUTATION_CHECKOUT_COMPLETE, variables)
# then
content = get_graphql_content(response)
data = content["data"]["checkoutComplete"]
assert not data["errors"]
- flush_post_commit_hooks()
assert Order.objects.count() == 1
order = Order.objects.first()
assert order.status == OrderStatus.PARTIALLY_FULFILLED
- flush_post_commit_hooks()
gift_card = GiftCard.objects.get()
assert GiftCardEvent.objects.filter(gift_card=gift_card, type=GiftCardEvents.BOUGHT)
send_notification_mock.assert_called_once_with(
diff --git a/saleor/graphql/checkout/tests/mutations/test_order_create_from_checkout.py b/saleor/graphql/checkout/tests/mutations/test_order_create_from_checkout.py
index d096224825..4e8508dedc 100644
--- a/saleor/graphql/checkout/tests/mutations/test_order_create_from_checkout.py
+++ b/saleor/graphql/checkout/tests/mutations/test_order_create_from_checkout.py
@@ -23,7 +23,6 @@ from .....order.models import Fulfillment, Order
from .....payment.model_helpers import get_subtotal
from .....plugins.manager import PluginsManager, get_plugins_manager
from .....product.models import ProductVariantChannelListing
-from .....tests.utils import flush_post_commit_hooks
from .....warehouse.models import Reservation, Stock, WarehouseClickAndCollectOption
from .....warehouse.tests.utils import get_available_quantity_for_stock
from ....tests.utils import assert_no_permission, get_graphql_content
@@ -483,6 +482,7 @@ def test_order_from_checkout_gift_card_bought(
address,
shipping_method,
payment_txn_captured,
+ django_capture_on_commit_callbacks,
):
# given
checkout = checkout_with_gift_card_items
@@ -526,11 +526,12 @@ def test_order_from_checkout_gift_card_bought(
variables = {"id": graphene.Node.to_global_id("Checkout", checkout.pk)}
# when
- response = app_api_client.post_graphql(
- MUTATION_ORDER_CREATE_FROM_CHECKOUT,
- variables,
- permissions=[permission_handle_checkouts],
- )
+ with django_capture_on_commit_callbacks(execute=True):
+ response = app_api_client.post_graphql(
+ MUTATION_ORDER_CREATE_FROM_CHECKOUT,
+ variables,
+ permissions=[permission_handle_checkouts],
+ )
# then
content = get_graphql_content(response)
@@ -538,13 +539,11 @@ def test_order_from_checkout_gift_card_bought(
assert not data["errors"]
assert Order.objects.count() == orders_count + 1
- flush_post_commit_hooks()
order = Order.objects.first()
assert order.status == OrderStatus.PARTIALLY_FULFILLED
gift_card = GiftCard.objects.get()
assert GiftCardEvent.objects.filter(gift_card=gift_card, type=GiftCardEvents.BOUGHT)
- flush_post_commit_hooks()
send_notification_mock.assert_called_once_with(
None,
app,
diff --git a/saleor/graphql/order/tests/mutations/test_fulfillment_refund_products.py b/saleor/graphql/order/tests/mutations/test_fulfillment_refund_products.py
index 1865fc79dd..678d5bbd02 100644
--- a/saleor/graphql/order/tests/mutations/test_fulfillment_refund_products.py
+++ b/saleor/graphql/order/tests/mutations/test_fulfillment_refund_products.py
@@ -12,7 +12,6 @@ from .....order.fetch import OrderLineInfo
from .....order.models import FulfillmentLine, FulfillmentStatus
from .....payment import ChargeStatus, PaymentError
from .....payment.interface import RefundData
-from .....tests.utils import flush_post_commit_hooks
from .....warehouse.models import Allocation, Stock
from ....core.utils import to_global_id_or_none
from ....tests.utils import assert_no_permission, get_graphql_content
@@ -996,6 +995,7 @@ def test_fulfillment_refund_products_calls_order_refunded(
permission_group_manage_orders,
fulfilled_order,
payment_dummy,
+ django_capture_on_commit_callbacks,
):
payment_dummy.total = fulfilled_order.total_gross_amount
payment_dummy.captured_amount = payment_dummy.total
@@ -1048,10 +1048,10 @@ def test_fulfillment_refund_products_calls_order_refunded(
permission_group_manage_orders.user_set.add(staff_api_client.user)
# when
- staff_api_client.post_graphql(ORDER_FULFILL_REFUND_MUTATION, variables)
+ with django_capture_on_commit_callbacks(execute=True):
+ staff_api_client.post_graphql(ORDER_FULFILL_REFUND_MUTATION, variables)
# then
- flush_post_commit_hooks()
amount = fulfillment_line_to_refund.order_line.unit_price_gross_amount * 2
amount += order_line.unit_price_gross_amount * 2
amount = amount.quantize(Decimal("0.001"))
diff --git a/saleor/graphql/order/tests/mutations/test_fulfillment_return_products.py b/saleor/graphql/order/tests/mutations/test_fulfillment_return_products.py
index 38b040f720..1c1cc60057 100644
--- a/saleor/graphql/order/tests/mutations/test_fulfillment_return_products.py
+++ b/saleor/graphql/order/tests/mutations/test_fulfillment_return_products.py
@@ -12,7 +12,6 @@ from .....order.fetch import OrderLineInfo
from .....order.models import FulfillmentStatus, Order
from .....payment import ChargeStatus, PaymentError
from .....payment.interface import RefundData
-from .....tests.utils import flush_post_commit_hooks
from .....warehouse.models import Stock
from ....core.utils import to_global_id_or_none
from ....tests.utils import assert_no_permission, get_graphql_content
@@ -1071,6 +1070,7 @@ def test_fulfillment_return_products_calls_order_refunded(
permission_group_manage_orders,
fulfilled_order,
payment_dummy,
+ django_capture_on_commit_callbacks,
):
# given
payment_dummy.total = fulfilled_order.total_gross_amount
@@ -1129,10 +1129,10 @@ def test_fulfillment_return_products_calls_order_refunded(
permission_group_manage_orders.user_set.add(staff_api_client.user)
# when
- staff_api_client.post_graphql(ORDER_FULFILL_RETURN_MUTATION, variables)
+ with django_capture_on_commit_callbacks(execute=True):
+ staff_api_client.post_graphql(ORDER_FULFILL_RETURN_MUTATION, variables)
# then
- flush_post_commit_hooks()
amount = order_line.unit_price_gross_amount * 2
amount = amount.quantize(Decimal("0.001"))
diff --git a/saleor/graphql/order/tests/mutations/test_order_fulfill.py b/saleor/graphql/order/tests/mutations/test_order_fulfill.py
index 079e634a37..b5475bd0d8 100644
--- a/saleor/graphql/order/tests/mutations/test_order_fulfill.py
+++ b/saleor/graphql/order/tests/mutations/test_order_fulfill.py
@@ -13,7 +13,6 @@ from .....order.actions import call_order_events, order_fulfilled
from .....order.error_codes import OrderErrorCode
from .....order.models import Fulfillment, FulfillmentLine
from .....product.models import ProductVariant
-from .....tests.utils import flush_post_commit_hooks
from .....warehouse.models import Allocation, Stock
from .....webhook.event_types import WebhookEventAsyncType, WebhookEventSyncType
from ....tests.utils import assert_no_permission, get_graphql_content
@@ -660,7 +659,9 @@ def test_order_fulfill_with_gift_cards(
gift_card_shippable_order_line,
permission_group_manage_orders,
warehouse,
+ django_capture_on_commit_callbacks,
):
+ # given
query = ORDER_FULFILL_MUTATION
order_id = graphene.Node.to_global_id("Order", order.id)
permission_group_manage_orders.user_set.add(staff_api_client.user)
@@ -687,10 +688,15 @@ def test_order_fulfill_with_gift_cards(
],
},
}
- response = staff_api_client.post_graphql(query, variables)
+
+ # when
+ with django_capture_on_commit_callbacks(execute=True):
+ response = staff_api_client.post_graphql(query, variables)
+
content = get_graphql_content(response)
- flush_post_commit_hooks()
data = content["data"]["orderFulfill"]
+
+ # then
assert not data["errors"]
gift_cards = GiftCard.objects.all()
assert gift_cards.count() == 2
@@ -1507,6 +1513,7 @@ def test_order_fulfill_tracking_number_updated_event_triggered(
warehouse,
site_settings,
settings,
+ django_capture_on_commit_callbacks,
):
# given
settings.PLUGINS = ["saleor.plugins.webhook.plugin.WebhookPlugin"]
@@ -1538,8 +1545,8 @@ def test_order_fulfill_tracking_number_updated_event_triggered(
},
}
# when
- staff_api_client.post_graphql(query, variables)
- flush_post_commit_hooks()
+ with django_capture_on_commit_callbacks(execute=True):
+ staff_api_client.post_graphql(query, variables)
# then
assert mocked_webhooks.call_count == 2
diff --git a/saleor/graphql/order/tests/mutations/test_order_line_update.py b/saleor/graphql/order/tests/mutations/test_order_line_update.py
index c0c50a3db2..a47bb28cac 100644
--- a/saleor/graphql/order/tests/mutations/test_order_line_update.py
+++ b/saleor/graphql/order/tests/mutations/test_order_line_update.py
@@ -13,7 +13,6 @@ from .....order.actions import call_order_event
from .....order.error_codes import OrderErrorCode
from .....order.models import OrderEvent
from .....product.models import ProductVariant
-from .....tests.utils import flush_post_commit_hooks
from .....warehouse.models import Allocation, Stock
from .....webhook.event_types import WebhookEventAsyncType, WebhookEventSyncType
from ....tests.utils import assert_no_permission, get_graphql_content
@@ -60,7 +59,9 @@ def test_order_line_update_with_out_of_stock_webhook_for_two_lines_success_scena
order_with_lines,
permission_group_manage_orders,
staff_api_client,
+ django_capture_on_commit_callbacks,
):
+ # given
Stock.objects.update(quantity=5)
query = ORDER_LINE_UPDATE_MUTATION
order = order_with_lines
@@ -74,12 +75,14 @@ def test_order_line_update_with_out_of_stock_webhook_for_two_lines_success_scena
permission_group_manage_orders.user_set.add(staff_api_client.user)
- variables = {"lineId": first_line_id, "quantity": new_quantity}
- staff_api_client.post_graphql(query, variables)
- flush_post_commit_hooks()
- variables = {"lineId": second_line_id, "quantity": new_quantity}
- staff_api_client.post_graphql(query, variables)
+ # when
+ with django_capture_on_commit_callbacks(execute=True):
+ variables = {"lineId": first_line_id, "quantity": new_quantity}
+ staff_api_client.post_graphql(query, variables)
+ variables = {"lineId": second_line_id, "quantity": new_quantity}
+ staff_api_client.post_graphql(query, variables)
+ # then
assert out_of_stock_mock.call_count == 2
out_of_stock_mock.assert_called_with(Stock.objects.last())
diff --git a/saleor/graphql/order/tests/mutations/test_order_refund.py b/saleor/graphql/order/tests/mutations/test_order_refund.py
index ba9798aeac..6f2ed6db6c 100644
--- a/saleor/graphql/order/tests/mutations/test_order_refund.py
+++ b/saleor/graphql/order/tests/mutations/test_order_refund.py
@@ -7,7 +7,6 @@ from .....order import FulfillmentStatus
from .....order import events as order_events
from .....order.error_codes import OrderErrorCode
from .....payment import ChargeStatus
-from .....tests.utils import flush_post_commit_hooks
from ....payment.types import PaymentChargeStatusEnum
from ....tests.utils import assert_no_permission, get_graphql_content
@@ -39,6 +38,7 @@ def test_order_refund(
staff_api_client,
permission_group_manage_orders,
payment_txn_captured,
+ django_capture_on_commit_callbacks,
):
# given
permission_group_manage_orders.user_set.add(staff_api_client.user)
@@ -49,7 +49,8 @@ def test_order_refund(
variables = {"id": order_id, "amount": amount}
# when
- response = staff_api_client.post_graphql(query, variables)
+ with django_capture_on_commit_callbacks(execute=True):
+ response = staff_api_client.post_graphql(query, variables)
# then
content = get_graphql_content(response)
@@ -75,7 +76,6 @@ def test_order_refund(
assert refunded_fulfillment.total_refund_amount == amount
assert refunded_fulfillment.shipping_refund_amount is None
- flush_post_commit_hooks()
mock_order_updated.assert_called_once_with(order, webhooks=set())
mock_order_refunded.assert_called_once_with(order, webhooks=set())
assert amount < order.total.gross.amount
@@ -92,6 +92,7 @@ def test_order_fully_refunded(
staff_api_client,
permission_group_manage_orders,
payment_txn_captured,
+ django_capture_on_commit_callbacks,
):
# given
permission_group_manage_orders.user_set.add(staff_api_client.user)
@@ -106,7 +107,8 @@ def test_order_fully_refunded(
variables = {"id": order_id, "amount": amount}
# when
- response = staff_api_client.post_graphql(query, variables)
+ with django_capture_on_commit_callbacks(execute=True):
+ response = staff_api_client.post_graphql(query, variables)
# then
content = get_graphql_content(response)
@@ -130,7 +132,6 @@ def test_order_fully_refunded(
assert refunded_fulfillment.total_refund_amount == payment_txn_captured.total
assert refunded_fulfillment.shipping_refund_amount is None
- flush_post_commit_hooks()
mock_order_updated.assert_called_once_with(order, webhooks=set())
mock_order_refunded.assert_called_once_with(order, webhooks=set())
mock_order_fully_refunded.assert_called_once_with(order, webhooks=set())
@@ -170,6 +171,7 @@ def test_order_refund_by_app(
app_api_client,
permission_manage_orders,
payment_txn_captured,
+ django_capture_on_commit_callbacks,
):
# given
order = payment_txn_captured.order
@@ -179,9 +181,10 @@ def test_order_refund_by_app(
variables = {"id": order_id, "amount": amount}
# when
- response = app_api_client.post_graphql(
- query, variables, permissions=(permission_manage_orders,)
- )
+ with django_capture_on_commit_callbacks(execute=True):
+ response = app_api_client.post_graphql(
+ query, variables, permissions=(permission_manage_orders,)
+ )
# then
content = get_graphql_content(response)
@@ -205,7 +208,6 @@ def test_order_refund_by_app(
assert refunded_fulfillment.total_refund_amount == payment_txn_captured.total
assert refunded_fulfillment.shipping_refund_amount is None
- flush_post_commit_hooks()
mock_order_updated.assert_called_once_with(order, webhooks=set())
mock_order_refunded.assert_called_once_with(order, webhooks=set())
mock_order_fully_refunded.assert_called_once_with(order, webhooks=set())
diff --git a/saleor/graphql/payment/tests/mutations/test_payment_refund.py b/saleor/graphql/payment/tests/mutations/test_payment_refund.py
index 5fed9f73ec..233e62f8e1 100644
--- a/saleor/graphql/payment/tests/mutations/test_payment_refund.py
+++ b/saleor/graphql/payment/tests/mutations/test_payment_refund.py
@@ -4,7 +4,6 @@ import graphene
from .....payment import TransactionKind
from .....payment.models import ChargeStatus
-from .....tests.utils import flush_post_commit_hooks
from ....core.enums import PaymentErrorCode
from ....tests.utils import assert_no_permission, get_graphql_content
@@ -36,6 +35,7 @@ def test_payment_refund_success(
permission_group_manage_orders,
payment_txn_captured,
order_with_lines,
+ django_capture_on_commit_callbacks,
):
# given
permission_group_manage_orders.user_set.add(staff_api_client.user)
@@ -49,7 +49,8 @@ def test_payment_refund_success(
variables = {"paymentId": payment_id, "amount": str(payment.total)}
# when
- response = staff_api_client.post_graphql(REFUND_QUERY, variables)
+ with django_capture_on_commit_callbacks(execute=True):
+ response = staff_api_client.post_graphql(REFUND_QUERY, variables)
# then
content = get_graphql_content(response)
@@ -61,7 +62,6 @@ def test_payment_refund_success(
txn = payment.transactions.last()
assert txn.kind == TransactionKind.REFUND
- flush_post_commit_hooks()
mock_order_updated.assert_called_once_with(payment.order, webhooks=set())
mock_order_refunded.assert_called_once_with(payment.order, webhooks=set())
mock_order_fully_refunded.assert_called_once_with(payment.order, webhooks=set())
@@ -105,6 +105,7 @@ def test_payment_refund_success_by_app(
app_api_client,
permission_manage_orders,
payment_txn_captured,
+ django_capture_on_commit_callbacks,
):
# given
payment = payment_txn_captured
@@ -116,9 +117,10 @@ def test_payment_refund_success_by_app(
variables = {"paymentId": payment_id, "amount": str(payment.total)}
# when
- response = app_api_client.post_graphql(
- REFUND_QUERY, variables, permissions=(permission_manage_orders,)
- )
+ with django_capture_on_commit_callbacks(execute=True):
+ response = app_api_client.post_graphql(
+ REFUND_QUERY, variables, permissions=(permission_manage_orders,)
+ )
# then
content = get_graphql_content(response)
@@ -130,7 +132,6 @@ def test_payment_refund_success_by_app(
txn = payment.transactions.last()
assert txn.kind == TransactionKind.REFUND
- flush_post_commit_hooks()
mock_order_updated.assert_called_once_with(payment.order, webhooks=set())
mock_order_refunded.assert_called_once_with(payment.order, webhooks=set())
mock_order_fully_refunded.assert_called_once_with(payment.order, webhooks=set())
diff --git a/saleor/graphql/product/tests/deprecated/test_product_variant_bulk_create.py b/saleor/graphql/product/tests/deprecated/test_product_variant_bulk_create.py
index fe131d89c9..c680d3224f 100644
--- a/saleor/graphql/product/tests/deprecated/test_product_variant_bulk_create.py
+++ b/saleor/graphql/product/tests/deprecated/test_product_variant_bulk_create.py
@@ -7,7 +7,6 @@ import graphene
from .....attribute import AttributeInputType
from .....product.error_codes import ProductVariantBulkErrorCode
from .....product.models import ProductChannelListing, ProductVariant
-from .....tests.utils import flush_post_commit_hooks
from ....tests.utils import get_graphql_content
PRODUCT_VARIANT_BULK_CREATE_MUTATION = """
@@ -74,6 +73,7 @@ def test_product_variant_bulk_create_by_name(
permission_manage_products,
any_webhook,
settings,
+ django_capture_on_commit_callbacks,
):
# given
mocked_get_webhooks_for_event.return_value = [any_webhook]
@@ -100,11 +100,11 @@ def test_product_variant_bulk_create_by_name(
# when
staff_api_client.user.user_permissions.add(permission_manage_products)
- response = staff_api_client.post_graphql(
- PRODUCT_VARIANT_BULK_CREATE_MUTATION, variables
- )
+ with django_capture_on_commit_callbacks(execute=True):
+ response = staff_api_client.post_graphql(
+ PRODUCT_VARIANT_BULK_CREATE_MUTATION, variables
+ )
content = get_graphql_content(response)
- flush_post_commit_hooks()
data = content["data"]["productVariantBulkCreate"]
# then
@@ -133,6 +133,7 @@ def test_product_variant_bulk_create_by_attribute_id(
permission_manage_products,
any_webhook,
settings,
+ django_capture_on_commit_callbacks,
):
# given
mocked_get_webhooks_for_event.return_value = [any_webhook]
@@ -154,11 +155,11 @@ def test_product_variant_bulk_create_by_attribute_id(
variables = {"productId": product_id, "variants": variants}
staff_api_client.user.user_permissions.add(permission_manage_products)
- response = staff_api_client.post_graphql(
- PRODUCT_VARIANT_BULK_CREATE_MUTATION, variables
- )
+ with django_capture_on_commit_callbacks(execute=True):
+ response = staff_api_client.post_graphql(
+ PRODUCT_VARIANT_BULK_CREATE_MUTATION, variables
+ )
content = get_graphql_content(response)
- flush_post_commit_hooks()
data = content["data"]["productVariantBulkCreate"]
assert not data["errors"]
diff --git a/saleor/graphql/product/tests/mutations/test_product_variant_bulk_create.py b/saleor/graphql/product/tests/mutations/test_product_variant_bulk_create.py
index 6b3708b53a..35761961fb 100644
--- a/saleor/graphql/product/tests/mutations/test_product_variant_bulk_create.py
+++ b/saleor/graphql/product/tests/mutations/test_product_variant_bulk_create.py
@@ -16,7 +16,7 @@ from .....product.models import (
ProductVariant,
ProductVariantChannelListing,
)
-from .....tests.utils import dummy_editorjs, flush_post_commit_hooks
+from .....tests.utils import dummy_editorjs
from ....core.enums import ErrorPolicyEnum
from ....tests.utils import get_graphql_content
@@ -114,6 +114,7 @@ def test_product_variant_bulk_create_by_name(
permission_manage_products,
any_webhook,
settings,
+ django_capture_on_commit_callbacks,
):
# given
mocked_get_webhooks_for_event.return_value = [any_webhook]
@@ -151,11 +152,11 @@ def test_product_variant_bulk_create_by_name(
# when
staff_api_client.user.user_permissions.add(permission_manage_products)
- response = staff_api_client.post_graphql(
- PRODUCT_VARIANT_BULK_CREATE_MUTATION, variables
- )
+ with django_capture_on_commit_callbacks(execute=True):
+ response = staff_api_client.post_graphql(
+ PRODUCT_VARIANT_BULK_CREATE_MUTATION, variables
+ )
content = get_graphql_content(response, ignore_errors=True)
- flush_post_commit_hooks()
data = content["data"]["productVariantBulkCreate"]
# then
@@ -190,6 +191,7 @@ def test_product_variant_bulk_create_by_attribute_id(
permission_manage_products,
any_webhook,
settings,
+ django_capture_on_commit_callbacks,
):
# given
mocked_get_webhooks_for_event.return_value = [any_webhook]
@@ -214,11 +216,11 @@ def test_product_variant_bulk_create_by_attribute_id(
# when
staff_api_client.user.user_permissions.add(permission_manage_products)
- response = staff_api_client.post_graphql(
- PRODUCT_VARIANT_BULK_CREATE_MUTATION, variables
- )
+ with django_capture_on_commit_callbacks(execute=True):
+ response = staff_api_client.post_graphql(
+ PRODUCT_VARIANT_BULK_CREATE_MUTATION, variables
+ )
content = get_graphql_content(response)
- flush_post_commit_hooks()
data = content["data"]["productVariantBulkCreate"]
# then
@@ -240,6 +242,7 @@ def test_product_variant_bulk_create_by_attribute_external_ref(
product,
color_attribute,
permission_manage_products,
+ django_capture_on_commit_callbacks,
):
# given
product_variant_count = ProductVariant.objects.count()
@@ -269,11 +272,11 @@ def test_product_variant_bulk_create_by_attribute_external_ref(
# when
staff_api_client.user.user_permissions.add(permission_manage_products)
- response = staff_api_client.post_graphql(
- PRODUCT_VARIANT_BULK_CREATE_MUTATION, variables
- )
+ with django_capture_on_commit_callbacks(execute=True):
+ response = staff_api_client.post_graphql(
+ PRODUCT_VARIANT_BULK_CREATE_MUTATION, variables
+ )
content = get_graphql_content(response)
- flush_post_commit_hooks()
data = content["data"]["productVariantBulkCreate"]
# then
@@ -295,6 +298,7 @@ def test_product_variant_bulk_create_return_error_when_attribute_external_ref_an
product,
color_attribute,
permission_manage_products,
+ django_capture_on_commit_callbacks,
):
# given
product.product_type.variant_attributes.add(color_attribute)
@@ -323,11 +327,11 @@ def test_product_variant_bulk_create_return_error_when_attribute_external_ref_an
# when
staff_api_client.user.user_permissions.add(permission_manage_products)
- response = staff_api_client.post_graphql(
- PRODUCT_VARIANT_BULK_CREATE_MUTATION, variables
- )
+ with django_capture_on_commit_callbacks(execute=True):
+ response = staff_api_client.post_graphql(
+ PRODUCT_VARIANT_BULK_CREATE_MUTATION, variables
+ )
content = get_graphql_content(response)
- flush_post_commit_hooks()
data = content["data"]["productVariantBulkCreate"]
# then
@@ -344,6 +348,7 @@ def test_product_variant_bulk_create_will_create_new_attr_value_and_external_ref
product,
color_attribute,
permission_manage_products,
+ django_capture_on_commit_callbacks,
):
# given
product.product_type.variant_attributes.add(color_attribute)
@@ -376,11 +381,11 @@ def test_product_variant_bulk_create_will_create_new_attr_value_and_external_ref
# when
staff_api_client.user.user_permissions.add(permission_manage_products)
- response = staff_api_client.post_graphql(
- PRODUCT_VARIANT_BULK_CREATE_MUTATION, variables
- )
+ with django_capture_on_commit_callbacks(execute=True):
+ response = staff_api_client.post_graphql(
+ PRODUCT_VARIANT_BULK_CREATE_MUTATION, variables
+ )
content = get_graphql_content(response)
- flush_post_commit_hooks()
data = content["data"]["productVariantBulkCreate"]
# then
diff --git a/saleor/graphql/product/tests/mutations/test_product_variant_bulk_update.py b/saleor/graphql/product/tests/mutations/test_product_variant_bulk_update.py
index bde28de863..f935e5362b 100644
--- a/saleor/graphql/product/tests/mutations/test_product_variant_bulk_update.py
+++ b/saleor/graphql/product/tests/mutations/test_product_variant_bulk_update.py
@@ -14,7 +14,6 @@ from .....graphql.product.bulk_mutations.product_variant_bulk_update import (
from .....graphql.webhook.subscription_payload import get_pre_save_payload_key
from .....product.error_codes import ProductVariantBulkErrorCode
from .....product.models import ProductChannelListing
-from .....tests.utils import flush_post_commit_hooks
from .....warehouse.models import Stock
from .....webhook.event_types import WebhookEventAsyncType
from .....webhook.models import Webhook
@@ -99,6 +98,7 @@ def test_product_variant_bulk_update(
permission_manage_products,
any_webhook,
settings,
+ django_capture_on_commit_callbacks,
):
# given
mocked_get_webhooks_for_event.return_value = [any_webhook]
@@ -124,11 +124,11 @@ def test_product_variant_bulk_update(
# when
staff_api_client.user.user_permissions.add(permission_manage_products)
- response = staff_api_client.post_graphql(
- PRODUCT_VARIANT_BULK_UPDATE_MUTATION, variables
- )
+ with django_capture_on_commit_callbacks(execute=True):
+ response = staff_api_client.post_graphql(
+ PRODUCT_VARIANT_BULK_UPDATE_MUTATION, variables
+ )
content = get_graphql_content(response)
- flush_post_commit_hooks()
data = content["data"]["productVariantBulkUpdate"]
product_with_single_variant.refresh_from_db(fields=["search_index_dirty"])
@@ -166,6 +166,7 @@ def test_product_variant_bulk_create_stock_thread_race(
permission_manage_products,
any_webhook,
settings,
+ django_capture_on_commit_callbacks,
):
# given
mocked_get_webhooks_for_event.return_value = [any_webhook]
@@ -214,12 +215,12 @@ def test_product_variant_bulk_create_stock_thread_race(
# when
staff_api_client.user.user_permissions.add(permission_manage_products)
- response = staff_api_client.post_graphql(
- PRODUCT_VARIANT_BULK_UPDATE_MUTATION, variables
- )
+ with django_capture_on_commit_callbacks(execute=True):
+ response = staff_api_client.post_graphql(
+ PRODUCT_VARIANT_BULK_UPDATE_MUTATION, variables
+ )
content = get_graphql_content(response)
- flush_post_commit_hooks()
data = content["data"]["productVariantBulkUpdate"]
# then
@@ -243,6 +244,7 @@ def test_product_variant_bulk_update_stocks(
permission_manage_products,
any_webhook,
settings,
+ django_capture_on_commit_callbacks,
):
# given
mocked_get_webhooks_for_event.return_value = [any_webhook]
@@ -286,11 +288,11 @@ def test_product_variant_bulk_update_stocks(
# when
staff_api_client.user.user_permissions.add(permission_manage_products)
- response = staff_api_client.post_graphql(
- PRODUCT_VARIANT_BULK_UPDATE_MUTATION, variables
- )
+ with django_capture_on_commit_callbacks(execute=True):
+ response = staff_api_client.post_graphql(
+ PRODUCT_VARIANT_BULK_UPDATE_MUTATION, variables
+ )
content = get_graphql_content(response)
- flush_post_commit_hooks()
data = content["data"]["productVariantBulkUpdate"]
# then
@@ -357,6 +359,7 @@ def test_product_variant_bulk_update_and_remove_stock(
warehouse,
size_attribute,
permission_manage_products,
+ django_capture_on_commit_callbacks,
):
# given
variant = variant_with_many_stocks
@@ -379,11 +382,11 @@ def test_product_variant_bulk_update_and_remove_stock(
# when
staff_api_client.user.user_permissions.add(permission_manage_products)
- response = staff_api_client.post_graphql(
- PRODUCT_VARIANT_BULK_UPDATE_MUTATION, variables
- )
+ with django_capture_on_commit_callbacks(execute=True):
+ response = staff_api_client.post_graphql(
+ PRODUCT_VARIANT_BULK_UPDATE_MUTATION, variables
+ )
content = get_graphql_content(response)
- flush_post_commit_hooks()
data = content["data"]["productVariantBulkUpdate"]
# then
@@ -398,6 +401,7 @@ def test_product_variant_bulk_update_and_remove_stock_when_stock_not_exists(
warehouse,
size_attribute,
permission_manage_products,
+ django_capture_on_commit_callbacks,
):
# given
variant = variant_with_many_stocks
@@ -417,11 +421,11 @@ def test_product_variant_bulk_update_and_remove_stock_when_stock_not_exists(
# when
staff_api_client.user.user_permissions.add(permission_manage_products)
- response = staff_api_client.post_graphql(
- PRODUCT_VARIANT_BULK_UPDATE_MUTATION, variables
- )
+ with django_capture_on_commit_callbacks(execute=True):
+ response = staff_api_client.post_graphql(
+ PRODUCT_VARIANT_BULK_UPDATE_MUTATION, variables
+ )
content = get_graphql_content(response)
- flush_post_commit_hooks()
data = content["data"]["productVariantBulkUpdate"]
# then
@@ -440,6 +444,7 @@ def test_product_variant_bulk_update_stocks_with_invalid_warehouse(
warehouse,
size_attribute,
permission_manage_products,
+ django_capture_on_commit_callbacks,
):
# given
variant = variant_with_many_stocks
@@ -463,11 +468,11 @@ def test_product_variant_bulk_update_stocks_with_invalid_warehouse(
# when
staff_api_client.user.user_permissions.add(permission_manage_products)
- response = staff_api_client.post_graphql(
- PRODUCT_VARIANT_BULK_UPDATE_MUTATION, variables
- )
+ with django_capture_on_commit_callbacks(execute=True):
+ response = staff_api_client.post_graphql(
+ PRODUCT_VARIANT_BULK_UPDATE_MUTATION, variables
+ )
content = get_graphql_content(response)
- flush_post_commit_hooks()
data = content["data"]["productVariantBulkUpdate"]
stock_to_update.refresh_from_db()
@@ -676,6 +681,7 @@ def test_product_variant_bulk_update_with_already_existing_sku(
product_with_two_variants,
size_attribute,
permission_manage_products,
+ django_capture_on_commit_callbacks,
):
# given
variants = product_with_two_variants.variants.all()
@@ -690,11 +696,11 @@ def test_product_variant_bulk_update_with_already_existing_sku(
# when
staff_api_client.user.user_permissions.add(permission_manage_products)
- response = staff_api_client.post_graphql(
- PRODUCT_VARIANT_BULK_UPDATE_MUTATION, variables
- )
+ with django_capture_on_commit_callbacks(execute=True):
+ response = staff_api_client.post_graphql(
+ PRODUCT_VARIANT_BULK_UPDATE_MUTATION, variables
+ )
content = get_graphql_content(response)
- flush_post_commit_hooks()
data = content["data"]["productVariantBulkUpdate"]
# then
@@ -708,6 +714,7 @@ def test_product_variant_bulk_update_when_variant_not_exists(
staff_api_client,
product_with_two_variants,
permission_manage_products,
+ django_capture_on_commit_callbacks,
):
# given
product_id = graphene.Node.to_global_id("Product", product_with_two_variants.pk)
@@ -719,11 +726,11 @@ def test_product_variant_bulk_update_when_variant_not_exists(
# when
staff_api_client.user.user_permissions.add(permission_manage_products)
- response = staff_api_client.post_graphql(
- PRODUCT_VARIANT_BULK_UPDATE_MUTATION, variables
- )
+ with django_capture_on_commit_callbacks(execute=True):
+ response = staff_api_client.post_graphql(
+ PRODUCT_VARIANT_BULK_UPDATE_MUTATION, variables
+ )
content = get_graphql_content(response)
- flush_post_commit_hooks()
data = content["data"]["productVariantBulkUpdate"]
# then
@@ -747,6 +754,7 @@ def test_product_variant_bulk_update_attributes(
settings,
multiselect_attribute,
color_attribute,
+ django_capture_on_commit_callbacks,
):
# given
mocked_get_webhooks_for_event.return_value = [any_webhook]
@@ -786,11 +794,11 @@ def test_product_variant_bulk_update_attributes(
# when
staff_api_client.user.user_permissions.add(permission_manage_products)
- response = staff_api_client.post_graphql(
- PRODUCT_VARIANT_BULK_UPDATE_MUTATION, variables
- )
+ with django_capture_on_commit_callbacks(execute=True):
+ response = staff_api_client.post_graphql(
+ PRODUCT_VARIANT_BULK_UPDATE_MUTATION, variables
+ )
content = get_graphql_content(response)
- flush_post_commit_hooks()
data = content["data"]["productVariantBulkUpdate"]
# then
@@ -810,6 +818,7 @@ def test_generate_pre_save_payloads(
variant,
permission_manage_products,
webhook_app,
+ django_capture_on_commit_callbacks,
):
# given
SUBSCRIPTION_QUERY = """
@@ -839,8 +848,8 @@ def test_generate_pre_save_payloads(
# when
staff_api_client.user.user_permissions.add(permission_manage_products)
- staff_api_client.post_graphql(PRODUCT_VARIANT_BULK_UPDATE_MUTATION, variables)
- flush_post_commit_hooks()
+ with django_capture_on_commit_callbacks(execute=True):
+ staff_api_client.post_graphql(PRODUCT_VARIANT_BULK_UPDATE_MUTATION, variables)
# then
payload_key = get_pre_save_payload_key(webhook, variant)
diff --git a/saleor/graphql/product/tests/mutations/test_product_variant_create.py b/saleor/graphql/product/tests/mutations/test_product_variant_create.py
index 55fb88c49d..d49da9b391 100644
--- a/saleor/graphql/product/tests/mutations/test_product_variant_create.py
+++ b/saleor/graphql/product/tests/mutations/test_product_variant_create.py
@@ -11,7 +11,7 @@ from freezegun import freeze_time
from .....discount.utils.promotion import get_active_catalogue_promotion_rules
from .....product.error_codes import ProductErrorCode
-from .....tests.utils import dummy_editorjs, flush_post_commit_hooks
+from .....tests.utils import dummy_editorjs
from ....core.enums import WeightUnitsEnum
from ....tests.utils import get_graphql_content
@@ -87,6 +87,7 @@ def test_create_variant_with_name(
product_type,
permission_manage_products,
warehouse,
+ django_capture_on_commit_callbacks,
):
# given
product_id = graphene.Node.to_global_id("Product", product.pk)
@@ -124,11 +125,11 @@ def test_create_variant_with_name(
}
# when
- response = staff_api_client.post_graphql(
- CREATE_VARIANT_MUTATION, variables, permissions=[permission_manage_products]
- )
+ with django_capture_on_commit_callbacks(execute=True):
+ response = staff_api_client.post_graphql(
+ CREATE_VARIANT_MUTATION, variables, permissions=[permission_manage_products]
+ )
content = get_graphql_content(response)["data"]["productVariantCreate"]
- flush_post_commit_hooks()
# then
assert not content["errors"]
@@ -164,6 +165,7 @@ def test_create_variant_without_name(
product_type,
permission_manage_products,
warehouse,
+ django_capture_on_commit_callbacks,
):
# given
query = CREATE_VARIANT_MUTATION
@@ -194,11 +196,11 @@ def test_create_variant_without_name(
}
# when
- response = staff_api_client.post_graphql(
- query, variables, permissions=[permission_manage_products]
- )
+ with django_capture_on_commit_callbacks(execute=True):
+ response = staff_api_client.post_graphql(
+ query, variables, permissions=[permission_manage_products]
+ )
content = get_graphql_content(response)["data"]["productVariantCreate"]
- flush_post_commit_hooks()
# then
assert not content["errors"]
@@ -271,6 +273,7 @@ def test_create_variant_preorder(
product,
product_type,
permission_manage_products,
+ django_capture_on_commit_callbacks,
):
query = CREATE_VARIANT_MUTATION
product_id = graphene.Node.to_global_id("Product", product.pk)
@@ -299,11 +302,11 @@ def test_create_variant_preorder(
}
}
- response = staff_api_client.post_graphql(
- query, variables, permissions=[permission_manage_products]
- )
+ with django_capture_on_commit_callbacks(execute=True):
+ response = staff_api_client.post_graphql(
+ query, variables, permissions=[permission_manage_products]
+ )
content = get_graphql_content(response)["data"]["productVariantCreate"]
- flush_post_commit_hooks()
assert not content["errors"]
data = content["productVariant"]
@@ -325,6 +328,7 @@ def test_create_variant_no_required_attributes(
product_type,
permission_manage_products,
warehouse,
+ django_capture_on_commit_callbacks,
):
query = CREATE_VARIANT_MUTATION
product_id = graphene.Node.to_global_id("Product", product.pk)
@@ -352,11 +356,11 @@ def test_create_variant_no_required_attributes(
"trackInventory": True,
}
}
- response = staff_api_client.post_graphql(
- query, variables, permissions=[permission_manage_products]
- )
+ with django_capture_on_commit_callbacks(execute=True):
+ response = staff_api_client.post_graphql(
+ query, variables, permissions=[permission_manage_products]
+ )
content = get_graphql_content(response)["data"]["productVariantCreate"]
- flush_post_commit_hooks()
assert not content["errors"]
data = content["productVariant"]
@@ -382,6 +386,7 @@ def test_create_variant_with_file_attribute(
permission_manage_products,
warehouse,
site_settings,
+ django_capture_on_commit_callbacks,
):
query = CREATE_VARIANT_MUTATION
product_id = graphene.Node.to_global_id("Product", product.pk)
@@ -414,11 +419,11 @@ def test_create_variant_with_file_attribute(
"trackInventory": True,
}
}
- response = staff_api_client.post_graphql(
- query, variables, permissions=[permission_manage_products]
- )
+ with django_capture_on_commit_callbacks(execute=True):
+ response = staff_api_client.post_graphql(
+ query, variables, permissions=[permission_manage_products]
+ )
content = get_graphql_content(response)["data"]["productVariantCreate"]
- flush_post_commit_hooks()
assert not content["errors"]
data = content["productVariant"]
@@ -449,6 +454,7 @@ def test_create_variant_with_boolean_attribute(
boolean_attribute,
size_attribute,
warehouse,
+ django_capture_on_commit_callbacks,
):
product_type.variant_attributes.add(
boolean_attribute, through_defaults={"variant_selection": True}
@@ -477,11 +483,11 @@ def test_create_variant_with_boolean_attribute(
}
}
- response = staff_api_client.post_graphql(
- query, variables, permissions=[permission_manage_products]
- )
+ with django_capture_on_commit_callbacks(execute=True):
+ response = staff_api_client.post_graphql(
+ query, variables, permissions=[permission_manage_products]
+ )
content = get_graphql_content(response)["data"]["productVariantCreate"]
- flush_post_commit_hooks()
data = content["productVariant"]
assert not content["errors"]
@@ -517,6 +523,7 @@ def test_create_variant_with_file_attribute_new_value(
permission_manage_products,
warehouse,
site_settings,
+ django_capture_on_commit_callbacks,
):
query = CREATE_VARIANT_MUTATION
product_id = graphene.Node.to_global_id("Product", product.pk)
@@ -548,11 +555,11 @@ def test_create_variant_with_file_attribute_new_value(
"trackInventory": True,
}
}
- response = staff_api_client.post_graphql(
- query, variables, permissions=[permission_manage_products]
- )
+ with django_capture_on_commit_callbacks(execute=True):
+ response = staff_api_client.post_graphql(
+ query, variables, permissions=[permission_manage_products]
+ )
content = get_graphql_content(response)["data"]["productVariantCreate"]
- flush_post_commit_hooks()
assert not content["errors"]
data = content["productVariant"]
@@ -581,6 +588,7 @@ def test_create_variant_with_file_attribute_no_file_url_given(
file_attribute,
permission_manage_products,
warehouse,
+ django_capture_on_commit_callbacks,
):
query = CREATE_VARIANT_MUTATION
product_id = graphene.Node.to_global_id("Product", product.pk)
@@ -610,11 +618,11 @@ def test_create_variant_with_file_attribute_no_file_url_given(
"trackInventory": True,
}
}
- response = staff_api_client.post_graphql(
- query, variables, permissions=[permission_manage_products]
- )
+ with django_capture_on_commit_callbacks(execute=True):
+ response = staff_api_client.post_graphql(
+ query, variables, permissions=[permission_manage_products]
+ )
content = get_graphql_content(response)["data"]["productVariantCreate"]
- flush_post_commit_hooks()
errors = content["errors"]
data = content["productVariant"]
@@ -645,6 +653,7 @@ def test_create_variant_with_page_reference_attribute(
page_list,
permission_manage_products,
warehouse,
+ django_capture_on_commit_callbacks,
):
query = CREATE_VARIANT_MUTATION
product_id = graphene.Node.to_global_id("Product", product.pk)
@@ -677,11 +686,11 @@ def test_create_variant_with_page_reference_attribute(
"trackInventory": True,
}
}
- response = staff_api_client.post_graphql(
- query, variables, permissions=[permission_manage_products]
- )
+ with django_capture_on_commit_callbacks(execute=True):
+ response = staff_api_client.post_graphql(
+ query, variables, permissions=[permission_manage_products]
+ )
content = get_graphql_content(response)["data"]["productVariantCreate"]
- flush_post_commit_hooks()
assert not content["errors"]
data = content["productVariant"]
@@ -740,6 +749,7 @@ def test_create_variant_with_page_reference_attribute_no_references_given(
permission_manage_products,
warehouse,
site_settings,
+ django_capture_on_commit_callbacks,
):
query = CREATE_VARIANT_MUTATION
product_id = graphene.Node.to_global_id("Product", product.pk)
@@ -773,11 +783,11 @@ def test_create_variant_with_page_reference_attribute_no_references_given(
"trackInventory": True,
}
}
- response = staff_api_client.post_graphql(
- query, variables, permissions=[permission_manage_products]
- )
+ with django_capture_on_commit_callbacks(execute=True):
+ response = staff_api_client.post_graphql(
+ query, variables, permissions=[permission_manage_products]
+ )
content = get_graphql_content(response)["data"]["productVariantCreate"]
- flush_post_commit_hooks()
errors = content["errors"]
data = content["productVariant"]
@@ -804,6 +814,7 @@ def test_create_variant_with_product_reference_attribute(
product_list,
permission_manage_products,
warehouse,
+ django_capture_on_commit_callbacks,
):
query = CREATE_VARIANT_MUTATION
product_id = graphene.Node.to_global_id("Product", product.pk)
@@ -841,11 +852,11 @@ def test_create_variant_with_product_reference_attribute(
"trackInventory": True,
}
}
- response = staff_api_client.post_graphql(
- query, variables, permissions=[permission_manage_products]
- )
+ with django_capture_on_commit_callbacks(execute=True):
+ response = staff_api_client.post_graphql(
+ query, variables, permissions=[permission_manage_products]
+ )
content = get_graphql_content(response)["data"]["productVariantCreate"]
- flush_post_commit_hooks()
assert not content["errors"]
data = content["productVariant"]
@@ -904,6 +915,7 @@ def test_create_variant_with_product_reference_attribute_no_references_given(
permission_manage_products,
warehouse,
site_settings,
+ django_capture_on_commit_callbacks,
):
query = CREATE_VARIANT_MUTATION
product_id = graphene.Node.to_global_id("Product", product.pk)
@@ -937,11 +949,11 @@ def test_create_variant_with_product_reference_attribute_no_references_given(
"trackInventory": True,
}
}
- response = staff_api_client.post_graphql(
- query, variables, permissions=[permission_manage_products]
- )
+ with django_capture_on_commit_callbacks(execute=True):
+ response = staff_api_client.post_graphql(
+ query, variables, permissions=[permission_manage_products]
+ )
content = get_graphql_content(response)["data"]["productVariantCreate"]
- flush_post_commit_hooks()
errors = content["errors"]
data = content["productVariant"]
@@ -968,6 +980,7 @@ def test_create_variant_with_variant_reference_attribute(
product_list,
permission_manage_products,
warehouse,
+ django_capture_on_commit_callbacks,
):
# given
query = CREATE_VARIANT_MUTATION
@@ -1010,13 +1023,13 @@ def test_create_variant_with_variant_reference_attribute(
}
# when
- response = staff_api_client.post_graphql(
- query, variables, permissions=[permission_manage_products]
- )
+ with django_capture_on_commit_callbacks(execute=True):
+ response = staff_api_client.post_graphql(
+ query, variables, permissions=[permission_manage_products]
+ )
# then
content = get_graphql_content(response)["data"]["productVariantCreate"]
- flush_post_commit_hooks()
assert not content["errors"]
data = content["productVariant"]
@@ -1075,6 +1088,7 @@ def test_create_variant_with_variant_reference_attribute_no_references_given(
permission_manage_products,
warehouse,
site_settings,
+ django_capture_on_commit_callbacks,
):
# given
query = CREATE_VARIANT_MUTATION
@@ -1111,13 +1125,13 @@ def test_create_variant_with_variant_reference_attribute_no_references_given(
}
# when
- response = staff_api_client.post_graphql(
- query, variables, permissions=[permission_manage_products]
- )
+ with django_capture_on_commit_callbacks(execute=True):
+ response = staff_api_client.post_graphql(
+ query, variables, permissions=[permission_manage_products]
+ )
# then
content = get_graphql_content(response)["data"]["productVariantCreate"]
- flush_post_commit_hooks()
errors = content["errors"]
data = content["productVariant"]
@@ -1479,6 +1493,7 @@ def test_create_variant_with_rich_text_attribute(
staff_api_client,
rich_text_attribute,
warehouse,
+ django_capture_on_commit_callbacks,
):
product_type.variant_attributes.add(rich_text_attribute)
query = CREATE_VARIANT_MUTATION
@@ -1506,11 +1521,11 @@ def test_create_variant_with_rich_text_attribute(
}
}
- response = staff_api_client.post_graphql(
- query, variables, permissions=[permission_manage_products]
- )
+ with django_capture_on_commit_callbacks(execute=True):
+ response = staff_api_client.post_graphql(
+ query, variables, permissions=[permission_manage_products]
+ )
content = get_graphql_content(response)["data"]["productVariantCreate"]
- flush_post_commit_hooks()
data = content["productVariant"]
assert not content["errors"]
@@ -1529,6 +1544,7 @@ def test_create_variant_with_plain_text_attribute(
staff_api_client,
plain_text_attribute,
warehouse,
+ django_capture_on_commit_callbacks,
):
# given
product_type.variant_attributes.add(plain_text_attribute)
@@ -1558,13 +1574,13 @@ def test_create_variant_with_plain_text_attribute(
}
# when
- response = staff_api_client.post_graphql(
- query, variables, permissions=[permission_manage_products]
- )
+ with django_capture_on_commit_callbacks(execute=True):
+ response = staff_api_client.post_graphql(
+ query, variables, permissions=[permission_manage_products]
+ )
# then
content = get_graphql_content(response)["data"]["productVariantCreate"]
- flush_post_commit_hooks()
data = content["productVariant"]
assert not content["errors"]
@@ -1584,6 +1600,7 @@ def test_create_variant_with_date_attribute(
staff_api_client,
date_attribute,
warehouse,
+ django_capture_on_commit_callbacks,
):
product_type.variant_attributes.add(date_attribute)
@@ -1606,11 +1623,11 @@ def test_create_variant_with_date_attribute(
}
}
- response = staff_api_client.post_graphql(
- query, variables, permissions=[permission_manage_products]
- )
+ with django_capture_on_commit_callbacks(execute=True):
+ response = staff_api_client.post_graphql(
+ query, variables, permissions=[permission_manage_products]
+ )
content = get_graphql_content(response)["data"]["productVariantCreate"]
- flush_post_commit_hooks()
data = content["productVariant"]
variant = product.variants.last()
expected_attributes_data = {
@@ -1647,6 +1664,7 @@ def test_create_variant_with_date_time_attribute(
staff_api_client,
date_time_attribute,
warehouse,
+ django_capture_on_commit_callbacks,
):
product_type.variant_attributes.add(date_time_attribute)
@@ -1670,11 +1688,11 @@ def test_create_variant_with_date_time_attribute(
}
}
- response = staff_api_client.post_graphql(
- query, variables, permissions=[permission_manage_products]
- )
+ with django_capture_on_commit_callbacks(execute=True):
+ response = staff_api_client.post_graphql(
+ query, variables, permissions=[permission_manage_products]
+ )
content = get_graphql_content(response)["data"]["productVariantCreate"]
- flush_post_commit_hooks()
data = content["productVariant"]
variant = product.variants.last()
expected_attributes_data = {
@@ -1711,6 +1729,7 @@ def test_create_variant_with_empty_string_for_sku(
product_type,
permission_manage_products,
warehouse,
+ django_capture_on_commit_callbacks,
):
query = CREATE_VARIANT_MUTATION
product_id = graphene.Node.to_global_id("Product", product.pk)
@@ -1738,11 +1757,11 @@ def test_create_variant_with_empty_string_for_sku(
"trackInventory": True,
}
}
- response = staff_api_client.post_graphql(
- query, variables, permissions=[permission_manage_products]
- )
+ with django_capture_on_commit_callbacks(execute=True):
+ response = staff_api_client.post_graphql(
+ query, variables, permissions=[permission_manage_products]
+ )
content = get_graphql_content(response)["data"]["productVariantCreate"]
- flush_post_commit_hooks()
assert not content["errors"]
data = content["productVariant"]
@@ -1769,6 +1788,7 @@ def test_create_variant_without_sku(
product_type,
permission_manage_products,
warehouse,
+ django_capture_on_commit_callbacks,
):
query = CREATE_VARIANT_MUTATION
product_id = graphene.Node.to_global_id("Product", product.pk)
@@ -1794,11 +1814,11 @@ def test_create_variant_without_sku(
"trackInventory": True,
}
}
- response = staff_api_client.post_graphql(
- query, variables, permissions=[permission_manage_products]
- )
+ with django_capture_on_commit_callbacks(execute=True):
+ response = staff_api_client.post_graphql(
+ query, variables, permissions=[permission_manage_products]
+ )
content = get_graphql_content(response)["data"]["productVariantCreate"]
- flush_post_commit_hooks()
assert not content["errors"]
data = content["productVariant"]
diff --git a/saleor/graphql/product/tests/mutations/test_product_variant_delete.py b/saleor/graphql/product/tests/mutations/test_product_variant_delete.py
index 9a4d114a8a..e49a741942 100644
--- a/saleor/graphql/product/tests/mutations/test_product_variant_delete.py
+++ b/saleor/graphql/product/tests/mutations/test_product_variant_delete.py
@@ -8,7 +8,6 @@ from .....discount.utils.promotion import get_active_catalogue_promotion_rules
from .....order import OrderEvents, OrderStatus
from .....order.models import OrderEvent, OrderLine
from .....product.models import ProductVariant
-from .....tests.utils import flush_post_commit_hooks
from ....tests.utils import get_graphql_content
DELETE_VARIANT_BY_SKU_MUTATION = """
@@ -31,6 +30,7 @@ def test_delete_variant_by_sku(
staff_api_client,
product,
permission_manage_products,
+ django_capture_on_commit_callbacks,
):
# given
variant = product.variants.first()
@@ -38,13 +38,13 @@ def test_delete_variant_by_sku(
variables = {"sku": variant_sku}
# when
- response = staff_api_client.post_graphql(
- DELETE_VARIANT_BY_SKU_MUTATION,
- variables,
- permissions=[permission_manage_products],
- )
+ with django_capture_on_commit_callbacks(execute=True):
+ response = staff_api_client.post_graphql(
+ DELETE_VARIANT_BY_SKU_MUTATION,
+ variables,
+ permissions=[permission_manage_products],
+ )
content = get_graphql_content(response)
- flush_post_commit_hooks()
data = content["data"]["productVariantDelete"]
# then
@@ -77,17 +77,18 @@ def test_delete_variant(
staff_api_client,
product,
permission_manage_products,
+ django_capture_on_commit_callbacks,
):
query = DELETE_VARIANT_MUTATION
variant = product.variants.first()
variant_id = graphene.Node.to_global_id("ProductVariant", variant.pk)
variant_sku = variant.sku
variables = {"id": variant_id}
- response = staff_api_client.post_graphql(
- query, variables, permissions=[permission_manage_products]
- )
+ with django_capture_on_commit_callbacks(execute=True):
+ response = staff_api_client.post_graphql(
+ query, variables, permissions=[permission_manage_products]
+ )
content = get_graphql_content(response)
- flush_post_commit_hooks()
data = content["data"]["productVariantDelete"]
product_variant_deleted_webhook_mock.assert_called_once_with(variant)
@@ -103,17 +104,18 @@ def test_delete_variant_remove_checkout_lines(
staff_api_client,
checkout_with_items,
permission_manage_products,
+ django_capture_on_commit_callbacks,
):
query = DELETE_VARIANT_MUTATION
line = checkout_with_items.lines.first()
variant = line.variant
variant_id = graphene.Node.to_global_id("ProductVariant", variant.pk)
variables = {"id": variant_id}
- response = staff_api_client.post_graphql(
- query, variables, permissions=[permission_manage_products]
- )
+ with django_capture_on_commit_callbacks(execute=True):
+ response = staff_api_client.post_graphql(
+ query, variables, permissions=[permission_manage_products]
+ )
content = get_graphql_content(response)
- flush_post_commit_hooks()
data = content["data"]["productVariantDelete"]
assert data["productVariant"]["sku"] == variant.sku
@@ -134,6 +136,7 @@ def test_delete_variant_with_image(
variant_with_image,
permission_manage_products,
media_root,
+ django_capture_on_commit_callbacks,
):
"""Ensure deleting variant doesn't delete linked product image."""
@@ -142,11 +145,11 @@ def test_delete_variant_with_image(
variant_id = graphene.Node.to_global_id("ProductVariant", variant.pk)
variables = {"id": variant_id}
- response = staff_api_client.post_graphql(
- query, variables, permissions=[permission_manage_products]
- )
+ with django_capture_on_commit_callbacks(execute=True):
+ response = staff_api_client.post_graphql(
+ query, variables, permissions=[permission_manage_products]
+ )
content = get_graphql_content(response)
- flush_post_commit_hooks()
data = content["data"]["productVariantDelete"]
product_variant_deleted_webhook_mock.assert_called_once_with(variant)
@@ -378,6 +381,7 @@ def test_delete_variant_delete_product_channel_listing_without_available_channel
staff_api_client,
product,
permission_manage_products,
+ django_capture_on_commit_callbacks,
):
"""Test that the product is unlisted if all listed variants are removed."""
# given
@@ -393,13 +397,13 @@ def test_delete_variant_delete_product_channel_listing_without_available_channel
assert product.channel_listings.count() == 1
# when
- response = staff_api_client.post_graphql(
- query, variables, permissions=[permission_manage_products]
- )
+ with django_capture_on_commit_callbacks(execute=True):
+ response = staff_api_client.post_graphql(
+ query, variables, permissions=[permission_manage_products]
+ )
# then
content = get_graphql_content(response)
- flush_post_commit_hooks()
data = content["data"]["productVariantDelete"]
product_variant_deleted_webhook_mock.assert_called_once_with(variant)
@@ -419,6 +423,7 @@ def test_delete_variant_delete_product_channel_listing_not_deleted(
staff_api_client,
product_with_two_variants,
permission_manage_products,
+ django_capture_on_commit_callbacks,
):
"""Test that the product listing persists if any variant listings remain."""
# given
@@ -432,13 +437,13 @@ def test_delete_variant_delete_product_channel_listing_not_deleted(
product_channel_listing_count = product.channel_listings.count()
# when
- response = staff_api_client.post_graphql(
- query, variables, permissions=[permission_manage_products]
- )
+ with django_capture_on_commit_callbacks(execute=True):
+ response = staff_api_client.post_graphql(
+ query, variables, permissions=[permission_manage_products]
+ )
# then
content = get_graphql_content(response)
- flush_post_commit_hooks()
data = content["data"]["productVariantDelete"]
product_variant_deleted_webhook_mock.assert_called_once_with(variant)
@@ -474,6 +479,7 @@ def test_delete_variant_by_external_reference(
staff_api_client,
product,
permission_manage_products,
+ django_capture_on_commit_callbacks,
):
# given
query = DELETE_VARIANT_BY_EXTERNAL_REFERENCE
@@ -484,13 +490,13 @@ def test_delete_variant_by_external_reference(
variables = {"externalReference": ext_ref}
# when
- response = staff_api_client.post_graphql(
- query,
- variables,
- permissions=[permission_manage_products],
- )
+ with django_capture_on_commit_callbacks(execute=True):
+ response = staff_api_client.post_graphql(
+ query,
+ variables,
+ permissions=[permission_manage_products],
+ )
content = get_graphql_content(response)
- flush_post_commit_hooks()
data = content["data"]["productVariantDelete"]
# then
diff --git a/saleor/graphql/product/tests/mutations/test_product_variant_preorder_deactivate.py b/saleor/graphql/product/tests/mutations/test_product_variant_preorder_deactivate.py
index b801707b5a..34cc6cff69 100644
--- a/saleor/graphql/product/tests/mutations/test_product_variant_preorder_deactivate.py
+++ b/saleor/graphql/product/tests/mutations/test_product_variant_preorder_deactivate.py
@@ -4,7 +4,6 @@ import graphene
from .....core.exceptions import PreorderAllocationError
from .....product.error_codes import ProductErrorCode
-from .....tests.utils import flush_post_commit_hooks
from .....warehouse.models import Allocation
from ....tests.utils import assert_no_permission, get_graphql_content
@@ -40,6 +39,7 @@ def test_product_variant_deactivate_preorder(
permission_manage_products,
preorder_variant_global_and_channel_threshold,
preorder_allocation,
+ django_capture_on_commit_callbacks,
):
variant = preorder_variant_global_and_channel_threshold
variant_id = graphene.Node.to_global_id("ProductVariant", variant.pk)
@@ -48,14 +48,14 @@ def test_product_variant_deactivate_preorder(
stock__product_variant_id=variant.pk
).count()
- response = staff_api_client.post_graphql(
- QUERY_VARIANT_DEACTIVATE_PREORDER,
- {"id": variant_id},
- permissions=[permission_manage_products],
- )
+ with django_capture_on_commit_callbacks(execute=True):
+ response = staff_api_client.post_graphql(
+ QUERY_VARIANT_DEACTIVATE_PREORDER,
+ {"id": variant_id},
+ permissions=[permission_manage_products],
+ )
variant.refresh_from_db()
content = get_graphql_content(response)
- flush_post_commit_hooks()
data = content["data"]["productVariantPreorderDeactivate"]["productVariant"]
assert not data["preorder"]
diff --git a/saleor/graphql/product/tests/mutations/test_product_variant_stocks_update.py b/saleor/graphql/product/tests/mutations/test_product_variant_stocks_update.py
index f9c1aa28ca..b940dc0504 100644
--- a/saleor/graphql/product/tests/mutations/test_product_variant_stocks_update.py
+++ b/saleor/graphql/product/tests/mutations/test_product_variant_stocks_update.py
@@ -6,7 +6,6 @@ from django.core.exceptions import ValidationError
from django.db.models import Sum
from .....plugins.manager import get_plugins_manager
-from .....tests.utils import flush_post_commit_hooks
from .....warehouse.error_codes import StockErrorCode
from .....warehouse.models import Stock, Warehouse
from ....tests.utils import get_graphql_content
@@ -262,6 +261,7 @@ def test_update_or_create_variant_stocks_when_stock_out_of_quantity(
warehouses,
any_webhook,
settings,
+ django_capture_on_commit_callbacks,
):
# given
mocked_get_webhooks_for_event.return_value = [any_webhook]
@@ -273,12 +273,12 @@ def test_update_or_create_variant_stocks_when_stock_out_of_quantity(
)
stocks_data = [{"quantity": 10, "warehouse": "321"}]
- ProductVariantStocksUpdate.update_or_create_variant_stocks(
- variant, stocks_data, warehouses, get_plugins_manager(allow_replica=False)
- )
+ with django_capture_on_commit_callbacks(execute=True):
+ ProductVariantStocksUpdate.update_or_create_variant_stocks(
+ variant, stocks_data, warehouses, get_plugins_manager(allow_replica=False)
+ )
variant.refresh_from_db()
- flush_post_commit_hooks()
assert variant.stocks.count() == 1
assert {stock.quantity for stock in variant.stocks.all()} == {
data["quantity"] for data in stocks_data
@@ -322,6 +322,7 @@ def test_update_or_create_variant_with_back_in_stock_webhooks_only_success(
variant,
warehouses,
any_webhook,
+ django_capture_on_commit_callbacks,
):
Stock.objects.bulk_create(
[
@@ -337,13 +338,13 @@ def test_update_or_create_variant_with_back_in_stock_webhooks_only_success(
]
assert variant.stocks.aggregate(Sum("quantity"))["quantity__sum"] == 0
- ProductVariantStocksUpdate.update_or_create_variant_stocks(
- variant, stocks_data, warehouses, plugins
- )
+ with django_capture_on_commit_callbacks(execute=True):
+ ProductVariantStocksUpdate.update_or_create_variant_stocks(
+ variant, stocks_data, warehouses, plugins
+ )
assert variant.stocks.aggregate(Sum("quantity"))["quantity__sum"] == 10
- flush_post_commit_hooks()
product_variant_back_in_stock_webhook.assert_called_once_with(
Stock.objects.all()[1], webhooks=[any_webhook]
)
@@ -369,6 +370,7 @@ def test_update_or_create_variant_with_back_in_stock_webhooks_only_failed(
variant,
warehouses,
any_webhook,
+ django_capture_on_commit_callbacks,
):
Stock.objects.bulk_create(
[
@@ -385,13 +387,13 @@ def test_update_or_create_variant_with_back_in_stock_webhooks_only_failed(
]
assert variant.stocks.aggregate(Sum("quantity"))["quantity__sum"] == 0
- ProductVariantStocksUpdate.update_or_create_variant_stocks(
- variant, stocks_data, warehouses, plugins
- )
+ with django_capture_on_commit_callbacks(execute=True):
+ ProductVariantStocksUpdate.update_or_create_variant_stocks(
+ variant, stocks_data, warehouses, plugins
+ )
assert variant.stocks.aggregate(Sum("quantity"))["quantity__sum"] == 0
- flush_post_commit_hooks()
product_variant_back_in_stock_webhook.assert_not_called()
product_variant_stock_out_of_stock_webhook.assert_called_once_with(
Stock.objects.all()[1], webhooks=[any_webhook]
@@ -418,6 +420,7 @@ def test_update_or_create_variant_with_back_in_stock_webhooks_with_allocations(
variant,
warehouse,
any_webhook,
+ django_capture_on_commit_callbacks,
):
stock_quantity = 4
stock = Stock.objects.create(
@@ -435,11 +438,11 @@ def test_update_or_create_variant_with_back_in_stock_webhooks_with_allocations(
]
# when
- ProductVariantStocksUpdate.update_or_create_variant_stocks(
- variant, stocks_data, [warehouse], plugins
- )
+ with django_capture_on_commit_callbacks(execute=True):
+ ProductVariantStocksUpdate.update_or_create_variant_stocks(
+ variant, stocks_data, [warehouse], plugins
+ )
# then
- flush_post_commit_hooks()
product_variant_back_in_stock_webhook.assert_called_once_with(
stock, webhooks=[any_webhook]
)
@@ -465,6 +468,7 @@ def test_update_or_create_variant_with_out_of_stock_webhooks_with_allocations(
variant,
warehouse,
any_webhook,
+ django_capture_on_commit_callbacks,
):
stock_quantity = 4
stock = Stock.objects.create(
@@ -482,11 +486,11 @@ def test_update_or_create_variant_with_out_of_stock_webhooks_with_allocations(
]
# when
- ProductVariantStocksUpdate.update_or_create_variant_stocks(
- variant, stocks_data, [warehouse], plugins
- )
+ with django_capture_on_commit_callbacks(execute=True):
+ ProductVariantStocksUpdate.update_or_create_variant_stocks(
+ variant, stocks_data, [warehouse], plugins
+ )
# then
- flush_post_commit_hooks()
product_variant_stock_out_of_stock_webhook.assert_called_once_with(
stock, webhooks=[any_webhook]
)
@@ -512,6 +516,7 @@ def test_update_or_create_variant_stocks_with_out_of_stock_webhook_only(
variant,
warehouses,
any_webhook,
+ django_capture_on_commit_callbacks,
):
Stock.objects.bulk_create(
[
@@ -531,10 +536,10 @@ def test_update_or_create_variant_stocks_with_out_of_stock_webhook_only(
]
assert variant.stocks.aggregate(Sum("quantity"))["quantity__sum"] == 10
- ProductVariantStocksUpdate.update_or_create_variant_stocks(
- variant, stocks_data, warehouses, plugins
- )
- flush_post_commit_hooks()
+ with django_capture_on_commit_callbacks(execute=True):
+ ProductVariantStocksUpdate.update_or_create_variant_stocks(
+ variant, stocks_data, warehouses, plugins
+ )
assert variant.stocks.aggregate(Sum("quantity"))["quantity__sum"] == 2
assert product_variant_stock_out_of_stock_webhook.call_count == 1
diff --git a/saleor/graphql/product/tests/mutations/test_product_variant_update.py b/saleor/graphql/product/tests/mutations/test_product_variant_update.py
index ada5896ee2..54ced9559d 100644
--- a/saleor/graphql/product/tests/mutations/test_product_variant_update.py
+++ b/saleor/graphql/product/tests/mutations/test_product_variant_update.py
@@ -13,7 +13,6 @@ from .....attribute import AttributeInputType
from .....attribute.models import AttributeValue
from .....attribute.utils import associate_attribute_values_to_instance
from .....product.error_codes import ProductErrorCode
-from .....tests.utils import flush_post_commit_hooks
from ....tests.utils import get_graphql_content
@@ -100,6 +99,7 @@ def test_update_product_variant_by_id(
product,
size_attribute,
permission_manage_products,
+ django_capture_on_commit_callbacks,
):
query = """
mutation updateVariant (
@@ -149,12 +149,12 @@ def test_update_product_variant_by_id(
"externalReference": external_reference,
}
- response = staff_api_client.post_graphql(
- query, variables, permissions=[permission_manage_products]
- )
+ with django_capture_on_commit_callbacks(execute=True):
+ response = staff_api_client.post_graphql(
+ query, variables, permissions=[permission_manage_products]
+ )
variant.refresh_from_db()
content = get_graphql_content(response)
- flush_post_commit_hooks()
data = content["data"]["productVariantUpdate"]["productVariant"]
assert data["name"] == variant.name
@@ -173,6 +173,7 @@ def test_update_product_variant_marks_prices_as_dirty(
size_attribute,
permission_manage_products,
catalogue_promotion,
+ django_capture_on_commit_callbacks,
):
# given
query = """
@@ -201,14 +202,14 @@ def test_update_product_variant_marks_prices_as_dirty(
}
# when
- response = staff_api_client.post_graphql(
- query, variables, permissions=[permission_manage_products]
- )
+ with django_capture_on_commit_callbacks(execute=True):
+ response = staff_api_client.post_graphql(
+ query, variables, permissions=[permission_manage_products]
+ )
# then
variant.refresh_from_db()
get_graphql_content(response)
- flush_post_commit_hooks()
assert not catalogue_promotion.rules.filter(variants_dirty=False).exists()
@@ -258,6 +259,7 @@ def test_update_product_variant_by_sku(
product,
size_attribute,
permission_manage_products,
+ django_capture_on_commit_callbacks,
):
# given
variant = product.variants.first()
@@ -275,12 +277,13 @@ def test_update_product_variant_by_sku(
}
# when
- response = staff_api_client.post_graphql(
- UPDATE_VARIANT_BY_SKU, variables, permissions=[permission_manage_products]
- )
+ with django_capture_on_commit_callbacks(execute=True):
+ response = staff_api_client.post_graphql(
+ UPDATE_VARIANT_BY_SKU, variables, permissions=[permission_manage_products]
+ )
+
variant.refresh_from_db()
content = get_graphql_content(response)
- flush_post_commit_hooks()
data = content["data"]["productVariantUpdate"]["productVariant"]
# then
@@ -298,6 +301,7 @@ def test_update_product_variant_by_sku_return_error_when_sku_dont_exists(
product,
size_attribute,
permission_manage_products,
+ django_capture_on_commit_callbacks,
):
# given
variant = product.variants.first()
@@ -315,12 +319,12 @@ def test_update_product_variant_by_sku_return_error_when_sku_dont_exists(
}
# when
- response = staff_api_client.post_graphql(
- UPDATE_VARIANT_BY_SKU, variables, permissions=[permission_manage_products]
- )
+ with django_capture_on_commit_callbacks(execute=True):
+ response = staff_api_client.post_graphql(
+ UPDATE_VARIANT_BY_SKU, variables, permissions=[permission_manage_products]
+ )
variant.refresh_from_db()
content = get_graphql_content(response)
- flush_post_commit_hooks()
data = content["data"]["productVariantUpdate"]
# then
@@ -361,6 +365,7 @@ def test_update_product_variant_by_external_reference(
product,
size_attribute,
permission_manage_products,
+ django_capture_on_commit_callbacks,
):
# given
query = UPDATE_VARIANT_BY_EXTERNAL_REFERENCE
@@ -377,12 +382,12 @@ def test_update_product_variant_by_external_reference(
}
# when
- response = staff_api_client.post_graphql(
- query, variables, permissions=[permission_manage_products]
- )
+ with django_capture_on_commit_callbacks(execute=True):
+ response = staff_api_client.post_graphql(
+ query, variables, permissions=[permission_manage_products]
+ )
variant.refresh_from_db()
content = get_graphql_content(response)
- flush_post_commit_hooks()
data = content["data"]["productVariantUpdate"]
# then
@@ -2261,6 +2266,7 @@ def test_update_product_variant_change_preorder_data(
staff_api_client,
permission_manage_products,
preorder_variant_global_threshold,
+ django_capture_on_commit_callbacks,
):
variant = preorder_variant_global_threshold
variant_id = graphene.Node.to_global_id("ProductVariant", variant.pk)
@@ -2283,14 +2289,14 @@ def test_update_product_variant_change_preorder_data(
},
}
- response = staff_api_client.post_graphql(
- QUERY_UPDATE_VARIANT_PREORDER,
- variables,
- permissions=[permission_manage_products],
- )
+ with django_capture_on_commit_callbacks(execute=True):
+ response = staff_api_client.post_graphql(
+ QUERY_UPDATE_VARIANT_PREORDER,
+ variables,
+ permissions=[permission_manage_products],
+ )
variant.refresh_from_db()
content = get_graphql_content(response)
- flush_post_commit_hooks()
data = content["data"]["productVariantUpdate"]["productVariant"]
assert data["sku"] == sku
@@ -2302,6 +2308,7 @@ def test_update_product_variant_can_not_turn_off_preorder(
staff_api_client,
permission_manage_products,
preorder_variant_global_threshold,
+ django_capture_on_commit_callbacks,
):
"""Test that preorder cannot be disabled through updating the `preorder` field directly."""
variant = preorder_variant_global_threshold
@@ -2310,14 +2317,14 @@ def test_update_product_variant_can_not_turn_off_preorder(
variables = {"id": variant_id, "sku": sku, "preorder": None}
- response = staff_api_client.post_graphql(
- QUERY_UPDATE_VARIANT_PREORDER,
- variables,
- permissions=[permission_manage_products],
- )
+ with django_capture_on_commit_callbacks(execute=True):
+ response = staff_api_client.post_graphql(
+ QUERY_UPDATE_VARIANT_PREORDER,
+ variables,
+ permissions=[permission_manage_products],
+ )
variant.refresh_from_db()
content = get_graphql_content(response)
- flush_post_commit_hooks()
data = content["data"]["productVariantUpdate"]["productVariant"]
assert data["sku"] == sku
diff --git a/saleor/graphql/product/tests/test_bulk_delete.py b/saleor/graphql/product/tests/test_bulk_delete.py
index b8ac5e7046..8e8204b2e8 100644
--- a/saleor/graphql/product/tests/test_bulk_delete.py
+++ b/saleor/graphql/product/tests/test_bulk_delete.py
@@ -26,7 +26,6 @@ from ....product.models import (
ProductVariantChannelListing,
VariantMedia,
)
-from ....tests.utils import flush_post_commit_hooks
from ....thumbnail.models import Thumbnail
from ...tests.utils import get_graphql_content
@@ -116,6 +115,7 @@ def test_delete_categories_trigger_webhook(
category_list,
permission_manage_products,
settings,
+ django_capture_on_commit_callbacks,
):
# given
mocked_get_webhooks_for_event.return_value = [any_webhook]
@@ -129,11 +129,12 @@ def test_delete_categories_trigger_webhook(
}
# when
- response = staff_api_client.post_graphql(
- MUTATION_CATEGORY_BULK_DELETE,
- variables,
- permissions=[permission_manage_products],
- )
+ with django_capture_on_commit_callbacks(execute=True):
+ response = staff_api_client.post_graphql(
+ MUTATION_CATEGORY_BULK_DELETE,
+ variables,
+ permissions=[permission_manage_products],
+ )
content = get_graphql_content(response)
# then
@@ -195,6 +196,7 @@ def test_delete_categories_trigger_product_updated_webhook(
permission_manage_products,
any_webhook,
settings,
+ django_capture_on_commit_callbacks,
):
# given
mocked_get_webhooks_for_event.return_value = [any_webhook]
@@ -214,11 +216,12 @@ def test_delete_categories_trigger_product_updated_webhook(
for category in category_list
]
}
- response = staff_api_client.post_graphql(
- MUTATION_CATEGORY_BULK_DELETE,
- variables,
- permissions=[permission_manage_products],
- )
+ with django_capture_on_commit_callbacks(execute=True):
+ response = staff_api_client.post_graphql(
+ MUTATION_CATEGORY_BULK_DELETE,
+ variables,
+ permissions=[permission_manage_products],
+ )
content = get_graphql_content(response)
assert content["data"]["categoryBulkDelete"]["count"] == 3
@@ -398,6 +401,7 @@ def test_delete_collections_trigger_collection_deleted_webhook(
permission_manage_products,
any_webhook,
settings,
+ django_capture_on_commit_callbacks,
):
# given
mocked_get_webhooks_for_event.return_value = [any_webhook]
@@ -408,11 +412,12 @@ def test_delete_collections_trigger_collection_deleted_webhook(
for collection in collection_list
]
}
- response = staff_api_client.post_graphql(
- MUTATION_COLLECTION_BULK_DELETE,
- variables,
- permissions=[permission_manage_products],
- )
+ with django_capture_on_commit_callbacks(execute=True):
+ response = staff_api_client.post_graphql(
+ MUTATION_COLLECTION_BULK_DELETE,
+ variables,
+ permissions=[permission_manage_products],
+ )
content = get_graphql_content(response)
assert content["data"]["collectionBulkDelete"]["count"] == 3
@@ -436,6 +441,7 @@ def test_delete_collections_trigger_product_updated_webhook(
permission_manage_products,
any_webhook,
settings,
+ django_capture_on_commit_callbacks,
):
# given
mocked_get_webhooks_for_event.return_value = [any_webhook]
@@ -448,11 +454,12 @@ def test_delete_collections_trigger_product_updated_webhook(
for collection in collection_list
]
}
- response = staff_api_client.post_graphql(
- MUTATION_COLLECTION_BULK_DELETE,
- variables,
- permissions=[permission_manage_products],
- )
+ with django_capture_on_commit_callbacks(execute=True):
+ response = staff_api_client.post_graphql(
+ MUTATION_COLLECTION_BULK_DELETE,
+ variables,
+ permissions=[permission_manage_products],
+ )
content = get_graphql_content(response)
assert content["data"]["collectionBulkDelete"]["count"] == 3
@@ -483,6 +490,7 @@ def test_delete_products(
permission_manage_products,
order_list,
channel_USD,
+ django_capture_on_commit_callbacks,
):
# given
query = DELETE_PRODUCTS_MUTATION
@@ -539,9 +547,10 @@ def test_delete_products(
}
# when
- response = staff_api_client.post_graphql(
- query, variables, permissions=[permission_manage_products]
- )
+ with django_capture_on_commit_callbacks(execute=True):
+ response = staff_api_client.post_graphql(
+ query, variables, permissions=[permission_manage_products]
+ )
# then
content = get_graphql_content(response)
@@ -630,6 +639,7 @@ def test_delete_products_trigger_webhook(
permission_manage_products,
channel_USD,
settings,
+ django_capture_on_commit_callbacks,
):
# given
mocked_get_webhooks_for_event.return_value = [any_webhook]
@@ -642,9 +652,10 @@ def test_delete_products_trigger_webhook(
for product in product_list
]
}
- response = staff_api_client.post_graphql(
- query, variables, permissions=[permission_manage_products]
- )
+ with django_capture_on_commit_callbacks(execute=True):
+ response = staff_api_client.post_graphql(
+ query, variables, permissions=[permission_manage_products]
+ )
content = get_graphql_content(response)
assert content["data"]["productBulkDelete"]["count"] == 3
@@ -666,6 +677,7 @@ def test_delete_products_without_variants(
permission_manage_products,
channel_USD,
settings,
+ django_capture_on_commit_callbacks,
):
# given
mocked_get_webhooks_for_event.return_value = [any_webhook]
@@ -681,9 +693,10 @@ def test_delete_products_without_variants(
for product in product_list
]
}
- response = staff_api_client.post_graphql(
- query, variables, permissions=[permission_manage_products]
- )
+ with django_capture_on_commit_callbacks(execute=True):
+ response = staff_api_client.post_graphql(
+ query, variables, permissions=[permission_manage_products]
+ )
content = get_graphql_content(response)
assert content["data"]["productBulkDelete"]["count"] == 3
@@ -967,6 +980,7 @@ def test_delete_product_variants_by_sku(
permission_manage_products,
any_webhook,
settings,
+ django_capture_on_commit_callbacks,
):
# given
mocked_get_webhooks_for_event.return_value = [any_webhook]
@@ -984,13 +998,13 @@ def test_delete_product_variants_by_sku(
variables = {"skus": [variant.sku for variant in product_variant_list]}
# when
- response = staff_api_client.post_graphql(
- PRODUCT_VARIANT_BULK_DELETE_BY_SKU_MUTATION,
- variables,
- permissions=[permission_manage_products],
- )
+ with django_capture_on_commit_callbacks(execute=True):
+ response = staff_api_client.post_graphql(
+ PRODUCT_VARIANT_BULK_DELETE_BY_SKU_MUTATION,
+ variables,
+ permissions=[permission_manage_products],
+ )
content = get_graphql_content(response)
- flush_post_commit_hooks()
# then
assert content["data"]["productVariantBulkDelete"]["count"] == 4
@@ -1019,6 +1033,7 @@ def test_delete_product_variants_by_sku_task_for_recalculate_product_prices_call
permission_manage_products,
any_webhook,
settings,
+ django_capture_on_commit_callbacks,
):
# given
mocked_get_webhooks_for_event.return_value = [any_webhook]
@@ -1031,13 +1046,13 @@ def test_delete_product_variants_by_sku_task_for_recalculate_product_prices_call
variables = {"skus": [variant.sku for variant in variants]}
# when
- response = staff_api_client.post_graphql(
- PRODUCT_VARIANT_BULK_DELETE_BY_SKU_MUTATION,
- variables,
- permissions=[permission_manage_products],
- )
+ with django_capture_on_commit_callbacks(execute=True):
+ response = staff_api_client.post_graphql(
+ PRODUCT_VARIANT_BULK_DELETE_BY_SKU_MUTATION,
+ variables,
+ permissions=[permission_manage_products],
+ )
content = get_graphql_content(response)
- flush_post_commit_hooks()
# then
assert content["data"]["productVariantBulkDelete"]["count"] == len(variants)
@@ -1081,6 +1096,7 @@ def test_delete_product_variants(
permission_manage_products,
any_webhook,
settings,
+ django_capture_on_commit_callbacks,
):
# given
mocked_get_webhooks_for_event.return_value = [any_webhook]
@@ -1103,11 +1119,11 @@ def test_delete_product_variants(
for variant in product_variant_list
]
}
- response = staff_api_client.post_graphql(
- query, variables, permissions=[permission_manage_products]
- )
+ with django_capture_on_commit_callbacks(execute=True):
+ response = staff_api_client.post_graphql(
+ query, variables, permissions=[permission_manage_products]
+ )
content = get_graphql_content(response)
- flush_post_commit_hooks()
assert content["data"]["productVariantBulkDelete"]["count"] == 4
assert not ProductVariant.objects.filter(
@@ -1135,6 +1151,7 @@ def test_delete_product_variants_task_for_recalculate_product_prices_called(
permission_manage_products,
any_webhook,
settings,
+ django_capture_on_commit_callbacks,
):
# given
mocked_get_webhooks_for_event.return_value = [any_webhook]
@@ -1152,11 +1169,11 @@ def test_delete_product_variants_task_for_recalculate_product_prices_called(
for variant in variants
]
}
- response = staff_api_client.post_graphql(
- query, variables, permissions=[permission_manage_products]
- )
+ with django_capture_on_commit_callbacks(execute=True):
+ response = staff_api_client.post_graphql(
+ query, variables, permissions=[permission_manage_products]
+ )
content = get_graphql_content(response)
- flush_post_commit_hooks()
assert content["data"]["productVariantBulkDelete"]["count"] == len(variants)
assert not ProductVariant.objects.filter(
@@ -1203,6 +1220,7 @@ def test_delete_product_variants_removes_checkout_lines(
checkout,
product_list,
permission_manage_products,
+ django_capture_on_commit_callbacks,
):
query = PRODUCT_VARIANT_BULK_DELETE_MUTATION
@@ -1227,11 +1245,11 @@ def test_delete_product_variants_removes_checkout_lines(
for variant in variant_list
]
}
- response = staff_api_client.post_graphql(
- query, variables, permissions=[permission_manage_products]
- )
+ with django_capture_on_commit_callbacks(execute=True):
+ response = staff_api_client.post_graphql(
+ query, variables, permissions=[permission_manage_products]
+ )
content = get_graphql_content(response)
- flush_post_commit_hooks()
assert content["data"]["productVariantBulkDelete"]["count"] == 2
assert not ProductVariant.objects.filter(
@@ -1263,6 +1281,7 @@ def test_delete_product_variants_with_images(
media_root,
any_webhook,
settings,
+ django_capture_on_commit_callbacks,
):
# given
mocked_get_webhooks_for_event.return_value = [any_webhook]
@@ -1289,11 +1308,11 @@ def test_delete_product_variants_with_images(
for variant in product_variant_list
]
}
- response = staff_api_client.post_graphql(
- query, variables, permissions=[permission_manage_products]
- )
+ with django_capture_on_commit_callbacks(execute=True):
+ response = staff_api_client.post_graphql(
+ query, variables, permissions=[permission_manage_products]
+ )
content = get_graphql_content(response)
- flush_post_commit_hooks()
assert content["data"]["productVariantBulkDelete"]["count"] == 4
assert not ProductVariant.objects.filter(
@@ -1489,6 +1508,7 @@ def test_delete_product_variants_with_file_attribute(
file_attribute,
any_webhook,
settings,
+ django_capture_on_commit_callbacks,
):
# given
mocked_get_webhooks_for_event.return_value = [any_webhook]
@@ -1514,11 +1534,11 @@ def test_delete_product_variants_with_file_attribute(
for variant in product_variant_list
]
}
- response = staff_api_client.post_graphql(
- query, variables, permissions=[permission_manage_products]
- )
+ with django_capture_on_commit_callbacks(execute=True):
+ response = staff_api_client.post_graphql(
+ query, variables, permissions=[permission_manage_products]
+ )
content = get_graphql_content(response)
- flush_post_commit_hooks()
assert content["data"]["productVariantBulkDelete"]["count"] == 4
assert not ProductVariant.objects.filter(
diff --git a/saleor/graphql/product/tests/test_variant_channel_listing_update.py b/saleor/graphql/product/tests/test_variant_channel_listing_update.py
index 2e34366238..95a2767b46 100644
--- a/saleor/graphql/product/tests/test_variant_channel_listing_update.py
+++ b/saleor/graphql/product/tests/test_variant_channel_listing_update.py
@@ -4,7 +4,6 @@ import graphene
from ....product.error_codes import ProductErrorCode
from ....product.models import ProductChannelListing
-from ....tests.utils import flush_post_commit_hooks
from ...tests.utils import (
assert_negative_positive_decimal_value,
assert_no_permission,
@@ -188,6 +187,7 @@ def test_variant_channel_listing_update_as_staff_user(
permission_manage_products,
channel_USD,
channel_PLN,
+ django_capture_on_commit_callbacks,
):
# given
product_pln_channel_listing = ProductChannelListing.objects.create(
@@ -214,11 +214,12 @@ def test_variant_channel_listing_update_as_staff_user(
}
# when
- response = staff_api_client.post_graphql(
- PRODUCT_VARIANT_CHANNEL_LISTING_UPDATE_MUTATION,
- variables=variables,
- permissions=(permission_manage_products,),
- )
+ with django_capture_on_commit_callbacks(execute=True):
+ response = staff_api_client.post_graphql(
+ PRODUCT_VARIANT_CHANNEL_LISTING_UPDATE_MUTATION,
+ variables=variables,
+ permissions=(permission_manage_products,),
+ )
content = get_graphql_content(response)
# then
@@ -320,6 +321,7 @@ def test_variant_channel_listing_update_trigger_webhook_product_variant_updated(
permission_manage_products,
channel_USD,
channel_PLN,
+ django_capture_on_commit_callbacks,
):
# given
ProductChannelListing.objects.create(
@@ -346,13 +348,13 @@ def test_variant_channel_listing_update_trigger_webhook_product_variant_updated(
}
# when
- response = staff_api_client.post_graphql(
- PRODUCT_VARIANT_CHANNEL_LISTING_UPDATE_MUTATION,
- variables=variables,
- permissions=(permission_manage_products,),
- )
+ with django_capture_on_commit_callbacks(execute=True):
+ response = staff_api_client.post_graphql(
+ PRODUCT_VARIANT_CHANNEL_LISTING_UPDATE_MUTATION,
+ variables=variables,
+ permissions=(permission_manage_products,),
+ )
get_graphql_content(response)
- flush_post_commit_hooks()
# then
mock_product_variant_updated.assert_called_once_with(product.variants.last())
diff --git a/saleor/order/tests/test_fulfillments_actions.py b/saleor/order/tests/test_fulfillments_actions.py
index 79c3a5c671..9a8ae3c117 100644
--- a/saleor/order/tests/test_fulfillments_actions.py
+++ b/saleor/order/tests/test_fulfillments_actions.py
@@ -5,7 +5,6 @@ import pytest
from ...core.exceptions import InsufficientStock
from ...order import OrderEvents
from ...plugins.manager import get_plugins_manager
-from ...tests.utils import flush_post_commit_hooks
from ...warehouse.models import Allocation, Stock
from ..actions import create_fulfillments
from ..models import FulfillmentLine, OrderStatus
@@ -20,6 +19,7 @@ def test_create_fulfillments(
order_with_lines,
warehouse,
site_settings,
+ django_capture_on_commit_callbacks,
):
order = order_with_lines
order_line1, order_line2 = order.lines.all()
@@ -30,16 +30,16 @@ def test_create_fulfillments(
]
}
manager = get_plugins_manager(allow_replica=False)
- [fulfillment] = create_fulfillments(
- staff_user,
- None,
- order,
- fulfillment_lines_for_warehouses,
- manager,
- site_settings,
- True,
- )
- flush_post_commit_hooks()
+ with django_capture_on_commit_callbacks(execute=True):
+ [fulfillment] = create_fulfillments(
+ staff_user,
+ None,
+ order,
+ fulfillment_lines_for_warehouses,
+ manager,
+ site_settings,
+ True,
+ )
order.refresh_from_db()
fulfillment_lines = FulfillmentLine.objects.filter(
@@ -73,7 +73,6 @@ def test_create_fulfillments(
[fulfillment_lines[0].pk, fulfillment_lines[1].pk]
)
- flush_post_commit_hooks()
mock_email_fulfillment.assert_called_once_with(
order, order.fulfillments.get(), staff_user, None, manager
)
@@ -89,6 +88,7 @@ def test_create_fulfillments_require_approval(
order_with_lines,
warehouse,
site_settings,
+ django_capture_on_commit_callbacks,
):
order = order_with_lines
order_status = order.status
@@ -102,17 +102,17 @@ def test_create_fulfillments_require_approval(
]
}
manager = get_plugins_manager(allow_replica=False)
- [fulfillment] = create_fulfillments(
- staff_user,
- None,
- order,
- fulfillment_lines_for_warehouses,
- manager,
- site_settings,
- True,
- False,
- )
- flush_post_commit_hooks()
+ with django_capture_on_commit_callbacks(execute=True):
+ [fulfillment] = create_fulfillments(
+ staff_user,
+ None,
+ order,
+ fulfillment_lines_for_warehouses,
+ manager,
+ site_settings,
+ True,
+ False,
+ )
order.refresh_from_db()
fulfillment_lines = FulfillmentLine.objects.filter(
@@ -146,7 +146,6 @@ def test_create_fulfillments_require_approval(
[fulfillment_lines[0].pk, fulfillment_lines[1].pk]
)
- flush_post_commit_hooks()
mock_email_fulfillment.assert_not_called()
mock_fulfillment_approved.assert_not_called()
@@ -158,6 +157,7 @@ def test_create_fulfillments_require_approval_as_app(
order_with_lines,
warehouse,
site_settings,
+ django_capture_on_commit_callbacks,
):
order = order_with_lines
order_status = order.status
@@ -171,17 +171,17 @@ def test_create_fulfillments_require_approval_as_app(
]
}
manager = get_plugins_manager(allow_replica=False)
- [fulfillment] = create_fulfillments(
- None,
- app,
- order,
- fulfillment_lines_for_warehouses,
- manager,
- site_settings,
- True,
- False,
- )
- flush_post_commit_hooks()
+ with django_capture_on_commit_callbacks(execute=True):
+ [fulfillment] = create_fulfillments(
+ None,
+ app,
+ order,
+ fulfillment_lines_for_warehouses,
+ manager,
+ site_settings,
+ True,
+ False,
+ )
order.refresh_from_db()
fulfillment_lines = FulfillmentLine.objects.filter(
@@ -226,6 +226,7 @@ def test_create_fulfillments_without_notification(
order_with_lines,
warehouse,
site_settings,
+ django_capture_on_commit_callbacks,
):
order = order_with_lines
order_line1, order_line2 = order.lines.all()
@@ -236,16 +237,16 @@ def test_create_fulfillments_without_notification(
]
}
- [fulfillment] = create_fulfillments(
- staff_user,
- None,
- order,
- fulfillment_lines_for_warehouses,
- get_plugins_manager(allow_replica=False),
- site_settings,
- False,
- )
- flush_post_commit_hooks()
+ with django_capture_on_commit_callbacks(execute=True):
+ [fulfillment] = create_fulfillments(
+ staff_user,
+ None,
+ order,
+ fulfillment_lines_for_warehouses,
+ get_plugins_manager(allow_replica=False),
+ site_settings,
+ False,
+ )
order.refresh_from_db()
fulfillment_lines = FulfillmentLine.objects.filter(
@@ -278,6 +279,7 @@ def test_create_fulfillments_many_warehouses(
order_with_lines,
warehouses_with_shipping_zone,
site_settings,
+ django_capture_on_commit_callbacks,
):
order = order_with_lines
warehouse1, warehouse2 = warehouses_with_shipping_zone
@@ -301,16 +303,16 @@ def test_create_fulfillments_many_warehouses(
warehouse2.pk: [{"order_line": order_line2, "quantity": 1}],
}
- [fulfillment1, fulfillment2] = create_fulfillments(
- staff_user,
- None,
- order,
- fulfillment_lines_for_warehouses,
- get_plugins_manager(allow_replica=False),
- site_settings,
- False,
- )
- flush_post_commit_hooks()
+ with django_capture_on_commit_callbacks(execute=True):
+ [fulfillment1, fulfillment2] = create_fulfillments(
+ staff_user,
+ None,
+ order,
+ fulfillment_lines_for_warehouses,
+ get_plugins_manager(allow_replica=False),
+ site_settings,
+ False,
+ )
order.refresh_from_db()
fulfillment_lines = FulfillmentLine.objects.filter(
@@ -346,6 +348,7 @@ def test_create_fulfillments_with_one_line_empty_quantity(
order_with_lines,
warehouse,
site_settings,
+ django_capture_on_commit_callbacks,
):
order = order_with_lines
order_line1, order_line2 = order.lines.all()
@@ -357,16 +360,16 @@ def test_create_fulfillments_with_one_line_empty_quantity(
}
manager = get_plugins_manager(allow_replica=False)
- [fulfillment] = create_fulfillments(
- staff_user,
- None,
- order,
- fulfillment_lines_for_warehouses,
- manager,
- site_settings,
- True,
- )
- flush_post_commit_hooks()
+ with django_capture_on_commit_callbacks(execute=True):
+ [fulfillment] = create_fulfillments(
+ staff_user,
+ None,
+ order,
+ fulfillment_lines_for_warehouses,
+ manager,
+ site_settings,
+ True,
+ )
order.refresh_from_db()
fulfillment_lines = FulfillmentLine.objects.filter(
@@ -401,6 +404,7 @@ def test_create_fulfillments_with_variant_without_inventory_tracking(
order_with_line_without_inventory_tracking,
warehouse,
site_settings,
+ django_capture_on_commit_callbacks,
):
order = order_with_line_without_inventory_tracking
order_line = order.lines.get()
@@ -411,16 +415,16 @@ def test_create_fulfillments_with_variant_without_inventory_tracking(
}
manager = get_plugins_manager(allow_replica=False)
- [fulfillment] = create_fulfillments(
- staff_user,
- None,
- order,
- fulfillment_lines_for_warehouses,
- manager,
- site_settings,
- True,
- )
- flush_post_commit_hooks()
+ with django_capture_on_commit_callbacks(execute=True):
+ [fulfillment] = create_fulfillments(
+ staff_user,
+ None,
+ order,
+ fulfillment_lines_for_warehouses,
+ manager,
+ site_settings,
+ True,
+ )
order.refresh_from_db()
fulfillment_lines = FulfillmentLine.objects.filter(
@@ -450,6 +454,7 @@ def test_create_fulfillments_without_allocations(
order_with_lines,
warehouse,
site_settings,
+ django_capture_on_commit_callbacks,
):
order = order_with_lines
order_line1, order_line2 = order.lines.all()
@@ -462,16 +467,16 @@ def test_create_fulfillments_without_allocations(
}
manager = get_plugins_manager(allow_replica=False)
- [fulfillment] = create_fulfillments(
- staff_user,
- None,
- order,
- fulfillment_lines_for_warehouses,
- manager,
- site_settings,
- True,
- )
- flush_post_commit_hooks()
+ with django_capture_on_commit_callbacks(execute=True):
+ [fulfillment] = create_fulfillments(
+ staff_user,
+ None,
+ order,
+ fulfillment_lines_for_warehouses,
+ manager,
+ site_settings,
+ True,
+ )
order.refresh_from_db()
fulfillment_lines = FulfillmentLine.objects.filter(
@@ -615,6 +620,7 @@ def test_create_fullfilment_with_out_of_stock_webhook(
order_with_lines,
warehouse,
site_settings,
+ django_capture_on_commit_callbacks,
):
order = order_with_lines
order_line1, order_line2 = order.lines.all()
@@ -625,15 +631,15 @@ def test_create_fullfilment_with_out_of_stock_webhook(
]
}
manager = get_plugins_manager(allow_replica=False)
- create_fulfillments(
- user=staff_user,
- app=None,
- order=order,
- fulfillment_lines_for_warehouses=fulfillment_lines_for_warehouses,
- manager=manager,
- site_settings=site_settings,
- )
- flush_post_commit_hooks()
+ with django_capture_on_commit_callbacks(execute=True):
+ create_fulfillments(
+ user=staff_user,
+ app=None,
+ order=order,
+ fulfillment_lines_for_warehouses=fulfillment_lines_for_warehouses,
+ manager=manager,
+ site_settings=site_settings,
+ )
product_variant_out_of_stock_webhook.assert_called_once()
@@ -645,6 +651,7 @@ def test_create_fullfilment_with_out_of_stock_webhook_not_triggered(
order_with_lines,
warehouse,
site_settings,
+ django_capture_on_commit_callbacks,
):
order = order_with_lines
order_line1, order_line2 = order.lines.all()
@@ -655,16 +662,16 @@ def test_create_fullfilment_with_out_of_stock_webhook_not_triggered(
]
}
manager = get_plugins_manager(allow_replica=False)
- create_fulfillments(
- user=staff_user,
- app=None,
- order=order,
- fulfillment_lines_for_warehouses=fulfillment_lines_for_warehouses,
- manager=manager,
- site_settings=site_settings,
- approved=False,
- )
- flush_post_commit_hooks()
+ with django_capture_on_commit_callbacks(execute=True):
+ create_fulfillments(
+ user=staff_user,
+ app=None,
+ order=order,
+ fulfillment_lines_for_warehouses=fulfillment_lines_for_warehouses,
+ manager=manager,
+ site_settings=site_settings,
+ approved=False,
+ )
product_variant_out_of_stock_webhook.assert_not_called()
@@ -676,6 +683,7 @@ def test_create_fulfillments_quantity_allocated_lower_than_line_quantity(
order_with_lines,
warehouse,
site_settings,
+ django_capture_on_commit_callbacks,
):
"""Test that stock allocation is immune to overselling."""
# given
@@ -710,16 +718,16 @@ def test_create_fulfillments_quantity_allocated_lower_than_line_quantity(
manager = get_plugins_manager(allow_replica=False)
# when
- [fulfillment] = create_fulfillments(
- staff_user,
- None,
- order,
- fulfillment_lines_for_warehouses,
- manager,
- site_settings,
- True,
- )
- flush_post_commit_hooks()
+ with django_capture_on_commit_callbacks(execute=True):
+ [fulfillment] = create_fulfillments(
+ staff_user,
+ None,
+ order,
+ fulfillment_lines_for_warehouses,
+ manager,
+ site_settings,
+ True,
+ )
# then
order.refresh_from_db()
diff --git a/saleor/order/tests/test_order_actions.py b/saleor/order/tests/test_order_actions.py
index 97a3bb097f..a92198c96c 100644
--- a/saleor/order/tests/test_order_actions.py
+++ b/saleor/order/tests/test_order_actions.py
@@ -16,7 +16,6 @@ from ...payment.models import Payment
from ...plugins.manager import get_plugins_manager
from ...product.models import DigitalContent
from ...product.tests.utils import create_image
-from ...tests.utils import flush_post_commit_hooks
from ...warehouse.models import Allocation, Stock
from ...webhook.event_types import WebhookEventAsyncType, WebhookEventSyncType
from ...webhook.utils import get_webhooks_for_multiple_events
@@ -766,6 +765,7 @@ def test_order_refunded_by_user(
order_refunded_mock,
order,
checkout_with_item,
+ django_capture_on_commit_callbacks,
):
# given
payment = Payment.objects.create(
@@ -776,10 +776,10 @@ def test_order_refunded_by_user(
# when
manager = get_plugins_manager(allow_replica=False)
- order_refunded(order, order.user, app, amount, payment, manager)
+ with django_capture_on_commit_callbacks(execute=True):
+ order_refunded(order, order.user, app, amount, payment, manager)
# then
- flush_post_commit_hooks()
order_event = order.events.last()
assert order_event.type == OrderEvents.PAYMENT_REFUNDED
@@ -800,6 +800,7 @@ def test_order_refunded_by_app(
order,
checkout_with_item,
app,
+ django_capture_on_commit_callbacks,
):
# given
payment = Payment.objects.create(
@@ -809,10 +810,10 @@ def test_order_refunded_by_app(
# when
manager = get_plugins_manager(allow_replica=False)
- order_refunded(order, None, app, amount, payment, manager)
+ with django_capture_on_commit_callbacks(execute=True):
+ order_refunded(order, None, app, amount, payment, manager)
# then
- flush_post_commit_hooks()
order_event = order.events.last()
assert order_event.type == OrderEvents.PAYMENT_REFUNDED
@@ -1670,7 +1671,11 @@ def test_fulfill_digital_lines_no_allocation(
@patch("saleor.plugins.manager.PluginsManager.order_updated")
@patch("saleor.plugins.manager.PluginsManager.order_fully_paid")
def test_order_transaction_updated_order_fully_paid(
- order_fully_paid, order_updated, order_with_lines, transaction_item_generator
+ order_fully_paid,
+ order_updated,
+ order_with_lines,
+ transaction_item_generator,
+ django_capture_on_commit_callbacks,
):
# given
order_info = fetch_order_info(order_with_lines)
@@ -1683,19 +1688,19 @@ def test_order_transaction_updated_order_fully_paid(
)
# when
- order_transaction_updated(
- order_info=order_info,
- transaction_item=transaction_item,
- manager=manager,
- user=None,
- app=None,
- previous_authorized_value=Decimal(0),
- previous_charged_value=Decimal(0),
- previous_refunded_value=Decimal(0),
- )
+ with django_capture_on_commit_callbacks(execute=True):
+ order_transaction_updated(
+ order_info=order_info,
+ transaction_item=transaction_item,
+ manager=manager,
+ user=None,
+ app=None,
+ previous_authorized_value=Decimal(0),
+ previous_charged_value=Decimal(0),
+ previous_refunded_value=Decimal(0),
+ )
# then
- flush_post_commit_hooks()
order_fully_paid.assert_called_once_with(order_with_lines, webhooks=set())
order_updated.assert_called_once_with(order_with_lines, webhooks=set())
@@ -2084,7 +2089,11 @@ def test_order_transaction_updated_for_refunded_triggers_webhooks(
@patch("saleor.plugins.manager.PluginsManager.order_updated")
@patch("saleor.plugins.manager.PluginsManager.order_fully_paid")
def test_order_transaction_updated_order_partially_paid(
- order_fully_paid, order_updated, order_with_lines, transaction_item_generator
+ order_fully_paid,
+ order_updated,
+ order_with_lines,
+ transaction_item_generator,
+ django_capture_on_commit_callbacks,
):
# given
order_info = fetch_order_info(order_with_lines)
@@ -2097,19 +2106,19 @@ def test_order_transaction_updated_order_partially_paid(
)
# when
- order_transaction_updated(
- order_info=order_info,
- transaction_item=transaction_item,
- manager=manager,
- user=None,
- app=None,
- previous_authorized_value=Decimal(0),
- previous_charged_value=Decimal(0),
- previous_refunded_value=Decimal(0),
- )
+ with django_capture_on_commit_callbacks(execute=True):
+ order_transaction_updated(
+ order_info=order_info,
+ transaction_item=transaction_item,
+ manager=manager,
+ user=None,
+ app=None,
+ previous_authorized_value=Decimal(0),
+ previous_charged_value=Decimal(0),
+ previous_refunded_value=Decimal(0),
+ )
# then
- flush_post_commit_hooks()
assert not order_fully_paid.called
order_updated.assert_called_once_with(order_with_lines, webhooks=set())
@@ -2117,7 +2126,11 @@ def test_order_transaction_updated_order_partially_paid(
@patch("saleor.plugins.manager.PluginsManager.order_updated")
@patch("saleor.plugins.manager.PluginsManager.order_fully_paid")
def test_order_transaction_updated_order_partially_paid_and_multiple_transactions(
- order_fully_paid, order_updated, order_with_lines, transaction_item_generator
+ order_fully_paid,
+ order_updated,
+ order_with_lines,
+ transaction_item_generator,
+ django_capture_on_commit_callbacks,
):
# given
order_info = fetch_order_info(order_with_lines)
@@ -2133,19 +2146,19 @@ def test_order_transaction_updated_order_partially_paid_and_multiple_transaction
)
# when
- order_transaction_updated(
- order_info=order_info,
- transaction_item=transaction_item,
- manager=manager,
- user=None,
- app=None,
- previous_authorized_value=Decimal(0),
- previous_charged_value=Decimal(0),
- previous_refunded_value=Decimal(0),
- )
+ with django_capture_on_commit_callbacks(execute=True):
+ order_transaction_updated(
+ order_info=order_info,
+ transaction_item=transaction_item,
+ manager=manager,
+ user=None,
+ app=None,
+ previous_authorized_value=Decimal(0),
+ previous_charged_value=Decimal(0),
+ previous_refunded_value=Decimal(0),
+ )
# then
- flush_post_commit_hooks()
assert not order_fully_paid.called
order_updated.assert_called_once_with(order_with_lines, webhooks=set())
@@ -2153,7 +2166,11 @@ def test_order_transaction_updated_order_partially_paid_and_multiple_transaction
@patch("saleor.plugins.manager.PluginsManager.order_updated")
@patch("saleor.plugins.manager.PluginsManager.order_fully_paid")
def test_order_transaction_updated_with_the_same_transaction_charged_amount(
- order_fully_paid, order_updated, order_with_lines, transaction_item_generator
+ order_fully_paid,
+ order_updated,
+ order_with_lines,
+ transaction_item_generator,
+ django_capture_on_commit_callbacks,
):
# given
order_info = fetch_order_info(order_with_lines)
@@ -2168,19 +2185,19 @@ def test_order_transaction_updated_with_the_same_transaction_charged_amount(
)
# when
- order_transaction_updated(
- order_info=order_info,
- transaction_item=transaction_item,
- manager=manager,
- user=None,
- app=None,
- previous_authorized_value=Decimal(0),
- previous_charged_value=charged_value,
- previous_refunded_value=Decimal(0),
- )
+ with django_capture_on_commit_callbacks(execute=True):
+ order_transaction_updated(
+ order_info=order_info,
+ transaction_item=transaction_item,
+ manager=manager,
+ user=None,
+ app=None,
+ previous_authorized_value=Decimal(0),
+ previous_charged_value=charged_value,
+ previous_refunded_value=Decimal(0),
+ )
# then
- flush_post_commit_hooks()
assert not order_fully_paid.called
assert not order_updated.called
@@ -2188,7 +2205,11 @@ def test_order_transaction_updated_with_the_same_transaction_charged_amount(
@patch("saleor.plugins.manager.PluginsManager.order_updated")
@patch("saleor.plugins.manager.PluginsManager.order_fully_paid")
def test_order_transaction_updated_order_authorized(
- order_fully_paid, order_updated, order_with_lines, transaction_item_generator
+ order_fully_paid,
+ order_updated,
+ order_with_lines,
+ transaction_item_generator,
+ django_capture_on_commit_callbacks,
):
# given
order_info = fetch_order_info(order_with_lines)
@@ -2202,19 +2223,19 @@ def test_order_transaction_updated_order_authorized(
)
# when
- order_transaction_updated(
- order_info=order_info,
- transaction_item=transaction_item,
- manager=manager,
- user=None,
- app=None,
- previous_authorized_value=Decimal(0),
- previous_charged_value=Decimal(0),
- previous_refunded_value=Decimal(0),
- )
+ with django_capture_on_commit_callbacks(execute=True):
+ order_transaction_updated(
+ order_info=order_info,
+ transaction_item=transaction_item,
+ manager=manager,
+ user=None,
+ app=None,
+ previous_authorized_value=Decimal(0),
+ previous_charged_value=Decimal(0),
+ previous_refunded_value=Decimal(0),
+ )
# then
- flush_post_commit_hooks()
assert not order_fully_paid.called
order_updated.assert_called_once_with(order_with_lines, webhooks=set())
@@ -2222,7 +2243,11 @@ def test_order_transaction_updated_order_authorized(
@patch("saleor.plugins.manager.PluginsManager.order_updated")
@patch("saleor.plugins.manager.PluginsManager.order_fully_paid")
def test_order_transaction_updated_order_partially_authorized_and_multiple_transactions(
- order_fully_paid, order_updated, order_with_lines, transaction_item_generator
+ order_fully_paid,
+ order_updated,
+ order_with_lines,
+ transaction_item_generator,
+ django_capture_on_commit_callbacks,
):
# given
order_info = fetch_order_info(order_with_lines)
@@ -2238,19 +2263,19 @@ def test_order_transaction_updated_order_partially_authorized_and_multiple_trans
)
# when
- order_transaction_updated(
- order_info=order_info,
- transaction_item=transaction_item,
- manager=manager,
- user=None,
- app=None,
- previous_authorized_value=Decimal(0),
- previous_charged_value=Decimal(0),
- previous_refunded_value=Decimal(0),
- )
+ with django_capture_on_commit_callbacks(execute=True):
+ order_transaction_updated(
+ order_info=order_info,
+ transaction_item=transaction_item,
+ manager=manager,
+ user=None,
+ app=None,
+ previous_authorized_value=Decimal(0),
+ previous_charged_value=Decimal(0),
+ previous_refunded_value=Decimal(0),
+ )
# then
- flush_post_commit_hooks()
assert not order_fully_paid.called
order_updated.assert_called_once_with(order_with_lines, webhooks=set())
@@ -2258,7 +2283,11 @@ def test_order_transaction_updated_order_partially_authorized_and_multiple_trans
@patch("saleor.plugins.manager.PluginsManager.order_updated")
@patch("saleor.plugins.manager.PluginsManager.order_fully_paid")
def test_order_transaction_updated_with_the_same_transaction_authorized_amount(
- order_fully_paid, order_updated, order_with_lines, transaction_item_generator
+ order_fully_paid,
+ order_updated,
+ order_with_lines,
+ transaction_item_generator,
+ django_capture_on_commit_callbacks,
):
# given
order_info = fetch_order_info(order_with_lines)
@@ -2273,19 +2302,19 @@ def test_order_transaction_updated_with_the_same_transaction_authorized_amount(
)
# when
- order_transaction_updated(
- order_info=order_info,
- transaction_item=transaction_item,
- manager=manager,
- user=None,
- app=None,
- previous_authorized_value=authorized_value,
- previous_charged_value=Decimal(0),
- previous_refunded_value=Decimal(0),
- )
+ with django_capture_on_commit_callbacks(execute=True):
+ order_transaction_updated(
+ order_info=order_info,
+ transaction_item=transaction_item,
+ manager=manager,
+ user=None,
+ app=None,
+ previous_authorized_value=authorized_value,
+ previous_charged_value=Decimal(0),
+ previous_refunded_value=Decimal(0),
+ )
# then
- flush_post_commit_hooks()
assert not order_fully_paid.called
assert not order_updated.called
@@ -2293,7 +2322,11 @@ def test_order_transaction_updated_with_the_same_transaction_authorized_amount(
@patch("saleor.plugins.manager.PluginsManager.order_refunded")
@patch("saleor.plugins.manager.PluginsManager.order_fully_refunded")
def test_order_transaction_updated_order_fully_refunded(
- order_fully_refunded, order_refunded, order_with_lines, transaction_item_generator
+ order_fully_refunded,
+ order_refunded,
+ order_with_lines,
+ transaction_item_generator,
+ django_capture_on_commit_callbacks,
):
# given
order_info = fetch_order_info(order_with_lines)
@@ -2306,19 +2339,19 @@ def test_order_transaction_updated_order_fully_refunded(
)
# when
- order_transaction_updated(
- order_info=order_info,
- transaction_item=transaction_item,
- manager=manager,
- user=None,
- app=None,
- previous_authorized_value=Decimal(0),
- previous_charged_value=Decimal(0),
- previous_refunded_value=Decimal(0),
- )
+ with django_capture_on_commit_callbacks(execute=True):
+ order_transaction_updated(
+ order_info=order_info,
+ transaction_item=transaction_item,
+ manager=manager,
+ user=None,
+ app=None,
+ previous_authorized_value=Decimal(0),
+ previous_charged_value=Decimal(0),
+ previous_refunded_value=Decimal(0),
+ )
# then
- flush_post_commit_hooks()
order_fully_refunded.assert_called_once_with(order_with_lines, webhooks=set())
order_refunded.assert_called_once_with(order_with_lines, webhooks=set())
@@ -2326,7 +2359,11 @@ def test_order_transaction_updated_order_fully_refunded(
@patch("saleor.plugins.manager.PluginsManager.order_refunded")
@patch("saleor.plugins.manager.PluginsManager.order_fully_refunded")
def test_order_transaction_updated_order_partially_refunded(
- order_fully_refunded, order_refunded, order_with_lines, transaction_item_generator
+ order_fully_refunded,
+ order_refunded,
+ order_with_lines,
+ transaction_item_generator,
+ django_capture_on_commit_callbacks,
):
# given
order_info = fetch_order_info(order_with_lines)
@@ -2339,19 +2376,19 @@ def test_order_transaction_updated_order_partially_refunded(
)
# when
- order_transaction_updated(
- order_info=order_info,
- transaction_item=transaction_item,
- manager=manager,
- user=None,
- app=None,
- previous_authorized_value=Decimal(0),
- previous_charged_value=Decimal(0),
- previous_refunded_value=Decimal(0),
- )
+ with django_capture_on_commit_callbacks(execute=True):
+ order_transaction_updated(
+ order_info=order_info,
+ transaction_item=transaction_item,
+ manager=manager,
+ user=None,
+ app=None,
+ previous_authorized_value=Decimal(0),
+ previous_charged_value=Decimal(0),
+ previous_refunded_value=Decimal(0),
+ )
# then
- flush_post_commit_hooks()
assert not order_fully_refunded.called
order_refunded.assert_called_once_with(order_with_lines, webhooks=set())
@@ -2359,7 +2396,11 @@ def test_order_transaction_updated_order_partially_refunded(
@patch("saleor.plugins.manager.PluginsManager.order_refunded")
@patch("saleor.plugins.manager.PluginsManager.order_fully_refunded")
def test_order_transaction_updated_order_fully_refunded_and_multiple_transactions(
- order_fully_refunded, order_refunded, order_with_lines, transaction_item_generator
+ order_fully_refunded,
+ order_refunded,
+ order_with_lines,
+ transaction_item_generator,
+ django_capture_on_commit_callbacks,
):
# given
order_info = fetch_order_info(order_with_lines)
@@ -2376,19 +2417,19 @@ def test_order_transaction_updated_order_fully_refunded_and_multiple_transaction
)
# when
- order_transaction_updated(
- order_info=order_info,
- transaction_item=transaction_item,
- manager=manager,
- user=None,
- app=None,
- previous_authorized_value=Decimal(0),
- previous_charged_value=Decimal(0),
- previous_refunded_value=Decimal(0),
- )
+ with django_capture_on_commit_callbacks(execute=True):
+ order_transaction_updated(
+ order_info=order_info,
+ transaction_item=transaction_item,
+ manager=manager,
+ user=None,
+ app=None,
+ previous_authorized_value=Decimal(0),
+ previous_charged_value=Decimal(0),
+ previous_refunded_value=Decimal(0),
+ )
# then
- flush_post_commit_hooks()
order_fully_refunded.assert_called_once_with(order_with_lines, webhooks=set())
order_refunded.assert_called_once_with(order_with_lines, webhooks=set())
@@ -2401,6 +2442,7 @@ def test_order_transaction_updated_order_fully_refunded_with_transaction_and_pay
order_with_lines,
transaction_item_generator,
payment_dummy,
+ django_capture_on_commit_callbacks,
):
# given
payment = payment_dummy
@@ -2429,19 +2471,19 @@ def test_order_transaction_updated_order_fully_refunded_with_transaction_and_pay
)
# when
- order_transaction_updated(
- order_info=order_info,
- transaction_item=transaction_item,
- manager=manager,
- user=None,
- app=None,
- previous_authorized_value=Decimal(0),
- previous_charged_value=Decimal(0),
- previous_refunded_value=Decimal(0),
- )
+ with django_capture_on_commit_callbacks(execute=True):
+ order_transaction_updated(
+ order_info=order_info,
+ transaction_item=transaction_item,
+ manager=manager,
+ user=None,
+ app=None,
+ previous_authorized_value=Decimal(0),
+ previous_charged_value=Decimal(0),
+ previous_refunded_value=Decimal(0),
+ )
# then
- flush_post_commit_hooks()
order_refunded.assert_called_once_with(order_with_lines, webhooks=set())
order_fully_refunded.assert_called_once_with(order_with_lines, webhooks=set())
diff --git a/saleor/order/tests/test_order_actions_create_fulfillments_for_returned_products.py b/saleor/order/tests/test_order_actions_create_fulfillments_for_returned_products.py
index c37441f557..7fc550b419 100644
--- a/saleor/order/tests/test_order_actions_create_fulfillments_for_returned_products.py
+++ b/saleor/order/tests/test_order_actions_create_fulfillments_for_returned_products.py
@@ -5,7 +5,6 @@ from prices import Money, TaxedMoney
from ...payment.interface import RefundData
from ...plugins.manager import get_plugins_manager
-from ...tests.utils import flush_post_commit_hooks
from ...warehouse.models import Allocation, Stock
from .. import FulfillmentLineData, FulfillmentStatus, OrderEvents, OrderOrigin
from ..actions import create_fulfillments_for_returned_products
@@ -21,6 +20,7 @@ def test_create_return_fulfillment_only_order_lines(
order_with_lines,
payment_dummy_fully_charged,
staff_user,
+ django_capture_on_commit_callbacks,
):
order_with_lines.payments.add(payment_dummy_fully_charged)
payment = order_with_lines.get_last_payment()
@@ -35,18 +35,19 @@ def test_create_return_fulfillment_only_order_lines(
)
lines_count = order_with_lines.lines.count()
- response = create_fulfillments_for_returned_products(
- user=staff_user,
- app=None,
- order=order_with_lines,
- payment=payment,
- order_lines=[
- OrderLineInfo(line=line, quantity=2, replace=False)
- for line in order_lines_to_return
- ],
- fulfillment_lines=[],
- manager=get_plugins_manager(allow_replica=False),
- )
+ with django_capture_on_commit_callbacks(execute=True):
+ response = create_fulfillments_for_returned_products(
+ user=staff_user,
+ app=None,
+ order=order_with_lines,
+ payment=payment,
+ order_lines=[
+ OrderLineInfo(line=line, quantity=2, replace=False)
+ for line in order_lines_to_return
+ ],
+ fulfillment_lines=[],
+ manager=get_plugins_manager(allow_replica=False),
+ )
returned_fulfillment, replaced_fulfillment, replace_order = response
returned_fulfillment_lines = returned_fulfillment.lines.all()
@@ -71,7 +72,6 @@ def test_create_return_fulfillment_only_order_lines(
assert not replace_order
# check if we have correct events
- flush_post_commit_hooks()
events = order_with_lines.events.all()
assert events.count() == 1
returned_event = events[0]
@@ -95,6 +95,7 @@ def test_create_return_fulfillment_only_order_lines_with_refund(
order_with_lines,
payment_dummy_fully_charged,
staff_user,
+ django_capture_on_commit_callbacks,
):
order_with_lines.payments.add(payment_dummy_fully_charged)
payment = order_with_lines.get_last_payment()
@@ -113,19 +114,19 @@ def test_create_return_fulfillment_only_order_lines_with_refund(
OrderLineInfo(line=line, quantity=2, replace=False)
for line in order_lines_to_return
]
- response = create_fulfillments_for_returned_products(
- user=staff_user,
- app=None,
- order=order_with_lines,
- payment=payment,
- order_lines=order_lines_to_refund,
- fulfillment_lines=[],
- manager=get_plugins_manager(allow_replica=False),
- refund=True,
- )
+ with django_capture_on_commit_callbacks(execute=True):
+ response = create_fulfillments_for_returned_products(
+ user=staff_user,
+ app=None,
+ order=order_with_lines,
+ payment=payment,
+ order_lines=order_lines_to_refund,
+ fulfillment_lines=[],
+ manager=get_plugins_manager(allow_replica=False),
+ refund=True,
+ )
returned_fulfillment, replaced_fulfillment, replace_order = response
- flush_post_commit_hooks()
returned_fulfillment_lines = returned_fulfillment.lines.all()
assert returned_fulfillment.status == FulfillmentStatus.REFUNDED_AND_RETURNED
assert len(returned_fulfillment_lines) == lines_count
@@ -171,6 +172,7 @@ def test_create_return_fulfillment_only_order_lines_included_shipping_costs(
order_with_lines,
payment_dummy_fully_charged,
staff_user,
+ django_capture_on_commit_callbacks,
):
order_with_lines.payments.add(payment_dummy_fully_charged)
payment = order_with_lines.get_last_payment()
@@ -189,20 +191,20 @@ def test_create_return_fulfillment_only_order_lines_included_shipping_costs(
OrderLineInfo(line=line, quantity=2, replace=False)
for line in order_lines_to_return
]
- response = create_fulfillments_for_returned_products(
- user=staff_user,
- app=None,
- order=order_with_lines,
- payment=payment,
- order_lines=order_lines_to_refund,
- fulfillment_lines=[],
- manager=get_plugins_manager(allow_replica=False),
- refund=True,
- refund_shipping_costs=True,
- )
+ with django_capture_on_commit_callbacks(execute=True):
+ response = create_fulfillments_for_returned_products(
+ user=staff_user,
+ app=None,
+ order=order_with_lines,
+ payment=payment,
+ order_lines=order_lines_to_refund,
+ fulfillment_lines=[],
+ manager=get_plugins_manager(allow_replica=False),
+ refund=True,
+ refund_shipping_costs=True,
+ )
returned_fulfillment, replaced_fulfillment, replace_order = response
- flush_post_commit_hooks()
returned_fulfillment_lines = returned_fulfillment.lines.all()
assert returned_fulfillment.status == FulfillmentStatus.REFUNDED_AND_RETURNED
assert len(returned_fulfillment_lines) == lines_count
@@ -253,6 +255,7 @@ def test_create_return_fulfillment_only_order_lines_with_replace_request(
order_with_lines,
payment_dummy_fully_charged,
staff_user,
+ django_capture_on_commit_callbacks,
):
order_with_lines.payments.add(payment_dummy_fully_charged)
payment = order_with_lines.get_last_payment()
@@ -281,18 +284,18 @@ def test_create_return_fulfillment_only_order_lines_with_replace_request(
order_with_lines.private_metadata = {"priv_test_key": "priv_test_val"}
order_with_lines.save(update_fields=["metadata", "private_metadata"])
- response = create_fulfillments_for_returned_products(
- user=staff_user,
- app=None,
- order=order_with_lines,
- payment=payment,
- order_lines=order_lines_data,
- fulfillment_lines=[],
- manager=get_plugins_manager(allow_replica=False),
- )
+ with django_capture_on_commit_callbacks(execute=True):
+ response = create_fulfillments_for_returned_products(
+ user=staff_user,
+ app=None,
+ order=order_with_lines,
+ payment=payment,
+ order_lines=order_lines_data,
+ fulfillment_lines=[],
+ manager=get_plugins_manager(allow_replica=False),
+ )
returned_fulfillment, replaced_fulfillment, replace_order = response
- flush_post_commit_hooks()
returned_fulfillment_lines = returned_fulfillment.lines.all()
assert returned_fulfillment.status == FulfillmentStatus.RETURNED
# we replaced one line
@@ -384,6 +387,7 @@ def test_create_return_fulfillment_only_fulfillment_lines(
fulfilled_order,
payment_dummy_fully_charged,
staff_user,
+ django_capture_on_commit_callbacks,
):
fulfilled_order.payments.add(payment_dummy_fully_charged)
payment = fulfilled_order.get_last_payment()
@@ -391,22 +395,22 @@ def test_create_return_fulfillment_only_fulfillment_lines(
fulfillment_lines = FulfillmentLine.objects.filter(order_line_id__in=order_line_ids)
original_quantity = {line.id: line.quantity for line in fulfillment_lines}
- response = create_fulfillments_for_returned_products(
- user=staff_user,
- app=None,
- order=fulfilled_order,
- payment=payment,
- order_lines=[],
- fulfillment_lines=[
- FulfillmentLineData(line=line, quantity=2, replace=False)
- for line in fulfillment_lines
- ],
- manager=get_plugins_manager(allow_replica=False),
- )
+ with django_capture_on_commit_callbacks(execute=True):
+ response = create_fulfillments_for_returned_products(
+ user=staff_user,
+ app=None,
+ order=fulfilled_order,
+ payment=payment,
+ order_lines=[],
+ fulfillment_lines=[
+ FulfillmentLineData(line=line, quantity=2, replace=False)
+ for line in fulfillment_lines
+ ],
+ manager=get_plugins_manager(allow_replica=False),
+ )
returned_fulfillment, replaced_fulfillment, replace_order = response
- flush_post_commit_hooks()
returned_fulfillment_lines = returned_fulfillment.lines.all()
assert returned_fulfillment.status == FulfillmentStatus.RETURNED
assert returned_fulfillment_lines.count() == len(order_line_ids)
@@ -432,6 +436,7 @@ def test_create_return_fulfillment_only_fulfillment_lines_replace_order(
fulfilled_order,
payment_dummy_fully_charged,
staff_user,
+ django_capture_on_commit_callbacks,
):
fulfilled_order.payments.add(payment_dummy_fully_charged)
payment = fulfilled_order.get_last_payment()
@@ -449,19 +454,19 @@ def test_create_return_fulfillment_only_fulfillment_lines_replace_order(
fulfillment_lines_to_return[0].replace = True
fulfillment_lines_to_return[0].quantity = replace_quantity
- response = create_fulfillments_for_returned_products(
- user=staff_user,
- app=None,
- order=fulfilled_order,
- payment=payment,
- order_lines=[],
- fulfillment_lines=fulfillment_lines_to_return,
- manager=get_plugins_manager(allow_replica=False),
- )
+ with django_capture_on_commit_callbacks(execute=True):
+ response = create_fulfillments_for_returned_products(
+ user=staff_user,
+ app=None,
+ order=fulfilled_order,
+ payment=payment,
+ order_lines=[],
+ fulfillment_lines=fulfillment_lines_to_return,
+ manager=get_plugins_manager(allow_replica=False),
+ )
returned_fulfillment, replaced_fulfillment, replace_order = response
- flush_post_commit_hooks()
returned_fulfillment_lines = returned_fulfillment.lines.all()
assert returned_fulfillment.status == FulfillmentStatus.RETURNED
# make sure that all order lines from refund are in expected fulfillment
@@ -542,6 +547,7 @@ def test_create_return_fulfillment_with_lines_already_refunded(
channel_USD,
variant,
warehouse,
+ django_capture_on_commit_callbacks,
):
fulfilled_order.payments.add(payment_dummy_fully_charged)
payment = fulfilled_order.get_last_payment()
@@ -591,18 +597,19 @@ def test_create_return_fulfillment_with_lines_already_refunded(
fulfillment_lines_to_process.append(
FulfillmentLineData(line=refunded_fulfillment_line, quantity=2)
)
- create_fulfillments_for_returned_products(
- user=staff_user,
- app=None,
- order=fulfilled_order,
- payment=payment,
- order_lines=[],
- fulfillment_lines=fulfillment_lines_to_process,
- manager=get_plugins_manager(allow_replica=False),
- refund=True,
- )
- flush_post_commit_hooks()
+ with django_capture_on_commit_callbacks(execute=True):
+ create_fulfillments_for_returned_products(
+ user=staff_user,
+ app=None,
+ order=fulfilled_order,
+ payment=payment,
+ order_lines=[],
+ fulfillment_lines=fulfillment_lines_to_process,
+ manager=get_plugins_manager(allow_replica=False),
+ refund=True,
+ )
+
returned_and_refunded_fulfillment = Fulfillment.objects.get(
order=fulfilled_order, status=FulfillmentStatus.REFUNDED_AND_RETURNED
)
@@ -647,6 +654,7 @@ def test_create_return_fulfillment_only_order_lines_with_old_ids(
order_with_lines,
payment_dummy_fully_charged,
staff_user,
+ django_capture_on_commit_callbacks,
):
order_with_lines.payments.add(payment_dummy_fully_charged)
payment = order_with_lines.get_last_payment()
@@ -663,18 +671,19 @@ def test_create_return_fulfillment_only_order_lines_with_old_ids(
)
lines_count = order_with_lines.lines.count()
- response = create_fulfillments_for_returned_products(
- user=staff_user,
- app=None,
- order=order_with_lines,
- payment=payment,
- order_lines=[
- OrderLineInfo(line=line, quantity=2, replace=False)
- for line in order_lines_to_return
- ],
- fulfillment_lines=[],
- manager=get_plugins_manager(allow_replica=False),
- )
+ with django_capture_on_commit_callbacks(execute=True):
+ response = create_fulfillments_for_returned_products(
+ user=staff_user,
+ app=None,
+ order=order_with_lines,
+ payment=payment,
+ order_lines=[
+ OrderLineInfo(line=line, quantity=2, replace=False)
+ for line in order_lines_to_return
+ ],
+ fulfillment_lines=[],
+ manager=get_plugins_manager(allow_replica=False),
+ )
returned_fulfillment, replaced_fulfillment, replace_order = response
returned_fulfillment_lines = returned_fulfillment.lines.all()
@@ -699,7 +708,6 @@ def test_create_return_fulfillment_only_order_lines_with_old_ids(
assert not replace_order
# check if we have correct events
- flush_post_commit_hooks()
events = order_with_lines.events.all()
assert events.count() == 1
returned_event = events[0]
diff --git a/saleor/order/tests/test_order_actions_refund_products.py b/saleor/order/tests/test_order_actions_refund_products.py
index 0059e1f0c8..9314a49f3b 100644
--- a/saleor/order/tests/test_order_actions_refund_products.py
+++ b/saleor/order/tests/test_order_actions_refund_products.py
@@ -4,7 +4,6 @@ from unittest.mock import ANY, patch
from ...payment import ChargeStatus
from ...payment.interface import RefundData
from ...plugins.manager import get_plugins_manager
-from ...tests.utils import flush_post_commit_hooks
from ...warehouse.models import Allocation
from .. import FulfillmentLineData, FulfillmentStatus
from ..actions import create_refund_fulfillment
@@ -15,7 +14,11 @@ from ..models import FulfillmentLine
@patch("saleor.plugins.manager.PluginsManager.order_updated")
@patch("saleor.order.actions.gateway.refund")
def test_create_refund_fulfillment_only_order_lines(
- mocked_refund, mocked_order_updated, order_with_lines, payment_dummy
+ mocked_refund,
+ mocked_order_updated,
+ order_with_lines,
+ payment_dummy,
+ django_capture_on_commit_callbacks,
):
payment_dummy.captured_amount = payment_dummy.total
payment_dummy.charge_status = ChargeStatus.FULLY_CHARGED
@@ -36,17 +39,17 @@ def test_create_refund_fulfillment_only_order_lines(
order_refund_lines = [
OrderLineInfo(line=line, quantity=2) for line in order_lines_to_refund
]
- returned_fulfillemnt = create_refund_fulfillment(
- user=None,
- app=None,
- order=order_with_lines,
- payment=payment,
- order_lines_to_refund=order_refund_lines,
- fulfillment_lines_to_refund=[],
- manager=get_plugins_manager(allow_replica=False),
- )
+ with django_capture_on_commit_callbacks(execute=True):
+ returned_fulfillemnt = create_refund_fulfillment(
+ user=None,
+ app=None,
+ order=order_with_lines,
+ payment=payment,
+ order_lines_to_refund=order_refund_lines,
+ fulfillment_lines_to_refund=[],
+ manager=get_plugins_manager(allow_replica=False),
+ )
- flush_post_commit_hooks()
returned_fulfillment_lines = returned_fulfillemnt.lines.all()
assert returned_fulfillemnt.status == FulfillmentStatus.REFUNDED
assert len(returned_fulfillment_lines) == lines_count
@@ -85,7 +88,11 @@ def test_create_refund_fulfillment_only_order_lines(
@patch("saleor.plugins.manager.PluginsManager.order_updated")
@patch("saleor.order.actions.gateway.refund")
def test_create_refund_fulfillment_included_shipping_costs(
- mocked_refund, mocked_order_updated, order_with_lines, payment_dummy
+ mocked_refund,
+ mocked_order_updated,
+ order_with_lines,
+ payment_dummy,
+ django_capture_on_commit_callbacks,
):
payment_dummy.captured_amount = payment_dummy.total
payment_dummy.charge_status = ChargeStatus.FULLY_CHARGED
@@ -102,18 +109,18 @@ def test_create_refund_fulfillment_included_shipping_costs(
order_refund_lines = [
OrderLineInfo(line=line, quantity=2) for line in order_lines_to_refund
]
- returned_fulfillemnt = create_refund_fulfillment(
- user=None,
- app=None,
- order=order_with_lines,
- payment=payment,
- order_lines_to_refund=order_refund_lines,
- fulfillment_lines_to_refund=[],
- manager=get_plugins_manager(allow_replica=False),
- refund_shipping_costs=True,
- )
+ with django_capture_on_commit_callbacks(execute=True):
+ returned_fulfillemnt = create_refund_fulfillment(
+ user=None,
+ app=None,
+ order=order_with_lines,
+ payment=payment,
+ order_lines_to_refund=order_refund_lines,
+ fulfillment_lines_to_refund=[],
+ manager=get_plugins_manager(allow_replica=False),
+ refund_shipping_costs=True,
+ )
- flush_post_commit_hooks()
returned_fulfillment_lines = returned_fulfillemnt.lines.all()
assert returned_fulfillemnt.status == FulfillmentStatus.REFUNDED
assert len(returned_fulfillment_lines) == lines_count
@@ -147,7 +154,11 @@ def test_create_refund_fulfillment_included_shipping_costs(
@patch("saleor.plugins.manager.PluginsManager.order_updated")
@patch("saleor.order.actions.gateway.refund")
def test_create_refund_fulfillment_only_fulfillment_lines(
- mocked_refund, mocked_order_updated, fulfilled_order, payment_dummy
+ mocked_refund,
+ mocked_order_updated,
+ fulfilled_order,
+ payment_dummy,
+ django_capture_on_commit_callbacks,
):
payment_dummy.captured_amount = payment_dummy.total
payment_dummy.charge_status = ChargeStatus.FULLY_CHARGED
@@ -163,17 +174,17 @@ def test_create_refund_fulfillment_only_fulfillment_lines(
FulfillmentLineData(line=line, quantity=2)
for line in fulfillment_lines_to_refund
]
- returned_fulfillemnt = create_refund_fulfillment(
- user=None,
- app=None,
- order=fulfilled_order,
- payment=payment,
- order_lines_to_refund=[],
- fulfillment_lines_to_refund=fulfillment_refund_lines,
- manager=get_plugins_manager(allow_replica=False),
- )
+ with django_capture_on_commit_callbacks(execute=True):
+ returned_fulfillemnt = create_refund_fulfillment(
+ user=None,
+ app=None,
+ order=fulfilled_order,
+ payment=payment,
+ order_lines_to_refund=[],
+ fulfillment_lines_to_refund=fulfillment_refund_lines,
+ manager=get_plugins_manager(allow_replica=False),
+ )
- flush_post_commit_hooks()
returned_fulfillment_lines = returned_fulfillemnt.lines.all()
assert returned_fulfillemnt.status == FulfillmentStatus.REFUNDED
assert len(returned_fulfillment_lines) == len(order_line_ids)
@@ -204,7 +215,11 @@ def test_create_refund_fulfillment_only_fulfillment_lines(
@patch("saleor.plugins.manager.PluginsManager.order_updated")
@patch("saleor.order.actions.gateway.refund")
def test_create_refund_fulfillment_custom_amount(
- mocked_refund, mocked_order_updated, fulfilled_order, payment_dummy
+ mocked_refund,
+ mocked_order_updated,
+ fulfilled_order,
+ payment_dummy,
+ django_capture_on_commit_callbacks,
):
payment_dummy.captured_amount = payment_dummy.total
payment_dummy.charge_status = ChargeStatus.FULLY_CHARGED
@@ -221,18 +236,18 @@ def test_create_refund_fulfillment_custom_amount(
FulfillmentLineData(line=line, quantity=2)
for line in fulfillment_lines_to_refund
]
- returned_fulfillemnt = create_refund_fulfillment(
- user=None,
- app=None,
- order=fulfilled_order,
- payment=payment,
- order_lines_to_refund=[],
- fulfillment_lines_to_refund=fulfillment_refund_lines,
- manager=get_plugins_manager(allow_replica=False),
- amount=amount,
- )
+ with django_capture_on_commit_callbacks(execute=True):
+ returned_fulfillemnt = create_refund_fulfillment(
+ user=None,
+ app=None,
+ order=fulfilled_order,
+ payment=payment,
+ order_lines_to_refund=[],
+ fulfillment_lines_to_refund=fulfillment_refund_lines,
+ manager=get_plugins_manager(allow_replica=False),
+ amount=amount,
+ )
- flush_post_commit_hooks()
returned_fulfillment_lines = returned_fulfillemnt.lines.all()
assert returned_fulfillemnt.status == FulfillmentStatus.REFUNDED
assert len(returned_fulfillment_lines) == len(order_line_ids)
diff --git a/saleor/payment/gateways/stripe/tests/test_webhooks.py b/saleor/payment/gateways/stripe/tests/test_webhooks.py
index 820b3c3ef0..d13249b4f3 100644
--- a/saleor/payment/gateways/stripe/tests/test_webhooks.py
+++ b/saleor/payment/gateways/stripe/tests/test_webhooks.py
@@ -15,7 +15,6 @@ from .....checkout.fetch import fetch_checkout_info, fetch_checkout_lines
from .....order.actions import order_charged, order_refunded, order_voided
from .....payment.models import Transaction
from .....plugins.manager import get_plugins_manager
-from .....tests.utils import flush_post_commit_hooks
from .... import ChargeStatus, TransactionKind
from ....utils import price_to_minor_unit
from ..consts import (
@@ -1662,6 +1661,7 @@ def test_handle_successful_payment_intent_for_checkout_when_already_processing_c
checkout_with_items,
stripe_plugin,
channel_USD,
+ django_capture_on_commit_callbacks,
):
# given
plugin = stripe_plugin()
@@ -1694,7 +1694,6 @@ def test_handle_successful_payment_intent_for_checkout_when_already_processing_c
# when
def call_webhook_notification(*args, **kwargs):
- flush_post_commit_hooks()
handle_successful_payment_intent(
payment_intent, plugin.config, channel_USD.slug
)
@@ -1703,15 +1702,16 @@ def test_handle_successful_payment_intent_for_checkout_when_already_processing_c
"saleor.checkout.complete_checkout._process_payment",
call_webhook_notification,
):
- complete_checkout(
- manager,
- checkout_info,
- lines_info,
- {},
- False,
- None,
- None,
- )
+ with django_capture_on_commit_callbacks(execute=True):
+ complete_checkout(
+ manager,
+ checkout_info,
+ lines_info,
+ {},
+ False,
+ None,
+ None,
+ )
# then
payment.refresh_from_db()
diff --git a/saleor/payment/tests/test_utils.py b/saleor/payment/tests/test_utils.py
index abcf13757a..fd205debc9 100644
--- a/saleor/payment/tests/test_utils.py
+++ b/saleor/payment/tests/test_utils.py
@@ -11,7 +11,6 @@ from ...checkout import CheckoutAuthorizeStatus
from ...checkout.fetch import fetch_checkout_info, fetch_checkout_lines
from ...order import OrderAuthorizeStatus, OrderChargeStatus, OrderGrantedRefundStatus
from ...plugins.manager import get_plugins_manager
-from ...tests.utils import flush_post_commit_hooks
from .. import TransactionEventType
from ..interface import (
PaymentLineData,
@@ -678,6 +677,7 @@ def test_create_transaction_event_from_request_triggers_webhooks_when_fully_paid
transaction_item_generator,
app,
order_with_lines,
+ django_capture_on_commit_callbacks,
):
# given
order = order_with_lines
@@ -701,12 +701,12 @@ def test_create_transaction_event_from_request_triggers_webhooks_when_fully_paid
}
# when
- create_transaction_event_from_request_and_webhook_response(
- request_event, app, response_data
- )
+ with django_capture_on_commit_callbacks(execute=True):
+ create_transaction_event_from_request_and_webhook_response(
+ request_event, app, response_data
+ )
# then
- flush_post_commit_hooks()
order.refresh_from_db()
assert order.charge_status == OrderChargeStatus.FULL
mock_order_fully_paid.assert_called_once_with(order, webhooks=set())
@@ -725,6 +725,7 @@ def test_create_transaction_event_from_request_triggers_webhooks_when_partially_
transaction_item_generator,
app,
order_with_lines,
+ django_capture_on_commit_callbacks,
):
# given
order = order_with_lines
@@ -748,12 +749,12 @@ def test_create_transaction_event_from_request_triggers_webhooks_when_partially_
}
# when
- create_transaction_event_from_request_and_webhook_response(
- request_event, app, response_data
- )
+ with django_capture_on_commit_callbacks(execute=True):
+ create_transaction_event_from_request_and_webhook_response(
+ request_event, app, response_data
+ )
# then
- flush_post_commit_hooks()
order.refresh_from_db()
assert order_with_lines.charge_status == OrderChargeStatus.PARTIAL
assert not mock_order_fully_paid.called
@@ -772,6 +773,7 @@ def test_create_transaction_event_from_request_triggers_webhooks_when_fully_refu
transaction_item_generator,
app,
order_with_lines,
+ django_capture_on_commit_callbacks,
):
# given
order = order_with_lines
@@ -795,12 +797,12 @@ def test_create_transaction_event_from_request_triggers_webhooks_when_fully_refu
}
# when
- create_transaction_event_from_request_and_webhook_response(
- request_event, app, response_data
- )
+ with django_capture_on_commit_callbacks(execute=True):
+ create_transaction_event_from_request_and_webhook_response(
+ request_event, app, response_data
+ )
# then
- flush_post_commit_hooks()
order.refresh_from_db()
mock_order_fully_refunded.assert_called_once_with(order, webhooks=set())
@@ -819,6 +821,7 @@ def test_create_transaction_event_from_request_triggers_webhooks_partially_refun
transaction_item_generator,
app,
order_with_lines,
+ django_capture_on_commit_callbacks,
):
# given
order = order_with_lines
@@ -842,12 +845,12 @@ def test_create_transaction_event_from_request_triggers_webhooks_partially_refun
}
# when
- create_transaction_event_from_request_and_webhook_response(
- request_event, app, response_data
- )
+ with django_capture_on_commit_callbacks(execute=True):
+ create_transaction_event_from_request_and_webhook_response(
+ request_event, app, response_data
+ )
# then
- flush_post_commit_hooks()
order.refresh_from_db()
assert not mock_order_fully_refunded.called
@@ -864,6 +867,7 @@ def test_create_transaction_event_from_request_triggers_webhooks_when_authorized
transaction_item_generator,
app,
order_with_lines,
+ django_capture_on_commit_callbacks,
):
# given
order = order_with_lines
@@ -887,12 +891,12 @@ def test_create_transaction_event_from_request_triggers_webhooks_when_authorized
}
# when
- create_transaction_event_from_request_and_webhook_response(
- request_event, app, response_data
- )
+ with django_capture_on_commit_callbacks(execute=True):
+ create_transaction_event_from_request_and_webhook_response(
+ request_event, app, response_data
+ )
# then
- flush_post_commit_hooks()
order.refresh_from_db()
assert order_with_lines.authorize_status == OrderAuthorizeStatus.FULL
assert not mock_order_fully_paid.called
@@ -996,6 +1000,7 @@ def test_create_transaction_event_from_request_when_paid(
transaction_item_generator,
app,
checkout_with_prices,
+ django_capture_on_commit_callbacks,
):
# given
checkout = checkout_with_prices
@@ -1020,12 +1025,12 @@ def test_create_transaction_event_from_request_when_paid(
}
# when
- create_transaction_event_from_request_and_webhook_response(
- request_event, app, response_data
- )
+ with django_capture_on_commit_callbacks(execute=True):
+ create_transaction_event_from_request_and_webhook_response(
+ request_event, app, response_data
+ )
# then
- flush_post_commit_hooks()
checkout.refresh_from_db()
assert checkout.authorize_status == CheckoutAuthorizeStatus.FULL
mocked_checkout_fully_paid.assert_called_once_with(checkout, webhooks=set())
@@ -1040,6 +1045,7 @@ def test_create_transaction_event_from_request_when_authorized(
transaction_item_generator,
app,
checkout_with_prices,
+ django_capture_on_commit_callbacks,
):
# given
checkout = checkout_with_prices
@@ -1067,12 +1073,12 @@ def test_create_transaction_event_from_request_when_authorized(
}
# when
- create_transaction_event_from_request_and_webhook_response(
- request_event, app, response_data
- )
+ with django_capture_on_commit_callbacks(execute=True):
+ create_transaction_event_from_request_and_webhook_response(
+ request_event, app, response_data
+ )
# then
- flush_post_commit_hooks()
checkout.refresh_from_db()
assert checkout.authorize_status == CheckoutAuthorizeStatus.FULL
mocked_checkout_fully_paid.assert_not_called()
@@ -1088,6 +1094,7 @@ def test_create_transaction_event_from_request_when_paid_triggers_checkout_compl
app,
checkout_with_prices,
plugins_manager,
+ django_capture_on_commit_callbacks,
):
# given
checkout = checkout_with_prices
@@ -1118,12 +1125,12 @@ def test_create_transaction_event_from_request_when_paid_triggers_checkout_compl
}
# when
- create_transaction_event_from_request_and_webhook_response(
- request_event, app, response_data
- )
+ with django_capture_on_commit_callbacks(execute=True):
+ create_transaction_event_from_request_and_webhook_response(
+ request_event, app, response_data
+ )
# then
- flush_post_commit_hooks()
checkout.refresh_from_db()
assert checkout.authorize_status == CheckoutAuthorizeStatus.FULL
mocked_checkout_fully_paid.assert_called_once_with(checkout, webhooks=set())
@@ -1141,6 +1148,7 @@ def test_create_transaction_event_from_request_when_authorized_triggers_checkout
app,
checkout_with_prices,
plugins_manager,
+ django_capture_on_commit_callbacks,
):
# given
checkout = checkout_with_prices
@@ -1171,12 +1179,12 @@ def test_create_transaction_event_from_request_when_authorized_triggers_checkout
}
# when
- create_transaction_event_from_request_and_webhook_response(
- request_event, app, response_data
- )
+ with django_capture_on_commit_callbacks(execute=True):
+ create_transaction_event_from_request_and_webhook_response(
+ request_event, app, response_data
+ )
# then
- flush_post_commit_hooks()
checkout.refresh_from_db()
assert checkout.authorize_status == CheckoutAuthorizeStatus.FULL
mocked_checkout_fully_paid.assert_not_called()
@@ -2135,6 +2143,7 @@ def test_create_transaction_event_for_transaction_session_call_webhook_order_upd
webhook_app,
plugins_manager,
order_with_lines,
+ django_capture_on_commit_callbacks,
):
# given
expected_amount = Decimal("15")
@@ -2146,16 +2155,16 @@ def test_create_transaction_event_for_transaction_session_call_webhook_order_upd
transaction=transaction, include_in_calculations=False
)
# when
- create_transaction_event_for_transaction_session(
- request_event,
- webhook_app,
- manager=plugins_manager,
- transaction_webhook_response=response,
- )
+ with django_capture_on_commit_callbacks(execute=True):
+ create_transaction_event_for_transaction_session(
+ request_event,
+ webhook_app,
+ manager=plugins_manager,
+ transaction_webhook_response=response,
+ )
# then
order_with_lines.refresh_from_db()
- flush_post_commit_hooks()
assert not mock_order_fully_paid.called
mock_order_updated.assert_called_once_with(order_with_lines, webhooks=set())
@@ -2170,6 +2179,7 @@ def test_create_transaction_event_for_transaction_session_call_webhook_for_fully
webhook_app,
plugins_manager,
order_with_lines,
+ django_capture_on_commit_callbacks,
):
# given
response = transaction_session_response.copy()
@@ -2181,16 +2191,16 @@ def test_create_transaction_event_for_transaction_session_call_webhook_for_fully
)
# when
- create_transaction_event_for_transaction_session(
- request_event,
- webhook_app,
- manager=plugins_manager,
- transaction_webhook_response=response,
- )
+ with django_capture_on_commit_callbacks(execute=True):
+ create_transaction_event_for_transaction_session(
+ request_event,
+ webhook_app,
+ manager=plugins_manager,
+ transaction_webhook_response=response,
+ )
# then
order_with_lines.refresh_from_db()
- flush_post_commit_hooks()
mock_order_fully_paid.assert_called_once_with(order_with_lines, webhooks=set())
mock_order_updated.assert_called_once_with(order_with_lines, webhooks=set())
diff --git a/saleor/plugins/openid_connect/tests/test_utils.py b/saleor/plugins/openid_connect/tests/test_utils.py
index 71e5f9a9e8..0945ee9836 100644
--- a/saleor/plugins/openid_connect/tests/test_utils.py
+++ b/saleor/plugins/openid_connect/tests/test_utils.py
@@ -24,7 +24,6 @@ from ....core.jwt import (
jwt_user_payload,
)
from ....permission.models import Permission
-from ....tests.utils import flush_post_commit_hooks
from ..exceptions import AuthenticationError
from ..utils import (
JWKS_CACHE_TIME,
@@ -618,7 +617,10 @@ def test_get_or_create_user_from_payload_last_login_modifies(
@patch("saleor.plugins.manager.PluginsManager.customer_created")
def test_jwt_token_without_expiration_claim(
- mocked_customer_created_webhook, monkeypatch, decoded_access_token
+ mocked_customer_created_webhook,
+ monkeypatch,
+ decoded_access_token,
+ django_capture_on_commit_callbacks,
):
# given
monkeypatch.setattr(
@@ -636,19 +638,19 @@ def test_jwt_token_without_expiration_claim(
)
# when
- user = get_user_from_oauth_access_token_in_jwt_format(
- token_payload,
- "https://example.com",
- access_token="fake-token",
- use_scope_permissions=False,
- audience="",
- staff_user_domains=[],
- staff_default_group_name="",
- )
+ with django_capture_on_commit_callbacks(execute=True):
+ user = get_user_from_oauth_access_token_in_jwt_format(
+ token_payload,
+ "https://example.com",
+ access_token="fake-token",
+ use_scope_permissions=False,
+ audience="",
+ staff_user_domains=[],
+ staff_default_group_name="",
+ )
# then
assert user.email == "[email protected]"
- flush_post_commit_hooks()
mocked_customer_created_webhook.assert_called_once_with(user)
@@ -663,6 +665,7 @@ def test_jwt_token_without_expiration_claim_mixed_permissions_from_group(
monkeypatch,
decoded_access_token,
permission_group_manage_shipping,
+ django_capture_on_commit_callbacks,
):
# given
customer_user.groups.add(permission_group_manage_shipping)
@@ -683,15 +686,16 @@ def test_jwt_token_without_expiration_claim_mixed_permissions_from_group(
mocked_cache_get.side_effect = lambda cache_key: None
# when
- user = get_user_from_oauth_access_token_in_jwt_format(
- token_payload,
- "https://example.com",
- access_token="fake-token",
- use_scope_permissions=True,
- audience="",
- staff_user_domains=[customer_user.email.split("@")[1]],
- staff_default_group_name="",
- )
+ with django_capture_on_commit_callbacks(execute=True):
+ user = get_user_from_oauth_access_token_in_jwt_format(
+ token_payload,
+ "https://example.com",
+ access_token="fake-token",
+ use_scope_permissions=True,
+ audience="",
+ staff_user_domains=[customer_user.email.split("@")[1]],
+ staff_default_group_name="",
+ )
# then
manage_shipping = permission_group_manage_shipping.permissions.first()
@@ -700,7 +704,6 @@ def test_jwt_token_without_expiration_claim_mixed_permissions_from_group(
assert len(user.effective_permissions) > 1
# ensure that manage_shipping is not from openID scope permissions
assert "saleor:manage_shipping" not in token_payload["scope"]
- flush_post_commit_hooks()
mocked_customer_created_webhook.assert_not_called()
diff --git a/saleor/product/tests/test_category.py b/saleor/product/tests/test_category.py
index a7954c6600..4681de752f 100644
--- a/saleor/product/tests/test_category.py
+++ b/saleor/product/tests/test_category.py
@@ -2,7 +2,6 @@ from unittest.mock import patch
from ...discount.utils.promotion import get_active_catalogue_promotion_rules
from ...plugins.manager import get_plugins_manager
-from ...tests.utils import flush_post_commit_hooks
from ..models import Category
from ..utils import collect_categories_tree_products, delete_categories
@@ -21,7 +20,7 @@ def test_collect_categories_tree_products(categories_tree):
def test_delete_categories(
- categories_tree_with_published_products,
+ categories_tree_with_published_products, django_capture_on_commit_callbacks
):
# given
parent = categories_tree_with_published_products
@@ -29,15 +28,14 @@ def test_delete_categories(
product_list = [child.products.first(), parent.products.first()]
# when
- delete_categories([parent.pk], manager=get_plugins_manager(allow_replica=False))
+ with django_capture_on_commit_callbacks(execute=True):
+ delete_categories([parent.pk], manager=get_plugins_manager(allow_replica=False))
assert not Category.objects.filter(
id__in=[category.id for category in [parent, child]]
).exists()
# then
- flush_post_commit_hooks()
-
for product in product_list:
product.refresh_from_db()
assert not product.category
@@ -57,6 +55,7 @@ def test_delete_categories_trigger_product_updated_webhook(
categories_tree_with_published_products,
any_webhook,
settings,
+ django_capture_on_commit_callbacks,
):
# given
mocked_get_webhooks_for_event.return_value = [any_webhook]
@@ -67,8 +66,8 @@ def test_delete_categories_trigger_product_updated_webhook(
product_list = [child.products.first(), parent.products.first()]
# when
- delete_categories([parent.pk], manager=get_plugins_manager(allow_replica=False))
- flush_post_commit_hooks()
+ with django_capture_on_commit_callbacks(execute=True):
+ delete_categories([parent.pk], manager=get_plugins_manager(allow_replica=False))
# then
assert not Category.objects.filter(
diff --git a/saleor/tests/e2e/conftest.py b/saleor/tests/e2e/conftest.py
index 267710fb25..316d144193 100644
--- a/saleor/tests/e2e/conftest.py
+++ b/saleor/tests/e2e/conftest.py
@@ -7,7 +7,7 @@ from django.test.client import MULTIPART_CONTENT, Client
from ...account.models import User
from ...app.models import App
from ...graphql.tests.fixtures import BaseApiClient
-from ..utils import flush_post_commit_hooks
+from ...tests.utils import flush_post_commit_hooks
class E2eApiClient(BaseApiClient):
diff --git a/saleor/warehouse/tests/test_stock_management.py b/saleor/warehouse/tests/test_stock_management.py
index a4ff801c2c..aea29e3cce 100644
--- a/saleor/warehouse/tests/test_stock_management.py
+++ b/saleor/warehouse/tests/test_stock_management.py
@@ -9,7 +9,6 @@ from ...core.exceptions import InsufficientStock
from ...order.fetch import OrderLineInfo
from ...order.models import OrderLine
from ...plugins.manager import get_plugins_manager
-from ...tests.utils import flush_post_commit_hooks
from ...warehouse.models import Stock
from ..management import (
allocate_preorders,
@@ -693,14 +692,16 @@ def test_increase_allocation_insufficient_stock(allocation):
@mock.patch("saleor.plugins.manager.PluginsManager.product_variant_back_in_stock")
def test_increase_stock_with_back_in_stock_webhook_triggered_without_allocation(
- product_variant_back_in_stock_webhook, allocation
+ product_variant_back_in_stock_webhook,
+ allocation,
+ django_capture_on_commit_callbacks,
):
stock = allocation.stock
stock.quantity = 0
stock.save(update_fields=["quantity"])
- increase_stock(allocation.order_line, stock.warehouse, 50, allocate=False)
- flush_post_commit_hooks()
+ with django_capture_on_commit_callbacks(execute=True):
+ increase_stock(allocation.order_line, stock.warehouse, 50, allocate=False)
stock.refresh_from_db()
assert stock.quantity == 50
@@ -1034,41 +1035,47 @@ def test_deallocate_stock_for_order(order_line_with_allocation_in_many_stocks):
@mock.patch("saleor.plugins.manager.PluginsManager.product_variant_back_in_stock")
def test_increase_stock_with_back_in_stock_webhook_not_triggered(
- product_variant_back_in_stock_webhook, allocation
+ product_variant_back_in_stock_webhook,
+ allocation,
+ django_capture_on_commit_callbacks,
):
stock = allocation.stock
stock.quantity = 10
stock.save(update_fields=["quantity"])
- increase_stock(allocation.order_line, stock.warehouse, 50, allocate=False)
+ with django_capture_on_commit_callbacks(execute=True):
+ increase_stock(allocation.order_line, stock.warehouse, 50, allocate=False)
stock.refresh_from_db()
assert stock.quantity == 60
- flush_post_commit_hooks()
product_variant_back_in_stock_webhook.assert_not_called()
@mock.patch("saleor.plugins.manager.PluginsManager.product_variant_back_in_stock")
def test_increase_stock_with_back_in_stock_webhook_not_triggered_with_allocation(
- product_variant_back_in_stock_webhook, allocation
+ product_variant_back_in_stock_webhook,
+ allocation,
+ django_capture_on_commit_callbacks,
):
stock = allocation.stock
stock.quantity = 0
stock.save(update_fields=["quantity"])
- increase_stock(allocation.order_line, stock.warehouse, 30, allocate=True)
+ with django_capture_on_commit_callbacks(execute=True):
+ increase_stock(allocation.order_line, stock.warehouse, 30, allocate=True)
stock.refresh_from_db()
assert stock.quantity == 30
- flush_post_commit_hooks()
product_variant_back_in_stock_webhook.assert_not_called()
@mock.patch("saleor.plugins.manager.PluginsManager.product_variant_out_of_stock")
def test_decrease_stock_with_out_of_stock_webhook_triggered(
- product_variant_out_of_stock_webhook_mock, allocation
+ product_variant_out_of_stock_webhook_mock,
+ allocation,
+ django_capture_on_commit_callbacks,
):
stock = allocation.stock
stock.quantity = 50
@@ -1077,19 +1084,18 @@ def test_decrease_stock_with_out_of_stock_webhook_triggered(
allocation.save(update_fields=["quantity_allocated"])
warehouse_pk = allocation.stock.warehouse.pk
- decrease_stock(
- [
- OrderLineInfo(
- line=allocation.order_line,
- quantity=50,
- variant=stock.product_variant,
- warehouse_pk=warehouse_pk,
- )
- ],
- manager=get_plugins_manager(allow_replica=False),
- )
-
- flush_post_commit_hooks()
+ with django_capture_on_commit_callbacks(execute=True):
+ decrease_stock(
+ [
+ OrderLineInfo(
+ line=allocation.order_line,
+ quantity=50,
+ variant=stock.product_variant,
+ warehouse_pk=warehouse_pk,
+ )
+ ],
+ manager=get_plugins_manager(allow_replica=False),
+ )
product_variant_out_of_stock_webhook_mock.assert_called_once()
diff --git a/saleor/webhook/tests/test_tasks.py b/saleor/webhook/tests/test_tasks.py
index 0dc6263188..0202ac2a6f 100644
--- a/saleor/webhook/tests/test_tasks.py
+++ b/saleor/webhook/tests/test_tasks.py
@@ -14,7 +14,6 @@ from ...payment import TransactionEventType
from ...payment.interface import TransactionActionData
from ...payment.models import TransactionEvent
from ...payment.transaction_item_calculations import recalculate_transaction_amounts
-from ...tests.utils import flush_post_commit_hooks
from ..event_types import WebhookEventSyncType
from ..payloads import generate_transaction_action_request_payload
from ..transport.synchronous.transport import handle_transaction_request_task
@@ -44,6 +43,7 @@ def test_trigger_transaction_request(
staff_user,
permission_manage_payments,
app,
+ django_capture_on_commit_callbacks,
):
# given
event = transaction_item_created_by_app.events.create(
@@ -65,12 +65,14 @@ def test_trigger_transaction_request(
)
# when
- trigger_transaction_request(
- transaction_data, WebhookEventSyncType.TRANSACTION_REFUND_REQUESTED, staff_user
- )
+ with django_capture_on_commit_callbacks(execute=True):
+ trigger_transaction_request(
+ transaction_data,
+ WebhookEventSyncType.TRANSACTION_REFUND_REQUESTED,
+ staff_user,
+ )
# then
- flush_post_commit_hooks()
generated_payload = EventPayload.objects.first()
generated_delivery = EventDelivery.objects.first()
@@ -100,6 +102,7 @@ def test_trigger_transaction_request_with_webhook_subscription(
staff_user,
permission_manage_payments,
app,
+ django_capture_on_commit_callbacks,
):
# given
subscription = """
@@ -139,12 +142,14 @@ def test_trigger_transaction_request_with_webhook_subscription(
)
# when
- trigger_transaction_request(
- transaction_data, WebhookEventSyncType.TRANSACTION_REFUND_REQUESTED, staff_user
- )
+ with django_capture_on_commit_callbacks(execute=True):
+ trigger_transaction_request(
+ transaction_data,
+ WebhookEventSyncType.TRANSACTION_REFUND_REQUESTED,
+ staff_user,
+ )
# then
- flush_post_commit_hooks()
generated_payload = EventPayload.objects.first()
generated_delivery = EventDelivery.objects.first()