diff options
author | Lucas Hoffmann <lucc@posteo.de> | 2018-12-21 00:33:19 +0100 |
---|---|---|
committer | Lucas Hoffmann <lucc@posteo.de> | 2019-01-29 00:31:03 +0100 |
commit | 7f0a1b8cdd492917f84f90cade28cefa0a37c3e0 (patch) | |
tree | 84fc17675a16fba219d56c0c17b5c0183086e64a /tests/addressbook/test_external.py | |
parent | fd1348329b8697d9bb014e7d61560f3421d29e46 (diff) |
Rename test files
The two main reasons are
- to run `python3 -m unittest discover` without specifying a custom
`--pattern *_test.py`
- to include the test files automatically when generating the MANIFEST
file.
Diffstat (limited to 'tests/addressbook/test_external.py')
-rw-r--r-- | tests/addressbook/test_external.py | 68 |
1 files changed, 68 insertions, 0 deletions
diff --git a/tests/addressbook/test_external.py b/tests/addressbook/test_external.py new file mode 100644 index 00000000..ca76cc50 --- /dev/null +++ b/tests/addressbook/test_external.py @@ -0,0 +1,68 @@ +# Copyright (C) 2017 Lucas Hoffmann +# This file is released under the GNU GPL, version 3 or a later revision. +# For further details see the COPYING file +import unittest + +import mock + +from alot.addressbook import external + + +class TestExternalAddressbookGetContacts(unittest.TestCase): + + """Some test cases for + alot.addressbook.external.ExternalAddressbook.get_contacts""" + + regex = '(?P<name>.*)\t(?P<email>.*)' + + @staticmethod + def _patch_call_cmd(return_value): + return mock.patch('alot.addressbook.external.call_cmd', + mock.Mock(return_value=return_value)) + + def test_raises_if_external_command_exits_with_non_zero_status(self): + abook = external.ExternalAddressbook('foobar', '') + with self._patch_call_cmd(('', '', 42)): + with self.assertRaises(external.AddressbookError) as contextmgr: + abook.get_contacts() + expected = u'abook command "foobar" returned with return code 42' + self.assertEqual(contextmgr.exception.args[0], expected) + + def test_stderr_of_failing_command_is_part_of_exception_message(self): + stderr = 'some text printed on stderr of external command' + abook = external.ExternalAddressbook('foobar', '') + with self._patch_call_cmd(('', stderr, 42)): + with self.assertRaises(external.AddressbookError) as contextmgr: + abook.get_contacts() + self.assertIn(stderr, contextmgr.exception.args[0]) + + def test_returns_empty_list_when_command_returns_no_output(self): + abook = external.ExternalAddressbook('foobar', self.regex) + with self._patch_call_cmd(('', '', 0)) as call_cmd: + actual = abook.get_contacts() + self.assertListEqual(actual, []) + call_cmd.assert_called_once_with(['foobar']) + + def test_splits_results_from_provider_by_regex(self): + abook = external.ExternalAddressbook('foobar', self.regex) + with self._patch_call_cmd( + ('me\t<me@example.com>\nyou\t<you@other.domain>', '', 0)): + actual = abook.get_contacts() + expected = [('me', '<me@example.com>'), ('you', '<you@other.domain>')] + self.assertListEqual(actual, expected) + + def test_returns_empty_list_if_regex_has_no_name_submatches(self): + abook = external.ExternalAddressbook( + 'foobar', self.regex.replace('name', 'xname')) + with self._patch_call_cmd( + ('me\t<me@example.com>\nyou\t<you@other.domain>', '', 0)): + actual = abook.get_contacts() + self.assertListEqual(actual, []) + + def test_returns_empty_list_if_regex_has_no_email_submatches(self): + abook = external.ExternalAddressbook( + 'foobar', self.regex.replace('email', 'xemail')) + with self._patch_call_cmd( + ('me\t<me@example.com>\nyou\t<you@other.domain>', '', 0)): + actual = abook.get_contacts() + self.assertListEqual(actual, []) |