1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
|
from builtins import object
import shutil
import os.path
import tempfile
import unittest
import configparser
from unittest import mock
import responses
from bugwarrior import config
from bugwarrior.data import BugwarriorData
class AbstractServiceTest(object):
""" Ensures that certain test methods are implemented for each service. """
def test_to_taskwarrior(self):
""" Test Service.to_taskwarrior(). """
raise NotImplementedError
def test_issues(self):
"""
Test Service.issues().
- When the API is accessed via requests, use the responses library to
mock requests.
- When the API is accessed via a third party library, substitute a fake
implementation class for it.
"""
raise NotImplementedError
class ConfigTest(unittest.TestCase):
"""
Creates config files, configures the environment, and cleans up afterwards.
"""
def setUp(self):
self.old_environ = os.environ.copy()
self.tempdir = tempfile.mkdtemp(prefix='bugwarrior')
# Create temporary config files.
self.taskrc = os.path.join(self.tempdir, '.taskrc')
self.lists_path = os.path.join(self.tempdir, 'lists')
os.mkdir(self.lists_path)
with open(self.taskrc, 'w+') as fout:
fout.write('data.location=%s\n' % self.lists_path)
# Configure environment.
os.environ['HOME'] = self.tempdir
os.environ.pop(config.BUGWARRIORRC, None)
os.environ.pop('TASKRC', None)
os.environ.pop('XDG_CONFIG_HOME', None)
os.environ.pop('XDG_CONFIG_DIRS', None)
def tearDown(self):
shutil.rmtree(self.tempdir, ignore_errors=True)
os.environ = self.old_environ
class ServiceTest(ConfigTest):
GENERAL_CONFIG = {
'annotation_length': 100,
'description_length': 100,
}
SERVICE_CONFIG = {
}
@classmethod
def setUpClass(cls):
cls.maxDiff = None
def get_mock_service(
self, service_class, section='unspecified',
config_overrides=None, general_overrides=None
):
options = {
'general': self.GENERAL_CONFIG.copy(),
section: self.SERVICE_CONFIG.copy(),
}
if config_overrides:
options[section].update(config_overrides)
if general_overrides:
options['general'].update(general_overrides)
def has_option(section, name):
try:
return options[section][name]
except KeyError:
return False
def get_option(section, name):
try:
return options[section][name]
except KeyError:
raise configparser.NoOptionError(section, name)
def get_int(section, name):
return int(get_option(section, name))
config = mock.Mock()
config.has_option = mock.Mock(side_effect=has_option)
config.get = mock.Mock(side_effect=get_option)
config.getint = mock.Mock(side_effect=get_int)
config.data = BugwarriorData(self.lists_path)
service_instance = service_class(config, 'general', section)
return service_instance
@staticmethod
def add_response(url, **kwargs):
responses.add(responses.GET, url, match_querystring=True, **kwargs)
|