Skip to content

Commit

Permalink
tests: Implement CoroboTestCase
Browse files Browse the repository at this point in the history
Shift from `tests.helper.plugin_testbot` to
`CoroboTestCase` for loading plugins in the
TestBot.

Closes #584
  • Loading branch information
nvzard committed Jul 30, 2018
1 parent b0c095c commit 2ce9425
Show file tree
Hide file tree
Showing 5 changed files with 143 additions and 135 deletions.
25 changes: 11 additions & 14 deletions tests/ban_test.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
import logging
import unittest
from unittest.mock import MagicMock, patch, PropertyMock

import plugins.ban

from tests.helper import plugin_testbot
from tests.corobo_test_case import CoroboTestCase


class TestBan(unittest.TestCase):
class TestBan(CoroboTestCase):

def setUp(self):
super().setUp((plugins.ban.Ban,))
self.ban = self.load_plugin('Ban')
self.ban.bot_config.ROOMS_TO_JOIN = (
'coala/coala', 'coala/coala-bears')
self.ban.bot_config.BOT_IDENTITY['token'] = 'mocked?'

@patch('plugins.ban.requests')
@patch('plugins.ban.json')
def test_ban_cmd(self, mockjson, mockreq):
ban, testbot = plugin_testbot(plugins.ban.Ban, logging.ERROR)
ban.activate()

ban.bot_config.ROOMS_TO_JOIN = ('coala/coala', 'coala/coala-bears')
ban.bot_config.BOT_IDENTITY['token'] = 'mocked?'
status_mock = MagicMock()
type(status_mock).status_code = PropertyMock(return_value=200)
mockreq.post.return_value = status_mock
Expand All @@ -27,18 +27,14 @@ def test_ban_cmd(self, mockjson, mockreq):
{'id': '897', 'uri': 'coala/coala-bears'}
]
mockjson.loads.return_value = fake_room_data
testbot = self
testbot.assertCommand('!ban @nvzard',
'nvzard has been banned from: coala/coala, '
'coala/coala-bears')

@patch('plugins.ban.requests')
@patch('plugins.ban.json')
def test_unban_cmd(self, mockjson, mockreq):
ban, testbot = plugin_testbot(plugins.ban.Ban, logging.ERROR)
ban.activate()

ban.bot_config.ROOMS_TO_JOIN = ('coala/coala', 'coala/coala-bears')
ban.bot_config.BOT_IDENTITY['token'] = 'mocked?'
status_mock = MagicMock()
type(status_mock).status_code = PropertyMock(return_value=200)
mockreq.delete.return_value = status_mock
Expand All @@ -49,6 +45,7 @@ def test_unban_cmd(self, mockjson, mockreq):
{'id': '897', 'uri': 'coala/coala-bears'}
]
mockjson.loads.return_value = fake_room_data
testbot = self
testbot.assertCommand('!unban @nvzard',
'nvzard has been unbanned from: coala/coala, '
'coala/coala-bears')
44 changes: 44 additions & 0 deletions tests/corobo_test_case.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
from errbot.backends.test import FullStackTest
from errbot.plugin_info import PluginInfo
from errbot.templating import add_plugin_templates_path
from pathlib import Path

import logging


class CoroboTestCase(FullStackTest):

def setUp(self, klasses: tuple):
super().setUp(loglevel=logging.ERROR,
extra_config={'BACKEND': 'text'})
self.klasses = {}
self.plug_files = {}
self.plugins = {}

for klass in klasses:
self.klasses[klass.__name__] = klass
self.load_plugin_templates(klass)

def load_plugin_templates(self, klass):
plug_filename = klass.__module__.split('.')[-1] + '.plug'
plug_file_path = (Path(__file__)
.parent / '..' / 'plugins' / plug_filename)
with plug_file_path.open() as plugfile:
plug_info = PluginInfo.load_file(plugfile, plug_file_path)
self.plug_files[klass.__name__] = plug_info
add_plugin_templates_path(plug_info)

def load_plugin(self, plugin_name: str, mock_dict=False):
"""Load plugin manually"""
klass = self.klasses[plugin_name]
plugin = klass(self.bot, plugin_name)
plugin.activate()
self.plugins[plugin_name] = plugin
self.bot.plugin_manager.plugins[plugin_name] = plugin
plug_file = self.plug_files[plugin_name]
self.bot.plugin_manager.plugin_infos[plug_file.name] = plug_file

if mock_dict:
self.inject_mocks(plugin_name=plugin_name, mock_dict=mock_dict)

return plugin
14 changes: 6 additions & 8 deletions tests/git_stats_test.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import logging
from tempfile import mkdtemp
from errbot import BotPlugin
import unittest
from unittest.mock import create_autospec

from IGitt.GitHub.GitHubMergeRequest import GitHubMergeRequest
Expand All @@ -14,12 +12,13 @@
import IGitt
import plugins.git_stats
import plugins.labhub
from tests.helper import plugin_testbot
from tests.corobo_test_case import CoroboTestCase


class TestGitStats(unittest.TestCase):
class TestGitStats(CoroboTestCase):

def setUp(self):
super().setUp((plugins.git_stats.GitStats,))
plugins.git_stats.github3 = create_autospec(github3)
self.mock_org = create_autospec(github3.orgs.Organization)
self.mock_gh = create_autospec(github3.GitHub)
Expand All @@ -30,12 +29,10 @@ def setUp(self):

self.plugin = plugins.git_stats.GitStats
self.plugin.__bases__ = (BotPlugin, )
self.git_stats = self.load_plugin('GitStats')
self.git_stats.REPOS = {'test': self.mock_repo}

def test_pr_list(self):
git_stats, testbot = plugin_testbot(self.plugin, logging.ERROR)
git_stats.activate()

git_stats.REPOS = {'test': self.mock_repo}
mock_github_mr = create_autospec(GitHubMergeRequest)
mock_gitlab_mr = create_autospec(GitLabMergeRequest)
mock_github_issue = create_autospec(GitHubIssue)
Expand All @@ -49,6 +46,7 @@ def test_pr_list(self):
mock_repo_obj = create_autospec(Repo)
cmd_github = '!mergable {}'
cmd_gitlab = '!mergable {}'
testbot = self

self.mock_repo.merge_requests = [mock_github_mr]

Expand Down
21 changes: 0 additions & 21 deletions tests/helper.py

This file was deleted.

Loading

0 comments on commit 2ce9425

Please sign in to comment.