Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactoring tests #752

Merged
merged 4 commits into from
Nov 23, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions tests/test_connector_facebook.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,15 +88,13 @@ async def test_facebook_message_handler_invalid(self):
mock_request.json = amock.CoroutineMock()
mock_request.json.return_value = req_ob

with OpsDroid() as opsdroid, \
amock.patch('opsdroid.connector.facebook._LOGGER.error') \
as logmock:
with OpsDroid() as opsdroid:
connector.opsdroid = opsdroid
connector.opsdroid.parse = amock.CoroutineMock()

response = await connector.facebook_message_handler(mock_request)
self.assertFalse(connector.opsdroid.parse.called)
self.assertTrue(logmock.called)
self.assertLogs('_LOGGER', 'error')
self.assertEqual(type(response), aiohttp.web.Response)
self.assertEqual(response.status, 200)

Expand Down
12 changes: 4 additions & 8 deletions tests/test_connector_github.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,8 @@ def test_init(self):

def test_missing_token(self):
"""Test that attempt to connect without info raises an error."""
with mock.patch('opsdroid.connector.github._LOGGER.error') \
as logmock:
ConnectorGitHub({})
self.assertTrue(logmock.called)
ConnectorGitHub({})
self.assertLogs('_LOGGER', 'error')


class TestConnectorGitHubAsync(asynctest.TestCase):
Expand Down Expand Up @@ -66,15 +64,13 @@ async def test_connect_failure(self):
result.status = 401

with OpsDroid() as opsdroid, \
amock.patch('aiohttp.ClientSession.get') as patched_request, \
amock.patch('opsdroid.connector.github._LOGGER.error',) \
as logmock:
amock.patch('aiohttp.ClientSession.get') as patched_request:

patched_request.return_value = asyncio.Future()
patched_request.return_value.set_result(result)

await self.connector.connect(opsdroid)
self.assertTrue(logmock.called)
self.assertLogs('_LOGGER', 'error')

async def test_disconnect(self):
self.assertEqual(await self.connector.disconnect(None), None)
Expand Down
30 changes: 10 additions & 20 deletions tests/test_connector_rocketchat.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,8 @@ def test_init(self):

def test_missing_token(self):
"""Test that attempt to connect without info raises an error."""
with mock.patch('opsdroid.connector.rocketchat._LOGGER.error') \
as logmock:
RocketChat({})
self.assertTrue(logmock.called)
RocketChat({})
self.assertLogs('_LOGGER', 'error')


class TestConnectorRocketChatAsync(asynctest.TestCase):
Expand Down Expand Up @@ -75,16 +73,14 @@ async def test_connect(self):
}

with OpsDroid() as opsdroid, \
amock.patch('aiohttp.ClientSession.get') as patched_request, \
amock.patch('opsdroid.connector.rocketchat._LOGGER.debug',) \
as logmock:
amock.patch('aiohttp.ClientSession.get') as patched_request:

patched_request.return_value = asyncio.Future()
patched_request.return_value.set_result(connect_response)

await self.connector.connect(opsdroid)

self.assertTrue(logmock.called)
self.assertLogs('_LOGGER', 'debug')
self.assertNotEqual(200, patched_request.status)
self.assertTrue(patched_request.called)

Expand All @@ -93,15 +89,13 @@ async def test_connect_failure(self):
result.status = 401

with OpsDroid() as opsdroid, \
amock.patch('aiohttp.ClientSession.get') as patched_request, \
amock.patch('opsdroid.connector.rocketchat._LOGGER.error',) \
as logmock:
amock.patch('aiohttp.ClientSession.get') as patched_request:

patched_request.return_value = asyncio.Future()
patched_request.return_value.set_result(result)

await self.connector.connect(opsdroid)
self.assertTrue(logmock.called)
self.assertLogs('_LOGGER', 'error')

async def test_get_message(self):
connector_group = RocketChat({
Expand Down Expand Up @@ -208,9 +202,7 @@ async def test_respond(self):
post_response.status = 200

with OpsDroid() as opsdroid, \
amock.patch('aiohttp.ClientSession.post') as patched_request, \
amock.patch('opsdroid.connector.rocketchat._LOGGER.debug') \
as logmock:
amock.patch('aiohttp.ClientSession.post') as patched_request:

self.assertTrue(opsdroid.__class__.instances)
test_message = Message(text="This is a test",
Expand All @@ -222,16 +214,14 @@ async def test_respond(self):
patched_request.return_value.set_result(post_response)
await test_message.respond("Response")
self.assertTrue(patched_request.called)
self.assertTrue(logmock.called)
self.assertLogs('_LOGGER', 'debug')

async def test_respond_failure(self):
post_response = amock.Mock()
post_response.status = 401

with OpsDroid() as opsdroid, \
amock.patch('aiohttp.ClientSession.post') as patched_request, \
amock.patch('opsdroid.connector.rocketchat._LOGGER.debug') \
as logmock:
amock.patch('aiohttp.ClientSession.post') as patched_request:

self.assertTrue(opsdroid.__class__.instances)
test_message = Message(text="This is a test",
Expand All @@ -242,4 +232,4 @@ async def test_respond_failure(self):
patched_request.return_value = asyncio.Future()
patched_request.return_value.set_result(post_response)
await test_message.respond("Response")
self.assertTrue(logmock.called)
self.assertLogs('_LOGGER', 'debug')
5 changes: 2 additions & 3 deletions tests/test_connector_slack.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,9 +209,8 @@ async def test_react_invalid_name(self):
import slacker
connector = ConnectorSlack({"api-token": "abc123"})
connector.slacker.reactions.post = amock.CoroutineMock(side_effect=slacker.Error('invalid_name'))
with amock.patch('opsdroid.connector.slack._LOGGER.warning',) as logmock:
await connector.react(Message("test", "user", "room", connector, {'ts': 0}), "😀")
self.assertTrue(logmock.called)
await connector.react(Message("test", "user", "room", connector, {'ts': 0}), "😀")
self.assertLogs('_LOGGER', 'warning')

async def test_react_unknown_error(self):
import slacker
Expand Down
7 changes: 3 additions & 4 deletions tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -307,10 +307,9 @@ async def test_parse_dialogflow(self):
self.assertEqual(len(tasks), 1)

# Once apiai parser stops working, remove this test!
with amock.patch('opsdroid.core._LOGGER.warning') as logmock:
opsdroid.config["parsers"] = [{"name": "apiai"}]
tasks = await opsdroid.parse(message)
self.assertTrue(logmock.called)
opsdroid.config["parsers"] = [{"name": "apiai"}]
tasks = await opsdroid.parse(message)
self.assertLogs('_LOGGER', 'warning')

# But leave this bit
for task in tasks:
Expand Down
5 changes: 2 additions & 3 deletions tests/test_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ def test_del_rw(self):
self.assertTrue(mock_remove.called)

def test_move_config(self):
with mock.patch('opsdroid.helper._LOGGER.info') as logmock, \
mock.patch('os.mkdir') as mock_mkdir, \
with mock.patch('os.mkdir') as mock_mkdir, \
mock.patch('os.path.isdir') as mock_isdir, \
mock.patch('os.remove') as mock_remove:

Expand All @@ -32,7 +31,7 @@ def test_move_config(self):
tempfile.gettempdir())

self.assertTrue(mock_mkdir.called)
self.assertTrue(logmock.called)
self.assertLogs('_LOGGER', 'info')
self.assertTrue(mock_remove.called)

def test_file_is_ipython_notebook(self):
Expand Down
22 changes: 9 additions & 13 deletions tests/test_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,10 +218,9 @@ def test_no_dep(self):
config = {}
config['no-dep'] = True

with mock.patch('opsdroid.loader._LOGGER.debug') as logmock:
loader._install_module_dependencies(config)
self.assertTrue(logmock.called)
self.assertEqual(loader._install_module_dependencies(config), None)
loader._install_module_dependencies(config)
self.assertLogs('_LOGGER', 'debug')
self.assertEqual(loader._install_module_dependencies(config), None)

with mock.patch.object(loader, '_install_module_dependencies') \
as nodep:
Expand Down Expand Up @@ -454,10 +453,9 @@ def test_install_default_remote_module(self):
"install_path":
os.path.join(self._tmp_dir, "test_default_remote_module"),
"branch": "master"}
with mock.patch('opsdroid.loader._LOGGER.debug') as logmock, \
mock.patch.object(loader, 'pip_install_deps') as mockdeps:
with mock.patch.object(loader, 'pip_install_deps') as mockdeps:
loader._install_module(config)
self.assertTrue(logmock.called)
self.assertLogs('_LOGGER', 'debug')
mockdeps.assert_called_with(
os.path.join(config["install_path"], "requirements.txt"))

Expand Down Expand Up @@ -515,9 +513,8 @@ def test_install_local_module_failure(self):
"install_path": os.path.join(
self._tmp_dir, "test_local_module_failure"),
"path": os.path.join(self._tmp_dir, "doesnotexist")}
with mock.patch('opsdroid.loader._LOGGER.error') as logmock:
loader._install_local_module(config)
self.assertTrue(logmock.called)
loader._install_local_module(config)
self.assertLogs('_LOGGER', 'error')

def test_update_existing_local_module(self):
opsdroid, loader = self.setup()
Expand All @@ -531,9 +528,8 @@ def test_update_existing_local_module(self):
os.makedirs(config["install_path"], exist_ok=True, mode=0o777)
os.makedirs(config["path"], exist_ok=True, mode=0o777)

with mock.patch('opsdroid.loader._LOGGER.debug') as logmock:
loader._update_module(config)
self.assertTrue(logmock.called)
loader._update_module(config)
self.assertLogs('_LOGGER', 'debug')

shutil.rmtree(base_path, onerror=del_rw)

Expand Down
5 changes: 2 additions & 3 deletions tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,9 +131,8 @@ def test_configure_default_logging(self):

def test_welcome_message(self):
config = {"welcome-message": True}
with mock.patch('opsdroid.__main__._LOGGER.info') as logmock:
opsdroid.welcome_message(config)
self.assertTrue(logmock.called)
opsdroid.welcome_message(config)
self.assertLogs('_LOGGER', 'info')

def test_welcome_exception(self):
config = {}
Expand Down
7 changes: 3 additions & 4 deletions tests/test_matchers.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,9 @@ async def test_match_apiai(self):
self.assertEqual(len(opsdroid.skills), 2)
self.assertEqual(opsdroid.skills[1].matchers[0]["dialogflow_intent"], intent)
self.assertTrue(asyncio.iscoroutinefunction(opsdroid.skills[1]))
with mock.patch('opsdroid.matchers._LOGGER.warning') as logmock:
decorator = matchers.match_apiai_intent(intent)
opsdroid.skills.append(decorator(await self.getMockSkill()))
self.assertTrue(logmock.called)
decorator = matchers.match_apiai_intent(intent)
opsdroid.skills.append(decorator(await self.getMockSkill()))
self.assertLogs('_LOGGER', 'warning')

async def test_match_dialogflow(self):
with OpsDroid() as opsdroid:
Expand Down
5 changes: 2 additions & 3 deletions tests/test_parser_always.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,5 @@ async def test_parse_always_raises(self):
message = Message("Hello world", "user",
"default", mock_connector)

with amock.patch('opsdroid.core._LOGGER.exception') as logmock:
await parse_always(opsdroid, message)
self.assertTrue(logmock.called)
await parse_always(opsdroid, message)
self.assertLogs('_LOGGER', 'exception')
5 changes: 2 additions & 3 deletions tests/test_parser_crontab.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,5 @@ async def test_parse_crontab_raises(self):
opsdroid.skills.append(match_crontab("* * * * *")(mock_skill))
self.assertEqual(len(opsdroid.skills), 1)

with amock.patch('opsdroid.core._LOGGER.exception') as logmock:
await parse_crontab(opsdroid)
self.assertTrue(logmock.called)
await parse_crontab(opsdroid)
self.assertLogs('_LOGGER', 'exception')
8 changes: 4 additions & 4 deletions tests/test_parser_dialogflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,10 +115,10 @@ async def test_parse_dialogflow_raises(self):
opsdroid, opsdroid.skills, message, opsdroid.config['parsers'][0])
self.assertEqual(mock_skill, skills[0]["skill"])

with amock.patch('opsdroid.core._LOGGER.exception') as logmock:
await opsdroid.run_skill(
skills[0]["skill"], skills[0]["config"], message)
self.assertTrue(logmock.called)
await opsdroid.run_skill(
skills[0]["skill"], skills[0]["config"], message
)
self.assertLogs('_LOGGER', 'exception')

async def test_parse_dialogflow_failure(self):
with OpsDroid() as opsdroid:
Expand Down
8 changes: 4 additions & 4 deletions tests/test_parser_luisai.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,10 +170,10 @@ async def test_parse_luisai_raises(self):
opsdroid, opsdroid.skills, message, opsdroid.config['parsers'][0])
self.assertEqual(mock_skill, skills[0]["skill"])

with amock.patch('opsdroid.core._LOGGER.exception') as logmock:
await opsdroid.run_skill(
skills[0]["skill"], skills[0]["config"], message)
self.assertTrue(logmock.called)
await opsdroid.run_skill(
skills[0]["skill"], skills[0]["config"], message
)
self.assertLogs('_LOGGER', 'exception')

async def test_parse_luisai_failure(self):
with OpsDroid() as opsdroid:
Expand Down
8 changes: 4 additions & 4 deletions tests/test_parser_rasanlu.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,10 +198,10 @@ async def test_parse_rasanlu_raises(self):
opsdroid, opsdroid.skills, message, opsdroid.config['parsers'][0])
self.assertEqual(mock_skill, skills[0]["skill"])

with amock.patch('opsdroid.core._LOGGER.exception') as logmock:
await opsdroid.run_skill(
skills[0]["skill"], skills[0]["config"], message)
self.assertTrue(logmock.called)
await opsdroid.run_skill(
skills[0]["skill"], skills[0]["config"], message
)
self.assertLogs('_LOGGER', 'exception')

async def test_parse_rasanlu_failure(self):
with OpsDroid() as opsdroid:
Expand Down
18 changes: 8 additions & 10 deletions tests/test_parser_recastai.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,10 +151,9 @@ async def test_parse_recastai_raises(self):
opsdroid, opsdroid.skills, message, opsdroid.config['parsers'][0])
self.assertEqual(mock_skill, skills[0]["skill"])

with amock.patch('opsdroid.core._LOGGER.exception') as logmock:
await opsdroid.run_skill(
skills[0]["skill"], skills[0]["config"], message)
self.assertTrue(logmock.called)
await opsdroid.run_skill(
skills[0]["skill"], skills[0]["config"], message)
self.assertLogs('_LOGGER', 'exception')

async def test_parse_recastai_failure(self):
with OpsDroid() as opsdroid:
Expand Down Expand Up @@ -215,12 +214,11 @@ async def test_parse_recastai_no_intent(self):
'version': '2.10.1',
'timestamp': '2017-11-15T07:32:42.641604+00:00',
'status': 200}}
with amock.patch(
'opsdroid.parsers.recastai._LOGGER.error') as logmock:
skills = await recastai.parse_recastai(
opsdroid, opsdroid.skills, message, opsdroid.config['parsers'][0])
self.assertTrue(logmock.called)
self.assertFalse(skills)

skills = await recastai.parse_recastai(
opsdroid, opsdroid.skills, message, opsdroid.config['parsers'][0])
self.assertLogs('_LOGGER', 'error')
self.assertFalse(skills)

async def test_parse_recastai_low_score(self):
with OpsDroid() as opsdroid:
Expand Down
8 changes: 3 additions & 5 deletions tests/test_parser_witai.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,11 +114,9 @@ async def test_parse_witai_raises(self):
opsdroid, opsdroid.skills, message, opsdroid.config['parsers'][0])
self.assertEqual(mock_skill, skills[0]['skill'])


with amock.patch('opsdroid.core._LOGGER.exception') as logmock:
await opsdroid.run_skill(
skills[0], skills[0]['skill'].config, message)
self.assertTrue(logmock.called)
await opsdroid.run_skill(
skills[0], skills[0]['skill'].config, message)
self.assertLogs('_LOGGER', 'exception')

async def test_parse_witai_failure(self):
with OpsDroid() as opsdroid:
Expand Down