From: Menno Smits Date: Sun, 22 Sep 2013 22:30:51 +0000 (+0100) Subject: Renamed gm_search to gmail_seach and added unit tests X-Git-Tag: 0.11~3^2~28 X-Git-Url: http://git.khamidou.com/?a=commitdiff_plain;h=e00e8fa54eeb2d5a849f2d9069d60090bb9c1290;p=imapclient.git Renamed gm_search to gmail_seach and added unit tests --- diff --git a/imapclient/imapclient.py b/imapclient/imapclient.py index d72000e..d702675 100644 --- a/imapclient/imapclient.py +++ b/imapclient/imapclient.py @@ -573,21 +573,22 @@ class IMAPClient(object): 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: diff --git a/imapclient/test/test_search.py b/imapclient/test/test_search.py index 091d069..2440a6b 100644 --- a/imapclient/test/test_search.py +++ b/imapclient/test/test_search.py @@ -60,3 +60,56 @@ class TestSearch(IMAPClientTest): 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')