Fix-up type hints for tests.push module. (#14816)

This commit is contained in:
Patrick Cloke 2023-01-11 07:35:40 -05:00 committed by GitHub
parent d6bda5addd
commit 7f2cabf271
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 67 additions and 62 deletions

View file

@ -12,11 +12,11 @@
# See the License for the specific language governing permissions and
# limitations under the License.
from typing import Iterable, Optional, Tuple
from typing import Iterable, List, Optional, Tuple, cast
from synapse.api.constants import EventTypes, Membership
from synapse.api.room_versions import RoomVersions
from synapse.events import FrozenEvent
from synapse.events import EventBase, FrozenEvent
from synapse.push.presentable_names import calculate_room_name
from synapse.types import StateKey, StateMap
@ -51,13 +51,15 @@ class MockDataStore:
)
async def get_event(
self, event_id: StateKey, allow_none: bool = False
self, event_id: str, allow_none: bool = False
) -> Optional[FrozenEvent]:
assert allow_none, "Mock not configured for allow_none = False"
return self._events.get(event_id)
# Decode the state key from the event ID.
state_key = cast(Tuple[str, str], tuple(event_id.split("|", 1)))
return self._events.get(state_key)
async def get_events(self, event_ids: Iterable[StateKey]):
async def get_events(self, event_ids: Iterable[StateKey]) -> StateMap[EventBase]:
# This is cheating since it just returns all events.
return self._events
@ -68,17 +70,17 @@ class PresentableNamesTestCase(unittest.HomeserverTestCase):
def _calculate_room_name(
self,
events: StateMap[dict],
events: Iterable[Tuple[Tuple[str, str], dict]],
user_id: str = "",
fallback_to_members: bool = True,
fallback_to_single_member: bool = True,
):
# This isn't 100% accurate, but works with MockDataStore.
room_state_ids = {k[0]: k[0] for k in events}
) -> Optional[str]:
# Encode the state key into the event ID.
room_state_ids = {k[0]: "|".join(k[0]) for k in events}
return self.get_success(
calculate_room_name(
MockDataStore(events),
MockDataStore(events), # type: ignore[arg-type]
room_state_ids,
user_id or self.USER_ID,
fallback_to_members,
@ -86,9 +88,9 @@ class PresentableNamesTestCase(unittest.HomeserverTestCase):
)
)
def test_name(self):
def test_name(self) -> None:
"""A room name event should be used."""
events = [
events: List[Tuple[Tuple[str, str], dict]] = [
((EventTypes.Name, ""), {"name": "test-name"}),
]
self.assertEqual("test-name", self._calculate_room_name(events))
@ -100,9 +102,9 @@ class PresentableNamesTestCase(unittest.HomeserverTestCase):
events = [((EventTypes.Name, ""), {"name": 1})]
self.assertEqual(1, self._calculate_room_name(events))
def test_canonical_alias(self):
def test_canonical_alias(self) -> None:
"""An canonical alias should be used."""
events = [
events: List[Tuple[Tuple[str, str], dict]] = [
((EventTypes.CanonicalAlias, ""), {"alias": "#test-name:test"}),
]
self.assertEqual("#test-name:test", self._calculate_room_name(events))
@ -114,9 +116,9 @@ class PresentableNamesTestCase(unittest.HomeserverTestCase):
events = [((EventTypes.CanonicalAlias, ""), {"alias": "test-name"})]
self.assertEqual("Empty Room", self._calculate_room_name(events))
def test_invite(self):
def test_invite(self) -> None:
"""An invite has special behaviour."""
events = [
events: List[Tuple[Tuple[str, str], dict]] = [
((EventTypes.Member, self.USER_ID), {"membership": Membership.INVITE}),
((EventTypes.Member, self.OTHER_USER_ID), {"displayname": "Other User"}),
]
@ -140,9 +142,9 @@ class PresentableNamesTestCase(unittest.HomeserverTestCase):
]
self.assertEqual("Room Invite", self._calculate_room_name(events))
def test_no_members(self):
def test_no_members(self) -> None:
"""Behaviour of an empty room."""
events = []
events: List[Tuple[Tuple[str, str], dict]] = []
self.assertEqual("Empty Room", self._calculate_room_name(events))
# Note that events with invalid (or missing) membership are ignored.
@ -152,7 +154,7 @@ class PresentableNamesTestCase(unittest.HomeserverTestCase):
]
self.assertEqual("Empty Room", self._calculate_room_name(events))
def test_no_other_members(self):
def test_no_other_members(self) -> None:
"""Behaviour of a room with no other members in it."""
events = [
(
@ -185,7 +187,7 @@ class PresentableNamesTestCase(unittest.HomeserverTestCase):
self._calculate_room_name(events, user_id=self.OTHER_USER_ID),
)
def test_one_other_member(self):
def test_one_other_member(self) -> None:
"""Behaviour of a room with a single other member."""
events = [
((EventTypes.Member, self.USER_ID), {"membership": Membership.JOIN}),
@ -209,7 +211,7 @@ class PresentableNamesTestCase(unittest.HomeserverTestCase):
]
self.assertEqual("@user:test", self._calculate_room_name(events))
def test_other_members(self):
def test_other_members(self) -> None:
"""Behaviour of a room with multiple other members."""
# Two other members.
events = [