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

Commit

Permalink
Add type hints to some tests files (#12371)
Browse files Browse the repository at this point in the history
  • Loading branch information
dklimpel authored Apr 5, 2022
1 parent 42d8710 commit d666fc0
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 42 deletions.
1 change: 1 addition & 0 deletions changelog.d/12371.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add type hints to tests files.
1 change: 0 additions & 1 deletion mypy.ini
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@ exclude = (?x)
|tests/storage/test_base.py
|tests/storage/test_roommember.py
|tests/test_metrics.py
|tests/test_phone_home.py
|tests/test_server.py
|tests/test_state.py
|tests/test_terms_auth.py
Expand Down
14 changes: 11 additions & 3 deletions tests/storage/test__base.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,18 @@
# limitations under the License.

import secrets
from typing import Any, Dict, Generator, List, Tuple

from twisted.test.proto_helpers import MemoryReactor

from synapse.server import HomeServer
from synapse.util import Clock

from tests import unittest


class UpsertManyTests(unittest.HomeserverTestCase):
def prepare(self, reactor, clock, hs):
def prepare(self, reactor: MemoryReactor, clock: Clock, hs: HomeServer) -> None:
self.storage = hs.get_datastores().main

self.table_name = "table_" + secrets.token_hex(6)
Expand All @@ -40,11 +46,13 @@ def prepare(self, reactor, clock, hs):
)
)

def _dump_to_tuple(self, res):
def _dump_to_tuple(
self, res: List[Dict[str, Any]]
) -> Generator[Tuple[int, str, str], None, None]:
for i in res:
yield (i["id"], i["username"], i["value"])

def test_upsert_many(self):
def test_upsert_many(self) -> None:
"""
Upsert_many will perform the upsert operation across a batch of data.
"""
Expand Down
47 changes: 25 additions & 22 deletions tests/storage/test_monthly_active_users.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,15 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from typing import Any, Dict, List
from unittest.mock import Mock

from twisted.internet import defer
from twisted.test.proto_helpers import MemoryReactor

from synapse.api.constants import UserTypes
from synapse.server import HomeServer
from synapse.util import Clock

from tests import unittest
from tests.test_utils import make_awaitable
Expand All @@ -24,15 +28,15 @@
FORTY_DAYS = 40 * 24 * 60 * 60


def gen_3pids(count):
def gen_3pids(count: int) -> List[Dict[str, Any]]:
"""Generate `count` threepids as a list."""
return [
{"medium": "email", "address": "user%i@matrix.org" % i} for i in range(count)
]


class MonthlyActiveUsersTestCase(unittest.HomeserverTestCase):
def default_config(self):
def default_config(self) -> Dict[str, Any]:
config = default_config("test")

config.update({"limit_usage_by_mau": True, "max_mau_value": 50})
Expand All @@ -44,10 +48,10 @@ def default_config(self):

return config

def prepare(self, reactor, clock, homeserver):
self.store = homeserver.get_datastores().main
def prepare(self, reactor: MemoryReactor, clock: Clock, hs: HomeServer) -> None:
self.store = hs.get_datastores().main
# Advance the clock a bit
reactor.advance(FORTY_DAYS)
self.reactor.advance(FORTY_DAYS)

@override_config({"max_mau_value": 3, "mau_limit_reserved_threepids": gen_3pids(3)})
def test_initialise_reserved_users(self):
Expand Down Expand Up @@ -245,17 +249,17 @@ def test_populate_monthly_users_is_guest(self):
)
self.get_success(d)

self.store.upsert_monthly_active_user = Mock(return_value=make_awaitable(None))
self.store.upsert_monthly_active_user = Mock(return_value=make_awaitable(None)) # type: ignore[assignment]

d = self.store.populate_monthly_active_users(user_id)
self.get_success(d)

self.store.upsert_monthly_active_user.assert_not_called()

def test_populate_monthly_users_should_update(self):
self.store.upsert_monthly_active_user = Mock(return_value=make_awaitable(None))
self.store.upsert_monthly_active_user = Mock(return_value=make_awaitable(None)) # type: ignore[assignment]

self.store.is_trial_user = Mock(return_value=defer.succeed(False))
self.store.is_trial_user = Mock(return_value=defer.succeed(False)) # type: ignore[assignment]

self.store.user_last_seen_monthly_active = Mock(
return_value=defer.succeed(None)
Expand All @@ -266,9 +270,9 @@ def test_populate_monthly_users_should_update(self):
self.store.upsert_monthly_active_user.assert_called_once()

def test_populate_monthly_users_should_not_update(self):
self.store.upsert_monthly_active_user = Mock(return_value=make_awaitable(None))
self.store.upsert_monthly_active_user = Mock(return_value=make_awaitable(None)) # type: ignore[assignment]

self.store.is_trial_user = Mock(return_value=defer.succeed(False))
self.store.is_trial_user = Mock(return_value=defer.succeed(False)) # type: ignore[assignment]
self.store.user_last_seen_monthly_active = Mock(
return_value=defer.succeed(self.hs.get_clock().time_msec())
)
Expand Down Expand Up @@ -324,16 +328,15 @@ def test_support_user_not_add_to_mau_limits(self):
count = self.get_success(self.store.get_monthly_active_count())
self.assertEqual(count, 0)

d = self.store.register_user(
user_id=support_user_id, password_hash=None, user_type=UserTypes.SUPPORT
self.get_success(
self.store.register_user(
user_id=support_user_id, password_hash=None, user_type=UserTypes.SUPPORT
)
)
self.get_success(d)

d = self.store.upsert_monthly_active_user(support_user_id)
self.get_success(d)
self.get_success(self.store.upsert_monthly_active_user(support_user_id))

d = self.store.get_monthly_active_count()
count = self.get_success(d)
count = self.get_success(self.store.get_monthly_active_count())
self.assertEqual(count, 0)

# Note that the max_mau_value setting should not matter.
Expand All @@ -352,7 +355,7 @@ def test_track_monthly_users_without_cap(self):

@override_config({"limit_usage_by_mau": False, "mau_stats_only": False})
def test_no_users_when_not_tracking(self):
self.store.upsert_monthly_active_user = Mock(return_value=make_awaitable(None))
self.store.upsert_monthly_active_user = Mock(return_value=make_awaitable(None)) # type: ignore[assignment]

self.get_success(self.store.populate_monthly_active_users("@user:sever"))

Expand Down Expand Up @@ -388,16 +391,16 @@ def test_get_monthly_active_count_by_service(self):
self.store.register_user(user_id=native_user1, password_hash=None)
)

count = self.get_success(self.store.get_monthly_active_count_by_service())
self.assertEqual(count, {})
count1 = self.get_success(self.store.get_monthly_active_count_by_service())
self.assertEqual(count1, {})

self.get_success(self.store.upsert_monthly_active_user(native_user1))
self.get_success(self.store.upsert_monthly_active_user(appservice1_user1))
self.get_success(self.store.upsert_monthly_active_user(appservice1_user2))
self.get_success(self.store.upsert_monthly_active_user(appservice2_user1))

count = self.get_success(self.store.get_monthly_active_count())
self.assertEqual(count, 1)
count2 = self.get_success(self.store.get_monthly_active_count())
self.assertEqual(count2, 1)

d = self.store.get_monthly_active_count_by_service()
result = self.get_success(d)
Expand Down
25 changes: 14 additions & 11 deletions tests/storage/test_roommember.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,14 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from twisted.test.proto_helpers import MemoryReactor

from synapse.api.constants import Membership
from synapse.rest.admin import register_servlets_for_client_rest_resource
from synapse.rest.client import login, room
from synapse.server import HomeServer
from synapse.types import UserID, create_requester
from synapse.util import Clock

from tests import unittest
from tests.server import TestHomeServer
Expand All @@ -31,7 +34,7 @@ class RoomMemberStoreTestCase(unittest.HomeserverTestCase):
room.register_servlets,
]

def prepare(self, reactor, clock, hs: TestHomeServer):
def prepare(self, reactor: MemoryReactor, clock: Clock, hs: TestHomeServer) -> None:

# We can't test the RoomMemberStore on its own without the other event
# storage logic
Expand All @@ -44,7 +47,7 @@ def prepare(self, reactor, clock, hs: TestHomeServer):
# User elsewhere on another host
self.u_charlie = UserID.from_string("@charlie:elsewhere")

def test_one_member(self):
def test_one_member(self) -> None:

# Alice creates the room, and is automatically joined
self.room = self.helper.create_room_as(self.u_alice, tok=self.t_alice)
Expand All @@ -57,7 +60,7 @@ def test_one_member(self):

self.assertEqual([self.room], [m.room_id for m in rooms_for_user])

def test_count_known_servers(self):
def test_count_known_servers(self) -> None:
"""
_count_known_servers will calculate how many servers are in a room.
"""
Expand All @@ -68,7 +71,7 @@ def test_count_known_servers(self):
servers = self.get_success(self.store._count_known_servers())
self.assertEqual(servers, 2)

def test_count_known_servers_stat_counter_disabled(self):
def test_count_known_servers_stat_counter_disabled(self) -> None:
"""
If enabled, the metrics for how many servers are known will be counted.
"""
Expand All @@ -85,7 +88,7 @@ def test_count_known_servers_stat_counter_disabled(self):
@unittest.override_config(
{"enable_metrics": True, "metrics_flags": {"known_servers": True}}
)
def test_count_known_servers_stat_counter_enabled(self):
def test_count_known_servers_stat_counter_enabled(self) -> None:
"""
If enabled, the metrics for how many servers are known will be counted.
"""
Expand All @@ -107,7 +110,7 @@ def test_count_known_servers_stat_counter_enabled(self):
# It now knows about Charlie's server.
self.assertEqual(self.store._known_servers_count, 2)

def test_get_joined_users_from_context(self):
def test_get_joined_users_from_context(self) -> None:
room = self.helper.create_room_as(self.u_alice, tok=self.t_alice)
bob_event = self.get_success(
event_injection.inject_member_event(
Expand Down Expand Up @@ -161,7 +164,7 @@ def test_get_joined_users_from_context(self):
)
self.assertEqual(users.keys(), {self.u_alice, self.u_bob})

def test__null_byte_in_display_name_properly_handled(self):
def test__null_byte_in_display_name_properly_handled(self) -> None:
room = self.helper.create_room_as(self.u_alice, tok=self.t_alice)

res = self.get_success(
Expand Down Expand Up @@ -211,11 +214,11 @@ def test__null_byte_in_display_name_properly_handled(self):


class CurrentStateMembershipUpdateTestCase(unittest.HomeserverTestCase):
def prepare(self, reactor, clock, homeserver):
self.store = homeserver.get_datastores().main
self.room_creator = homeserver.get_room_creation_handler()
def prepare(self, reactor: MemoryReactor, clock: Clock, hs: HomeServer) -> None:
self.store = hs.get_datastores().main
self.room_creator = hs.get_room_creation_handler()

def test_can_rerun_update(self):
def test_can_rerun_update(self) -> None:
# First make sure we have completed all updates.
self.wait_for_background_updates()

Expand Down
12 changes: 7 additions & 5 deletions tests/test_phone_home.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,24 @@
from unittest import mock

from synapse.app.phone_stats_home import phone_stats_home
from synapse.types import JsonDict

from tests.unittest import HomeserverTestCase


class PhoneHomeStatsTestCase(HomeserverTestCase):
def test_performance_frozen_clock(self):
def test_performance_frozen_clock(self) -> None:
"""
If time doesn't move, don't error out.
"""
past_stats = [
(self.hs.get_clock().time(), resource.getrusage(resource.RUSAGE_SELF))
]
stats = {}
stats: JsonDict = {}
self.get_success(phone_stats_home(self.hs, stats, past_stats))
self.assertEqual(stats["cpu_average"], 0)

def test_performance_100(self):
def test_performance_100(self) -> None:
"""
1 second of usage over 1 second is 100% CPU usage.
"""
Expand All @@ -43,7 +44,8 @@ def test_performance_100(self):
old_resource.ru_maxrss = real_res.ru_maxrss

past_stats = [(self.hs.get_clock().time(), old_resource)]
stats = {}
stats: JsonDict = {}
self.reactor.advance(1)
self.get_success(phone_stats_home(self.hs, stats, past_stats))
# `old_resource` has type `Mock` instead of `struct_rusage`
self.get_success(phone_stats_home(self.hs, stats, past_stats)) # type: ignore[arg-type]
self.assertApproximates(stats["cpu_average"], 100, tolerance=2.5)

0 comments on commit d666fc0

Please sign in to comment.