forked from dbader/schedule
-
Notifications
You must be signed in to change notification settings - Fork 40
/
test_schedule.py
457 lines (368 loc) · 14 KB
/
test_schedule.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
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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
"""Unit tests for schedule.py"""
import unittest
import mock
import datetime
import time
# Silence "missing docstring", "method could be a function",
# "class already defined", and "too many public methods" messages:
# pylint: disable-msg=R0201,C0111,E0102,R0904,R0901
import schedule
from schedule import every
def make_mock_job(name=None):
job = mock.Mock()
job.__name__ = name or 'job'
return job
class SchedulerTests(unittest.TestCase):
def setUp(self):
schedule.clear()
def test_time_units(self):
assert every().seconds.unit == 'seconds'
assert every().minutes.unit == 'minutes'
assert every().hours.unit == 'hours'
assert every().days.unit == 'days'
assert every().weeks.unit == 'weeks'
def test_singular_time_units_match_plural_units(self):
assert every().second.unit == every().seconds.unit
assert every().minute.unit == every().minutes.unit
assert every().hour.unit == every().hours.unit
assert every().day.unit == every().days.unit
assert every().week.unit == every().weeks.unit
def test_at_time(self):
mock_job = make_mock_job()
assert every().day.at('10:30').do(mock_job).next_run.hour == 10
assert every().day.at('10:30').do(mock_job).next_run.minute == 30
def test_next_run_time(self):
# Monkey-patch datetime.datetime to get predictable (=testable) results
class MockDate(datetime.datetime):
@classmethod
def today(cls):
return cls(2010, 1, 6)
@classmethod
def now(cls):
return cls(2010, 1, 6, 12, 15)
original_datetime = datetime.datetime
datetime.datetime = MockDate
mock_job = make_mock_job()
assert every().minute.do(mock_job).next_run.minute == 16
assert every(5).minutes.do(mock_job).next_run.minute == 20
assert every().hour.do(mock_job).next_run.hour == 13
assert every().day.do(mock_job).next_run.day == 7
assert every().day.at('09:00').do(mock_job).next_run.day == 7
assert every().day.at('12:30').do(mock_job).next_run.day == 6
assert every().week.do(mock_job).next_run.day == 13
datetime.datetime = original_datetime
def test_run_all(self):
mock_job = make_mock_job()
every().minute.do(mock_job)
every().hour.do(mock_job)
every().day.at('11:00').do(mock_job)
schedule.run_all()
assert mock_job.call_count == 3
def test_job_func_args_are_passed_on(self):
mock_job = make_mock_job()
every().second.do(mock_job, 1, 2, 'three', foo=23, bar={})
schedule.run_all()
mock_job.assert_called_once_with(1, 2, 'three', foo=23, bar={})
def test_to_string(self):
def job_fun():
pass
s = str(every().minute.do(job_fun, 'foo', bar=23))
assert 'job_fun' in s
assert 'foo' in s
assert 'bar=23' in s
assert len(str(every().minute.do(lambda: 1))) > 1
assert len(str(every().day.at("10:30").do(lambda: 1))) > 1
def test_run_pending(self):
"""Check that run_pending() runs pending jobs.
We do this by overriding datetime.datetime with mock objects
that represent increasing system times.
Please note that it is *intended behavior that run_pending() does not
run missed jobs*. For example, if you've registered a job that
should run every minute and you only call run_pending() in one hour
increments then your job won't be run 60 times in between but
only once.
"""
# Monkey-patch datetime.datetime to get predictable (=testable) results
class MockDate(datetime.datetime):
@classmethod
def today(cls):
return cls(2010, 1, 6)
@classmethod
def now(cls):
return cls(2010, 1, 6, 12, 15)
original_datetime = datetime.datetime
datetime.datetime = MockDate
mock_job = make_mock_job()
every().minute.do(mock_job)
every().hour.do(mock_job)
every().day.do(mock_job)
schedule.run_pending()
assert mock_job.call_count == 0
# Minutely
class MockDate(datetime.datetime):
@classmethod
def today(cls):
return cls(2010, 1, 6)
@classmethod
def now(cls):
return cls(2010, 1, 6, 12, 16)
datetime.datetime = MockDate
schedule.run_pending()
assert mock_job.call_count == 1
# Minutely, hourly
class MockDate(datetime.datetime):
@classmethod
def today(cls):
return cls(2010, 1, 6)
@classmethod
def now(cls):
return cls(2010, 1, 6, 13, 16)
datetime.datetime = MockDate
mock_job.reset_mock()
schedule.run_pending()
assert mock_job.call_count == 2
# Minutely, hourly, daily
class MockDate(datetime.datetime):
@classmethod
def today(cls):
return cls(2010, 1, 7)
@classmethod
def now(cls):
return cls(2010, 1, 7, 13, 16)
datetime.datetime = MockDate
mock_job.reset_mock()
schedule.run_pending()
assert mock_job.call_count == 3
datetime.datetime = original_datetime
def test_run_every_n_days_at_specific_time(self):
class MockDate(datetime.datetime):
@classmethod
def today(cls):
return cls(2010, 1, 6)
@classmethod
def now(cls):
return cls(2010, 1, 6, 13, 16)
original_datetime = datetime.datetime
datetime.datetime = MockDate
mock_job = make_mock_job()
every(2).days.at("11:30").do(mock_job)
schedule.run_pending()
assert mock_job.call_count == 0
class MockDate(datetime.datetime):
@classmethod
def today(cls):
return cls(2010, 1, 7)
@classmethod
def now(cls):
return cls(2010, 1, 7, 13, 16)
datetime.datetime = MockDate
schedule.run_pending()
assert mock_job.call_count == 0
class MockDate(datetime.datetime):
@classmethod
def today(cls):
return cls(2010, 1, 8)
@classmethod
def now(cls):
return cls(2010, 1, 8, 13, 16)
datetime.datetime = MockDate
schedule.run_pending()
assert mock_job.call_count == 1
class MockDate(datetime.datetime):
@classmethod
def today(cls):
return cls(2010, 1, 10)
@classmethod
def now(cls):
return cls(2010, 1, 10, 13, 16)
datetime.datetime = MockDate
schedule.run_pending()
assert mock_job.call_count == 2
datetime.datetime = original_datetime
def test_next_run_property(self):
class MockDate(datetime.datetime):
@classmethod
def today(cls):
return cls(2010, 1, 6)
@classmethod
def now(cls):
return cls(2010, 1, 6, 13, 16)
original_datetime = datetime.datetime
datetime.datetime = MockDate
hourly_job = make_mock_job('hourly')
daily_job = make_mock_job('daily')
every().day.do(daily_job)
every().hour.do(hourly_job)
assert len(schedule.jobs) == 2
# Make sure the hourly job is first
assert schedule.next_run() == original_datetime(2010, 1, 6, 14, 16)
assert schedule.idle_seconds() == 60 * 60
datetime.datetime = original_datetime
def test_run_continuously(self):
"""Check that run_continuously() runs pending jobs.
We do this by overriding datetime.datetime with mock objects
that represent increasing system times.
Please note that it is *intended behavior that run_continuously()
does not run missed jobs*. For example, if you've registered a job
that should run every minute and you set a continuous run interval
of one hour then your job won't be run 60 times at each interval but
only once.
"""
# Monkey-patch datetime.datetime to get predictable (=testable) results
class MockDate(datetime.datetime):
@classmethod
def today(cls):
return cls(2010, 1, 6)
@classmethod
def now(cls):
return cls(2010, 1, 6, 12, 15, 0)
original_datetime = datetime.datetime
datetime.datetime = MockDate
mock_job = make_mock_job()
# Secondly Tests
# Initialize everything.
schedule.clear()
mock_job.reset_mock()
every().second.do(mock_job)
# Start a new continuous run thread.
stop_thread_flag = schedule.run_continuously(0)
# Allow a small time for separate thread to register time stamps.
time.sleep(0.001)
assert mock_job.call_count == 0
# Secondly first second.
class MockDate(datetime.datetime):
@classmethod
def today(cls):
return cls(2010, 1, 6)
@classmethod
def now(cls):
return cls(2010, 1, 6, 12, 15, 1)
mock_job.reset_mock()
datetime.datetime = MockDate
# Allow a small time for separate thread to register time stamps.
time.sleep(0.001)
assert mock_job.call_count == 1
# Secondly second second.
class MockDate(datetime.datetime):
@classmethod
def today(cls):
return cls(2010, 1, 6)
@classmethod
def now(cls):
return cls(2010, 1, 6, 12, 15, 2)
datetime.datetime = MockDate
# Allow a small time for separate thread to register time stamps.
time.sleep(0.001)
assert mock_job.call_count == 2
# Minutely Tests
# (Re)Initialize everything.
schedule.clear()
mock_job.reset_mock()
stop_thread_flag.set()
every().minute.do(mock_job)
# Start a new continuous run thread.
stop_thread_flag = schedule.run_continuously(0)
# Allow a small time for separate thread to register time stamps.
time.sleep(0.001)
assert mock_job.call_count == 0
# Minutely first minute.
class MockDate(datetime.datetime):
@classmethod
def today(cls):
return cls(2010, 1, 6)
@classmethod
def now(cls):
return cls(2010, 1, 6, 12, 16, 2)
mock_job.reset_mock()
datetime.datetime = MockDate
# Allow a small time for separate thread to register time stamps.
time.sleep(0.001)
assert mock_job.call_count == 1
# Minutely second minute.
class MockDate(datetime.datetime):
@classmethod
def today(cls):
return cls(2010, 1, 6)
@classmethod
def now(cls):
return cls(2010, 1, 6, 12, 17, 2)
datetime.datetime = MockDate
# Allow a small time for separate thread to register time stamps.
time.sleep(0.001)
assert mock_job.call_count == 2
# Hourly Tests
# (Re)Initialize everything.
schedule.clear()
mock_job.reset_mock()
stop_thread_flag.set()
every().hour.do(mock_job)
# Start a new continuous run thread.
stop_thread_flag = schedule.run_continuously(0)
# Allow a small time for separate thread to register time stamps.
time.sleep(0.001)
assert mock_job.call_count == 0
# Hourly first hour.
class MockDate(datetime.datetime):
@classmethod
def today(cls):
return cls(2010, 1, 6)
@classmethod
def now(cls):
return cls(2010, 1, 6, 13, 17, 2)
mock_job.reset_mock()
datetime.datetime = MockDate
# Allow a small time for separate thread to register time stamps.
time.sleep(0.001)
assert mock_job.call_count == 1
# Hourly second hour.
class MockDate(datetime.datetime):
@classmethod
def today(cls):
return cls(2010, 1, 6)
@classmethod
def now(cls):
return cls(2010, 1, 6, 14, 17, 2)
datetime.datetime = MockDate
# Allow a small time for separate thread to register time stamps.
time.sleep(0.001)
assert mock_job.call_count == 2
# Daily Tests
# (Re)Initialize everything.
schedule.clear()
mock_job.reset_mock()
stop_thread_flag.set()
every().day.do(mock_job)
# Start a new continuous run thread.
stop_thread_flag = schedule.run_continuously(0)
# Allow a small time for separate thread to register time stamps.
time.sleep(0.001)
assert mock_job.call_count == 0
# Daily first day.
class MockDate(datetime.datetime):
@classmethod
def today(cls):
return cls(2010, 1, 6)
@classmethod
def now(cls):
return cls(2010, 1, 7, 14, 17, 2)
mock_job.reset_mock()
datetime.datetime = MockDate
# Allow a small time for separate thread to register time stamps.
time.sleep(0.001)
assert mock_job.call_count == 1
# Daily second day.
class MockDate(datetime.datetime):
@classmethod
def today(cls):
return cls(2010, 1, 6)
@classmethod
def now(cls):
return cls(2010, 1, 8, 14, 17, 2)
datetime.datetime = MockDate
# Allow a small time for separate thread to register time stamps.
time.sleep(0.001)
assert mock_job.call_count == 2
schedule.clear()
mock_job.reset_mock()
stop_thread_flag.set()
datetime.datetime = original_datetime