Skip to content
This repository
Fetching contributors…

Cannot retrieve contributors at this time

file 189 lines (157 sloc) 6.642 kb
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 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
import unittest
import datetime

class Test_Utc(unittest.TestCase):
    def _makeOne(self):
        from ..iso8601 import Utc
        return Utc()

    def test_utcoffset(self):
        from ..iso8601 import ZERO
        inst = self._makeOne()
        result = inst.utcoffset(None)
        self.assertEqual(result, ZERO)

    def test_tzname(self):
        inst = self._makeOne()
        result = inst.tzname(None)
        self.assertEqual(result, "UTC")

    def test_dst(self):
        from ..iso8601 import ZERO
        inst = self._makeOne()
        result = inst.dst(None)
        self.assertEqual(result, ZERO)

    def test_picklability(self):
        from ..iso8601 import ZERO
        from ..compat import loads, dumps, HIGHEST_PROTOCOL
        inst = self._makeOne()
        for protocol in range(HIGHEST_PROTOCOL + 1):
            inst2 = loads(dumps(inst, protocol))
            self.assertEqual(inst2.utcoffset(None), ZERO)
            self.assertEqual(inst2.tzname(None), 'UTC')
            self.assertEqual(inst2.dst(None), ZERO)

class Test_FixedOffset(unittest.TestCase):
    def _makeOne(self):
        from ..iso8601 import FixedOffset
        return FixedOffset(1, 30, 'oneandahalf')

    def test_utcoffset(self):
        inst = self._makeOne()
        result = inst.utcoffset(None)
        self.assertEqual(result, datetime.timedelta(hours=1, minutes=30))

    def test_tzname(self):
        inst = self._makeOne()
        result = inst.tzname(None)
        self.assertEqual(result, 'oneandahalf')

    def test_dst(self):
        from ..iso8601 import ZERO
        inst = self._makeOne()
        result = inst.dst(None)
        self.assertEqual(result, ZERO)

    def test_picklability(self):
        from ..iso8601 import ZERO
        from ..compat import loads, dumps, HIGHEST_PROTOCOL
        inst = self._makeOne()
        for protocol in range(HIGHEST_PROTOCOL + 1):
            inst2 = loads(dumps(inst, protocol))
            self.assertEqual(inst2.utcoffset(None),
                            datetime.timedelta(hours=1, minutes=30))
            self.assertEqual(inst2.tzname(None), 'oneandahalf')
            self.assertEqual(inst2.dst(None), ZERO)

    def test___repr__(self):
        inst = self._makeOne()
        result = inst.__repr__()
        self.assertEqual(result, "<FixedOffset 'oneandahalf'>")

class Test_parse_timezone(unittest.TestCase):
    def _callFUT(self, tzstring, **kw):
        from ..iso8601 import parse_timezone
        return parse_timezone(tzstring, **kw)

    def test_default_Z(self):
        from ..iso8601 import UTC
        result = self._callFUT('Z')
        self.assertEqual(result, UTC)

    def test_Z_with_default_timezone(self):
        from ..iso8601 import UTC, FixedOffset
        tz = FixedOffset(1, 0, 'myname')
        result = self._callFUT('Z', default_timezone=tz)
        self.assertEqual(result, UTC)

    def test_default_None(self):
        from ..iso8601 import UTC
        result = self._callFUT(None)
        self.assertEqual(result, UTC)

    def test_None_with_default_timezone(self):
        from ..iso8601 import FixedOffset
        tz = FixedOffset(1, 0, 'myname')
        result = self._callFUT(None, default_timezone=tz)
        self.assertEqual(result, tz)

    def test_positive(self):
        tzstring = "+01:00"
        result = self._callFUT(tzstring)
        self.assertEqual(result.utcoffset(None),
                         datetime.timedelta(hours=1, minutes=0))

    def test_positive_without_colon(self):
        tzstring = "+0100"
        result = self._callFUT(tzstring)
        self.assertEqual(result.utcoffset(None),
                         datetime.timedelta(hours=1, minutes=0))

    def test_positive_without_minutes(self):
        tzstring = "+01"
        result = self._callFUT(tzstring)
        self.assertEqual(result.utcoffset(None),
                         datetime.timedelta(hours=1, minutes=0))

    def test_negative(self):
        tzstring = "-01:00"
        result = self._callFUT(tzstring)
        self.assertEqual(result.utcoffset(None),
                         datetime.timedelta(hours=-1, minutes=0))

    def test_negative_without_colon(self):
        tzstring = "-0100"
        result = self._callFUT(tzstring)
        self.assertEqual(result.utcoffset(None),
                         datetime.timedelta(hours=-1, minutes=0))

    def test_negative_without_minutes(self):
        tzstring = "-01"
        result = self._callFUT(tzstring)
        self.assertEqual(result.utcoffset(None),
                         datetime.timedelta(hours=-1, minutes=0))

class Test_parse_date(unittest.TestCase):
    def _callFUT(self, datestring):
        from ..iso8601 import parse_date
        return parse_date(datestring)

    def test_notastring(self):
        from ..iso8601 import ParseError
        self.assertRaises(ParseError, self._callFUT, None)

    def test_cantparse(self):
        from ..iso8601 import ParseError
        self.assertRaises(ParseError, self._callFUT, 'garbage')

    def test_outfrange(self):
        from ..iso8601 import ParseError
        self.assertRaises(ParseError, self._callFUT, '2013-05-32')

    def test_normal(self):
        from ..iso8601 import UTC
        result = self._callFUT("2007-01-25T12:00:00Z")
        self.assertEqual(result,
                         datetime.datetime(2007, 1, 25, 12, 0, tzinfo=UTC))

    def test_fraction(self):
        from ..iso8601 import UTC
        result = self._callFUT("2007-01-25T12:00:00.123Z")
        self.assertEqual(result,
                         datetime.datetime(2007, 1, 25, 12, 0, 0, 123000,
                                           tzinfo=UTC))

    def test_no_seconds(self):
        from ..iso8601 import UTC
        result = self._callFUT("2007-01-25T12:00Z")
        self.assertEqual(result,
                         datetime.datetime(2007, 1, 25, 12, 0, 0, 0,
                                           tzinfo=UTC))

    def test_no_minutes(self):
        from ..iso8601 import UTC
        result = self._callFUT("2007-01-25T12Z")
        self.assertEqual(result,
                         datetime.datetime(2007, 1, 25, 12, 0, 0, 0,
                                           tzinfo=UTC))

    def test_no_hours(self):
        from ..iso8601 import UTC
        result = self._callFUT("2007-01-25")
        self.assertEqual(result,
                         datetime.datetime(2007, 1, 25, 0, 0, 0, 0,
                                           tzinfo=UTC))

    def test_slash_separated_raises_ParseError(self):
        from ..iso8601 import ParseError
        self.assertRaises(ParseError, self._callFUT, "2007/01/25")
Something went wrong with that request. Please try again.