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

Commit

Permalink
Add missing type hints to tests. (#15027)
Browse files Browse the repository at this point in the history
  • Loading branch information
clokep authored Feb 8, 2023
1 parent 55e4d27 commit 4eed7b2
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 76 deletions.
1 change: 1 addition & 0 deletions changelog.d/15027.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Improve type hints.
18 changes: 0 additions & 18 deletions mypy.ini
Original file line number Diff line number Diff line change
Expand Up @@ -69,27 +69,9 @@ disallow_untyped_defs = False
[mypy-tests.server_notices.test_resource_limits_server_notices]
disallow_untyped_defs = False

[mypy-tests.test_distributor]
disallow_untyped_defs = False

[mypy-tests.test_event_auth]
disallow_untyped_defs = False

[mypy-tests.test_federation]
disallow_untyped_defs = False

[mypy-tests.test_mau]
disallow_untyped_defs = False

[mypy-tests.test_rust]
disallow_untyped_defs = False

[mypy-tests.test_test_utils]
disallow_untyped_defs = False

[mypy-tests.test_types]
disallow_untyped_defs = False

[mypy-tests.test_utils.*]
disallow_untyped_defs = False

Expand Down
12 changes: 6 additions & 6 deletions tests/test_distributor.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@


class DistributorTestCase(unittest.TestCase):
def setUp(self):
def setUp(self) -> None:
self.dist = Distributor()

def test_signal_dispatch(self):
def test_signal_dispatch(self) -> None:
self.dist.declare("alert")

observer = Mock()
Expand All @@ -33,7 +33,7 @@ def test_signal_dispatch(self):
self.dist.fire("alert", 1, 2, 3)
observer.assert_called_with(1, 2, 3)

def test_signal_catch(self):
def test_signal_catch(self) -> None:
self.dist.declare("alarm")

observers = [Mock() for i in (1, 2)]
Expand All @@ -51,7 +51,7 @@ def test_signal_catch(self):
self.assertEqual(mock_logger.warning.call_count, 1)
self.assertIsInstance(mock_logger.warning.call_args[0][0], str)

def test_signal_prereg(self):
def test_signal_prereg(self) -> None:
observer = Mock()
self.dist.observe("flare", observer)

Expand All @@ -60,8 +60,8 @@ def test_signal_prereg(self):

observer.assert_called_with(4, 5)

def test_signal_undeclared(self):
def code():
def test_signal_undeclared(self) -> None:
def code() -> None:
self.dist.fire("notification")

self.assertRaises(KeyError, code)
32 changes: 17 additions & 15 deletions tests/test_event_auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,13 @@
class _StubEventSourceStore:
"""A stub implementation of the EventSourceStore"""

def __init__(self):
def __init__(self) -> None:
self._store: Dict[str, EventBase] = {}

def add_event(self, event: EventBase):
def add_event(self, event: EventBase) -> None:
self._store[event.event_id] = event

def add_events(self, events: Iterable[EventBase]):
def add_events(self, events: Iterable[EventBase]) -> None:
for event in events:
self._store[event.event_id] = event

Expand All @@ -59,7 +59,7 @@ async def get_events(


class EventAuthTestCase(unittest.TestCase):
def test_rejected_auth_events(self):
def test_rejected_auth_events(self) -> None:
"""
Events that refer to rejected events in their auth events are rejected
"""
Expand Down Expand Up @@ -109,7 +109,7 @@ def test_rejected_auth_events(self):
)
)

def test_create_event_with_prev_events(self):
def test_create_event_with_prev_events(self) -> None:
"""A create event with prev_events should be rejected
https://spec.matrix.org/v1.3/rooms/v9/#authorization-rules
Expand Down Expand Up @@ -150,7 +150,7 @@ def test_create_event_with_prev_events(self):
event_auth.check_state_independent_auth_rules(event_store, bad_event)
)

def test_duplicate_auth_events(self):
def test_duplicate_auth_events(self) -> None:
"""Events with duplicate auth_events should be rejected
https://spec.matrix.org/v1.3/rooms/v9/#authorization-rules
Expand Down Expand Up @@ -196,7 +196,7 @@ def test_duplicate_auth_events(self):
event_auth.check_state_independent_auth_rules(event_store, bad_event2)
)

def test_unexpected_auth_events(self):
def test_unexpected_auth_events(self) -> None:
"""Events with excess auth_events should be rejected
https://spec.matrix.org/v1.3/rooms/v9/#authorization-rules
Expand Down Expand Up @@ -236,7 +236,7 @@ def test_unexpected_auth_events(self):
event_auth.check_state_independent_auth_rules(event_store, bad_event)
)

def test_random_users_cannot_send_state_before_first_pl(self):
def test_random_users_cannot_send_state_before_first_pl(self) -> None:
"""
Check that, before the first PL lands, the creator is the only user
that can send a state event.
Expand All @@ -263,7 +263,7 @@ def test_random_users_cannot_send_state_before_first_pl(self):
auth_events,
)

def test_state_default_level(self):
def test_state_default_level(self) -> None:
"""
Check that users above the state_default level can send state and
those below cannot
Expand Down Expand Up @@ -298,7 +298,7 @@ def test_state_default_level(self):
auth_events,
)

def test_alias_event(self):
def test_alias_event(self) -> None:
"""Alias events have special behavior up through room version 6."""
creator = "@creator:example.com"
other = "@other:example.com"
Expand Down Expand Up @@ -333,7 +333,7 @@ def test_alias_event(self):
auth_events,
)

def test_msc2432_alias_event(self):
def test_msc2432_alias_event(self) -> None:
"""After MSC2432, alias events have no special behavior."""
creator = "@creator:example.com"
other = "@other:example.com"
Expand Down Expand Up @@ -366,7 +366,9 @@ def test_msc2432_alias_event(self):
)

@parameterized.expand([(RoomVersions.V1, True), (RoomVersions.V6, False)])
def test_notifications(self, room_version: RoomVersion, allow_modification: bool):
def test_notifications(
self, room_version: RoomVersion, allow_modification: bool
) -> None:
"""
Notifications power levels get checked due to MSC2209.
"""
Expand Down Expand Up @@ -395,7 +397,7 @@ def test_notifications(self, room_version: RoomVersion, allow_modification: bool
with self.assertRaises(AuthError):
event_auth.check_state_dependent_auth_rules(pl_event, auth_events)

def test_join_rules_public(self):
def test_join_rules_public(self) -> None:
"""
Test joining a public room.
"""
Expand Down Expand Up @@ -460,7 +462,7 @@ def test_join_rules_public(self):
auth_events.values(),
)

def test_join_rules_invite(self):
def test_join_rules_invite(self) -> None:
"""
Test joining an invite only room.
"""
Expand Down Expand Up @@ -835,7 +837,7 @@ def _power_levels_event(
)


def _alias_event(room_version: RoomVersion, sender: str, **kwargs) -> EventBase:
def _alias_event(room_version: RoomVersion, sender: str, **kwargs: Any) -> EventBase:
data = {
"room_id": TEST_ROOM_ID,
**_maybe_get_event_id_dict_for_room_version(room_version),
Expand Down
35 changes: 22 additions & 13 deletions tests/test_mau.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,17 @@

"""Tests REST events for /rooms paths."""

from typing import List
from typing import List, Optional

from twisted.test.proto_helpers import MemoryReactor

from synapse.api.constants import APP_SERVICE_REGISTRATION_TYPE, LoginType
from synapse.api.errors import Codes, HttpResponseException, SynapseError
from synapse.appservice import ApplicationService
from synapse.rest.client import register, sync
from synapse.server import HomeServer
from synapse.types import JsonDict
from synapse.util import Clock

from tests import unittest
from tests.unittest import override_config
Expand All @@ -30,7 +35,7 @@ class TestMauLimit(unittest.HomeserverTestCase):

servlets = [register.register_servlets, sync.register_servlets]

def default_config(self):
def default_config(self) -> JsonDict:
config = default_config("test")

config.update(
Expand All @@ -53,10 +58,12 @@ def default_config(self):

return config

def prepare(self, reactor, clock, homeserver):
def prepare(
self, reactor: MemoryReactor, clock: Clock, homeserver: HomeServer
) -> None:
self.store = homeserver.get_datastores().main

def test_simple_deny_mau(self):
def test_simple_deny_mau(self) -> None:
# Create and sync so that the MAU counts get updated
token1 = self.create_user("kermit1")
self.do_sync_for_user(token1)
Expand All @@ -75,7 +82,7 @@ def test_simple_deny_mau(self):
self.assertEqual(e.code, 403)
self.assertEqual(e.errcode, Codes.RESOURCE_LIMIT_EXCEEDED)

def test_as_ignores_mau(self):
def test_as_ignores_mau(self) -> None:
"""Test that application services can still create users when the MAU
limit has been reached. This only works when application service
user ip tracking is disabled.
Expand Down Expand Up @@ -113,7 +120,7 @@ def test_as_ignores_mau(self):

self.create_user("as_kermit4", token=as_token, appservice=True)

def test_allowed_after_a_month_mau(self):
def test_allowed_after_a_month_mau(self) -> None:
# Create and sync so that the MAU counts get updated
token1 = self.create_user("kermit1")
self.do_sync_for_user(token1)
Expand All @@ -132,7 +139,7 @@ def test_allowed_after_a_month_mau(self):
self.do_sync_for_user(token3)

@override_config({"mau_trial_days": 1})
def test_trial_delay(self):
def test_trial_delay(self) -> None:
# We should be able to register more than the limit initially
token1 = self.create_user("kermit1")
self.do_sync_for_user(token1)
Expand Down Expand Up @@ -165,7 +172,7 @@ def test_trial_delay(self):
self.assertEqual(e.errcode, Codes.RESOURCE_LIMIT_EXCEEDED)

@override_config({"mau_trial_days": 1})
def test_trial_users_cant_come_back(self):
def test_trial_users_cant_come_back(self) -> None:
self.hs.config.server.mau_trial_days = 1

# We should be able to register more than the limit initially
Expand Down Expand Up @@ -216,7 +223,7 @@ def test_trial_users_cant_come_back(self):
# max_mau_value should not matter
{"max_mau_value": 1, "limit_usage_by_mau": False, "mau_stats_only": True}
)
def test_tracked_but_not_limited(self):
def test_tracked_but_not_limited(self) -> None:
# Simply being able to create 2 users indicates that the
# limit was not reached.
token1 = self.create_user("kermit1")
Expand All @@ -236,10 +243,10 @@ def test_tracked_but_not_limited(self):
"mau_appservice_trial_days": {"SomeASID": 1, "AnotherASID": 2},
}
)
def test_as_trial_days(self):
def test_as_trial_days(self) -> None:
user_tokens: List[str] = []

def advance_time_and_sync():
def advance_time_and_sync() -> None:
self.reactor.advance(24 * 60 * 61)
for token in user_tokens:
self.do_sync_for_user(token)
Expand Down Expand Up @@ -300,7 +307,9 @@ def advance_time_and_sync():
},
)

def create_user(self, localpart, token=None, appservice=False):
def create_user(
self, localpart: str, token: Optional[str] = None, appservice: bool = False
) -> str:
request_data = {
"username": localpart,
"password": "monkey",
Expand All @@ -326,7 +335,7 @@ def create_user(self, localpart, token=None, appservice=False):

return access_token

def do_sync_for_user(self, token):
def do_sync_for_user(self, token: str) -> None:
channel = self.make_request("GET", "/sync", access_token=token)

if channel.code != 200:
Expand Down
2 changes: 1 addition & 1 deletion tests/test_rust.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@
class RustTestCase(unittest.TestCase):
"""Basic tests to ensure that we can call into Rust code."""

def test_basic(self):
def test_basic(self) -> None:
result = sum_as_string(1, 2)
self.assertEqual("3", result)
16 changes: 8 additions & 8 deletions tests/test_test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,25 +17,25 @@


class MockClockTestCase(unittest.TestCase):
def setUp(self):
def setUp(self) -> None:
self.clock = MockClock()

def test_advance_time(self):
def test_advance_time(self) -> None:
start_time = self.clock.time()

self.clock.advance_time(20)

self.assertEqual(20, self.clock.time() - start_time)

def test_later(self):
def test_later(self) -> None:
invoked = [0, 0]

def _cb0():
def _cb0() -> None:
invoked[0] = 1

self.clock.call_later(10, _cb0)

def _cb1():
def _cb1() -> None:
invoked[1] = 1

self.clock.call_later(20, _cb1)
Expand All @@ -51,15 +51,15 @@ def _cb1():

self.assertTrue(invoked[1])

def test_cancel_later(self):
def test_cancel_later(self) -> None:
invoked = [0, 0]

def _cb0():
def _cb0() -> None:
invoked[0] = 1

t0 = self.clock.call_later(10, _cb0)

def _cb1():
def _cb1() -> None:
invoked[1] = 1

self.clock.call_later(20, _cb1)
Expand Down
Loading

0 comments on commit 4eed7b2

Please sign in to comment.