Skip to content
This repository was archived by the owner on Apr 26, 2024. It is now read-only.

Commit 2fae1a3

Browse files
authored
Improve tests for get_unread_push_actions_for_user_in_range_*. (#13893)
* Adds a docstring. * Reduces a small amount of duplicated code. * Improves tests.
1 parent 58ab967 commit 2fae1a3

File tree

3 files changed

+97
-30
lines changed

3 files changed

+97
-30
lines changed

changelog.d/13893.feature

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Experimental support for thread-specific receipts ([MSC3771](https://github.com/matrix-org/matrix-spec-proposals/pull/3771)).

synapse/storage/databases/main/event_push_actions.py

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -559,7 +559,18 @@ def f(txn: LoggingTransaction) -> List[str]:
559559

560560
def _get_receipts_by_room_txn(
561561
self, txn: LoggingTransaction, user_id: str
562-
) -> List[Tuple[str, int]]:
562+
) -> Dict[str, int]:
563+
"""
564+
Generate a map of room ID to the latest stream ordering that has been
565+
read by the given user.
566+
567+
Args:
568+
txn:
569+
user_id: The user to fetch receipts for.
570+
571+
Returns:
572+
A map of room ID to stream ordering for all rooms the user has a receipt in.
573+
"""
563574
receipt_types_clause, args = make_in_list_sql_clause(
564575
self.database_engine,
565576
"receipt_type",
@@ -580,7 +591,10 @@ def _get_receipts_by_room_txn(
580591

581592
args.extend((user_id,))
582593
txn.execute(sql, args)
583-
return cast(List[Tuple[str, int]], txn.fetchall())
594+
return {
595+
room_id: latest_stream_ordering
596+
for room_id, latest_stream_ordering in txn.fetchall()
597+
}
584598

585599
async def get_unread_push_actions_for_user_in_range_for_http(
586600
self,
@@ -605,12 +619,10 @@ async def get_unread_push_actions_for_user_in_range_for_http(
605619
The list will have between 0~limit entries.
606620
"""
607621

608-
receipts_by_room = dict(
609-
await self.db_pool.runInteraction(
610-
"get_unread_push_actions_for_user_in_range_http_receipts",
611-
self._get_receipts_by_room_txn,
612-
user_id=user_id,
613-
),
622+
receipts_by_room = await self.db_pool.runInteraction(
623+
"get_unread_push_actions_for_user_in_range_http_receipts",
624+
self._get_receipts_by_room_txn,
625+
user_id=user_id,
614626
)
615627

616628
def get_push_actions_txn(
@@ -679,12 +691,10 @@ async def get_unread_push_actions_for_user_in_range_for_email(
679691
The list will have between 0~limit entries.
680692
"""
681693

682-
receipts_by_room = dict(
683-
await self.db_pool.runInteraction(
684-
"get_unread_push_actions_for_user_in_range_email_receipts",
685-
self._get_receipts_by_room_txn,
686-
user_id=user_id,
687-
),
694+
receipts_by_room = await self.db_pool.runInteraction(
695+
"get_unread_push_actions_for_user_in_range_email_receipts",
696+
self._get_receipts_by_room_txn,
697+
user_id=user_id,
688698
)
689699

690700
def get_push_actions_txn(

tests/storage/test_event_push_actions.py

Lines changed: 72 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
from typing import Tuple
16+
1517
from twisted.test.proto_helpers import MemoryReactor
1618

1719
from synapse.rest import admin
@@ -22,8 +24,6 @@
2224

2325
from tests.unittest import HomeserverTestCase
2426

25-
USER_ID = "@user:example.com"
26-
2727

2828
class EventPushActionsStoreTestCase(HomeserverTestCase):
2929
servlets = [
@@ -38,21 +38,13 @@ def prepare(self, reactor: MemoryReactor, clock: Clock, hs: HomeServer) -> None:
3838
assert persist_events_store is not None
3939
self.persist_events_store = persist_events_store
4040

41-
def test_get_unread_push_actions_for_user_in_range_for_http(self) -> None:
42-
self.get_success(
43-
self.store.get_unread_push_actions_for_user_in_range_for_http(
44-
USER_ID, 0, 1000, 20
45-
)
46-
)
41+
def _create_users_and_room(self) -> Tuple[str, str, str, str, str]:
42+
"""
43+
Creates two users and a shared room.
4744
48-
def test_get_unread_push_actions_for_user_in_range_for_email(self) -> None:
49-
self.get_success(
50-
self.store.get_unread_push_actions_for_user_in_range_for_email(
51-
USER_ID, 0, 1000, 20
52-
)
53-
)
54-
55-
def test_count_aggregation(self) -> None:
45+
Returns:
46+
Tuple of (user 1 ID, user 1 token, user 2 ID, user 2 token, room ID).
47+
"""
5648
# Create a user to receive notifications and send receipts.
5749
user_id = self.register_user("user1235", "pass")
5850
token = self.login("user1235", "pass")
@@ -65,6 +57,70 @@ def test_count_aggregation(self) -> None:
6557
room_id = self.helper.create_room_as(user_id, tok=token)
6658
self.helper.join(room_id, other_id, tok=other_token)
6759

60+
return user_id, token, other_id, other_token, room_id
61+
62+
def test_get_unread_push_actions_for_user_in_range(self) -> None:
63+
"""Test getting unread push actions for HTTP and email pushers."""
64+
user_id, token, _, other_token, room_id = self._create_users_and_room()
65+
66+
# Create two events, one of which is a highlight.
67+
self.helper.send_event(
68+
room_id,
69+
type="m.room.message",
70+
content={"msgtype": "m.text", "body": "msg"},
71+
tok=other_token,
72+
)
73+
event_id = self.helper.send_event(
74+
room_id,
75+
type="m.room.message",
76+
content={"msgtype": "m.text", "body": user_id},
77+
tok=other_token,
78+
)["event_id"]
79+
80+
# Fetch unread actions for HTTP pushers.
81+
http_actions = self.get_success(
82+
self.store.get_unread_push_actions_for_user_in_range_for_http(
83+
user_id, 0, 1000, 20
84+
)
85+
)
86+
self.assertEqual(2, len(http_actions))
87+
88+
# Fetch unread actions for email pushers.
89+
email_actions = self.get_success(
90+
self.store.get_unread_push_actions_for_user_in_range_for_email(
91+
user_id, 0, 1000, 20
92+
)
93+
)
94+
self.assertEqual(2, len(email_actions))
95+
96+
# Send a receipt, which should clear any actions.
97+
self.get_success(
98+
self.store.insert_receipt(
99+
room_id,
100+
"m.read",
101+
user_id=user_id,
102+
event_ids=[event_id],
103+
thread_id=None,
104+
data={},
105+
)
106+
)
107+
http_actions = self.get_success(
108+
self.store.get_unread_push_actions_for_user_in_range_for_http(
109+
user_id, 0, 1000, 20
110+
)
111+
)
112+
self.assertEqual([], http_actions)
113+
email_actions = self.get_success(
114+
self.store.get_unread_push_actions_for_user_in_range_for_email(
115+
user_id, 0, 1000, 20
116+
)
117+
)
118+
self.assertEqual([], email_actions)
119+
120+
def test_count_aggregation(self) -> None:
121+
# Create a user to receive notifications and send receipts.
122+
user_id, token, _, other_token, room_id = self._create_users_and_room()
123+
68124
last_event_id: str
69125

70126
def _assert_counts(noitf_count: int, highlight_count: int) -> None:

0 commit comments

Comments
 (0)