Add missing type hints in tests (#14879)

* FIx-up type hints in tests.logging.
* Add missing type hints to test_transactions.
This commit is contained in:
Patrick Cloke 2023-01-26 14:45:24 -05:00 committed by GitHub
parent 345576bc34
commit fc35e0673f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 75 additions and 42 deletions

View file

@ -14,24 +14,28 @@
import json
import logging
from io import BytesIO, StringIO
from typing import cast
from unittest.mock import Mock, patch
from twisted.web.http import HTTPChannel
from twisted.web.server import Request
from synapse.http.site import SynapseRequest
from synapse.logging._terse_json import JsonFormatter, TerseJsonFormatter
from synapse.logging.context import LoggingContext, LoggingContextFilter
from synapse.types import JsonDict
from tests.logging import LoggerCleanupMixin
from tests.server import FakeChannel
from tests.server import FakeChannel, get_clock
from tests.unittest import TestCase
class TerseJsonTestCase(LoggerCleanupMixin, TestCase):
def setUp(self):
def setUp(self) -> None:
self.output = StringIO()
self.reactor, _ = get_clock()
def get_log_line(self):
def get_log_line(self) -> JsonDict:
# One log message, with a single trailing newline.
data = self.output.getvalue()
logs = data.splitlines()
@ -39,7 +43,7 @@ class TerseJsonTestCase(LoggerCleanupMixin, TestCase):
self.assertEqual(data.count("\n"), 1)
return json.loads(logs[0])
def test_terse_json_output(self):
def test_terse_json_output(self) -> None:
"""
The Terse JSON formatter converts log messages to JSON.
"""
@ -61,7 +65,7 @@ class TerseJsonTestCase(LoggerCleanupMixin, TestCase):
self.assertCountEqual(log.keys(), expected_log_keys)
self.assertEqual(log["log"], "Hello there, wally!")
def test_extra_data(self):
def test_extra_data(self) -> None:
"""
Additional information can be included in the structured logging.
"""
@ -93,7 +97,7 @@ class TerseJsonTestCase(LoggerCleanupMixin, TestCase):
self.assertEqual(log["int"], 3)
self.assertIs(log["bool"], True)
def test_json_output(self):
def test_json_output(self) -> None:
"""
The Terse JSON formatter converts log messages to JSON.
"""
@ -114,7 +118,7 @@ class TerseJsonTestCase(LoggerCleanupMixin, TestCase):
self.assertCountEqual(log.keys(), expected_log_keys)
self.assertEqual(log["log"], "Hello there, wally!")
def test_with_context(self):
def test_with_context(self) -> None:
"""
The logging context should be added to the JSON response.
"""
@ -139,7 +143,7 @@ class TerseJsonTestCase(LoggerCleanupMixin, TestCase):
self.assertEqual(log["log"], "Hello there, wally!")
self.assertEqual(log["request"], "name")
def test_with_request_context(self):
def test_with_request_context(self) -> None:
"""
Information from the logging context request should be added to the JSON response.
"""
@ -154,11 +158,13 @@ class TerseJsonTestCase(LoggerCleanupMixin, TestCase):
site.server_version_string = "Server v1"
site.reactor = Mock()
site.experimental_cors_msc3886 = False
request = SynapseRequest(FakeChannel(site, None), site)
request = SynapseRequest(
cast(HTTPChannel, FakeChannel(site, self.reactor)), site
)
# Call requestReceived to finish instantiating the object.
request.content = BytesIO()
# Partially skip some of the internal processing of SynapseRequest.
request._started_processing = Mock()
# Partially skip some internal processing of SynapseRequest.
request._started_processing = Mock() # type: ignore[assignment]
request.request_metrics = Mock(spec=["name"])
with patch.object(Request, "render"):
request.requestReceived(b"POST", b"/_matrix/client/versions", b"1.1")
@ -200,7 +206,7 @@ class TerseJsonTestCase(LoggerCleanupMixin, TestCase):
self.assertEqual(log["protocol"], "1.1")
self.assertEqual(log["user_agent"], "")
def test_with_exception(self):
def test_with_exception(self) -> None:
"""
The logging exception type & value should be added to the JSON response.
"""