]> khamidou — GitHub mirrors - imapclient.git/commitdiff
Renamed gm_search to gmail_seach and added unit tests
authorMenno Smits <menno@freshfoo.com>
Sun, 22 Sep 2013 22:30:51 +0000 (23:30 +0100)
committerMenno Smits <menno@freshfoo.com>
Sun, 22 Sep 2013 22:30:51 +0000 (23:30 +0100)
imapclient/imapclient.py
imapclient/test/test_search.py

index d72000e2e0b1dfeb8b008892b00ff80e53fb8165..d702675be5bd3bc88c11913dcf87fcdf83c79522 100644 (file)
@@ -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:
index 091d0694a22900ed3b085dd944d0e9996a41a528..2440a6b9e89af1b6cffd42972ed7abd26a6080cb 100644 (file)
@@ -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')