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