File: test_datetime_util.py

package info (click to toggle)
python-imapclient 3.0.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 668 kB
  • sloc: python: 5,355; sh: 14; makefile: 11
file content (80 lines) | stat: -rw-r--r-- 2,812 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# Copyright (c) 2014, Menno Smits
# Released subject to the New BSD License
# Please see http://en.wikipedia.org/wiki/BSD_licenses

import unittest
from datetime import date, datetime
from unittest.mock import patch

from imapclient.datetime_util import (
    datetime_to_INTERNALDATE,
    datetime_to_native,
    format_criteria_date,
    parse_to_datetime,
)
from imapclient.fixed_offset import FixedOffset


class TestParsing(unittest.TestCase):
    def check_normalised_and_not(self, in_string, expected_datetime):
        self.assertEqual(
            parse_to_datetime(in_string), datetime_to_native(expected_datetime)
        )
        self.assertEqual(
            parse_to_datetime(in_string, normalise=False), expected_datetime
        )

    def test_rfc822_style(self):
        self.check_normalised_and_not(
            b"Sun, 24 Mar 2013 22:06:10 +0200",
            datetime(2013, 3, 24, 22, 6, 10, 0, FixedOffset(120)),
        )

    def test_internaldate_style(self):
        self.check_normalised_and_not(
            b" 9-Feb-2007 17:08:08 -0430",
            datetime(2007, 2, 9, 17, 8, 8, 0, FixedOffset(-4 * 60 - 30)),
        )
        self.check_normalised_and_not(
            b"19-Feb-2007 17:08:08 0400",
            datetime(2007, 2, 19, 17, 8, 8, 0, FixedOffset(4 * 60)),
        )

    def test_dots_for_time_separator(self):
        # As reported in issue #154.
        self.check_normalised_and_not(
            b"Sat, 8 May 2010 16.03.09 +0200",
            datetime(2010, 5, 8, 16, 3, 9, 0, FixedOffset(120)),
        )
        self.check_normalised_and_not(
            b"Tue, 18 May 2010 16.03.09 -0200",
            datetime(2010, 5, 18, 16, 3, 9, 0, FixedOffset(-120)),
        )
        self.check_normalised_and_not(
            b"Wednesday,18 August 2010 16.03.09 -0200",
            datetime(2010, 8, 18, 16, 3, 9, 0, FixedOffset(-120)),
        )

    def test_invalid(self):
        self.assertRaises(ValueError, parse_to_datetime, b"ABC")


class TestDatetimeToINTERNALDATE(unittest.TestCase):
    def test_with_timezone(self):
        dt = datetime(2009, 1, 2, 3, 4, 5, 0, FixedOffset(2 * 60 + 30))
        self.assertEqual(datetime_to_INTERNALDATE(dt), "02-Jan-2009 03:04:05 +0230")

    @patch("imapclient.datetime_util.FixedOffset.for_system")
    def test_without_timezone(self, for_system):
        dt = datetime(2009, 1, 2, 3, 4, 5, 0)
        for_system.return_value = FixedOffset(-5 * 60)

        self.assertEqual(datetime_to_INTERNALDATE(dt), "02-Jan-2009 03:04:05 -0500")


class TestCriteriaDateFormatting(unittest.TestCase):
    def test_basic(self):
        self.assertEqual(format_criteria_date(date(1996, 2, 22)), b"22-Feb-1996")

    def test_single_digit_day(self):
        self.assertEqual(format_criteria_date(date(1996, 4, 4)), b"04-Apr-1996")