Browse Source

ref: remove exam-specific assertChanges (#42326)

<!-- Describe your PR here. -->
anthony sottile 2 years ago
parent
commit
9729d7a779

+ 24 - 24
tests/sentry/incidents/test_logic.py

@@ -348,15 +348,14 @@ class CreateIncidentActivityTest(TestCase, BaseIncidentsTest):
     def test_comment(self):
         incident = self.create_incident()
         comment = "hello"
-        with self.assertChanges(
-            lambda: IncidentSubscription.objects.filter(incident=incident, user=self.user).exists(),
-            before=False,
-            after=True,
-        ):
-            self.record_event.reset_mock()
-            activity = create_incident_activity(
-                incident, IncidentActivityType.COMMENT, user=self.user, comment=comment
-            )
+
+        assert not IncidentSubscription.objects.filter(incident=incident, user=self.user).exists()
+        self.record_event.reset_mock()
+        activity = create_incident_activity(
+            incident, IncidentActivityType.COMMENT, user=self.user, comment=comment
+        )
+        assert IncidentSubscription.objects.filter(incident=incident, user=self.user).exists()
+
         assert activity.incident == incident
         assert activity.type == IncidentActivityType.COMMENT.value
         assert activity.user == self.user
@@ -381,21 +380,22 @@ class CreateIncidentActivityTest(TestCase, BaseIncidentsTest):
         subscribed_mentioned_member = self.create_user()
         IncidentSubscription.objects.create(incident=incident, user=subscribed_mentioned_member)
         comment = f"hello **@{mentioned_member.username}** and **@{subscribed_mentioned_member.username}**"
-        with self.assertChanges(
-            lambda: IncidentSubscription.objects.filter(
-                incident=incident, user=mentioned_member
-            ).exists(),
-            before=False,
-            after=True,
-        ):
-            self.record_event.reset_mock()
-            activity = create_incident_activity(
-                incident,
-                IncidentActivityType.COMMENT,
-                user=self.user,
-                comment=comment,
-                mentioned_user_ids=[mentioned_member.id, subscribed_mentioned_member.id],
-            )
+
+        assert not IncidentSubscription.objects.filter(
+            incident=incident, user=mentioned_member
+        ).exists()
+        self.record_event.reset_mock()
+        activity = create_incident_activity(
+            incident,
+            IncidentActivityType.COMMENT,
+            user=self.user,
+            comment=comment,
+            mentioned_user_ids=[mentioned_member.id, subscribed_mentioned_member.id],
+        )
+        assert IncidentSubscription.objects.filter(
+            incident=incident, user=mentioned_member
+        ).exists()
+
         assert activity.incident == incident
         assert activity.type == IncidentActivityType.COMMENT.value
         assert activity.user == self.user

+ 100 - 119
tests/sentry/models/test_groupsubscription.py

@@ -201,12 +201,11 @@ class GetParticipantsTest(TestCase):
         # Implicit subscription, ensure the project setting overrides the
         # default global option.
 
-        with self.assertChanges(
-            get_participants,
-            before={ExternalProviders.EMAIL: {self.user: GroupSubscriptionReason.implicit}},
-            after={},
-        ):
-            self.update_project_setting_never()
+        assert get_participants() == {
+            ExternalProviders.EMAIL: {self.user: GroupSubscriptionReason.implicit}
+        }
+        self.update_project_setting_never()
+        assert get_participants() == {}
 
         NotificationSetting.objects.remove_for_user(self.user, NotificationSettingTypes.WORKFLOW)
 
@@ -215,12 +214,11 @@ class GetParticipantsTest(TestCase):
 
         self.update_user_settings_always()
 
-        with self.assertChanges(
-            get_participants,
-            before={ExternalProviders.EMAIL: {self.user: GroupSubscriptionReason.implicit}},
-            after={},
-        ):
-            self.update_project_setting_never()
+        assert get_participants() == {
+            ExternalProviders.EMAIL: {self.user: GroupSubscriptionReason.implicit}
+        }
+        self.update_project_setting_never()
+        assert get_participants() == {}
 
         NotificationSetting.objects.remove_for_user(self.user, NotificationSettingTypes.WORKFLOW)
 
@@ -234,15 +232,14 @@ class GetParticipantsTest(TestCase):
             reason=GroupSubscriptionReason.comment,
         )
 
-        with self.assertChanges(
-            get_participants,
-            before={
-                ExternalProviders.EMAIL: {self.user: GroupSubscriptionReason.comment},
-                ExternalProviders.SLACK: {self.user: GroupSubscriptionReason.comment},
-            },
-            after={ExternalProviders.SLACK: {self.user: GroupSubscriptionReason.comment}},
-        ):
-            self.update_user_setting_never()
+        assert get_participants() == {
+            ExternalProviders.EMAIL: {self.user: GroupSubscriptionReason.comment},
+            ExternalProviders.SLACK: {self.user: GroupSubscriptionReason.comment},
+        }
+        self.update_user_setting_never()
+        assert get_participants() == {
+            ExternalProviders.SLACK: {self.user: GroupSubscriptionReason.comment}
+        }
 
         NotificationSetting.objects.remove_for_user(self.user, NotificationSettingTypes.WORKFLOW)
 
@@ -250,50 +247,45 @@ class GetParticipantsTest(TestCase):
 
         self.update_user_setting_subscribe_only()
 
-        with self.assertChanges(
-            get_participants,
-            before={
-                ExternalProviders.EMAIL: {self.user: GroupSubscriptionReason.comment},
-                ExternalProviders.SLACK: {self.user: GroupSubscriptionReason.comment},
-            },
-            after={ExternalProviders.SLACK: {self.user: GroupSubscriptionReason.comment}},
-        ):
-            self.update_project_setting_never()
+        assert get_participants() == {
+            ExternalProviders.EMAIL: {self.user: GroupSubscriptionReason.comment},
+            ExternalProviders.SLACK: {self.user: GroupSubscriptionReason.comment},
+        }
+        self.update_project_setting_never()
+        assert get_participants() == {
+            ExternalProviders.SLACK: {self.user: GroupSubscriptionReason.comment}
+        }
 
         NotificationSetting.objects.remove_for_user(self.user, NotificationSettingTypes.WORKFLOW)
 
         # Explicit subscription, overridden by the project option which also
         # overrides the default option.
 
-        with self.assertChanges(
-            get_participants,
-            before={
-                ExternalProviders.EMAIL: {self.user: GroupSubscriptionReason.comment},
-                ExternalProviders.SLACK: {self.user: GroupSubscriptionReason.comment},
-            },
-            after={ExternalProviders.SLACK: {self.user: GroupSubscriptionReason.comment}},
-        ):
-            self.update_project_setting_never()
+        assert get_participants() == {
+            ExternalProviders.EMAIL: {self.user: GroupSubscriptionReason.comment},
+            ExternalProviders.SLACK: {self.user: GroupSubscriptionReason.comment},
+        }
+        self.update_project_setting_never()
+        assert get_participants() == {
+            ExternalProviders.SLACK: {self.user: GroupSubscriptionReason.comment}
+        }
 
     def test_participating_only(self):
         get_participants = functools.partial(GroupSubscription.objects.get_participants, self.group)
 
         # Implicit subscription, ensure the project setting overrides the default global option.
 
-        with self.assertChanges(
-            get_participants,
-            before={
-                ExternalProviders.EMAIL: {self.user: GroupSubscriptionReason.implicit},
-            },
-            after={},
-        ):
-            NotificationSetting.objects.update_settings(
-                ExternalProviders.EMAIL,
-                NotificationSettingTypes.WORKFLOW,
-                NotificationSettingOptionValues.SUBSCRIBE_ONLY,
-                user=self.user,
-                project=self.project,
-            )
+        assert get_participants() == {
+            ExternalProviders.EMAIL: {self.user: GroupSubscriptionReason.implicit},
+        }
+        NotificationSetting.objects.update_settings(
+            ExternalProviders.EMAIL,
+            NotificationSettingTypes.WORKFLOW,
+            NotificationSettingOptionValues.SUBSCRIBE_ONLY,
+            user=self.user,
+            project=self.project,
+        )
+        assert get_participants() == {}
 
         NotificationSetting.objects.remove_for_user(self.user, NotificationSettingTypes.WORKFLOW)
 
@@ -301,33 +293,29 @@ class GetParticipantsTest(TestCase):
         # explicit global option.
         self.update_user_settings_always()
 
-        with self.assertChanges(
-            get_participants,
-            before={ExternalProviders.EMAIL: {self.user: GroupSubscriptionReason.implicit}},
-            after={},
-        ):
-            self.update_project_setting_never()
+        assert get_participants() == {
+            ExternalProviders.EMAIL: {self.user: GroupSubscriptionReason.implicit}
+        }
+        self.update_project_setting_never()
+        assert get_participants() == {}
 
         NotificationSetting.objects.remove_for_user(self.user, NotificationSettingTypes.WORKFLOW)
 
         # Ensure the global default is applied.
         self.update_user_setting_subscribe_only()
 
-        with self.assertChanges(
-            get_participants,
-            before={},
-            after={
-                ExternalProviders.EMAIL: {self.user: GroupSubscriptionReason.comment},
-                ExternalProviders.SLACK: {self.user: GroupSubscriptionReason.comment},
-            },
-        ):
-            subscription = GroupSubscription.objects.create(
-                user_id=self.user.id,
-                group=self.group,
-                project=self.project,
-                is_active=True,
-                reason=GroupSubscriptionReason.comment,
-            )
+        assert get_participants() == {}
+        subscription = GroupSubscription.objects.create(
+            user_id=self.user.id,
+            group=self.group,
+            project=self.project,
+            is_active=True,
+            reason=GroupSubscriptionReason.comment,
+        )
+        assert get_participants() == {
+            ExternalProviders.EMAIL: {self.user: GroupSubscriptionReason.comment},
+            ExternalProviders.SLACK: {self.user: GroupSubscriptionReason.comment},
+        }
 
         subscription.delete()
         NotificationSetting.objects.remove_for_user(self.user, NotificationSettingTypes.WORKFLOW)
@@ -335,21 +323,18 @@ class GetParticipantsTest(TestCase):
         # Ensure the project setting overrides the global default.
         self.update_project_setting_subscribe_only()
 
-        with self.assertChanges(
-            get_participants,
-            before={},
-            after={
-                ExternalProviders.EMAIL: {self.user: GroupSubscriptionReason.comment},
-                ExternalProviders.SLACK: {self.user: GroupSubscriptionReason.comment},
-            },
-        ):
-            subscription = GroupSubscription.objects.create(
-                user_id=self.user.id,
-                group=self.group,
-                project=self.project,
-                is_active=True,
-                reason=GroupSubscriptionReason.comment,
-            )
+        assert get_participants() == {}
+        subscription = GroupSubscription.objects.create(
+            user_id=self.user.id,
+            group=self.group,
+            project=self.project,
+            is_active=True,
+            reason=GroupSubscriptionReason.comment,
+        )
+        assert get_participants() == {
+            ExternalProviders.EMAIL: {self.user: GroupSubscriptionReason.comment},
+            ExternalProviders.SLACK: {self.user: GroupSubscriptionReason.comment},
+        }
 
         subscription.delete()
         NotificationSetting.objects.remove_for_user(self.user, NotificationSettingTypes.WORKFLOW)
@@ -359,21 +344,18 @@ class GetParticipantsTest(TestCase):
         self.update_user_settings_always()
         self.update_project_setting_subscribe_only()
 
-        with self.assertChanges(
-            get_participants,
-            before={},
-            after={
-                ExternalProviders.EMAIL: {self.user: GroupSubscriptionReason.comment},
-                ExternalProviders.SLACK: {self.user: GroupSubscriptionReason.comment},
-            },
-        ):
-            subscription = GroupSubscription.objects.create(
-                user_id=self.user.id,
-                group=self.group,
-                project=self.project,
-                is_active=True,
-                reason=GroupSubscriptionReason.comment,
-            )
+        assert get_participants() == {}
+        subscription = GroupSubscription.objects.create(
+            user_id=self.user.id,
+            group=self.group,
+            project=self.project,
+            is_active=True,
+            reason=GroupSubscriptionReason.comment,
+        )
+        assert get_participants() == {
+            ExternalProviders.EMAIL: {self.user: GroupSubscriptionReason.comment},
+            ExternalProviders.SLACK: {self.user: GroupSubscriptionReason.comment},
+        }
 
         subscription.delete()
         NotificationSetting.objects.remove_for_user(self.user, NotificationSettingTypes.WORKFLOW)
@@ -381,21 +363,20 @@ class GetParticipantsTest(TestCase):
         self.update_user_setting_subscribe_only()
         self.update_project_setting_always()
 
-        with self.assertChanges(
-            get_participants,
-            before={ExternalProviders.EMAIL: {self.user: GroupSubscriptionReason.implicit}},
-            after={
-                ExternalProviders.EMAIL: {self.user: GroupSubscriptionReason.comment},
-                ExternalProviders.SLACK: {self.user: GroupSubscriptionReason.comment},
-            },
-        ):
-            subscription = GroupSubscription.objects.create(
-                user_id=self.user.id,
-                group=self.group,
-                project=self.project,
-                is_active=True,
-                reason=GroupSubscriptionReason.comment,
-            )
+        assert get_participants() == {
+            ExternalProviders.EMAIL: {self.user: GroupSubscriptionReason.implicit}
+        }
+        subscription = GroupSubscription.objects.create(
+            user_id=self.user.id,
+            group=self.group,
+            project=self.project,
+            is_active=True,
+            reason=GroupSubscriptionReason.comment,
+        )
+        assert get_participants() == {
+            ExternalProviders.EMAIL: {self.user: GroupSubscriptionReason.comment},
+            ExternalProviders.SLACK: {self.user: GroupSubscriptionReason.comment},
+        }
 
     def test_does_not_include_nonmember(self):
         org = self.create_organization()

+ 3 - 3
tests/snuba/incidents/test_tasks.py

@@ -134,10 +134,10 @@ class HandleSnubaQueryUpdateTest(TestCase):
         subscriber_registry[INCIDENTS_SNUBA_SUBSCRIPTION_TYPE] = shutdown_callback
 
         with self.feature(["organizations:incidents", "organizations:performance-view"]):
-            with self.assertChanges(
-                lambda: active_incident().exists(), before=False, after=True
-            ), self.tasks(), self.capture_on_commit_callbacks(execute=True):
+            assert not active_incident().exists()
+            with self.tasks(), self.capture_on_commit_callbacks(execute=True):
                 consumer.run()
+            assert active_incident().exists()
 
         assert len(mail.outbox) == 1
         handler = EmailActionHandler(self.action, active_incident().get(), self.project)