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

Commit

Permalink
Factor out some common code.
Browse files Browse the repository at this point in the history
  • Loading branch information
clokep committed Jan 20, 2022
1 parent f8962d2 commit 6cd6068
Showing 1 changed file with 29 additions and 38 deletions.
67 changes: 29 additions & 38 deletions tests/rest/client/test_relations.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
from synapse.api.constants import EventTypes, RelationTypes
from synapse.rest import admin
from synapse.rest.client import login, register, relations, room, sync
from synapse.types import JsonDict

from tests import unittest
from tests.server import FakeChannel
Expand Down Expand Up @@ -486,12 +487,13 @@ def test_bundled_aggregations(self):
self.assertEquals(200, channel.code, channel.json_body)
thread_2 = channel.json_body["event_id"]

def assert_bundle(actual):
def assert_bundle(event_json: JsonDict) -> None:
"""Assert the expected values of the bundled aggregations."""
relations_dict = event_json["unsigned"].get("m.relations")

# Ensure the fields are as expected.
self.assertCountEqual(
actual.keys(),
relations_dict.keys(),
(
RelationTypes.ANNOTATION,
RelationTypes.REFERENCE,
Expand All @@ -507,20 +509,20 @@ def assert_bundle(actual):
{"type": "m.reaction", "key": "b", "count": 1},
]
},
actual[RelationTypes.ANNOTATION],
relations_dict[RelationTypes.ANNOTATION],
)

self.assertEquals(
{"chunk": [{"event_id": reply_1}, {"event_id": reply_2}]},
actual[RelationTypes.REFERENCE],
relations_dict[RelationTypes.REFERENCE],
)

self.assertEquals(
2,
actual[RelationTypes.THREAD].get("count"),
relations_dict[RelationTypes.THREAD].get("count"),
)
self.assertTrue(
actual[RelationTypes.THREAD].get("current_user_participated")
relations_dict[RelationTypes.THREAD].get("current_user_participated")
)
# The latest thread event has some fields that don't matter.
self.assert_dict(
Expand All @@ -537,28 +539,17 @@ def assert_bundle(actual):
"type": "m.room.test",
"user_id": self.user_id,
},
actual[RelationTypes.THREAD].get("latest_event"),
relations_dict[RelationTypes.THREAD].get("latest_event"),
)

def _find_and_assert_event(events):
"""
Find the parent event in a chunk of events and assert that it has the proper bundled aggregations.
"""
for event in events:
if event["event_id"] == self.parent_id:
break
else:
raise AssertionError(f"Event {self.parent_id} not found in chunk")
assert_bundle(event["unsigned"].get("m.relations"))

# Request the event directly.
channel = self.make_request(
"GET",
f"/rooms/{self.room}/event/{self.parent_id}",
access_token=self.user_token,
)
self.assertEquals(200, channel.code, channel.json_body)
assert_bundle(channel.json_body["unsigned"].get("m.relations"))
assert_bundle(channel.json_body)

# Request the room messages.
channel = self.make_request(
Expand All @@ -567,7 +558,7 @@ def _find_and_assert_event(events):
access_token=self.user_token,
)
self.assertEquals(200, channel.code, channel.json_body)
_find_and_assert_event(channel.json_body["chunk"])
assert_bundle(self._find_event_in_chunk(channel.json_body["chunk"]))

# Request the room context.
channel = self.make_request(
Expand All @@ -576,14 +567,14 @@ def _find_and_assert_event(events):
access_token=self.user_token,
)
self.assertEquals(200, channel.code, channel.json_body)
assert_bundle(channel.json_body["event"]["unsigned"].get("m.relations"))
assert_bundle(channel.json_body["event"])

# Request sync.
channel = self.make_request("GET", "/sync", access_token=self.user_token)
self.assertEquals(200, channel.code, channel.json_body)
room_timeline = channel.json_body["rooms"]["join"][self.room]["timeline"]
self.assertTrue(room_timeline["limited"])
_find_and_assert_event(room_timeline["events"])
self._find_event_in_chunk(room_timeline["events"])

# Note that /relations is tested separately in test_aggregation_get_event_for_thread
# since it needs different data configured.
Expand Down Expand Up @@ -781,8 +772,9 @@ def test_edit(self):

edit_event_id = channel.json_body["event_id"]

def assert_bundle(relations_dict):
def assert_bundle(event_json: JsonDict) -> None:
"""Assert the expected values of the bundled aggregations."""
relations_dict = event_json["unsigned"].get("m.relations")
self.assertIn(RelationTypes.REPLACE, relations_dict)

m_replace_dict = relations_dict[RelationTypes.REPLACE]
Expand All @@ -793,25 +785,14 @@ def assert_bundle(relations_dict):
{"event_id": edit_event_id, "sender": self.user_id}, m_replace_dict
)

def _find_and_assert_event(events):
"""
Find the parent event in a chunk of events and assert that it has the proper bundled aggregations.
"""
for event in events:
if event["event_id"] == self.parent_id:
break
else:
raise AssertionError(f"Event {self.parent_id} not found in chunk")
assert_bundle(event["unsigned"].get("m.relations"))

channel = self.make_request(
"GET",
f"/rooms/{self.room}/event/{self.parent_id}",
access_token=self.user_token,
)
self.assertEquals(200, channel.code, channel.json_body)
self.assertEquals(channel.json_body["content"], new_body)
assert_bundle(channel.json_body["unsigned"].get("m.relations"))
assert_bundle(channel.json_body)

# Request the room messages.
channel = self.make_request(
Expand All @@ -820,7 +801,7 @@ def _find_and_assert_event(events):
access_token=self.user_token,
)
self.assertEquals(200, channel.code, channel.json_body)
_find_and_assert_event(channel.json_body["chunk"])
assert_bundle(self._find_event_in_chunk(channel.json_body["chunk"]))

# Request the room context.
channel = self.make_request(
Expand All @@ -829,7 +810,7 @@ def _find_and_assert_event(events):
access_token=self.user_token,
)
self.assertEquals(200, channel.code, channel.json_body)
assert_bundle(channel.json_body["event"]["unsigned"].get("m.relations"))
assert_bundle(channel.json_body["event"])

# Request sync, but limit the timeline so it becomes limited (and includes
# bundled aggregations).
Expand All @@ -842,7 +823,7 @@ def _find_and_assert_event(events):
self.assertEquals(200, channel.code, channel.json_body)
room_timeline = channel.json_body["rooms"]["join"][self.room]["timeline"]
self.assertTrue(room_timeline["limited"])
_find_and_assert_event(room_timeline["events"])
assert_bundle(self._find_event_in_chunk(room_timeline["events"]))

# Note that /relations is tested separately in test_aggregation_get_event_for_thread
# since it needs different data configured.
Expand Down Expand Up @@ -1152,6 +1133,16 @@ def test_unknown_relations(self):
self.assertEquals(200, channel.code, channel.json_body)
self.assertEquals(channel.json_body["chunk"], [])

def _find_event_in_chunk(self, events: List[JsonDict]) -> JsonDict:
"""
Find the parent event in a chunk of events and assert that it has the proper bundled aggregations.
"""
for event in events:
if event["event_id"] == self.parent_id:
return event

raise AssertionError(f"Event {self.parent_id} not found in chunk")

def _send_relation(
self,
relation_type: str,
Expand Down

0 comments on commit 6cd6068

Please sign in to comment.