summaryrefslogtreecommitdiff
path: root/src/isodate/tests/test_datetime.py
blob: 3cdfe42fe258db46c2fda67ee748dbea8e80e5ee (plain)
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
##############################################################################
# Copyright 2009, Gerhard Weis
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
#  * Redistributions of source code must retain the above copyright notice,
#    this list of conditions and the following disclaimer.
#  * Redistributions in binary form must reproduce the above copyright notice,
#    this list of conditions and the following disclaimer in the documentation
#    and/or other materials provided with the distribution.
#  * Neither the name of the authors nor the names of its contributors
#    may be used to endorse or promote products derived from this software
#    without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT
##############################################################################
'''
Test cases for the isodatetime module.
'''
import unittest
from datetime import datetime

from isodate import parse_datetime, UTC, FixedOffset, datetime_isoformat
from isodate import ISO8601Error
from isodate import DATE_BAS_COMPLETE, TIME_BAS_MINUTE, TIME_BAS_COMPLETE
from isodate import DATE_EXT_COMPLETE, TIME_EXT_MINUTE, TIME_EXT_COMPLETE
from isodate import TZ_BAS, TZ_EXT, TZ_HOUR
from isodate import DATE_BAS_ORD_COMPLETE, DATE_EXT_ORD_COMPLETE
from isodate import DATE_BAS_WEEK_COMPLETE, DATE_EXT_WEEK_COMPLETE

# the following list contains tuples of ISO datetime strings and the expected
# result from the parse_datetime method. A result of None means an ISO8601Error
# is expected.
TEST_CASES = [('19850412T1015', datetime(1985, 4, 12, 10, 15),
               DATE_BAS_COMPLETE + 'T' + TIME_BAS_MINUTE,
               '19850412T1015'),
              ('1985-04-12T10:15', datetime(1985, 4, 12, 10, 15),
               DATE_EXT_COMPLETE + 'T' + TIME_EXT_MINUTE,
               '1985-04-12T10:15'),
              ('1985102T1015Z', datetime(1985, 4, 12, 10, 15, tzinfo=UTC),
               DATE_BAS_ORD_COMPLETE + 'T' + TIME_BAS_MINUTE + TZ_BAS,
               '1985102T1015Z'),
              ('1985-102T10:15Z', datetime(1985, 4, 12, 10, 15, tzinfo=UTC),
               DATE_EXT_ORD_COMPLETE + 'T' + TIME_EXT_MINUTE + TZ_EXT,
               '1985-102T10:15Z'),
              ('1985W155T1015+0400', datetime(1985, 4, 12, 10, 15,
                                              tzinfo=FixedOffset(4, 0,
                                                                 '+0400')),
               DATE_BAS_WEEK_COMPLETE + 'T' + TIME_BAS_MINUTE + TZ_BAS,
               '1985W155T1015+0400'),
              ('1985-W15-5T10:15+04', datetime(1985, 4, 12, 10, 15,
                                               tzinfo=FixedOffset(4, 0,
                                                                  '+0400'),),
               DATE_EXT_WEEK_COMPLETE + 'T' + TIME_EXT_MINUTE + TZ_HOUR,
               '1985-W15-5T10:15+04'),
              ('20110410T101225.123000Z',
               datetime(2011, 4, 10, 10, 12, 25, 123000, tzinfo=UTC),
               DATE_BAS_COMPLETE + 'T' + TIME_BAS_COMPLETE + ".%f" + TZ_BAS,
               '20110410T101225.123000Z'),
              ('2012-10-12T08:29:46.069178Z',
               datetime(2012, 10, 12, 8, 29, 46, 69178, tzinfo=UTC),
               DATE_EXT_COMPLETE + 'T' + TIME_EXT_COMPLETE + '.%f' + TZ_BAS,
               '2012-10-12T08:29:46.069178Z'),
              ('2012-10-12T08:29:46.691780Z',
               datetime(2012, 10, 12, 8, 29, 46, 691780, tzinfo=UTC),
               DATE_EXT_COMPLETE + 'T' + TIME_EXT_COMPLETE + '.%f' + TZ_BAS,
               '2012-10-12T08:29:46.691780Z'),
              ('2012-10-30T08:55:22.1234567Z',
               datetime(2012, 10, 30, 8, 55, 22, 123457, tzinfo=UTC),
               DATE_EXT_COMPLETE + 'T' + TIME_EXT_COMPLETE + '.%f' + TZ_BAS,
               '2012-10-30T08:55:22.123457Z'),
              ('2012-10-30T08:55:22.1234561Z',
               datetime(2012, 10, 30, 8, 55, 22, 123456, tzinfo=UTC),
               DATE_EXT_COMPLETE + 'T' + TIME_EXT_COMPLETE + '.%f' + TZ_BAS,
               '2012-10-30T08:55:22.123456Z'),
              ('2014-08-18 14:55:22.123456Z', None,
               DATE_EXT_COMPLETE + 'T' + TIME_EXT_COMPLETE + '.%f' + TZ_BAS,
               '2014-08-18T14:55:22.123456Z'),
              ]


def create_testcase(datetimestring, expectation, format, output):
    """
    Create a TestCase class for a specific test.

    This allows having a separate TestCase for each test tuple from the
    TEST_CASES list, so that a failed test won't stop other tests.
    """

    class TestDateTime(unittest.TestCase):
        '''
        A test case template to parse an ISO datetime string into a
        datetime object.
        '''

        def test_parse(self):
            '''
            Parse an ISO datetime string and compare it to the expected value.
            '''
            if expectation is None:
                self.assertRaises(ISO8601Error, parse_datetime, datetimestring)
            else:
                self.assertEqual(parse_datetime(datetimestring), expectation)

        def test_format(self):
            '''
            Take datetime object and create ISO string from it.
            This is the reverse test to test_parse.
            '''
            if expectation is None:
                self.assertRaises(AttributeError,
                                  datetime_isoformat, expectation, format)
            else:
                self.assertEqual(datetime_isoformat(expectation, format),
                                 output)

    return unittest.TestLoader().loadTestsFromTestCase(TestDateTime)


def test_suite():
    '''
    Construct a TestSuite instance for all test cases.
    '''
    suite = unittest.TestSuite()
    for datetimestring, expectation, format, output in TEST_CASES:
        suite.addTest(create_testcase(datetimestring, expectation,
                                      format, output))
    return suite


# load_tests Protocol
def load_tests(loader, tests, pattern):
    return test_suite()


if __name__ == '__main__':
    unittest.main(defaultTest='test_suite')