diff --git a/engine/apps/alerts/tests/test_paging.py b/engine/apps/alerts/tests/test_paging.py index 05a457206d..7984fa44d4 100644 --- a/engine/apps/alerts/tests/test_paging.py +++ b/engine/apps/alerts/tests/test_paging.py @@ -1,4 +1,4 @@ -from unittest.mock import patch +from unittest.mock import call, patch import pytest from django.utils import timezone @@ -78,7 +78,7 @@ def test_direct_paging_user(make_organization, make_user_for_organization, djang assert len(callbacks) == 3 # notifications sent for u, important in ((user, False), (other_user, True)): - assert notify_task.apply_async.called_with( + notify_task.apply_async.assert_any_call( (u.pk, ag.pk), {"important": important, "notify_even_acknowledged": True, "notify_anyway": True} ) expected_info = {"user": u.public_primary_key, "important": important} @@ -158,7 +158,11 @@ def test_direct_paging_no_team_and_no_users(make_organization, make_user_for_org @pytest.mark.django_db def test_direct_paging_reusing_alert_group( - make_organization, make_user_for_organization, make_alert_receive_channel, make_alert_group + make_organization, + make_user_for_organization, + make_alert_receive_channel, + make_alert_group, + django_capture_on_commit_callbacks, ): organization = make_organization() user = make_user_for_organization(organization) @@ -166,8 +170,9 @@ def test_direct_paging_reusing_alert_group( alert_receive_channel = make_alert_receive_channel(organization=organization) alert_group = make_alert_group(alert_receive_channel=alert_receive_channel) - with patch("apps.alerts.paging.notify_user_task") as notify_task: - direct_paging(organization, from_user, "Fire!", users=[(user, False)], alert_group=alert_group) + with django_capture_on_commit_callbacks(execute=True): + with patch("apps.alerts.paging.notify_user_task") as notify_task: + direct_paging(organization, from_user, "Fire!", users=[(user, False)], alert_group=alert_group) # no new alert group is created alert_groups = AlertGroup.objects.all() @@ -176,9 +181,9 @@ def test_direct_paging_reusing_alert_group( # notifications sent ag = alert_groups.get() - assert notify_task.apply_async.called_with( - (user.pk, ag.pk), {"important": False, "notify_even_acknowledged": True, "notify_anyway": True} - ) + notify_task.apply_async.assert_has_calls([ + call((user.pk, ag.pk), {"important": False, "notify_even_acknowledged": True, "notify_anyway": True}) + ]) @pytest.mark.django_db @@ -229,29 +234,32 @@ def test_unpage_user_ok(make_organization, make_user_for_organization, make_aler @pytest.mark.django_db -def test_direct_paging_always_create_group(make_organization, make_user_for_organization): +def test_direct_paging_always_create_group( + make_organization, + make_user_for_organization, + django_capture_on_commit_callbacks, +): organization = make_organization() user = make_user_for_organization(organization) from_user = make_user_for_organization(organization) msg = "Help!" users = [(user, False)] - with patch("apps.alerts.paging.notify_user_task") as notify_task: - # although calling twice with same params, there should be 2 alert groups - direct_paging(organization, from_user, msg, users=users) - direct_paging(organization, from_user, msg, users=users) + with django_capture_on_commit_callbacks(execute=True): + with patch("apps.alerts.paging.notify_user_task") as notify_task: + # although calling twice with same params, there should be 2 alert groups + direct_paging(organization, from_user, msg, users=users) + direct_paging(organization, from_user, msg, users=users) # alert group created alert_groups = AlertGroup.objects.all() assert alert_groups.count() == 2 # notifications sent - assert notify_task.apply_async.called_with( - (user.pk, alert_groups[0].pk), {"important": False, "notify_even_acknowledged": True, "notify_anyway": True} - ) - assert notify_task.apply_async.called_with( - (user.pk, alert_groups[1].pk), {"important": False, "notify_even_acknowledged": True, "notify_anyway": True} - ) + notify_task.apply_async.assert_has_calls([ + call((user.pk, alert_groups[0].pk), {"important": False, "notify_even_acknowledged": True, "notify_anyway": True}), + call((user.pk, alert_groups[1].pk), {"important": False, "notify_even_acknowledged": True, "notify_anyway": True}), + ]) @pytest.mark.django_db diff --git a/engine/apps/schedules/tests/test_shift_swap_request.py b/engine/apps/schedules/tests/test_shift_swap_request.py index ab1d87c3e5..e87bb156f1 100644 --- a/engine/apps/schedules/tests/test_shift_swap_request.py +++ b/engine/apps/schedules/tests/test_shift_swap_request.py @@ -23,7 +23,7 @@ def test_soft_delete(shift_swap_request_setup): ssr.refresh_from_db() assert ssr.deleted_at is not None - assert mock_refresh_final.apply_async.called_with((ssr.schedule.pk,)) + mock_refresh_final.apply_async.assert_called_with((ssr.schedule.pk,)) assert ShiftSwapRequest.objects.all().count() == 0 assert ShiftSwapRequest.objects_with_deleted.all().count() == 1 @@ -100,7 +100,7 @@ def test_take( mock_notify_beneficiary_about_taken_shift_swap_request.apply_async.assert_called_once_with((ssr.pk,)) # final schedule refresh was triggered - assert mock_refresh_final.apply_async.called_with((ssr.schedule.pk,)) + mock_refresh_final.apply_async.assert_called_with((ssr.schedule.pk,)) @pytest.mark.django_db diff --git a/engine/apps/slack/tests/test_scenario_steps/test_paging.py b/engine/apps/slack/tests/test_scenario_steps/test_paging.py index 3d46425d8e..a46ce6abdf 100644 --- a/engine/apps/slack/tests/test_scenario_steps/test_paging.py +++ b/engine/apps/slack/tests/test_scenario_steps/test_paging.py @@ -301,7 +301,13 @@ def test_trigger_paging_additional_responders(make_organization_and_user_with_sl with patch.object(step._slack_client, "api_call"): step.process_scenario(slack_user_identity, slack_team_identity, payload) - mock_direct_paging.assert_called_once_with(organization, user, "The Message", team, [(user, True)]) + mock_direct_paging.assert_called_once_with( + organization=organization, + from_user=user, + message="The Message", + team=team, + users=[(user, True)], + ) @pytest.mark.django_db @@ -315,7 +321,13 @@ def test_page_team(make_organization_and_user_with_slack_identities, make_team): with patch.object(step._slack_client, "api_call"): step.process_scenario(slack_user_identity, slack_team_identity, payload) - mock_direct_paging.assert_called_once_with(organization, user, "The Message", team) + mock_direct_paging.assert_called_once_with( + organization=organization, + from_user=user, + message="The Message", + team=team, + users=[], + ) @pytest.mark.django_db