]> khamidou — GitHub mirrors - imapclient.git/commitdiff
Clean up Gmail label tests
authorMenno Smits <menno@freshfoo.com>
Mon, 5 Sep 2016 11:45:42 +0000 (23:45 +1200)
committerMenno Smits <menno@freshfoo.com>
Mon, 5 Sep 2016 12:14:45 +0000 (00:14 +1200)
Extracted to a new test file and reworked tests to test to a deeper
layer.

imapclient/test/test_imapclient.py
imapclient/test/test_store.py [new file with mode: 0644]

index 70c3dd2f0d101b1518f77aade1f5ab0515b60d30..71d3867ded3aef51fc38d6dcef5ab31c36207e50 100644 (file)
@@ -12,7 +12,6 @@ from datetime import datetime
 import six
 from mock import patch, sentinel, Mock
 
-from imapclient import DELETED, SEEN, ANSWERED, FLAGGED, DRAFT, RECENT
 from imapclient.fixed_offset import FixedOffset
 from .testable_imapclient import TestableIMAPClient as IMAPClient
 from .imapclient_test import IMAPClientTest
@@ -389,46 +388,6 @@ class TestTimeNormalisation(IMAPClientTest):
         check(False)
 
 
-class TestGmailLabels(IMAPClientTest):
-
-    def setUp(self):
-        super(TestGmailLabels, self).setUp()
-        patcher = patch.object(
-            self.client,
-            '_store',
-            autospec=True,
-            return_value=sentinel.label_set)
-        patcher.start()
-        self.addCleanup(patcher.stop)
-
-    def test_get(self):
-        with patch.object(self.client, 'fetch', autospec=True,
-                          return_value={123: {b'X-GM-LABELS': [b'foo', b'bar']},
-                                        444: {b'X-GM-LABELS': [b'foo']}}):
-            out = self.client.get_gmail_labels(sentinel.messages)
-            self.client.fetch.assert_called_with(sentinel.messages, [b'X-GM-LABELS'])
-            self.assertEqual(out, {123: [b'foo', b'bar'],
-                                   444: [b'foo']})
-
-    def test_add(self):
-        self.client.add_gmail_labels(sentinel.messages, 'f"o"o')
-        self.client._store.assert_called_with(
-            b'+X-GM-LABELS', sentinel.messages,
-            ['"f\\"o\\"o"'], b'X-GM-LABELS')
-
-    def test_remove(self):
-        self.client.remove_gmail_labels(sentinel.messages, ['q\\ux'])
-        self.client._store.assert_called_with(
-            b'-X-GM-LABELS', sentinel.messages,
-            ['"q\\\\ux"'], b'X-GM-LABELS')
-
-    def test_set(self):
-        self.client.set_gmail_labels(sentinel.messages, ['faz', 'baz'])
-        self.client._store.assert_called_with(
-            b'X-GM-LABELS', sentinel.messages,
-            ['"faz"', '"baz"'], b'X-GM-LABELS')
-
-
 class TestNamespace(IMAPClientTest):
 
     def set_return(self, value):
@@ -548,14 +507,6 @@ class TestId(IMAPClientTest):
         self.assertRaises(TypeError, self.client.id_, 'bananarama')
 
 
-class TestFlags(IMAPClientTest):
-
-    def test_flags_are_bytes(self):
-        for flag in DELETED, SEEN, ANSWERED, FLAGGED, DRAFT, RECENT:
-            if not isinstance(flag, six.binary_type):
-                self.fail("%r flag is not bytes" % flag)
-
-
 class TestRawCommand(IMAPClientTest):
 
     def setUp(self):
diff --git a/imapclient/test/test_store.py b/imapclient/test/test_store.py
new file mode 100644 (file)
index 0000000..760513b
--- /dev/null
@@ -0,0 +1,63 @@
+# Copyright (c) 2016, Menno Smits
+# Released subject to the New BSD License
+# Please see http://en.wikipedia.org/wiki/BSD_licenses
+
+from __future__ import unicode_literals
+
+import six
+from mock import patch, sentinel, Mock
+
+from imapclient import DELETED, SEEN, ANSWERED, FLAGGED, DRAFT, RECENT
+from .imapclient_test import IMAPClientTest
+
+
+class TestFlagsConsts(IMAPClientTest):
+
+    def test_flags_are_bytes(self):
+        for flag in DELETED, SEEN, ANSWERED, FLAGGED, DRAFT, RECENT:
+            if not isinstance(flag, six.binary_type):
+                self.fail("%r flag is not bytes" % flag)
+
+
+class TestGmailLabels(IMAPClientTest):
+
+    def setUp(self):
+        super(TestGmailLabels, self).setUp()
+        self.client._command_and_check = Mock()
+
+    def test_get(self):
+        with patch.object(self.client, 'fetch', autospec=True,
+                          return_value={123: {b'X-GM-LABELS': [b'foo', b'bar']},
+                                        444: {b'X-GM-LABELS': [b'foo']}}):
+            out = self.client.get_gmail_labels(sentinel.messages)
+            self.client.fetch.assert_called_with(sentinel.messages, [b'X-GM-LABELS'])
+            self.assertEqual(out, {123: [b'foo', b'bar'],
+                                   444: [b'foo']})
+
+    def test_set(self):
+        self.check(self.client.set_gmail_labels, b'X-GM-LABELS')
+
+    def test_add(self):
+        self.check(self.client.add_gmail_labels, b'+X-GM-LABELS')
+
+    def test_remove(self):
+        self.check(self.client.remove_gmail_labels, b'-X-GM-LABELS')
+
+    def check(self, meth, expected_store_command):
+        cc = self.client._command_and_check
+        cc.return_value = [
+            '11 (X-GM-LABELS (blah "f\\"o\\"o") UID 1)',
+            '22 (X-GM-LABELS ("f\\"o\\"o") UID 2)',
+            '11 (UID 1 FLAGS (dont))',
+            '22 (UID 2 FLAGS (care))',
+        ]
+        resp = meth([1, 2], 'f"o"o')
+        cc.assert_called_once_with(
+            'store', b"1,2",
+            expected_store_command,
+            '("f\\"o\\"o")',
+            uid=True)
+        self.assertEqual(resp, {
+            1: (b'blah', b'f"o"o'),
+            2: (b'f"o"o',),
+        })