Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
joeyorlando committed Jun 10, 2024
1 parent 7448556 commit d459705
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 23 deletions.
46 changes: 27 additions & 19 deletions engine/apps/alerts/tests/test_paging.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from unittest.mock import patch
from unittest.mock import call, patch

import pytest
from django.utils import timezone
Expand Down Expand Up @@ -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}
Expand Down Expand Up @@ -158,16 +158,21 @@ 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)
from_user = make_user_for_organization(organization)
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()
Expand 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
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions engine/apps/schedules/tests/test_shift_swap_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
16 changes: 14 additions & 2 deletions engine/apps/slack/tests/test_scenario_steps/test_paging.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit d459705

Please sign in to comment.