return []
return [long(i) for i in data.split()]
- def gm_search(self, query, charset=None):
- """Do a search using Gmail's X-GM-RAW attribute.
+ def gmail_search(self, query, charset=None):
+ """Search using Gmail's X-GM-RAW attribute.
- *query* should be a valid Gmail search query string. Example values
- include::
+ *query* should be a valid Gmail search query string. For
+ example::
'has:attachment in:unread'
See https://developers.google.com/gmail/imap_extensions#extension_of_the_search_command_x-gm-raw
for more info.
- *charset* specifies the character set of the strings in the
- criteria. It defaults to US-ASCII.
+ *charset* specifies the character set used to encode the
+ search string. It defaults to US-ASCII.
+
"""
- # we use the literal feature to allow querying for non-ascii subjects
+ # the the query is sent as a literal to allow for 7-bit query strings
if charset:
query = query.encode(charset)
else:
self.assertRaisesRegex(IMAPClient.Error,
'bad karma',
self.client.search, 'FOO')
+
+
+class TestGmailSearch(IMAPClientTest):
+
+ def test_with_uid(self):
+ self.client.use_uid = True
+ self.client._imap.uid.return_value = ('OK', [b'1 2 44'])
+
+ result = self.client.gmail_search('FOO')
+
+ self.client._imap.uid.assert_called_once_with('SEARCH', 'X-GM-RAW')
+ self.assertEqual(self.client._imap.literal, b'FOO')
+ self.assertEqual(result, [1, 2, 44])
+
+ def test_without_uid(self):
+ self.client.use_uid = False
+ self.client._imap.search.return_value = ('OK', [b'1 2 44'])
+
+ result = self.client.gmail_search('FOO')
+
+ self.client._imap.search.assert_called_once_with(None, 'X-GM-RAW')
+ self.assertEqual(self.client._imap.literal, b'FOO')
+ self.assertEqual(result, [1, 2, 44])
+
+ def test_with_uid_with_charset(self):
+ self.client.use_uid = True
+ self.client._imap.uid.return_value = ('OK', [b'1 2 44'])
+
+ result = self.client.gmail_search('\u2620', 'UTF8')
+
+ self.client._imap.uid.assert_called_once_with(
+ 'SEARCH',
+ 'CHARSET', 'UTF8',
+ 'X-GM-RAW')
+ self.assertEqual(self.client._imap.literal, b'\xe2\x98\xa0')
+ self.assertEqual(result, [1, 2, 44])
+
+ def test_without_uid_with_charset(self):
+ self.client.use_uid = False
+ self.client._imap.search.return_value = ('OK', [b'1 2 44'])
+
+ result = self.client.gmail_search('\u2620', 'UTF8')
+
+ self.client._imap.search.assert_called_once_with('UTF8', 'X-GM-RAW')
+ self.assertEqual(self.client._imap.literal, b'\xe2\x98\xa0')
+ self.assertEqual(result, [1, 2, 44])
+
+ def test_error_from_server(self):
+ self.client._imap.uid.return_value = ('NO', [b'bad karma'])
+
+ self.assertRaisesRegex(IMAPClient.Error,
+ 'bad karma',
+ self.client.gmail_search, 'FOO')