/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
1
# Copyright (C) 2007, 2009, 2010, 2011, 2016 Canonical Ltd
1551.12.29 by Aaron Bentley
Copy and extend patch date formatting code, add patch-date parsing
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
4183.7.1 by Sabin Iacob
update FSF mailing address
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
1551.12.29 by Aaron Bentley
Copy and extend patch date formatting code, add patch-date parsing
16
5580.1.1 by Jelmer Vernooij
Convert bzrlib.timestamp.unpack_highres_date doc test to a unit test.
17
import random
18
import time
19
20
6622.1.34 by Jelmer Vernooij
Rename brzlib => breezy.
21
from breezy import (
1551.12.29 by Aaron Bentley
Copy and extend patch date formatting code, add patch-date parsing
22
    tests,
23
    timestamp,
24
    )
6622.1.34 by Jelmer Vernooij
Rename brzlib => breezy.
25
from breezy.osutils import local_time_offset
5580.1.1 by Jelmer Vernooij
Convert bzrlib.timestamp.unpack_highres_date doc test to a unit test.
26
1551.12.29 by Aaron Bentley
Copy and extend patch date formatting code, add patch-date parsing
27
28
class TestPatchHeader(tests.TestCase):
29
1551.12.36 by Aaron Bentley
Fix failing tests
30
    def test_format_patch_date(self):
2425.6.1 by Martin Pool
Fix formatting of timezones in bundles and merge directives.
31
        # epoch is always in utc
1551.12.29 by Aaron Bentley
Copy and extend patch date formatting code, add patch-date parsing
32
        self.assertEqual('1970-01-01 00:00:00 +0000',
7143.15.2 by Jelmer Vernooij
Run autopep8.
33
                         timestamp.format_patch_date(0))
34
        self.assertEqual('1970-01-01 00:00:00 +0000',
35
                         timestamp.format_patch_date(0, 5 * 3600))
36
        self.assertEqual('1970-01-01 00:00:00 +0000',
37
                         timestamp.format_patch_date(0, -5 * 3600))
2425.6.1 by Martin Pool
Fix formatting of timezones in bundles and merge directives.
38
        # regular timestamp with typical timezone
1551.12.29 by Aaron Bentley
Copy and extend patch date formatting code, add patch-date parsing
39
        self.assertEqual('2007-03-06 10:04:19 -0500',
7143.15.2 by Jelmer Vernooij
Run autopep8.
40
                         timestamp.format_patch_date(1173193459, -5 * 3600))
2425.6.1 by Martin Pool
Fix formatting of timezones in bundles and merge directives.
41
        # the timezone part is HHMM
42
        self.assertEqual('2007-03-06 09:34:19 -0530',
7143.15.2 by Jelmer Vernooij
Run autopep8.
43
                         timestamp.format_patch_date(1173193459, -5.5 * 3600))
2425.6.1 by Martin Pool
Fix formatting of timezones in bundles and merge directives.
44
        # timezones can be offset by single minutes (but no less)
45
        self.assertEqual('2007-03-06 15:05:19 +0001',
7143.15.2 by Jelmer Vernooij
Run autopep8.
46
                         timestamp.format_patch_date(1173193459, +1 * 60))
1551.12.29 by Aaron Bentley
Copy and extend patch date formatting code, add patch-date parsing
47
48
    def test_parse_patch_date(self):
7143.15.5 by Jelmer Vernooij
More PEP8 fixes.
49
        self.assertEqual(
50
            (0, 0),
51
            timestamp.parse_patch_date('1970-01-01 00:00:00 +0000'))
2425.6.1 by Martin Pool
Fix formatting of timezones in bundles and merge directives.
52
        # even though we don't emit pre-epoch dates, we can parse them
7143.15.5 by Jelmer Vernooij
More PEP8 fixes.
53
        self.assertEqual(
54
            (0, -5 * 3600),
55
            timestamp.parse_patch_date('1969-12-31 19:00:00 -0500'))
56
        self.assertEqual(
57
            (0, +5 * 3600),
58
            timestamp.parse_patch_date('1970-01-01 05:00:00 +0500'))
59
        self.assertEqual(
60
            (1173193459, -5 * 3600),
61
            timestamp.parse_patch_date('2007-03-06 10:04:19 -0500'))
2425.6.1 by Martin Pool
Fix formatting of timezones in bundles and merge directives.
62
        # offset of three minutes
7143.15.5 by Jelmer Vernooij
More PEP8 fixes.
63
        self.assertEqual(
64
            (1173193459, +3 * 60),
65
            timestamp.parse_patch_date('2007-03-06 15:07:19 +0003'))
6280.2.1 by Matt Giuca
bzrlib.timestamp: More robust handling of time stamp string. (LP: #892657)
66
        # No space between time and offset
7143.15.5 by Jelmer Vernooij
More PEP8 fixes.
67
        self.assertEqual(
68
            (1173193459, -5 * 3600),
69
            timestamp.parse_patch_date('2007-03-06 10:04:19-0500'))
6280.2.1 by Matt Giuca
bzrlib.timestamp: More robust handling of time stamp string. (LP: #892657)
70
        # Extra spacing
7143.15.5 by Jelmer Vernooij
More PEP8 fixes.
71
        self.assertEqual(
72
            (1173193459, -5 * 3600),
73
            timestamp.parse_patch_date('2007-03-06     10:04:19     -0500'))
6280.2.1 by Matt Giuca
bzrlib.timestamp: More robust handling of time stamp string. (LP: #892657)
74
75
    def test_parse_patch_date_bad(self):
76
        self.assertRaises(ValueError, timestamp.parse_patch_date,
7143.15.2 by Jelmer Vernooij
Run autopep8.
77
                          'NOT A TIME')
6280.2.1 by Matt Giuca
bzrlib.timestamp: More robust handling of time stamp string. (LP: #892657)
78
        # Extra data at end
79
        self.assertRaises(ValueError, timestamp.parse_patch_date,
7143.15.2 by Jelmer Vernooij
Run autopep8.
80
                          '2007-03-06 10:04:19 -0500x')
6280.2.1 by Matt Giuca
bzrlib.timestamp: More robust handling of time stamp string. (LP: #892657)
81
        # Missing day
82
        self.assertRaises(ValueError, timestamp.parse_patch_date,
7143.15.2 by Jelmer Vernooij
Run autopep8.
83
                          '2007-03 10:04:19 -0500')
6280.2.1 by Matt Giuca
bzrlib.timestamp: More robust handling of time stamp string. (LP: #892657)
84
        # Missing seconds
85
        self.assertRaises(ValueError, timestamp.parse_patch_date,
7143.15.2 by Jelmer Vernooij
Run autopep8.
86
                          '2007-03-06 10:04 -0500')
6280.2.1 by Matt Giuca
bzrlib.timestamp: More robust handling of time stamp string. (LP: #892657)
87
        # Missing offset
88
        self.assertRaises(ValueError, timestamp.parse_patch_date,
7143.15.2 by Jelmer Vernooij
Run autopep8.
89
                          '2007-03-06 10:04:19')
6280.2.1 by Matt Giuca
bzrlib.timestamp: More robust handling of time stamp string. (LP: #892657)
90
        # Missing plus or minus in offset
91
        self.assertRaises(ValueError, timestamp.parse_patch_date,
7143.15.2 by Jelmer Vernooij
Run autopep8.
92
                          '2007-03-06 10:04:19 0500')
6280.2.2 by Matt Giuca
bzrlib.timestamp: Now checks offset hour and minute to ensure they are within correct range.
93
        # Invalid hour in offset
94
        self.assertRaises(ValueError, timestamp.parse_patch_date,
7143.15.2 by Jelmer Vernooij
Run autopep8.
95
                          '2007-03-06 10:04:19 +2400')
6280.2.2 by Matt Giuca
bzrlib.timestamp: Now checks offset hour and minute to ensure they are within correct range.
96
        self.assertRaises(ValueError, timestamp.parse_patch_date,
7143.15.2 by Jelmer Vernooij
Run autopep8.
97
                          '2007-03-06 10:04:19 -2400')
6280.2.2 by Matt Giuca
bzrlib.timestamp: Now checks offset hour and minute to ensure they are within correct range.
98
        # Invalid minute in offset
99
        self.assertRaises(ValueError, timestamp.parse_patch_date,
7143.15.2 by Jelmer Vernooij
Run autopep8.
100
                          '2007-03-06 10:04:19 -0560')
6280.2.1 by Matt Giuca
bzrlib.timestamp: More robust handling of time stamp string. (LP: #892657)
101
        # Too many digits in offset
102
        self.assertRaises(ValueError, timestamp.parse_patch_date,
7143.15.2 by Jelmer Vernooij
Run autopep8.
103
                          '2007-03-06 10:04:19 79500')
6280.2.1 by Matt Giuca
bzrlib.timestamp: More robust handling of time stamp string. (LP: #892657)
104
        # Minus sign in middle of offset
105
        self.assertRaises(ValueError, timestamp.parse_patch_date,
7143.15.2 by Jelmer Vernooij
Run autopep8.
106
                          '2007-03-06 10:04:19 +05-5')
5580.1.1 by Jelmer Vernooij
Convert bzrlib.timestamp.unpack_highres_date doc test to a unit test.
107
108
109
class UnpackHighresDateTests(tests.TestCase):
110
111
    def test_unpack_highres_date(self):
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
112
        self.assertEqual(
5580.1.1 by Jelmer Vernooij
Convert bzrlib.timestamp.unpack_highres_date doc test to a unit test.
113
            (1120153132.3508501, -18000),
7143.15.5 by Jelmer Vernooij
More PEP8 fixes.
114
            timestamp.unpack_highres_date(
115
                'Thu 2005-06-30 12:38:52.350850105 -0500'))
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
116
        self.assertEqual(
5580.1.1 by Jelmer Vernooij
Convert bzrlib.timestamp.unpack_highres_date doc test to a unit test.
117
            (1120153132.3508501, 0),
7143.15.5 by Jelmer Vernooij
More PEP8 fixes.
118
            timestamp.unpack_highres_date(
119
                'Thu 2005-06-30 17:38:52.350850105 +0000'))
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
120
        self.assertEqual(
5580.1.1 by Jelmer Vernooij
Convert bzrlib.timestamp.unpack_highres_date doc test to a unit test.
121
            (1120153132.3508501, 7200),
7143.15.5 by Jelmer Vernooij
More PEP8 fixes.
122
            timestamp.unpack_highres_date(
123
                'Thu 2005-06-30 19:38:52.350850105 +0200'))
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
124
        self.assertEqual(
5580.1.1 by Jelmer Vernooij
Convert bzrlib.timestamp.unpack_highres_date doc test to a unit test.
125
            (1152428738.867522, 19800),
7143.15.5 by Jelmer Vernooij
More PEP8 fixes.
126
            timestamp.unpack_highres_date(
127
                'Sun 2006-07-09 12:35:38.867522001 +0530'))
5580.1.1 by Jelmer Vernooij
Convert bzrlib.timestamp.unpack_highres_date doc test to a unit test.
128
129
    def test_random(self):
130
        t = time.time()
131
        o = local_time_offset()
7143.15.2 by Jelmer Vernooij
Run autopep8.
132
        t2, o2 = timestamp.unpack_highres_date(
133
            timestamp.format_highres_date(t, o))
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
134
        self.assertEqual(t, t2)
135
        self.assertEqual(o, o2)
7143.15.2 by Jelmer Vernooij
Run autopep8.
136
        t -= 24 * 3600 * 365 * 2  # Start 2 years ago
137
        o = -12 * 3600
6651.2.2 by Martin
Apply 2to3 xrange fix and fix up with sixish range
138
        for count in range(500):
7143.15.2 by Jelmer Vernooij
Run autopep8.
139
            t += random.random() * 24 * 3600 * 30
7123.1.1 by Jelmer Vernooij
Guard against time_t overflowing.
140
            try:
7211.2.1 by Jelmer Vernooij
Include offset in check as well.
141
                time.gmtime(t + o)
7206.1.1 by Jelmer Vernooij
Handle overflow errors on Python 2.
142
            except (OverflowError, ValueError):
7123.1.1 by Jelmer Vernooij
Guard against time_t overflowing.
143
                # We've reached the maximum for time_t on this platform
144
                break
145
            if time.localtime(t).tm_year > 9998:
7143.15.5 by Jelmer Vernooij
More PEP8 fixes.
146
                # strptime doesn't always understand years with more than 4
147
                # digits.
7123.1.1 by Jelmer Vernooij
Guard against time_t overflowing.
148
                break
7143.15.2 by Jelmer Vernooij
Run autopep8.
149
            # Add 1 wrap around from [-12, 12]
150
            o = ((o / 3600 + 13) % 25 - 12) * 3600
5580.1.1 by Jelmer Vernooij
Convert bzrlib.timestamp.unpack_highres_date doc test to a unit test.
151
            date = timestamp.format_highres_date(t, o)
152
            t2, o2 = timestamp.unpack_highres_date(date)
7143.15.5 by Jelmer Vernooij
More PEP8 fixes.
153
            self.assertEqual(
154
                t, t2,
155
                'Failed on date %r, %s,%s diff:%s' % (date, t, o, t2 - t))
156
            self.assertEqual(
157
                o, o2,
158
                'Failed on date %r, %s,%s diff:%s' % (date, t, o, t2 - t))