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

Commit e301ee6

Browse files
author
David Robertson
authored
Properly typecheck tests.app (#14984
1 parent f0cae26 commit e301ee6

File tree

5 files changed

+29
-15
lines changed

5 files changed

+29
-15
lines changed

changelog.d/14984.misc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Improve type hints.

mypy.ini

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ exclude = (?x)
3333
|synapse/storage/schema/
3434

3535
|tests/api/test_auth.py
36-
|tests/app/test_openid_listener.py
3736
|tests/appservice/test_scheduler.py
3837
|tests/federation/test_federation_catch_up.py
3938
|tests/federation/test_federation_sender.py
@@ -74,6 +73,9 @@ disallow_untyped_defs = False
7473
[mypy-tests.*]
7574
disallow_untyped_defs = False
7675

76+
[mypy-tests.app.*]
77+
disallow_untyped_defs = True
78+
7779
[mypy-tests.config.*]
7880
disallow_untyped_defs = True
7981

tests/app/test_homeserver_start.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020

2121
class HomeserverAppStartTestCase(ConfigFileTestCase):
22-
def test_wrong_start_caught(self):
22+
def test_wrong_start_caught(self) -> None:
2323
# Generate a config with a worker_app
2424
self.generate_config()
2525
# Add a blank line as otherwise the next addition ends up on a line with a comment

tests/app/test_openid_listener.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,26 +11,32 @@
1111
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
14+
from typing import List
1415
from unittest.mock import Mock, patch
1516

1617
from parameterized import parameterized
1718

19+
from twisted.test.proto_helpers import MemoryReactor
20+
1821
from synapse.app.generic_worker import GenericWorkerServer
1922
from synapse.app.homeserver import SynapseHomeServer
2023
from synapse.config.server import parse_listener_def
24+
from synapse.server import HomeServer
25+
from synapse.types import JsonDict
26+
from synapse.util import Clock
2127

2228
from tests.server import make_request
2329
from tests.unittest import HomeserverTestCase
2430

2531

2632
class FederationReaderOpenIDListenerTests(HomeserverTestCase):
27-
def make_homeserver(self, reactor, clock):
33+
def make_homeserver(self, reactor: MemoryReactor, clock: Clock) -> HomeServer:
2834
hs = self.setup_test_homeserver(
2935
federation_http_client=None, homeserver_to_use=GenericWorkerServer
3036
)
3137
return hs
3238

33-
def default_config(self):
39+
def default_config(self) -> JsonDict:
3440
conf = super().default_config()
3541
# we're using FederationReaderServer, which uses a SlavedStore, so we
3642
# have to tell the FederationHandler not to try to access stuff that is only
@@ -47,7 +53,7 @@ def default_config(self):
4753
(["openid"], "auth_fail"),
4854
]
4955
)
50-
def test_openid_listener(self, names, expectation):
56+
def test_openid_listener(self, names: List[str], expectation: str) -> None:
5157
"""
5258
Test different openid listener configurations.
5359
@@ -81,7 +87,7 @@ def test_openid_listener(self, names, expectation):
8187

8288
@patch("synapse.app.homeserver.KeyResource", new=Mock())
8389
class SynapseHomeserverOpenIDListenerTests(HomeserverTestCase):
84-
def make_homeserver(self, reactor, clock):
90+
def make_homeserver(self, reactor: MemoryReactor, clock: Clock) -> HomeServer:
8591
hs = self.setup_test_homeserver(
8692
federation_http_client=None, homeserver_to_use=SynapseHomeServer
8793
)
@@ -95,7 +101,7 @@ def make_homeserver(self, reactor, clock):
95101
(["openid"], "auth_fail"),
96102
]
97103
)
98-
def test_openid_listener(self, names, expectation):
104+
def test_openid_listener(self, names: List[str], expectation: str) -> None:
99105
"""
100106
Test different openid listener configurations.
101107

tests/app/test_phone_stats_home.py

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
import synapse
22
from synapse.app.phone_stats_home import start_phone_stats_home
33
from synapse.rest.client import login, room
4+
from synapse.server import HomeServer
5+
from synapse.util import Clock
46

57
from tests import unittest
8+
from tests.server import ThreadedMemoryReactorClock
69
from tests.unittest import HomeserverTestCase
710

811
FIVE_MINUTES_IN_SECONDS = 300
@@ -19,7 +22,7 @@ class PhoneHomeTestCase(HomeserverTestCase):
1922
# Override the retention time for the user_ips table because otherwise it
2023
# gets pruned too aggressively for our R30 test.
2124
@unittest.override_config({"user_ips_max_age": "365d"})
22-
def test_r30_minimum_usage(self):
25+
def test_r30_minimum_usage(self) -> None:
2326
"""
2427
Tests the minimum amount of interaction necessary for the R30 metric
2528
to consider a user 'retained'.
@@ -68,7 +71,7 @@ def test_r30_minimum_usage(self):
6871
r30_results = self.get_success(self.hs.get_datastores().main.count_r30_users())
6972
self.assertEqual(r30_results, {"all": 0})
7073

71-
def test_r30_minimum_usage_using_default_config(self):
74+
def test_r30_minimum_usage_using_default_config(self) -> None:
7275
"""
7376
Tests the minimum amount of interaction necessary for the R30 metric
7477
to consider a user 'retained'.
@@ -122,7 +125,7 @@ def test_r30_minimum_usage_using_default_config(self):
122125
r30_results = self.get_success(self.hs.get_datastores().main.count_r30_users())
123126
self.assertEqual(r30_results, {"all": 0})
124127

125-
def test_r30_user_must_be_retained_for_at_least_a_month(self):
128+
def test_r30_user_must_be_retained_for_at_least_a_month(self) -> None:
126129
"""
127130
Tests that a newly-registered user must be retained for a whole month
128131
before appearing in the R30 statistic, even if they post every day
@@ -164,12 +167,14 @@ class PhoneHomeR30V2TestCase(HomeserverTestCase):
164167
login.register_servlets,
165168
]
166169

167-
def _advance_to(self, desired_time_secs: float):
170+
def _advance_to(self, desired_time_secs: float) -> None:
168171
now = self.hs.get_clock().time()
169172
assert now < desired_time_secs
170173
self.reactor.advance(desired_time_secs - now)
171174

172-
def make_homeserver(self, reactor, clock):
175+
def make_homeserver(
176+
self, reactor: ThreadedMemoryReactorClock, clock: Clock
177+
) -> HomeServer:
173178
hs = super(PhoneHomeR30V2TestCase, self).make_homeserver(reactor, clock)
174179

175180
# We don't want our tests to actually report statistics, so check
@@ -181,7 +186,7 @@ def make_homeserver(self, reactor, clock):
181186
start_phone_stats_home(hs)
182187
return hs
183188

184-
def test_r30v2_minimum_usage(self):
189+
def test_r30v2_minimum_usage(self) -> None:
185190
"""
186191
Tests the minimum amount of interaction necessary for the R30v2 metric
187192
to consider a user 'retained'.
@@ -250,7 +255,7 @@ def test_r30v2_minimum_usage(self):
250255
r30_results, {"all": 0, "android": 0, "electron": 0, "ios": 0, "web": 0}
251256
)
252257

253-
def test_r30v2_user_must_be_retained_for_at_least_a_month(self):
258+
def test_r30v2_user_must_be_retained_for_at_least_a_month(self) -> None:
254259
"""
255260
Tests that a newly-registered user must be retained for a whole month
256261
before appearing in the R30v2 statistic, even if they post every day
@@ -316,7 +321,7 @@ def test_r30v2_user_must_be_retained_for_at_least_a_month(self):
316321
r30_results, {"all": 1, "android": 1, "electron": 0, "ios": 0, "web": 0}
317322
)
318323

319-
def test_r30v2_returning_dormant_users_not_counted(self):
324+
def test_r30v2_returning_dormant_users_not_counted(self) -> None:
320325
"""
321326
Tests that dormant users (users inactive for a long time) do not
322327
contribute to R30v2 when they return for just a single day.

0 commit comments

Comments
 (0)