diff options
author | Dylan Baker <baker.dylan.c@gmail.com> | 2017-06-09 13:06:51 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-06-09 13:06:51 -0700 |
commit | cc140e518be6f97ca7856d5121e4246c495af29b (patch) | |
tree | c265d94ba9bc426f80b5fa17cf5280b4ddba696e | |
parent | 449881837fd4ce9cbafe139b1f4246b7040a048c (diff) | |
parent | 46a0be7f7df62961cacdc952b4263b604a4ae983 (diff) |
Merge pull request #1063 from lucc/tests/abook
tests for addressbook classes
-rw-r--r-- | alot/addressbook/external.py | 2 | ||||
-rw-r--r-- | tests/addressbook/__init__.py | 0 | ||||
-rw-r--r-- | tests/addressbook/abook_test.py | 40 | ||||
-rw-r--r-- | tests/addressbook/external_test.py | 62 |
4 files changed, 103 insertions, 1 deletions
diff --git a/alot/addressbook/external.py b/alot/addressbook/external.py index 6a89dd15..9de86f36 100644 --- a/alot/addressbook/external.py +++ b/alot/addressbook/external.py @@ -68,7 +68,7 @@ class ExternalAddressbook(AddressBook): m = re.match(self.regex, l, self.reflags) if m: info = m.groupdict() - if 'email' and 'name' in info: + if 'email' in info and 'name' in info: email = info['email'].strip() name = info['name'] res.append((name, email)) diff --git a/tests/addressbook/__init__.py b/tests/addressbook/__init__.py new file mode 100644 index 00000000..e69de29b --- /dev/null +++ b/tests/addressbook/__init__.py diff --git a/tests/addressbook/abook_test.py b/tests/addressbook/abook_test.py new file mode 100644 index 00000000..32be2cbe --- /dev/null +++ b/tests/addressbook/abook_test.py @@ -0,0 +1,40 @@ +# 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 +from __future__ import absolute_import + +import os +import tempfile +import unittest + +from alot.addressbook import abook +from alot.settings.errors import ConfigError + + +class TestAbookAddressBook(unittest.TestCase): + + def test_abook_file_can_not_be_empty(self): + with self.assertRaises(ConfigError): + abook.AbookAddressBook("/dev/null") + + def test_get_contacts_lists_all_emails(self): + data = """ + [format] + version = unknown + program = alot-test-suite + [1] + name = me + email = me@example.com + [2] + name = you + email = you@other.domain, you@example.com + """ + with tempfile.NamedTemporaryFile(delete=False) as tmp: + tmp.write(data) + path = tmp.name + self.addCleanup(os.unlink, path) + addressbook = abook.AbookAddressBook(path) + actual = addressbook.get_contacts() + expected = [('me', 'me@example.com'), ('you', 'you@other.domain'), + ('you', 'you@example.com')] + self.assertListEqual(actual, expected) diff --git a/tests/addressbook/external_test.py b/tests/addressbook/external_test.py new file mode 100644 index 00000000..2211769e --- /dev/null +++ b/tests/addressbook/external_test.py @@ -0,0 +1,62 @@ +# 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 +from __future__ import absolute_import + +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_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, []) |