test_classes: Add functions to get read/unread user IDs for a message.

This commit adds two functions in `zerver/lib/test_classes.py`. The
first function returns the `user_ids` of users for whom the message
is marked as unread. The second function returns the `user_ids` of
users for whom the message is marked read, based on the `message_id`.
This commit is contained in:
Saubhagya Patel
2025-04-18 15:25:06 +05:30
committed by Tim Abbott
parent 934042d47e
commit 3d33a05455
2 changed files with 24 additions and 26 deletions

View File

@@ -1264,6 +1264,22 @@ Output:
data = self.get_messages_response(anchor, num_before, num_after, use_first_unread_anchor)
return data["messages"]
def get_user_ids_for_whom_message_read(self, message_id: int) -> set[int]:
user_ids = set(
UserMessage.objects.filter(message_id=message_id)
.extra(where=[UserMessage.where_read()]) # noqa: S610
.values_list("user_profile_id", flat=True)
)
return user_ids
def get_user_ids_for_whom_message_unread(self, message_id: int) -> set[int]:
user_ids = set(
UserMessage.objects.filter(message_id=message_id)
.extra(where=[UserMessage.where_unread()]) # noqa: S610
.values_list("user_profile_id", flat=True)
)
return user_ids
def users_subscribed_to_stream(self, stream_name: str, realm: Realm) -> list[UserProfile]:
stream = Stream.objects.get(name=stream_name, realm=realm)
recipient = Recipient.objects.get(type_id=stream.id, type=Recipient.STREAM)

View File

@@ -1666,20 +1666,11 @@ class MessageMoveTopicTest(ZulipTestCase):
)
# Check topic resolved notification message is only unread for participants.
assert (
UserMessage.objects.filter(
user_profile__in=[admin_user, hamlet, aaron], message__id=messages[2].id
)
.extra(where=[UserMessage.where_unread()]) # noqa: S610
.count()
== 3
)
unread_user_ids = self.get_user_ids_for_whom_message_unread(messages[2].id)
self.assertEqual(unread_user_ids, {admin_user.id, hamlet.id, aaron.id})
assert (
not UserMessage.objects.filter(user_profile=cordelia, message__id=messages[2].id)
.extra(where=[UserMessage.where_unread()]) # noqa: S610
.exists()
)
read_user_ids = self.get_user_ids_for_whom_message_read(messages[2].id)
self.assertEqual(read_user_ids, {cordelia.id})
# Now move to a weird state and confirm we get the normal topic moved message.
weird_topic_name = "✔ ✔✔" + original_topic_name
@@ -1738,20 +1729,11 @@ class MessageMoveTopicTest(ZulipTestCase):
)
# Check topic unresolved notification message is only unread for participants.
assert (
UserMessage.objects.filter(
user_profile__in=[admin_user, hamlet, aaron], message__id=messages[4].id
)
.extra(where=[UserMessage.where_unread()]) # noqa: S610
.count()
== 3
)
unread_user_ids = self.get_user_ids_for_whom_message_unread(messages[4].id)
self.assertEqual(unread_user_ids, {admin_user.id, hamlet.id, aaron.id})
assert (
not UserMessage.objects.filter(user_profile=cordelia, message__id=messages[4].id)
.extra(where=[UserMessage.where_unread()]) # noqa: S610
.exists()
)
read_user_ids = self.get_user_ids_for_whom_message_read(messages[4].id)
self.assertEqual(read_user_ids, {cordelia.id})
@override_settings(RESOLVE_TOPIC_UNDO_GRACE_PERIOD_SECONDS=60)
def test_mark_topic_as_resolved_within_grace_period(self) -> None: