| 1 |
# Copyright (c) 2014, Menno Smits |
| 2 |
# Released subject to the New BSD License |
| 3 |
# Please see http://en.wikipedia.org/wiki/BSD_licenses |
| 4 |
|
| 5 |
import unittest |
| 6 |
from datetime import date, datetime |
| 7 |
from unittest.mock import patch |
| 8 |
|
| 9 |
from imapclient.datetime_util import ( |
| 10 |
datetime_to_INTERNALDATE, |
| 11 |
datetime_to_native, |
| 12 |
format_criteria_date, |
| 13 |
parse_to_datetime, |
| 14 |
) |
| 15 |
from imapclient.fixed_offset import FixedOffset |
| 16 |
|
| 17 |
|
| 18 |
class TestParsing(unittest.TestCase): |
| 19 |
def check_normalised_and_not(self, in_string, expected_datetime): |
| 20 |
self.assertEqual( |
| 21 |
parse_to_datetime(in_string), datetime_to_native(expected_datetime) |
| 22 |
) |
| 23 |
self.assertEqual( |
| 24 |
parse_to_datetime(in_string, normalise=False), expected_datetime |
| 25 |
) |
| 26 |
|
| 27 |
def test_rfc822_style(self): |
| 28 |
self.check_normalised_and_not( |
| 29 |
b"Sun, 24 Mar 2013 22:06:10 +0200", |
| 30 |
datetime(2013, 3, 24, 22, 6, 10, 0, FixedOffset(120)), |
| 31 |
) |
| 32 |
|
| 33 |
def test_internaldate_style(self): |
| 34 |
self.check_normalised_and_not( |
| 35 |
b" 9-Feb-2007 17:08:08 -0430", |
| 36 |
datetime(2007, 2, 9, 17, 8, 8, 0, FixedOffset(-4 * 60 - 30)), |
| 37 |
) |
| 38 |
self.check_normalised_and_not( |
| 39 |
b"19-Feb-2007 17:08:08 0400", |
| 40 |
datetime(2007, 2, 19, 17, 8, 8, 0, FixedOffset(4 * 60)), |
| 41 |
) |
| 42 |
|
| 43 |
def test_dots_for_time_separator(self): |
| 44 |
# As reported in issue #154. |
| 45 |
self.check_normalised_and_not( |
| 46 |
b"Sat, 8 May 2010 16.03.09 +0200", |
| 47 |
datetime(2010, 5, 8, 16, 3, 9, 0, FixedOffset(120)), |
| 48 |
) |
| 49 |
self.check_normalised_and_not( |
| 50 |
b"Tue, 18 May 2010 16.03.09 -0200", |
| 51 |
datetime(2010, 5, 18, 16, 3, 9, 0, FixedOffset(-120)), |
| 52 |
) |
| 53 |
self.check_normalised_and_not( |
| 54 |
b"Wednesday,18 August 2010 16.03.09 -0200", |
| 55 |
datetime(2010, 8, 18, 16, 3, 9, 0, FixedOffset(-120)), |
| 56 |
) |
| 57 |
|
| 58 |
def test_invalid(self): |
| 59 |
self.assertRaises(ValueError, parse_to_datetime, b"ABC") |
| 60 |
|
| 61 |
|
| 62 |
class TestDatetimeToINTERNALDATE(unittest.TestCase): |
| 63 |
def test_with_timezone(self): |
| 64 |
dt = datetime(2009, 1, 2, 3, 4, 5, 0, FixedOffset(2 * 60 + 30)) |
| 65 |
self.assertEqual(datetime_to_INTERNALDATE(dt), "02-Jan-2009 03:04:05 +0230") |
| 66 |
|
| 67 |
@patch("imapclient.datetime_util.FixedOffset.for_system") |
| 68 |
def test_without_timezone(self, for_system): |
| 69 |
dt = datetime(2009, 1, 2, 3, 4, 5, 0) |
| 70 |
for_system.return_value = FixedOffset(-5 * 60) |
| 71 |
|
| 72 |
self.assertEqual(datetime_to_INTERNALDATE(dt), "02-Jan-2009 03:04:05 -0500") |
| 73 |
|
| 74 |
|
| 75 |
class TestCriteriaDateFormatting(unittest.TestCase): |
| 76 |
def test_basic(self): |
| 77 |
self.assertEqual(format_criteria_date(date(1996, 2, 22)), b"22-Feb-1996") |
| 78 |
|
| 79 |
def test_single_digit_day(self): |
| 80 |
self.assertEqual(format_criteria_date(date(1996, 4, 4)), b"04-Apr-1996") |