test_00_imaputil.py 3.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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 offlineimap import imaputil
  19. from offlineimap.ui import UI_LIST, setglobalui
  20. from offlineimap.CustomConfig import CustomConfigParser
  21. from test.OLItest import OLITestLib
  22. # Things need to be setup first, usually setup.py initializes everything.
  23. # but if e.g. called from command line, we take care of default values here:
  24. if not OLITestLib.cred_file:
  25. OLITestLib(cred_file='./test/credentials.conf', cmd='./offlineimap.py')
  26. def setUpModule():
  27. logging.info("Set Up test module %s" % __name__)
  28. tdir = OLITestLib.create_test_dir(suffix=__name__)
  29. def tearDownModule():
  30. logging.info("Tear Down test module")
  31. # comment out next line to keep testdir after test runs. TODO: make nicer
  32. OLITestLib.delete_test_dir()
  33. #Stuff that can be used
  34. #self.assertEqual(self.seq, range(10))
  35. # should raise an exception for an immutable sequence
  36. #self.assertRaises(TypeError, random.shuffle, (1,2,3))
  37. #self.assertTrue(element in self.seq)
  38. #self.assertFalse(element in self.seq)
  39. class TestInternalFunctions(unittest.TestCase):
  40. """While the other test files test OfflineImap as a program, these
  41. tests directly invoke internal helper functions to guarantee that
  42. they deliver results as expected"""
  43. @classmethod
  44. def setUpClass(cls):
  45. #This is run before all tests in this class
  46. config= OLITestLib.get_default_config()
  47. setglobalui(UI_LIST['quiet'](config))
  48. def test_01_imapsplit(self):
  49. """Test imaputil.imapsplit()"""
  50. res = imaputil.imapsplit(b'(\\HasNoChildren) "." "INBOX.Sent"')
  51. self.assertEqual(res, [b'(\\HasNoChildren)', b'"."', b'"INBOX.Sent"'])
  52. res = imaputil.imapsplit(b'"mo\\" o" sdfsdf')
  53. self.assertEqual(res, [b'"mo\\" o"', b'sdfsdf'])
  54. def test_02_flagsplit(self):
  55. """Test imaputil.flagsplit()"""
  56. res = imaputil.flagsplit(b'(\\Draft \\Deleted)')
  57. self.assertEqual(res, [b'\\Draft', b'\\Deleted'])
  58. res = imaputil.flagsplit(b'(FLAGS (\\Seen Old) UID 4807)')
  59. self.assertEqual(res, [b'FLAGS', b'(\\Seen Old)', b'UID', b'4807'])
  60. def test_04_flags2hash(self):
  61. """Test imaputil.flags2hash()"""
  62. res = imaputil.flags2hash(b'(FLAGS (\\Seen Old) UID 4807)')
  63. self.assertEqual(res, {b'FLAGS': b'(\\Seen Old)', b'UID': b'4807'})
  64. def test_05_flagsimap2maildir(self):
  65. """Test imaputil.flagsimap2maildir()"""
  66. res = imaputil.flagsimap2maildir(b'(\\Draft \\Deleted)')
  67. self.assertEqual(res, set(b'DT'))
  68. def test_06_flagsmaildir2imap(self):
  69. """Test imaputil.flagsmaildir2imap()"""
  70. res = imaputil.flagsmaildir2imap(set(b'DR'))
  71. self.assertEqual(res, b'(\\Answered \\Draft)')
  72. # test all possible flags
  73. res = imaputil.flagsmaildir2imap(set(b'SRFTD'))
  74. self.assertEqual(res, b'(\\Answered \\Deleted \\Draft \\Flagged \\Seen)')
  75. def test_07_uid_sequence(self):
  76. """Test imaputil.uid_sequence()"""
  77. res = imaputil.uid_sequence([1,2,3,4,5,10,12,13])
  78. self.assertEqual(res, b'1:5,10,12:13')