Fix Context initialization in tests

This commit is contained in:
Tulir Asokan
2019-03-16 17:22:16 +02:00
parent 7c46bf4b9e
commit e16182ee6a
2 changed files with 23 additions and 50 deletions
+19 -43
View File
@@ -6,9 +6,8 @@ from _pytest.fixtures import FixtureRequest
from pytest_mock import MockFixture from pytest_mock import MockFixture
import mautrix_telegram.commands.handler import mautrix_telegram.commands.handler
from mautrix_telegram.commands.handler import ( from mautrix_telegram.commands.handler import (CommandEvent, CommandHandler, CommandProcessor,
CommandEvent, CommandHandler, CommandProcessor, HelpSection HelpSection)
)
from mautrix_telegram.config import Config from mautrix_telegram.config import Config
from mautrix_telegram.context import Context from mautrix_telegram.context import Context
from mautrix_telegram.types import MatrixEventID, MatrixRoomID, MatrixUserID from mautrix_telegram.types import MatrixEventID, MatrixRoomID, MatrixUserID
@@ -25,13 +24,7 @@ def context(request: FixtureRequest) -> Context:
""" """
# Config(path, registration_path, base_path) # Config(path, registration_path, base_path)
config = getattr(request.cls, 'config', Config("", "", "")) config = getattr(request.cls, 'config', Config("", "", ""))
return Context( return Context(az=Mock(), config=config, loop=Mock(), session_container=Mock(), bot=Mock())
Mock(), # az
Mock(), # db
config, # config
Mock(), # loop
Mock() # session_container
)
@pytest.fixture @pytest.fixture
@@ -97,15 +90,12 @@ class TestCommandEvent:
mock_az.intent.send_notice.assert_called_with( mock_az.intent.send_notice.assert_called_with(
MatrixRoomID("#mock_room:example.org"), MatrixRoomID("#mock_room:example.org"),
"**This** <i>was</i><br/><strong>all</strong>fun*!", "**This** <i>was</i><br/><strong>all</strong>fun*!",
html=( html="<p><strong>This</strong> &lt;i&gt;was&lt;/i&gt;&lt;br/&gt;"
"<p><strong>This</strong> &lt;i&gt;was&lt;/i&gt;&lt;br/&gt;" "&lt;strong&gt;all&lt;/strong&gt;fun*!</p>\n"
"&lt;strong&gt;all&lt;/strong&gt;fun*!</p>\n"
),
) )
def test_reply_with_cmdprefix( def test_reply_with_cmdprefix(self, command_processor: CommandProcessor, mocker: MockFixture
self, command_processor: CommandProcessor, mocker: MockFixture ) -> None:
) -> None:
mocker.patch("mautrix_telegram.user.config", self.config) mocker.patch("mautrix_telegram.user.config", self.config)
evt = CommandEvent( evt = CommandEvent(
@@ -121,11 +111,8 @@ class TestCommandEvent:
mock_az = command_processor.az mock_az = command_processor.az
evt.reply( evt.reply("$cmdprefix+sp ....$cmdprefix+sp...$cmdprefix $cmdprefix", allow_html=False,
"$cmdprefix+sp ....$cmdprefix+sp...$cmdprefix $cmdprefix", render_markdown=False)
allow_html=False,
render_markdown=False,
)
mock_az.intent.send_notice.assert_called_with( mock_az.intent.send_notice.assert_called_with(
MatrixRoomID("#mock_room:example.org"), MatrixRoomID("#mock_room:example.org"),
@@ -133,9 +120,8 @@ class TestCommandEvent:
html=None, html=None,
) )
def test_reply_with_cmdprefix_in_management_room( def test_reply_with_cmdprefix_in_management_room(self, command_processor: CommandProcessor,
self, command_processor: CommandProcessor, mocker: MockFixture mocker: MockFixture) -> None:
) -> None:
mocker.patch("mautrix_telegram.user.config", self.config) mocker.patch("mautrix_telegram.user.config", self.config)
evt = CommandEvent( evt = CommandEvent(
@@ -163,6 +149,7 @@ class TestCommandEvent:
html="<p>....tg+sp...tg tg</p>\n", html="<p>....tg+sp...tg tg</p>\n",
) )
class TestCommandHandler: class TestCommandHandler:
config = Config("", "", "") config = Config("", "", "")
config["bridge.permissions"] = {"*": "noperm"} config["bridge.permissions"] = {"*": "noperm"}
@@ -301,12 +288,8 @@ class TestCommandProcessor:
config["bridge.permissions"] = {"*": "relaybot"} config["bridge.permissions"] = {"*": "relaybot"}
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_handle( async def test_handle(self, command_processor: CommandProcessor, boolean2: Tuple[bool, bool],
self, mocker: MockFixture) -> None:
command_processor: CommandProcessor,
boolean2: Tuple[bool, bool],
mocker: MockFixture,
) -> None:
mocker.patch('mautrix_telegram.user.config', self.config) mocker.patch('mautrix_telegram.user.config', self.config)
mocker.patch( mocker.patch(
'mautrix_telegram.commands.handler.command_handlers', 'mautrix_telegram.commands.handler.command_handlers',
@@ -330,12 +313,8 @@ class TestCommandProcessor:
command_handlers["help"].mock.assert_called_once() # type: ignore command_handlers["help"].mock.assert_called_once() # type: ignore
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_handle_unknown_command( async def test_handle_unknown_command(self, command_processor: CommandProcessor,
self, boolean2: Tuple[bool, bool], mocker: MockFixture) -> None:
command_processor: CommandProcessor,
boolean2: Tuple[bool, bool],
mocker: MockFixture,
) -> None:
mocker.patch('mautrix_telegram.user.config', self.config) mocker.patch('mautrix_telegram.user.config', self.config)
mocker.patch( mocker.patch(
'mautrix_telegram.commands.handler.command_handlers', 'mautrix_telegram.commands.handler.command_handlers',
@@ -361,12 +340,9 @@ class TestCommandProcessor:
command_handlers["unknown-command"].mock.assert_called_once() # type: ignore command_handlers["unknown-command"].mock.assert_called_once() # type: ignore
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_handle_delegated_handler( async def test_handle_delegated_handler(self, command_processor: CommandProcessor,
self, boolean2: Tuple[bool, bool],
command_processor: CommandProcessor, mocker: MockFixture) -> None:
boolean2: Tuple[bool, bool],
mocker: MockFixture,
) -> None:
mocker.patch('mautrix_telegram.user.config', self.config) mocker.patch('mautrix_telegram.user.config', self.config)
mocker.patch( mocker.patch(
'mautrix_telegram.commands.handler.command_handlers', 'mautrix_telegram.commands.handler.command_handlers',
+4 -7
View File
@@ -12,19 +12,16 @@ def boolean(request: FixtureRequest) -> bool:
@pytest.fixture @pytest.fixture
def boolean1(boolean: bool) -> Tuple[bool]: def boolean1(boolean: bool) -> Tuple[bool]:
return (boolean,) return boolean,
@pytest.fixture(params=[True, False]) @pytest.fixture(params=[True, False])
def boolean2(request: FixtureRequest, boolean: bool) -> Tuple[bool, bool]: def boolean2(request: FixtureRequest, boolean: bool) -> Tuple[bool, bool]:
return (boolean, request.param) return boolean, request.param
@pytest.fixture(params=[True, False]) @pytest.fixture(params=[True, False])
def boolean3( def boolean3(request: FixtureRequest, boolean2: Tuple[bool, bool]) -> Tuple[bool, bool, bool]:
request: FixtureRequest, boolean2: Tuple[bool, bool] return boolean2[0], boolean2[1], request.param
) -> Tuple[bool, bool, bool]:
return (boolean2[0], boolean2[1], request.param)
# … # …