This repository has been archived by the owner on May 26, 2021. It is now read-only.
forked from icholy/durationpy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.py
99 lines (84 loc) · 2.72 KB
/
test.py
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
import unittest
import durationpy
from datetime import timedelta
millisecond = 1
second = 1000 * millisecond
minute = 60 * second
hour = 60 * minute
day = 24 * hour
week = 7 * day
month = 30 * day
year = 365 * day
cases = [
# simple
["0", True, 0],
["5s", True, 5 * second],
["30s", True, 30 * second],
["1478s", True, 1478 * second],
# sign
["-5s", True, -5 * second],
["+5s", True, 5 * second],
["-0", True, 0],
["+0", True, 0],
# decimal
["5.0s", True, 5 * second],
["5.6s", True, 5*second + 600*millisecond],
["5.s", True, 5 * second],
[".5s", True, 500 * millisecond],
["1.0s", True, 1 * second],
["1.00s", True, 1 * second],
["1.004s", True, 1*second + 4*millisecond],
["1.0040s", True, 1*second + 4*millisecond],
["100.00100s", True, 100*second + 1*millisecond],
# different units
["13ms", True, 13 * millisecond],
["14s", True, 14 * second],
["15m", True, 15 * minute],
["16h", True, 16 * hour],
["11d", True, 11 * day],
["10w", True, 10 * week],
# composite durations
["3h30m", True, 3*hour + 30*minute],
["10.5s4m", True, 4*minute + 10*second + 500*millisecond],
["-2m3.4s", True, -(2*minute + 3*second + 400*millisecond)],
["1h2m3s4ms", True, 1*hour + 2*minute + 3*second + 4*millisecond],
["10w5d39h9m14.425s", True, 10*week + 5*day + 39*hour + 9*minute + 14*second + 425*millisecond],
# large value
["52763797000ms", True, 52763797000 * millisecond],
# errors
["", False, 0],
["3", False, 0],
["-", False, 0],
["s", False, 0],
[".", False, 0],
["-.", False, 0],
[".s", False, 0],
["+.s", False, 0],
# extended
["5y2mm", True, 5*year + 2*month],
["7d", True, 7*day],
["1y4w1h", True, 1*year + 4*week + 1*hour],
["-7d", True, -7*day]
]
class DurationTest(unittest.TestCase):
def test_parser(self):
for [input, passes, expected] in cases:
if passes:
actual = durationpy.from_str(input).total_seconds() * 1000
self.assertEqual(
expected, actual,
"{}, expecting {}, got {}".format(input, expected, actual))
else:
with self.assertRaises(durationpy.DurationError):
durationpy.from_str(input)
def test_formatter(self):
for [input, passes, expected] in cases:
if passes:
dt = durationpy.from_str(input)
ds = durationpy.to_str(dt)
actual = durationpy.from_str(ds).total_seconds() * 1000
self.assertEqual(
expected, actual,
"{}, expecting {}, got {}".format(input, expected, actual))
if __name__ == '__main__':
unittest.main()