~ed.so/duplicity/reuse-passphrase-for-signing-fix

« back to all changes in this revision

Viewing changes to testing/dup_timetest.py

  • Committer: bescoto
  • Date: 2002-10-29 01:49:46 UTC
  • Revision ID: vcs-imports@canonical.com-20021029014946-3m4rmm5plom7pl6q
Initial checkin

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
import sys
 
2
sys.path.insert(0, "../src")
 
3
import unittest, time, types
 
4
import globals, dup_time
 
5
 
 
6
class TimeTest(unittest.TestCase):
 
7
        def testConversion(self):
 
8
                """test timetostring and stringtotime"""
 
9
                dup_time.setcurtime()
 
10
                assert type(dup_time.curtime) is types.FloatType
 
11
                assert type(dup_time.curtimestr) is types.StringType
 
12
                assert (dup_time.cmp(int(dup_time.curtime), dup_time.curtimestr) == 0 or
 
13
                                dup_time.cmp(int(dup_time.curtime) + 1, dup_time.curtimestr) == 0)
 
14
                time.sleep(1.05)
 
15
                assert dup_time.cmp(time.time(), dup_time.curtime) == 1
 
16
                assert dup_time.cmp(dup_time.timetostring(time.time()), dup_time.curtimestr) == 1
 
17
 
 
18
        def testConversion_separator(self):
 
19
                """Same as testConversion, but change time Separator"""
 
20
                globals.time_separator = "_"
 
21
                self.testConversion()
 
22
                globals.time_separator = ":"
 
23
 
 
24
        def testCmp(self):
 
25
                """Test time comparisons"""
 
26
                cmp = dup_time.cmp
 
27
                assert cmp(1,2) == -1
 
28
                assert cmp(2,2) == 0
 
29
                assert cmp(5,1) == 1
 
30
                assert cmp("2001-09-01T21:49:04Z", "2001-08-01T21:49:04Z") == 1
 
31
                assert cmp("2001-09-01T04:49:04+03:23", "2001-09-01T21:49:04Z") == -1
 
32
                assert cmp("2001-09-01T12:00:00Z", "2001-09-01T04:00:00-08:00") == 0
 
33
                assert cmp("2001-09-01T12:00:00-08:00",
 
34
                                   "2001-09-01T12:00:00-07:00") == 1
 
35
 
 
36
        def testCmp_separator(self):
 
37
                """Like testCmp but with new separator"""
 
38
                globals.time_separator = "_"
 
39
                cmp = dup_time.cmp
 
40
                assert cmp(1,2) == -1
 
41
                assert cmp(2,2) == 0
 
42
                assert cmp(5,1) == 1
 
43
                assert cmp("2001-09-01T21_49_04Z", "2001-08-01T21_49_04Z") == 1
 
44
                assert cmp("2001-09-01T04_49_04+03_23", "2001-09-01T21_49_04Z") == -1
 
45
                assert cmp("2001-09-01T12_00_00Z", "2001-09-01T04_00_00-08_00") == 0
 
46
                assert cmp("2001-09-01T12_00_00-08_00",
 
47
                                   "2001-09-01T12_00_00-07_00") == 1
 
48
                globals.time_separator = ":"
 
49
 
 
50
        def testStringtotime(self):
 
51
                """Test converting string to time"""
 
52
                timesec = int(time.time())
 
53
                assert timesec == int(dup_time.stringtotime(dup_time.timetostring(timesec)))
 
54
                assert not dup_time.stringtotime("2001-18-83T03:03:03Z")
 
55
                assert not dup_time.stringtotime("2001-01-23L03:03:03L")
 
56
                assert not dup_time.stringtotime("2001_01_23T03:03:03Z")
 
57
 
 
58
        def testIntervals(self):
 
59
                """Test converting strings to intervals"""
 
60
                i2s = dup_time.intstringtoseconds
 
61
                for s in ["32", "", "d", "231I", "MM", "s", "-2h"]:
 
62
                        try: i2s(s)
 
63
                        except dup_time.TimeException: pass
 
64
                        else: assert 0, s
 
65
                assert i2s("7D") == 7*86400
 
66
                assert i2s("232s") == 232
 
67
                assert i2s("2M") == 2*30*86400
 
68
                assert i2s("400m") == 400*60
 
69
                assert i2s("1Y") == 365*86400
 
70
                assert i2s("30h") == 30*60*60
 
71
                assert i2s("3W") == 3*7*86400
 
72
 
 
73
        def testIntervalsComposite(self):
 
74
                """Like above, but allow composite intervals"""
 
75
                i2s = dup_time.intstringtoseconds
 
76
                assert i2s("7D2h") == 7*86400 + 2*3600
 
77
                assert i2s("2Y3s") == 2*365*86400 + 3
 
78
                assert i2s("1M2W4D2h5m20s") == (30*86400 + 2*7*86400 + 4*86400 +
 
79
                                                                                2*3600 + 5*60 + 20)
 
80
 
 
81
        def testPrettyIntervals(self):
 
82
                """Test printable interval conversion"""
 
83
                assert dup_time.inttopretty(3600) == "1 hour"
 
84
                assert dup_time.inttopretty(7220) == "2 hours 20 seconds"
 
85
                assert dup_time.inttopretty(0) == "0 seconds"
 
86
                assert dup_time.inttopretty(353) == "5 minutes 53 seconds"
 
87
                assert dup_time.inttopretty(3661) == "1 hour 1 minute 1 second"
 
88
                assert dup_time.inttopretty(353.234234) == "5 minutes 53.23 seconds"
 
89
 
 
90
        def testGenericString(self):
 
91
                """Test genstrtotime, conversion of arbitrary string to time"""
 
92
                g2t = dup_time.genstrtotime
 
93
                assert g2t('now', 1000) == 1000
 
94
                assert g2t('2h3s', 10000) == 10000 - 2*3600 - 3
 
95
                assert g2t('2001-09-01T21:49:04Z') == \
 
96
                           dup_time.stringtotime('2001-09-01T21:49:04Z')
 
97
                assert g2t('2002-04-26T04:22:01') == \
 
98
                           dup_time.stringtotime('2002-04-26T04:22:01' + dup_time.gettzd())
 
99
                t = dup_time.stringtotime('2001-05-12T00:00:00' + dup_time.gettzd())
 
100
                assert g2t('2001-05-12') == t
 
101
                assert g2t('2001/05/12') == t
 
102
                assert g2t('5/12/2001') == t
 
103
                assert g2t('123456') == 123456
 
104
 
 
105
        def testGenericStringErrors(self):
 
106
                """Test genstrtotime on some bad strings"""
 
107
                g2t = dup_time.genstrtotime
 
108
                self.assertRaises(dup_time.TimeException, g2t, "hello")
 
109
                self.assertRaises(dup_time.TimeException, g2t, "")
 
110
                self.assertRaises(dup_time.TimeException, g2t, "3q")
 
111
 
 
112
        def testSleeping(self):
 
113
                """Test sleep and sleep ratio"""
 
114
                sleep_ratio = 0.5
 
115
                time1 = time.time()
 
116
                dup_time.sleep(0) # set initial time
 
117
                time.sleep(1)
 
118
                time2 = time.time()
 
119
                dup_time.sleep(sleep_ratio)
 
120
                time3 = time.time()
 
121
                time.sleep(0.5)
 
122
                time4 = time.time()
 
123
                dup_time.sleep(sleep_ratio)
 
124
                time5 = time.time()
 
125
 
 
126
                sleep_ratio = 0.25
 
127
                time.sleep(0.75)
 
128
                time6 = time.time()
 
129
                dup_time.sleep(sleep_ratio)
 
130
                time7 = time.time()
 
131
                
 
132
                assert 0.9 < time3 - time2 < 1.1, time3 - time2
 
133
                assert 0.4 < time5 - time4 < 0.6, time5 - time4
 
134
                assert 0.2 < time7 - time6 < 0.3, time7 - time6
 
135
 
 
136
 
 
137
if __name__ == '__main__': unittest.main()