Repository navigation
Expand file tree
/
Copy pathtest_diff.py
More file actions
322 lines (229 loc) · 15 KB
/
Copy pathtest_diff.py
File metadata and controls
322 lines (229 loc) · 15 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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
# -*- coding: utf-8 -*-
from datetime import date
from pendulum import Pendulum, Date
from .. import AbstractTestCase
class DiffTest(AbstractTestCase):
def test_diff_in_years_positive(self):
dt = Date(2000, 1, 1)
self.assertEqual(1, dt.diff(dt.add(years=1)).in_years())
def test_diff_in_years_negative_with_sign(self):
dt = Date(2000, 1, 1)
self.assertEqual(-1, dt.diff(dt.subtract(years=1), False).in_years())
def test_diff_in_years_negative_no_sign(self):
dt = Date(2000, 1, 1)
self.assertEqual(1, dt.diff(dt.subtract(years=1)).in_years())
def test_diff_in_years_vs_default_now(self):
self.assertEqual(1, Date.today().subtract(years=1).diff().in_years())
def test_diff_in_years_ensure_is_truncated(self):
dt = Date(2000, 1, 1)
self.assertEqual(1, dt.diff(dt.add(years=1).add(months=7)).in_years())
def test_diff_in_months_positive(self):
dt = Date(2000, 1, 1)
self.assertEqual(13, dt.diff(dt.add(years=1).add(months=1)).in_months())
def test_diff_in_months_negative_with_sign(self):
dt = Date(2000, 1, 1)
self.assertEqual(-11, dt.diff(dt.subtract(years=1).add(months=1), False).in_months())
def test_diff_in_months_negative_no_sign(self):
dt = Date(2000, 1, 1)
self.assertEqual(11, dt.diff(dt.subtract(years=1).add(months=1)).in_months())
def test_diff_in_months_vs_default_now(self):
self.assertEqual(12, Date.today().subtract(years=1).diff().in_months())
def test_diff_in_months_ensure_is_truncated(self):
dt = Date(2000, 1, 1)
self.assertEqual(1, dt.diff(dt.add(months=1).add(days=16)).in_months())
def test_diff_in_days_positive(self):
dt = Date(2000, 1, 1)
self.assertEqual(366, dt.diff(dt.add(years=1)).in_days())
def test_diff_in_days_negative_with_sign(self):
dt = Date(2000, 1, 1)
self.assertEqual(-365, dt.diff(dt.subtract(years=1), False).in_days())
def test_diff_in_days_negative_no_sign(self):
dt = Date(2000, 1, 1)
self.assertEqual(365, dt.diff(dt.subtract(years=1)).in_days())
def test_diff_in_days_vs_default_now(self):
self.assertEqual(7, Date.today().subtract(weeks=1).diff().in_days())
def test_diff_in_weekdays_positive(self):
dt = Date(2000, 1, 1)
self.assertEqual(21, dt.diff(dt.end_of('month')).in_weekdays())
def test_diff_in_weekdays_negative_no_sign(self):
dt = Date(2000, 1, 31)
self.assertEqual(21, dt.diff(dt.start_of('month')).in_weekdays())
def test_diff_in_weekdays_negative_with_sign(self):
dt = Date(2000, 1, 31)
self.assertEqual(-21, dt.diff(dt.start_of('month'), False).in_weekdays())
def test_diff_in_weekend_days_positive(self):
dt = Date(2000, 1, 1)
self.assertEqual(10, dt.diff(dt.end_of('month')).in_weekend_days())
def test_diff_in_weekend_days_negative_no_sign(self):
dt = Date(2000, 1, 31)
self.assertEqual(10, dt.diff(dt.start_of('month')).in_weekend_days())
def test_diff_in_weekend_days_negative_with_sign(self):
dt = Date(2000, 1, 31)
self.assertEqual(-10, dt.diff(dt.start_of('month'), False).in_weekend_days())
def test_diff_in_weeks_positive(self):
dt = Date(2000, 1, 1)
self.assertEqual(52, dt.diff(dt.add(years=1)).in_weeks())
def test_diff_in_weeks_negative_with_sign(self):
dt = Date(2000, 1, 1)
self.assertEqual(-52, dt.diff(dt.subtract(years=1), False).in_weeks())
def test_diff_in_weeks_negative_no_sign(self):
dt = Date(2000, 1, 1)
self.assertEqual(52, dt.diff(dt.subtract(years=1)).in_weeks())
def test_diff_in_weeks_vs_default_now(self):
self.assertEqual(1, Date.today().subtract(weeks=1).diff().in_weeks())
def test_diff_in_weeks_ensure_is_truncated(self):
dt = Date(2000, 1, 1)
self.assertEqual(0, dt.diff(dt.add(weeks=1).subtract(days=1)).in_weeks())
def test_diff_for_humans_now_and_day(self):
self.assertEqual('1 day ago', Date.today().subtract(days=1).diff_for_humans())
def test_diff_for_humans_now_and_days(self):
self.assertEqual('2 days ago', Date.today().subtract(days=2).diff_for_humans())
def test_diff_for_humans_now_and_nearly_week(self):
self.assertEqual('6 days ago', Date.today().subtract(days=6).diff_for_humans())
def test_diff_for_humans_now_and_week(self):
self.assertEqual('1 week ago', Date.today().subtract(weeks=1).diff_for_humans())
def test_diff_for_humans_now_and_weeks(self):
self.assertEqual('2 weeks ago', Date.today().subtract(weeks=2).diff_for_humans())
def test_diff_for_humans_now_and_nearly_month(self):
self.assertEqual('3 weeks ago', Date.today().subtract(weeks=3).diff_for_humans())
def test_diff_for_humans_now_and_month(self):
with self.wrap_with_test_now(Pendulum.create(2016, 3, 1)):
self.assertEqual('4 weeks ago', Date.today().subtract(weeks=4).diff_for_humans())
self.assertEqual('1 month ago', Date.today().subtract(months=1).diff_for_humans())
with self.wrap_with_test_now(Pendulum.create(2017, 2, 28)):
self.assertEqual('1 month ago', Date.today().subtract(weeks=4).diff_for_humans())
def test_diff_for_humans_now_and_months(self):
self.assertEqual('2 months ago', Date.today().subtract(months=2).diff_for_humans())
def test_diff_for_humans_now_and_nearly_year(self):
self.assertEqual('11 months ago', Date.today().subtract(months=11).diff_for_humans())
def test_diff_for_humans_now_and_year(self):
self.assertEqual('1 year ago', Date.today().subtract(years=1).diff_for_humans())
def test_diff_for_humans_now_and_years(self):
self.assertEqual('2 years ago', Date.today().subtract(years=2).diff_for_humans())
def test_diff_for_humans_now_and_future_day(self):
self.assertEqual('1 day from now', Date.today().add(days=1).diff_for_humans())
def test_diff_for_humans_now_and_future_days(self):
self.assertEqual('2 days from now', Date.today().add(days=2).diff_for_humans())
def test_diff_for_humans_now_and_nearly_future_week(self):
self.assertEqual('6 days from now', Date.today().add(days=6).diff_for_humans())
def test_diff_for_humans_now_and_future_week(self):
self.assertEqual('1 week from now', Date.today().add(weeks=1).diff_for_humans())
def test_diff_for_humans_now_and_future_weeks(self):
self.assertEqual('2 weeks from now', Date.today().add(weeks=2).diff_for_humans())
def test_diff_for_humans_now_and_nearly_future_month(self):
self.assertEqual('3 weeks from now', Date.today().add(weeks=3).diff_for_humans())
def test_diff_for_humans_now_and_future_month(self):
with self.wrap_with_test_now(Pendulum.create(2016, 3, 1)):
self.assertEqual('4 weeks from now', Date.today().add(weeks=4).diff_for_humans())
self.assertEqual('1 month from now', Date.today().add(months=1).diff_for_humans())
with self.wrap_with_test_now(Pendulum.create(2017, 3, 31)):
self.assertEqual('1 month from now', Date.today().add(months=1).diff_for_humans())
with self.wrap_with_test_now(Pendulum.create(2017, 4, 30)):
self.assertEqual('1 month from now', Date.today().add(months=1).diff_for_humans())
with self.wrap_with_test_now(Pendulum.create(2017, 1, 31)):
self.assertEqual('1 month from now', Date.today().add(weeks=4).diff_for_humans())
def test_diff_for_humans_now_and_future_months(self):
self.assertEqual('2 months from now', Date.today().add(months=2).diff_for_humans())
def test_diff_for_humans_now_and_nearly_future_year(self):
self.assertEqual('11 months from now', Date.today().add(months=11).diff_for_humans())
def test_diff_for_humans_now_and_future_year(self):
self.assertEqual('1 year from now', Date.today().add(years=1).diff_for_humans())
def test_diff_for_humans_now_and_future_years(self):
self.assertEqual('2 years from now', Date.today().add(years=2).diff_for_humans())
def test_diff_for_humans_other_and_day(self):
self.assertEqual('1 day before', Date.today().diff_for_humans(Date.today().add(days=1)))
def test_diff_for_humans_other_and_days(self):
self.assertEqual('2 days before', Date.today().diff_for_humans(Date.today().add(days=2)))
def test_diff_for_humans_other_and_nearly_week(self):
self.assertEqual('6 days before', Date.today().diff_for_humans(Date.today().add(days=6)))
def test_diff_for_humans_other_and_week(self):
self.assertEqual('1 week before', Date.today().diff_for_humans(Date.today().add(weeks=1)))
def test_diff_for_humans_other_and_weeks(self):
self.assertEqual('2 weeks before', Date.today().diff_for_humans(Date.today().add(weeks=2)))
def test_diff_for_humans_other_and_nearly_month(self):
self.assertEqual('3 weeks before', Date.today().diff_for_humans(Date.today().add(weeks=3)))
def test_diff_for_humans_other_and_month(self):
with self.wrap_with_test_now(Pendulum.create(2016, 3, 1)):
self.assertEqual('4 weeks before', Date.today().diff_for_humans(Date.today().add(weeks=4)))
self.assertEqual('1 month before', Date.today().diff_for_humans(Date.today().add(months=1)))
with self.wrap_with_test_now(Pendulum.create(2017, 3, 31)):
self.assertEqual('1 month before', Date.today().diff_for_humans(Date.today().add(months=1)))
with self.wrap_with_test_now(Pendulum.create(2017, 4, 30)):
self.assertEqual('1 month before', Date.today().diff_for_humans(Date.today().add(months=1)))
with self.wrap_with_test_now(Pendulum.create(2017, 1, 31)):
self.assertEqual('1 month before', Date.today().diff_for_humans(Date.today().add(weeks=4)))
def test_diff_for_humans_other_and_months(self):
self.assertEqual('2 months before', Date.today().diff_for_humans(Date.today().add(months=2)))
def test_diff_for_humans_other_and_nearly_year(self):
self.assertEqual('11 months before', Date.today().diff_for_humans(Date.today().add(months=11)))
def test_diff_for_humans_other_and_year(self):
self.assertEqual('1 year before', Date.today().diff_for_humans(Date.today().add(years=1)))
def test_diff_for_humans_other_and_years(self):
self.assertEqual('2 years before', Date.today().diff_for_humans(Date.today().add(years=2)))
def test_diff_for_humans_other_and_future_day(self):
self.assertEqual('1 day after', Date.today().diff_for_humans(Date.today().subtract(days=1)))
def test_diff_for_humans_other_and_future_days(self):
self.assertEqual('2 days after', Date.today().diff_for_humans(Date.today().subtract(days=2)))
def test_diff_for_humans_other_and_nearly_future_week(self):
self.assertEqual('6 days after', Date.today().diff_for_humans(Date.today().subtract(days=6)))
def test_diff_for_humans_other_and_future_week(self):
self.assertEqual('1 week after', Date.today().diff_for_humans(Date.today().subtract(weeks=1)))
def test_diff_for_humans_other_and_future_weeks(self):
self.assertEqual('2 weeks after', Date.today().diff_for_humans(Date.today().subtract(weeks=2)))
def test_diff_for_humans_other_and_nearly_future_month(self):
self.assertEqual('3 weeks after', Date.today().diff_for_humans(Date.today().subtract(weeks=3)))
def test_diff_for_humans_other_and_future_month(self):
with self.wrap_with_test_now(Pendulum.create(2016, 3, 1)):
self.assertEqual('4 weeks after', Date.today().diff_for_humans(Date.today().subtract(weeks=4)))
self.assertEqual('1 month after', Date.today().diff_for_humans(Date.today().subtract(months=1)))
with self.wrap_with_test_now(Pendulum.create(2017, 2, 28)):
self.assertEqual('1 month after', Date.today().diff_for_humans(Date.today().subtract(weeks=4)))
def test_diff_for_humans_other_and_future_months(self):
self.assertEqual('2 months after', Date.today().diff_for_humans(Date.today().subtract(months=2)))
def test_diff_for_humans_other_and_nearly_future_year(self):
self.assertEqual('11 months after', Date.today().diff_for_humans(Date.today().subtract(months=11)))
def test_diff_for_humans_other_and_future_year(self):
self.assertEqual('1 year after', Date.today().diff_for_humans(Date.today().subtract(years=1)))
def test_diff_for_humans_other_and_future_years(self):
self.assertEqual('2 years after', Date.today().diff_for_humans(Date.today().subtract(years=2)))
def test_diff_for_humans_absolute_days(self):
self.assertEqual('2 days', Date.today().diff_for_humans(Date.today().subtract(days=2), True))
self.assertEqual('2 days', Date.today().diff_for_humans(Date.today().add(days=2), True))
def test_diff_for_humans_absolute_weeks(self):
self.assertEqual('2 weeks', Date.today().diff_for_humans(Date.today().subtract(weeks=2), True))
self.assertEqual('2 weeks', Date.today().diff_for_humans(Date.today().add(weeks=2), True))
def test_diff_for_humans_absolute_months(self):
self.assertEqual('2 months', Date.today().diff_for_humans(Date.today().subtract(months=2), True))
self.assertEqual('2 months', Date.today().diff_for_humans(Date.today().add(months=2), True))
def test_diff_for_humans_absolute_years(self):
self.assertEqual('1 year', Date.today().diff_for_humans(Date.today().subtract(years=1), True))
self.assertEqual('1 year', Date.today().diff_for_humans(Date.today().add(years=1), True))
def test_diff_accuracy(self):
# DST
today = Pendulum.create(2017, 3, 7, tz='America/Toronto')
with self.wrap_with_test_now(today):
diff = today.add(days=6).diff(today, abs=True)
self.assertEqual(5, diff.days)
self.assertEqual(6, diff.remaining_days)
self.assertEqual(0, diff.hours)
self.assertEqual('6 days', diff.in_words())
def test_diff_for_humans_accuracy(self):
today = Pendulum.today()
with self.wrap_with_test_now(today.add(days=1)):
self.assertEqual('1 year', today.add(years=1).diff_for_humans(absolute=True))
with self.wrap_with_test_now(today):
self.assertEqual('6 days', today.add(days=6).diff_for_humans(absolute=True))
self.assertEqual('1 week', today.add(days=7).diff_for_humans(absolute=True))
self.assertEqual('3 weeks', today.add(days=20).diff_for_humans(absolute=True))
self.assertEqual('2 weeks', today.add(days=14).diff_for_humans(absolute=True))
self.assertEqual('2 weeks', today.add(days=13).diff_for_humans(absolute=True))
# DST
today = Pendulum.create(2017, 3, 7, tz='America/Toronto')
with self.wrap_with_test_now(today):
self.assertEqual('6 days', today.add(days=6).diff_for_humans(absolute=True))
def test_subtraction(self):
d = Date(2016, 7, 5)
future_dt = date(2016, 7, 6)
future = d.add(days=1)
self.assertEqual(86400, (future - d).total_seconds())
self.assertEqual(86400, (future_dt - d).total_seconds())