test_00_imaputil.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. # Copyright (C) 2012- Sebastian Spaeth & contributors
  2. #
  3. # This program is free software; you can redistribute it and/or modify
  4. # it under the terms of the GNU General Public License as published by
  5. # the Free Software Foundation; either version 2 of the License, or
  6. # (at your option) any later version.
  7. #
  8. # This program is distributed in the hope that it will be useful,
  9. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. # GNU General Public License for more details.
  12. #
  13. # You should have received a copy of the GNU General Public License
  14. # along with this program; if not, write to the Free Software
  15. # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  16. import unittest
  17. import logging
  18. from test.OLItest import OLITestLib
  19. from offlineimap import imaputil
  20. from offlineimap.ui import UI_LIST, setglobalui
  21. # Things need to be setup first, usually setup.py initializes everything.
  22. # but if e.g. called from command line, we take care of default values here:
  23. if not OLITestLib.cred_file:
  24. OLITestLib(cred_file='./test/credentials.conf', cmd='./offlineimap.py')
  25. def setUpModule():
  26. logging.info("Set Up test module %s" % __name__)
  27. OLITestLib.create_test_dir(suffix=__name__)
  28. def tearDownModule():
  29. logging.info("Tear Down test module")
  30. # comment out next line to keep testdir after test runs. TODO: make nicer
  31. OLITestLib.delete_test_dir()
  32. # Stuff that can be used
  33. # self.assertEqual(self.seq, range(10))
  34. # should raise an exception for an immutable sequence
  35. # self.assertRaises(TypeError, random.shuffle, (1,2,3))
  36. # self.assertTrue(element in self.seq)
  37. # self.assertFalse(element in self.seq)
  38. class TestInternalFunctions(unittest.TestCase):
  39. """While the other test files test OfflineImap as a program, these
  40. tests directly invoke internal helper functions to guarantee that
  41. they deliver results as expected"""
  42. @classmethod
  43. def setUpClass(cls):
  44. # This is run before all tests in this class
  45. config = OLITestLib.get_default_config()
  46. setglobalui(UI_LIST['quiet'](config))
  47. def test_01_imapsplit(self):
  48. """Test imaputil.imapsplit()"""
  49. res = imaputil.imapsplit('(\\HasNoChildren) "." "INBOX.Sent"')
  50. self.assertEqual(res, ['(\\HasNoChildren)', '"."', '"INBOX.Sent"'])
  51. res = imaputil.imapsplit('"mo\\" o" sdfsdf')
  52. self.assertEqual(res, ['"mo\\" o"', 'sdfsdf'])
  53. def test_02_flagsplit(self):
  54. """Test imaputil.flagsplit()"""
  55. res = imaputil.flagsplit('(\\Draft \\Deleted)')
  56. self.assertEqual(res, ['\\Draft', '\\Deleted'])
  57. res = imaputil.flagsplit('(FLAGS (\\Seen Old) UID 4807)')
  58. self.assertEqual(res, ['FLAGS', '(\\Seen Old)', 'UID', '4807'])
  59. def test_04_flags2hash(self):
  60. """Test imaputil.flags2hash()"""
  61. res = imaputil.flags2hash('(FLAGS (\\Seen Old) UID 4807)')
  62. self.assertEqual(res, {'FLAGS': '(\\Seen Old)', 'UID': '4807'})
  63. def test_05_flagsimap2maildir(self):
  64. """Test imaputil.flagsimap2maildir()"""
  65. res = imaputil.flagsimap2maildir('(\\Draft \\Deleted)')
  66. self.assertEqual(res, set('DT'))
  67. def test_06_flagsmaildir2imap(self):
  68. """Test imaputil.flagsmaildir2imap()"""
  69. res = imaputil.flagsmaildir2imap(set('DR'))
  70. self.assertEqual(res, '(\\Answered \\Draft)')
  71. # test all possible flags
  72. res = imaputil.flagsmaildir2imap(set('SRFTD'))
  73. self.assertEqual(res, '(\\Answered \\Deleted \\Draft \\Flagged \\Seen)')
  74. def test_07_uid_sequence(self):
  75. """Test imaputil.uid_sequence()"""
  76. res = imaputil.uid_sequence([1, 2, 3, 4, 5, 10, 12, 13])
  77. self.assertEqual(res, '1:5,10,12:13')