return []
return [long(i) for i in data.split()]
+ def thread(self, algorithm='REFERENCES', criteria='ALL', charset='UTF-8'):
+ """Return a list of messages threads matching *criteria*.
+
+ Each thread is a list of messages ids.
+
+ See `RFC 5256 <https://tools.ietf.org/html/rfc5256>`_ for more details.
+ """
+ if not self.has_capability('THREAD=' + algorithm):
+ raise ValueError('server does not support %s threading algorithm'
+ % algorithm)
+
+ if not criteria:
+ raise ValueError('no criteria specified')
+
+ if isinstance(criteria, basestring):
+ criteria = (criteria,)
+ crit_list = ['(%s)' % c for c in criteria]
+
+ args = [algorithm]
+ if charset:
+ args.append(charset)
+ args.extend(crit_list)
+
+ data = self._command_and_check('thread', *args, uid=True)
+ return parse_response(data)
+
def sort(self, sort_criteria, criteria='ALL', charset='UTF-8'):
"""Return a list of message ids sorted by *sort_criteria* and
optionally filtered by *criteria*.
expected = [first_id, first_id - 1, first_id - 2]
self.assertListEqual(messages, expected)
+ def test_thread(self):
+ thread_algo = None
+ for cap in self.client.capabilities():
+ if cap.startswith('THREAD='):
+ thread_algo = cap.split('=')[-1]
+ break
+ if not thread_algo:
+ return self.skipTest("Server doesn't support THREAD")
+
+ msg_tmpl = 'Subject: %s\r\n\r\nBody'
+ subjects = ('a', 'b', 'c')
+ for subject in subjects:
+ msg = msg_tmpl % subject
+ self.client.append(self.base_folder, msg)
+
+ messages = self.client.thread()
+
+ self.assertEqual(len(messages), 3)
+ self.assertIsInstance(messages[0], tuple)
+ first_id = messages[0][0]
+ expected = ((first_id,), (first_id + 1,), (first_id + 2,))
+ self.assertTupleEqual(messages, expected)
+
def test_copy(self):
self.append_msg(SIMPLE_MESSAGE)
target_folder = self.add_prefix_to_folder('target')
self.assertEquals(self.client._cached_capabilities, ('FOO', 'BAR'))
+class TestThread(IMAPClientTest):
+
+ def test_thread_without_uid(self):
+ self.client._cached_capabilities = ('THREAD=REFERENCES',)
+ self.client.use_uid = False
+ self.client._imap.thread.return_value = ('OK', ['(1 2)(3)(4 5 6)'])
+
+ threads = self.client.thread()
+
+ self.assertSequenceEqual(threads, ((1, 2), (3,), (4, 5, 6)))
+
+ def test_thread_with_uid(self):
+ self.client._cached_capabilities = ('THREAD=REFERENCES',)
+ self.client.use_uid = True
+ self.client._imap.uid.return_value = ('OK', ['(1 2)(3)(4 5 6)'])
+
+ threads = self.client.thread()
+
+ self.assertSequenceEqual(threads, ((1, 2), (3,), (4, 5, 6)))
+
+ def test_no_support(self):
+ self.client._cached_capabilities = ('NOT-THREAD',)
+ self.assertRaises(ValueError, self.client.thread)
+
+ def test_no_support2(self):
+ self.client._cached_capabilities = ('THREAD=FOO',)
+ self.assertRaises(ValueError, self.client.thread)
+
+ def test_all_args_with_uid(self):
+ self.client._cached_capabilities = ('THREAD=FOO',)
+ self.client._imap.uid.return_value = ('OK', [])
+
+ self.client.thread(algorithm='FOO', criteria='STUFF', charset='ASCII')
+
+ self.client._imap.uid.assert_called_once_with('thread', 'FOO', 'ASCII', '(STUFF)')
+
+ def test_all_args_without_uid(self):
+ self.client.use_uid = False
+ self.client._cached_capabilities = ('THREAD=FOO',)
+ self.client._imap.thread.return_value = ('OK', [])
+
+ self.client.thread(algorithm='FOO', criteria='STUFF', charset='ASCII')
+
+ self.client._imap.thread.assert_called_once_with('FOO', 'ASCII','(STUFF)')
+
if __name__ == '__main__':
unittest.main()