forked from nickmaccarthy/python-datemath
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tests.py
182 lines (149 loc) · 12.3 KB
/
tests.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
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
# !/usr/bin/python
# coding=utf-8
import unittest2 as unittest
import arrow
from datetime import datetime as pydatetime
from datetime import timedelta
from datemath import dm, datemath
from datemath.helpers import DateMathException as DateMathException
from dateutil import tz
iso8601 = 'YYYY-MM-DDTHH:mm:ssZZ'
class TestDM(unittest.TestCase):
def testParse(self):
# Make sure our helpers return the correct objects
self.assertIsInstance(datemath('now'), pydatetime)
self.assertIsInstance(dm('now'), arrow.arrow.Arrow)
# Baisc dates
self.assertEqual(dm('2016.01.02').format(iso8601), '2016-01-02T00:00:00+00:00')
self.assertEqual(dm('2016-01-02').format(iso8601), '2016-01-02T00:00:00+00:00')
self.assertEqual(dm('2016-01-02 01:00:00').format(iso8601), '2016-01-02T01:00:00+00:00')
# Rounding Tests
self.assertEqual(dm('2016-01-01||/d').format('YYYY-MM-DDTHH:mm:ssZZ'), '2016-01-01T00:00:00+00:00')
self.assertEqual(dm('2014-11-18||/y').format('YYYY-MM-DDTHH:mm:ssZZ'), '2014-01-01T00:00:00+00:00')
self.assertEqual(dm('2016-01-01 14:00:00||/w').format('YYYY-MM-DDTHH:mm:ssZZ'), '2015-12-28T00:00:00+00:00')
self.assertEqual(dm('2014-11||/M').format('YYYY-MM-DDTHH:mm:ssZZ'), '2014-11-01T00:00:00+00:00')
self.assertEqual(dm('2016-01-02||/M+1h+1m').format(iso8601), '2016-01-01T01:01:00+00:00')
self.assertEqual(dm('2016-01-02||/d+1h').format(iso8601), '2016-01-02T01:00:00+00:00')
self.assertEqual(dm('2016-01-02T14:02:00||/h').format(iso8601), '2016-01-02T14:00:00+00:00')
self.assertEqual(dm('2016-01-02T14:02:00||/H').format(iso8601), '2016-01-02T14:00:00+00:00')
# Rounding Up Tests
self.assertEqual(dm('2016-01-01||/d', roundDown=False).format('YYYY-MM-DDTHH:mm:ssZZ'), '2016-01-01T23:59:59+00:00')
self.assertEqual(dm('2014-11-18||/y', roundDown=False).format('YYYY-MM-DDTHH:mm:ssZZ'), '2014-12-31T23:59:59+00:00')
# Timezone Tests
self.assertEqual(dm('now', tz='US/Pacific').format(iso8601), arrow.utcnow().to('US/Pacific').format(iso8601))
self.assertEqual(dm('2017-09-22 10:20:00', tz='US/Pacific').datetime, pydatetime(2017, 9, 22, 10, 20, 00, tzinfo=tz.gettz('US/Pacific')))
self.assertEqual(dm('2016-01-01', tz='UTC'), arrow.get('2016-01-01').to('UTC'))
self.assertEqual(dm('2016-01-01', tz='US/Eastern'), pydatetime(2016, 1, 1, tzinfo=tz.gettz('US/Eastern')))
self.assertEqual(datemath('2016-01-01T01:00:00', tz='US/Central'), pydatetime(2016, 1, 1, 1, 0, 0, tzinfo=tz.gettz('US/Central')))
self.assertEqual(datemath('2016-01-01T02:00:00', tz='US/Eastern'), pydatetime(2016, 1, 1, 2, tzinfo=tz.gettz('US/Eastern')))
# TZ offset inside of date string
self.assertEqual(datemath('2016-01-01T16:20:00.5+12:00'), pydatetime(2016, 1, 1, 16, 20, 0, 500000, tzinfo=tz.tzoffset(None, timedelta(hours=12))))
self.assertEqual(datemath('2016-01-01T16:20:00.5-05:00'), pydatetime(2016, 1, 1, 16, 20, 0, 500000, tzinfo=tz.tzoffset(None, timedelta(hours=-5))))
self.assertEqual(datemath('2016-01-01T16:20:00.5-00:00'), pydatetime(2016, 1, 1, 16, 20, 0, 500000, tzinfo=tz.tzoffset(None, timedelta(hours=0))))
# TZ offset inside of date string with datemath
self.assertEqual(datemath('2016-01-01T16:20:00.5+12:00||+1d'), pydatetime(2016, 1, 2, 16, 20, 0, 500000, tzinfo=tz.tzoffset(None, timedelta(hours=12))))
self.assertEqual(datemath('2016-01-01T16:20:00.6+12:00||+2d+1h'), pydatetime(2016, 1, 3, 17, 20, 0, 600000, tzinfo=tz.tzoffset(None, timedelta(hours=12))))
# If a TZ offset is in a datetime string, and there is a tz param used, the TZ offset will take precedence for the returned timeobj
self.assertEqual(datemath('2016-01-01T16:20:00.6+12:00||+2d+1h', tz='US/Eastern'), pydatetime(2016, 1, 3, 17, 20, 0, 600000, tzinfo=tz.tzoffset(None, timedelta(hours=12))))
# relitive formats
# addition
self.assertEqual(dm('+1s').format(iso8601), arrow.utcnow().shift(seconds=+1).format(iso8601))
self.assertEqual(dm('+1m').format(iso8601), arrow.utcnow().shift(minutes=+1).format(iso8601))
self.assertEqual(dm('+1h').format(iso8601), arrow.utcnow().shift(hours=+1).format(iso8601))
self.assertEqual(dm('+1d').format(iso8601), arrow.utcnow().shift(days=+1).format(iso8601))
self.assertEqual(dm('+1w').format(iso8601), arrow.utcnow().shift(weeks=+1).format(iso8601))
self.assertEqual(dm('+1M').format(iso8601), arrow.utcnow().shift(months=+1).format(iso8601))
self.assertEqual(dm('+1Y').format(iso8601), arrow.utcnow().shift(years=+1).format(iso8601))
self.assertEqual(dm('+1y').format(iso8601), arrow.utcnow().shift(years=+1).format(iso8601))
# subtraction
self.assertEqual(dm('-1s').format(iso8601), arrow.utcnow().shift(seconds=-1).format(iso8601))
self.assertEqual(dm('-1m').format(iso8601), arrow.utcnow().shift(minutes=-1).format(iso8601))
self.assertEqual(dm('-1h').format(iso8601), arrow.utcnow().shift(hours=-1).format(iso8601))
self.assertEqual(dm('-1d').format(iso8601), arrow.utcnow().shift(days=-1).format(iso8601))
self.assertEqual(dm('-1w').format(iso8601), arrow.utcnow().shift(weeks=-1).format(iso8601))
self.assertEqual(dm('-1M').format(iso8601), arrow.utcnow().shift(months=-1).format(iso8601))
self.assertEqual(dm('-1Y').format(iso8601), arrow.utcnow().shift(years=-1).format(iso8601))
self.assertEqual(dm('-1y').format(iso8601), arrow.utcnow().shift(years=-1).format(iso8601))
# rounding
self.assertEqual(dm('/s').format(iso8601), arrow.utcnow().floor('second').format(iso8601))
self.assertEqual(dm('/m').format(iso8601), arrow.utcnow().floor('minute').format(iso8601))
self.assertEqual(dm('/h').format(iso8601), arrow.utcnow().floor('hour').format(iso8601))
self.assertEqual(dm('/d').format(iso8601), arrow.utcnow().floor('day').format(iso8601))
self.assertEqual(dm('/w').format(iso8601), arrow.utcnow().floor('week').format(iso8601))
self.assertEqual(dm('/M').format(iso8601), arrow.utcnow().floor('month').format(iso8601))
self.assertEqual(dm('/Y').format(iso8601), arrow.utcnow().floor('year').format(iso8601))
self.assertEqual(dm('/y').format(iso8601), arrow.utcnow().floor('year').format(iso8601))
# rounding up
self.assertEqual(dm('/s', roundDown=False).format(iso8601), arrow.utcnow().ceil('second').format(iso8601))
self.assertEqual(dm('/m', roundDown=False).format(iso8601), arrow.utcnow().ceil('minute').format(iso8601))
self.assertEqual(dm('/h', roundDown=False).format(iso8601), arrow.utcnow().ceil('hour').format(iso8601))
self.assertEqual(dm('/d', roundDown=False).format(iso8601), arrow.utcnow().ceil('day').format(iso8601))
self.assertEqual(dm('/w', roundDown=False).format(iso8601), arrow.utcnow().ceil('week').format(iso8601))
self.assertEqual(dm('/M', roundDown=False).format(iso8601), arrow.utcnow().ceil('month').format(iso8601))
self.assertEqual(dm('/Y', roundDown=False).format(iso8601), arrow.utcnow().ceil('year').format(iso8601))
self.assertEqual(dm('/y', roundDown=False).format(iso8601), arrow.utcnow().ceil('year').format(iso8601))
# roundDown option tests
self.assertEqual(dm('2016-01-01T14:00:00||/d').format(iso8601), '2016-01-01T00:00:00+00:00')
self.assertEqual(dm('2016-01-01T14:00:00||/d', roundDown=False).format(iso8601), '2016-01-01T23:59:59+00:00')
# complicated date math
self.assertEqual(dm('now/d-1h').format(iso8601), arrow.utcnow().floor('day').shift(hours=-1).format(iso8601))
self.assertEqual(dm('+1h').format(iso8601), arrow.utcnow().shift(hours=+1).format(iso8601))
self.assertEqual(dm('/M+2d').format(iso8601), arrow.utcnow().floor('month').shift(days=+2).format(iso8601))
self.assertEqual(dm('now/w+2d-2h').format(iso8601), arrow.utcnow().floor('week').shift(days=+2, hours=-2).format(iso8601))
self.assertEqual(dm('now/M+1w-2h+10s').format(iso8601), arrow.utcnow().floor('month').shift(weeks=+1, hours=-2, seconds=+10).format(iso8601))
self.assertEqual(dm('now-1d/d').format(iso8601), arrow.utcnow().shift(days=-1).floor('day').format(iso8601))
self.assertEqual(dm('now+1d/d').format(iso8601), arrow.utcnow().shift(days=1).floor('day').format(iso8601))
self.assertEqual(dm('now-10d/d').format(iso8601), arrow.utcnow().shift(days=-10).floor('day').format(iso8601))
self.assertEqual(dm('now+10d/d').format(iso8601), arrow.utcnow().shift(days=10).floor('day').format(iso8601))
self.assertEqual(dm('now-29d/d').format(iso8601), arrow.utcnow().shift(days=-29).floor('day').format(iso8601))
# future
self.assertEqual(dm('+1s').format(iso8601), arrow.utcnow().shift(seconds=+1).format(iso8601))
self.assertEqual(dm('+1s+2m+3h').format(iso8601), arrow.utcnow().shift(seconds=+1, minutes=+2, hours=+3).format(iso8601))
self.assertEqual(dm('+1m').format(iso8601), arrow.utcnow().shift(minutes=+1).format(iso8601))
self.assertEqual(dm('+1m+5h').format(iso8601), arrow.utcnow().shift(minutes=+1, hours=+5).format(iso8601))
self.assertEqual(dm('/d+1m+5h').format(iso8601), arrow.utcnow().floor('day').shift(minutes=+1, hours=+5).format(iso8601))
self.assertEqual(dm('+1h').format(iso8601), arrow.utcnow().shift(hours=+1).format(iso8601))
self.assertEqual(dm('+1w').format(iso8601), arrow.utcnow().shift(weeks=+1).format(iso8601))
self.assertEqual(dm('+1w+12d').format(iso8601), arrow.utcnow().shift(weeks=+1, days=+12).format(iso8601))
self.assertEqual(dm('+2y').format(iso8601), arrow.utcnow().shift(years=+2).format(iso8601))
self.assertEqual(dm('+2y+22d+4h').format(iso8601), arrow.utcnow().shift(years=+2, days=+22, hours=+4).format(iso8601))
# past
self.assertEqual(dm('-3w').format(iso8601), arrow.utcnow().shift(weeks=-3).format(iso8601))
self.assertEqual(dm('-3W').format(iso8601), arrow.utcnow().shift(weeks=-3).format(iso8601))
self.assertEqual(dm('-3w-2d-6h').format(iso8601), arrow.utcnow().shift(weeks=-3, days=-2, hours=-6).format(iso8601))
self.assertEqual(dm('-3w-2d-22h-36s').format(iso8601), arrow.utcnow().shift(weeks=-3, days=-2, hours=-22, seconds=-36).format(iso8601))
self.assertEqual(dm('-6y-3w-2d-22h-36s').format(iso8601), arrow.utcnow().shift(years=-6, weeks=-3, days=-2, hours=-22, seconds=-36).format(iso8601))
import datetime
delta = datetime.timedelta(seconds=1)
# datetime objects
self.assertAlmostEqual(dm('now').datetime, arrow.utcnow().datetime, delta=delta)
self.assertAlmostEqual(dm('now+1d').datetime, arrow.utcnow().shift(days=+1).datetime, delta=delta)
self.assertAlmostEqual(dm('/w').datetime, arrow.utcnow().floor('week').datetime, delta=delta)
# Floats
self.assertEqual(dm('now-2.5h').format(iso8601), arrow.utcnow().shift(hours=-2.5).format(iso8601))
self.assertEqual(dm('now-2.5d').format(iso8601), arrow.utcnow().shift(days=-2.5).format(iso8601))
# Test epoch timestamps
self.assertEqual(dm('1451610061').format(iso8601), '2016-01-01T01:01:01+00:00')
self.assertEqual(dm(1367900664).format(iso8601), '2013-05-07T04:24:24+00:00')
self.assertRaises(DateMathException, dm, '1451610061000')
try:
dm(1451610061000)
except DateMathException as e:
self.assertTrue('Unable to parse epoch timestamps in millis' in str(e))
# Catch invalid timeunits
self.assertRaises(DateMathException, dm, '+1,')
self.assertRaises(DateMathException, dm, '+1.')
self.assertRaises(DateMathException, dm, '+1ö')
self.assertRaises(DateMathException, dm, '+1ä')
self.assertRaises(DateMathException, dm, '+1ü')
self.assertRaises(DateMathException, dm, '+1ß')
self.assertRaises(DateMathException, dm, '2')
self.assertRaises(DateMathException, datemath, '2')
self.assertRaises(DateMathException, dm, '123')
self.assertRaises(DateMathException, datemath, '123')
try:
dm('+1,')
except DateMathException as e:
self.assertTrue('is not a valid timeunit' in str(e))
if __name__ == "__main__":
unittest.main()