Skip to content

Commit

Permalink
Fix Context initialization in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tulir committed Mar 16, 2019
1 parent 7c46bf4 commit e16182e
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 50 deletions.
62 changes: 19 additions & 43 deletions tests/commands/test_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@
from pytest_mock import MockFixture

import mautrix_telegram.commands.handler
from mautrix_telegram.commands.handler import (
CommandEvent, CommandHandler, CommandProcessor, HelpSection
)
from mautrix_telegram.commands.handler import (CommandEvent, CommandHandler, CommandProcessor,
HelpSection)
from mautrix_telegram.config import Config
from mautrix_telegram.context import Context
from mautrix_telegram.types import MatrixEventID, MatrixRoomID, MatrixUserID
Expand All @@ -25,13 +24,7 @@ def context(request: FixtureRequest) -> Context:
"""
# Config(path, registration_path, base_path)
config = getattr(request.cls, 'config', Config("", "", ""))
return Context(
Mock(), # az
Mock(), # db
config, # config
Mock(), # loop
Mock() # session_container
)
return Context(az=Mock(), config=config, loop=Mock(), session_container=Mock(), bot=Mock())


@pytest.fixture
Expand Down Expand Up @@ -97,15 +90,12 @@ def test_reply(
mock_az.intent.send_notice.assert_called_with(
MatrixRoomID("#mock_room:example.org"),
"**This** <i>was</i><br/><strong>all</strong>fun*!",
html=(
"<p><strong>This</strong> &lt;i&gt;was&lt;/i&gt;&lt;br/&gt;"
"&lt;strong&gt;all&lt;/strong&gt;fun*!</p>\n"
),
html="<p><strong>This</strong> &lt;i&gt;was&lt;/i&gt;&lt;br/&gt;"
"&lt;strong&gt;all&lt;/strong&gt;fun*!</p>\n"
)

def test_reply_with_cmdprefix(
self, command_processor: CommandProcessor, mocker: MockFixture
) -> None:
def test_reply_with_cmdprefix(self, command_processor: CommandProcessor, mocker: MockFixture
) -> None:
mocker.patch("mautrix_telegram.user.config", self.config)

evt = CommandEvent(
Expand All @@ -121,21 +111,17 @@ def test_reply_with_cmdprefix(

mock_az = command_processor.az

evt.reply(
"$cmdprefix+sp ....$cmdprefix+sp...$cmdprefix $cmdprefix",
allow_html=False,
render_markdown=False,
)
evt.reply("$cmdprefix+sp ....$cmdprefix+sp...$cmdprefix $cmdprefix", allow_html=False,
render_markdown=False)

mock_az.intent.send_notice.assert_called_with(
MatrixRoomID("#mock_room:example.org"),
"tg ....tg+sp...tg tg",
html=None,
)

def test_reply_with_cmdprefix_in_management_room(
self, command_processor: CommandProcessor, mocker: MockFixture
) -> None:
def test_reply_with_cmdprefix_in_management_room(self, command_processor: CommandProcessor,
mocker: MockFixture) -> None:
mocker.patch("mautrix_telegram.user.config", self.config)

evt = CommandEvent(
Expand Down Expand Up @@ -163,6 +149,7 @@ def test_reply_with_cmdprefix_in_management_room(
html="<p>....tg+sp...tg tg</p>\n",
)


class TestCommandHandler:
config = Config("", "", "")
config["bridge.permissions"] = {"*": "noperm"}
Expand Down Expand Up @@ -301,12 +288,8 @@ class TestCommandProcessor:
config["bridge.permissions"] = {"*": "relaybot"}

@pytest.mark.asyncio
async def test_handle(
self,
command_processor: CommandProcessor,
boolean2: Tuple[bool, bool],
mocker: MockFixture,
) -> None:
async def test_handle(self, command_processor: CommandProcessor, boolean2: Tuple[bool, bool],
mocker: MockFixture) -> None:
mocker.patch('mautrix_telegram.user.config', self.config)
mocker.patch(
'mautrix_telegram.commands.handler.command_handlers',
Expand All @@ -330,12 +313,8 @@ async def test_handle(
command_handlers["help"].mock.assert_called_once() # type: ignore

@pytest.mark.asyncio
async def test_handle_unknown_command(
self,
command_processor: CommandProcessor,
boolean2: Tuple[bool, bool],
mocker: MockFixture,
) -> None:
async def test_handle_unknown_command(self, command_processor: CommandProcessor,
boolean2: Tuple[bool, bool], mocker: MockFixture) -> None:
mocker.patch('mautrix_telegram.user.config', self.config)
mocker.patch(
'mautrix_telegram.commands.handler.command_handlers',
Expand All @@ -361,12 +340,9 @@ async def test_handle_unknown_command(
command_handlers["unknown-command"].mock.assert_called_once() # type: ignore

@pytest.mark.asyncio
async def test_handle_delegated_handler(
self,
command_processor: CommandProcessor,
boolean2: Tuple[bool, bool],
mocker: MockFixture,
) -> None:
async def test_handle_delegated_handler(self, command_processor: CommandProcessor,
boolean2: Tuple[bool, bool],
mocker: MockFixture) -> None:
mocker.patch('mautrix_telegram.user.config', self.config)
mocker.patch(
'mautrix_telegram.commands.handler.command_handlers',
Expand Down
11 changes: 4 additions & 7 deletions tests/utils/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,16 @@ def boolean(request: FixtureRequest) -> bool:

@pytest.fixture
def boolean1(boolean: bool) -> Tuple[bool]:
return (boolean,)
return boolean,


@pytest.fixture(params=[True, False])
def boolean2(request: FixtureRequest, boolean: bool) -> Tuple[bool, bool]:
return (boolean, request.param)
return boolean, request.param


@pytest.fixture(params=[True, False])
def boolean3(
request: FixtureRequest, boolean2: Tuple[bool, bool]
) -> Tuple[bool, bool, bool]:
return (boolean2[0], boolean2[1], request.param)

def boolean3(request: FixtureRequest, boolean2: Tuple[bool, bool]) -> Tuple[bool, bool, bool]:
return boolean2[0], boolean2[1], request.param

# …

0 comments on commit e16182e

Please sign in to comment.