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 29, 2018
1 parent b0c095c commit 43fe7b9
Show file tree
Hide file tree
Showing 5 changed files with 258 additions and 260 deletions.
35 changes: 15 additions & 20 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,13 @@ def test_ban_cmd(self, mockjson, mockreq):
{'id': '897', 'uri': 'coala/coala-bears'}
]
mockjson.loads.return_value = fake_room_data
testbot.assertCommand('!ban @nvzard',
'nvzard has been banned from: coala/coala, '
'coala/coala-bears')
self.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 +44,6 @@ def test_unban_cmd(self, mockjson, mockreq):
{'id': '897', 'uri': 'coala/coala-bears'}
]
mockjson.loads.return_value = fake_room_data
testbot.assertCommand('!unban @nvzard',
'nvzard has been unbanned from: coala/coala, '
'coala/coala-bears')
self.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
57 changes: 27 additions & 30 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 @@ -53,10 +50,10 @@ def test_pr_list(self):
self.mock_repo.merge_requests = [mock_github_mr]

# Non-existing repo
testbot.assertCommand(cmd_github.format('b'),
'Repository doesn\'t exist.')
testbot.assertCommand(cmd_gitlab.format('b'),
'Repository doesn\'t exist.')
self.assertCommand(cmd_github.format('b'),
'Repository doesn\'t exist.')
self.assertCommand(cmd_gitlab.format('b'),
'Repository doesn\'t exist.')

# PR is suitable
mock_github_mr.labels = ['process/approved', 'difficulty/newcomer']
Expand All @@ -68,49 +65,49 @@ def test_pr_list(self):
mock_repo_obj.head.commit.hexsha = '1'
mock_github_mr.base.sha = '1'
mock_gitlab_mr.base.sha = '1'
testbot.assertCommand(cmd_github.format('test'),
'PRs ready to be merged:\n '
'http://www.example.com/')
self.assertCommand(cmd_github.format('test'),
'PRs ready to be merged:\n '
'http://www.example.com/')
self.mock_repo.get_clone.return_value = (
mock_repo_obj, mkdtemp('mock_repo/'))
testbot.assertCommand(cmd_gitlab.format('test'),
'PRs ready to be merged:\n '
'http://www.example.com/')
self.assertCommand(cmd_gitlab.format('test'),
'PRs ready to be merged:\n '
'http://www.example.com/')

# PR is not suitable (wrong labels)
mock_github_mr.labels = ['process/wip', 'difficulty/newcomer']
mock_gitlab_mr.labels = ['process/wip', 'difficulty/newcomer']
self.mock_repo.get_clone.return_value = (
mock_repo_obj, mkdtemp('mock_repo/'))
testbot.assertCommand(cmd_github.format('test'),
'No merge-ready PRs!')
self.assertCommand(cmd_github.format('test'),
'No merge-ready PRs!')
self.mock_repo.get_clone.return_value = (
mock_repo_obj, mkdtemp('mock_repo/'))
testbot.assertCommand(cmd_gitlab.format('test'),
'No merge-ready PRs!')
self.assertCommand(cmd_gitlab.format('test'),
'No merge-ready PRs!')
mock_github_mr.labels = ['process/approved', 'difficulty/newcomer']
mock_gitlab_mr.labels = ['process/approved', 'difficulty/newcomer']

# PR is not suitable (needs rebase)
mock_repo_obj.head.commit.hexsha = '2'
self.mock_repo.get_clone.return_value = (
mock_repo_obj, mkdtemp('mock_repo/'))
testbot.assertCommand(cmd_github.format('test'),
'No merge-ready PRs!')
self.assertCommand(cmd_github.format('test'),
'No merge-ready PRs!')
self.mock_repo.get_clone.return_value = (
mock_repo_obj, mkdtemp('mock_repo/'))
testbot.assertCommand(cmd_gitlab.format('test'),
'No merge-ready PRs!')
self.assertCommand(cmd_gitlab.format('test'),
'No merge-ready PRs!')
mock_repo_obj.head.commit.hexsha = '1'

# PR is not suitable (already closed)
mock_github_mr.state = 'closed'
mock_gitlab_mr.state = 'closed'
self.mock_repo.get_clone.return_value = (
mock_repo_obj, mkdtemp('mock_repo/'))
testbot.assertCommand(cmd_github.format('test'),
'No merge-ready PRs!')
self.assertCommand(cmd_github.format('test'),
'No merge-ready PRs!')
self.mock_repo.get_clone.return_value = (
mock_repo_obj, mkdtemp('mock_repo/'))
testbot.assertCommand(cmd_gitlab.format('test'),
'No merge-ready PRs!')
self.assertCommand(cmd_gitlab.format('test'),
'No merge-ready PRs!')
21 changes: 0 additions & 21 deletions tests/helper.py

This file was deleted.

Loading

0 comments on commit 43fe7b9

Please sign in to comment.