summaryrefslogtreecommitdiff
path: root/tests/addressbook
diff options
context:
space:
mode:
authorLucas Hoffmann <l-m-h@web.de>2017-01-31 11:34:33 +0100
committerLucas Hoffmann <l-m-h@web.de>2017-06-08 16:09:08 +0200
commit6affff711d357155eae49f595a0112aaff04ed8b (patch)
treec37bb248bc1fa89578d938ed27baca3889b11231 /tests/addressbook
parent3b935dff34da6bfed4d8643946a86086bdb78cba (diff)
Add tests for addressbook classes
Diffstat (limited to 'tests/addressbook')
-rw-r--r--tests/addressbook/__init__.py0
-rw-r--r--tests/addressbook/abook_test.py38
-rw-r--r--tests/addressbook/external_test.py55
3 files changed, 93 insertions, 0 deletions
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..13eb9b5a
--- /dev/null
+++ b/tests/addressbook/abook_test.py
@@ -0,0 +1,38 @@
+# 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 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
+ 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..1459348d
--- /dev/null
+++ b/tests/addressbook/external_test.py
@@ -0,0 +1,55 @@
+# 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_and_email_submatches(self):
+ abook = external.ExternalAddressbook(
+ 'foobar', self.regex.replace('name', 'xname').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, [])