import unittest

import imap_tools
from tests.utils import MailboxTestCase, TEST_MAILBOX_NAME_SET, get_test_mailbox
from imap_tools.errors import MailboxCopyError


class ActionTest(MailboxTestCase):
    base_test_msg_cnt = 6

    @classmethod
    def setUpClass(cls):
        # clear temp folders
        for test_mailbox_name in TEST_MAILBOX_NAME_SET:
            mailbox = get_test_mailbox(test_mailbox_name)
            mailbox.folder.set(mailbox.folder_test_temp1)
            for_del_1 = [msg.uid for msg in mailbox.fetch()]
            if for_del_1:
                mailbox.delete([msg.uid for msg in mailbox.fetch()])
            mailbox.folder.set(mailbox.folder_test_temp2)
            for_del_2 = [msg.uid for msg in mailbox.fetch()]
            if for_del_2:
                mailbox.delete(for_del_2)

    def test_action(self):
        for mailbox in self.mailbox_set.values():
            # FETCH
            mailbox.folder.set(mailbox.folder_test_base)
            self.assertEqual(len(list(mailbox.fetch())), self.base_test_msg_cnt)

            # COPY
            mailbox.folder.set(mailbox.folder_test_base)
            uid_set = [msg.uid for msg in mailbox.fetch()]
            mailbox.copy(uid_set, mailbox.folder_test_temp1)
            if mailbox.mailbox_name != 'YAHOO':
                # YAHOO:
                #   imaplib.IMAP4.error: UID command error: BAD [b'[TRYCREATE] UID COPY failed -
                #   No mailbox exists with name - "__nonexistent_folder__"']
                with self.assertRaises(MailboxCopyError):
                    mailbox.copy(uid_set, '__nonexistent_folder__')
            self.assertEqual(len(list(mailbox.fetch())), self.base_test_msg_cnt)
            mailbox.folder.set(mailbox.folder_test_temp1)
            self.assertEqual(len(list(mailbox.fetch())), self.base_test_msg_cnt)

            # MOVE
            mailbox.folder.set(mailbox.folder_test_temp1)
            mailbox.move([msg.uid for msg in mailbox.fetch()], mailbox.folder_test_temp2)
            self.assertEqual(len(list(mailbox.fetch())), 0)
            mailbox.folder.set(mailbox.folder_test_temp2)
            self.assertEqual(len(list(mailbox.fetch())), self.base_test_msg_cnt)

            # FLAG
            mailbox.folder.set(mailbox.folder_test_temp2)
            mailbox.flag([msg.uid for msg in mailbox.fetch()], imap_tools.MailMessageFlags.FLAGGED, True)
            self.assertTrue(all([imap_tools.MailMessageFlags.FLAGGED in msg.flags for msg in mailbox.fetch()]))

            # SEEN
            mailbox.folder.set(mailbox.folder_test_temp2)
            mailbox.seen([msg.uid for msg in mailbox.fetch()], False)
            self.assertTrue(all([imap_tools.MailMessageFlags.SEEN not in msg.flags
                                 for msg in mailbox.fetch(mark_seen=False)]))

            # DELETE
            mailbox.folder.set(mailbox.folder_test_temp2)
            mailbox.delete([msg.uid for msg in mailbox.fetch()])
            self.assertEqual(len(list(mailbox.fetch())), 0)


if __name__ == "__main__":
    unittest.main()

@@ -157,16 +157,65 @@
 yError%0A%0A
+FETCH_ARGS = dict(bulk=True, headers_only=True)%0A%0A
 %0Aclass A
@@ -562,32 +562,44 @@
 n mailbox.fetch(
+**FETCH_ARGS
 )%5D%0A            i
@@ -668,32 +668,44 @@
 n mailbox.fetch(
+**FETCH_ARGS
 )%5D)%0A            
@@ -800,32 +800,44 @@
 n mailbox.fetch(
+**FETCH_ARGS
 )%5D%0A            i
@@ -1090,32 +1090,44 @@
 t(mailbox.fetch(
+**FETCH_ARGS
 ))), self.base_t
@@ -1272,16 +1272,28 @@
 x.fetch(
+**FETCH_ARGS
 )%5D%0A     
@@ -1765,32 +1765,44 @@
 t(mailbox.fetch(
+**FETCH_ARGS
 ))), self.base_t
@@ -1916,32 +1916,44 @@
 t(mailbox.fetch(
+**FETCH_ARGS
 ))), self.base_t
@@ -2098,24 +2098,36 @@
 ilbox.fetch(
+**FETCH_ARGS
 )%5D, mailbox.
@@ -2189,32 +2189,44 @@
 t(mailbox.fetch(
+**FETCH_ARGS
 ))), 0)%0A        
@@ -2323,24 +2323,36 @@
 ilbox.fetch(
+**FETCH_ARGS
 ))), self.ba
@@ -2501,24 +2501,36 @@
 ilbox.fetch(
+**FETCH_ARGS
 )%5D, imap_too
@@ -2584,32 +2584,49 @@
 self.assertTrue(
+%0A                
 all(%5Bimap_tools.
@@ -2688,16 +2688,28 @@
 x.fetch(
+**FETCH_ARGS
 )%5D))%0A%0A  
@@ -2842,16 +2842,28 @@
 x.fetch(
+**FETCH_ARGS
 )%5D, Fals
@@ -3167,16 +3167,28 @@
 x.fetch(
+**FETCH_ARGS
 )%5D)%0A    
@@ -3235,16 +3235,28 @@
 x.fetch(
+**FETCH_ARGS
 ))), 0)%0A

increase testing speed