forked from spotify/luigi
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrange_test.py
executable file
·1243 lines (1040 loc) · 48.9 KB
/
range_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
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
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# -*- coding: utf-8 -*-
#
# Copyright 2012-2015 Spotify AB
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
import datetime
import fnmatch
from helpers import unittest, LuigiTestCase
import luigi
import mock
from luigi.mock import MockTarget, MockFileSystem
from luigi.tools.range import (RangeDaily, RangeDailyBase, RangeEvent,
RangeHourly, RangeHourlyBase,
RangeByMinutes, RangeByMinutesBase,
_constrain_glob, _get_filesystems_and_globs)
class CommonDateMinuteTask(luigi.Task):
dh = luigi.DateMinuteParameter()
def output(self):
return MockTarget(self.dh.strftime('/n2000y01a05n/%Y_%m-_-%daww/21mm%H%Mdara21/ooo'))
class CommonDateHourTask(luigi.Task):
dh = luigi.DateHourParameter()
def output(self):
return MockTarget(self.dh.strftime('/n2000y01a05n/%Y_%m-_-%daww/21mm%Hdara21/ooo'))
class CommonDateTask(luigi.Task):
d = luigi.DateParameter()
def output(self):
return MockTarget(self.d.strftime('/n2000y01a05n/%Y_%m-_-%daww/21mm01dara21/ooo'))
task_a_paths = [
'TaskA/2014-03-20/18',
'TaskA/2014-03-20/21',
'TaskA/2014-03-20/23',
'TaskA/2014-03-21/00',
'TaskA/2014-03-21/00.attempt.1',
'TaskA/2014-03-21/00.attempt.2',
'TaskA/2014-03-21/01',
'TaskA/2014-03-21/02',
'TaskA/2014-03-21/03.attempt-temp-2014-03-21T13-22-58.165969',
'TaskA/2014-03-21/03.attempt.1',
'TaskA/2014-03-21/03.attempt.2',
'TaskA/2014-03-21/03.attempt.3',
'TaskA/2014-03-21/03.attempt.latest',
'TaskA/2014-03-21/04.attempt-temp-2014-03-21T13-23-09.078249',
'TaskA/2014-03-21/12',
'TaskA/2014-03-23/12',
]
task_b_paths = [
'TaskB/no/worries2014-03-20/23',
'TaskB/no/worries2014-03-21/01',
'TaskB/no/worries2014-03-21/03',
'TaskB/no/worries2014-03-21/04.attempt-yadayada',
'TaskB/no/worries2014-03-21/05',
]
mock_contents = task_a_paths + task_b_paths
expected_a = [
'TaskA(dh=2014-03-20T17)',
'TaskA(dh=2014-03-20T19)',
'TaskA(dh=2014-03-20T20)',
]
# expected_reverse = [
# ]
expected_wrapper = [
'CommonWrapperTask(dh=2014-03-21T00)',
'CommonWrapperTask(dh=2014-03-21T02)',
'CommonWrapperTask(dh=2014-03-21T03)',
'CommonWrapperTask(dh=2014-03-21T04)',
'CommonWrapperTask(dh=2014-03-21T05)',
]
class TaskA(luigi.Task):
dh = luigi.DateHourParameter()
def output(self):
return MockTarget(self.dh.strftime('TaskA/%Y-%m-%d/%H'))
class TaskB(luigi.Task):
dh = luigi.DateHourParameter()
complicator = luigi.Parameter()
def output(self):
return MockTarget(self.dh.strftime('TaskB/%%s%Y-%m-%d/%H') % self.complicator)
class TaskC(luigi.Task):
dh = luigi.DateHourParameter()
def output(self):
return MockTarget(self.dh.strftime('not/a/real/path/%Y-%m-%d/%H'))
class CommonWrapperTask(luigi.WrapperTask):
dh = luigi.DateHourParameter()
def requires(self):
yield TaskA(dh=self.dh)
yield TaskB(dh=self.dh, complicator='no/worries') # str(self.dh) would complicate beyond working
class TaskMinutesA(luigi.Task):
dm = luigi.DateMinuteParameter()
def output(self):
return MockTarget(self.dm.strftime('TaskA/%Y-%m-%d/%H%M'))
class TaskMinutesB(luigi.Task):
dm = luigi.DateMinuteParameter()
complicator = luigi.Parameter()
def output(self):
return MockTarget(self.dm.strftime('TaskB/%%s%Y-%m-%d/%H%M') % self.complicator)
class TaskMinutesC(luigi.Task):
dm = luigi.DateMinuteParameter()
def output(self):
return MockTarget(self.dm.strftime('not/a/real/path/%Y-%m-%d/%H%M'))
class CommonWrapperTaskMinutes(luigi.WrapperTask):
dm = luigi.DateMinuteParameter()
def requires(self):
yield TaskMinutesA(dm=self.dm)
yield TaskMinutesB(dm=self.dm, complicator='no/worries') # str(self.dh) would complicate beyond working
def mock_listdir(contents):
def contents_listdir(_, glob):
for path in fnmatch.filter(contents, glob + '*'):
yield path
return contents_listdir
def mock_exists_always_true(_, _2):
yield True
def mock_exists_always_false(_, _2):
yield False
class ConstrainGlobTest(unittest.TestCase):
def test_limit(self):
glob = '/[0-9][0-9][0-9][0-9]/[0-9][0-9]/[0-9][0-9]/[0-9][0-9]'
paths = [(datetime.datetime(2013, 12, 31, 5) + datetime.timedelta(hours=h)).strftime('/%Y/%m/%d/%H') for h in range(40)]
self.assertEqual(sorted(_constrain_glob(glob, paths)), [
'/2013/12/31/[0-2][0-9]',
'/2014/01/01/[0-2][0-9]',
])
paths.pop(26)
self.assertEqual(sorted(_constrain_glob(glob, paths, 6)), [
'/2013/12/31/0[5-9]',
'/2013/12/31/1[0-9]',
'/2013/12/31/2[0-3]',
'/2014/01/01/0[012345689]',
'/2014/01/01/1[0-9]',
'/2014/01/01/2[0]',
])
self.assertEqual(sorted(_constrain_glob(glob, paths[:7], 10)), [
'/2013/12/31/05',
'/2013/12/31/06',
'/2013/12/31/07',
'/2013/12/31/08',
'/2013/12/31/09',
'/2013/12/31/10',
'/2013/12/31/11',
])
def test_no_wildcards(self):
glob = '/2014/01'
paths = '/2014/01'
self.assertEqual(_constrain_glob(glob, paths), [
'/2014/01',
])
def datetime_to_epoch(dt):
td = dt - datetime.datetime(1970, 1, 1)
return td.days * 86400 + td.seconds + td.microseconds / 1E6
class RangeDailyBaseTest(unittest.TestCase):
maxDiff = None
def setUp(self):
# yucky to create separate callbacks; would be nicer if the callback
# received an instance of a subclass of Event, so one callback could
# accumulate all types
@RangeDailyBase.event_handler(RangeEvent.DELAY)
def callback_delay(*args):
self.events.setdefault(RangeEvent.DELAY, []).append(args)
@RangeDailyBase.event_handler(RangeEvent.COMPLETE_COUNT)
def callback_complete_count(*args):
self.events.setdefault(RangeEvent.COMPLETE_COUNT, []).append(args)
@RangeDailyBase.event_handler(RangeEvent.COMPLETE_FRACTION)
def callback_complete_fraction(*args):
self.events.setdefault(RangeEvent.COMPLETE_FRACTION, []).append(args)
self.events = {}
def test_consistent_formatting(self):
task = RangeDailyBase(of=CommonDateTask,
start=datetime.date(2016, 1, 1))
self.assertEqual(task._format_range([datetime.datetime(2016, 1, 2, 13), datetime.datetime(2016, 2, 29, 23)]), '[2016-01-02, 2016-02-29]')
def _empty_subcase(self, kwargs, expected_events):
calls = []
class RangeDailyDerived(RangeDailyBase):
def missing_datetimes(self, task_cls, finite_datetimes):
args = [self, task_cls, finite_datetimes]
calls.append(args)
return args[-1][:5]
task = RangeDailyDerived(of=CommonDateTask,
**kwargs)
self.assertEqual(task.requires(), [])
self.assertEqual(calls, [])
self.assertEqual(task.requires(), [])
self.assertEqual(calls, []) # subsequent requires() should return the cached result, never call missing_datetimes
self.assertEqual(self.events, expected_events)
self.assertTrue(task.complete())
def test_stop_before_days_back(self):
# nothing to do because stop is earlier
self._empty_subcase(
{
'now': datetime_to_epoch(datetime.datetime(2015, 1, 1, 4)),
'stop': datetime.date(2014, 3, 20),
'days_back': 4,
'days_forward': 20,
'reverse': True,
},
{
'event.tools.range.delay': [
('CommonDateTask', 0),
],
'event.tools.range.complete.count': [
('CommonDateTask', 0),
],
'event.tools.range.complete.fraction': [
('CommonDateTask', 1.),
],
}
)
def _nonempty_subcase(self, kwargs, expected_finite_datetimes_range, expected_requires, expected_events):
calls = []
class RangeDailyDerived(RangeDailyBase):
def missing_datetimes(self, finite_datetimes):
# I only changed tests for number of arguments at this one
# place to test both old and new behavior
calls.append((self, finite_datetimes))
return finite_datetimes[:7]
task = RangeDailyDerived(of=CommonDateTask,
**kwargs)
self.assertEqual(list(map(str, task.requires())), expected_requires)
self.assertEqual((min(calls[0][1]), max(calls[0][1])), expected_finite_datetimes_range)
self.assertEqual(list(map(str, task.requires())), expected_requires)
self.assertEqual(len(calls), 1) # subsequent requires() should return the cached result, not call missing_datetimes again
self.assertEqual(self.events, expected_events)
self.assertFalse(task.complete())
def test_start_long_before_long_days_back_and_with_long_days_forward(self):
self._nonempty_subcase(
{
'now': datetime_to_epoch(datetime.datetime(2017, 10, 22, 12, 4, 29)),
'start': datetime.date(2011, 3, 20),
'stop': datetime.date(2025, 1, 29),
'task_limit': 4,
'days_back': 3 * 365,
'days_forward': 3 * 365,
},
(datetime.datetime(2014, 10, 24), datetime.datetime(2020, 10, 21)),
[
'CommonDateTask(d=2014-10-24)',
'CommonDateTask(d=2014-10-25)',
'CommonDateTask(d=2014-10-26)',
'CommonDateTask(d=2014-10-27)',
],
{
'event.tools.range.delay': [
('CommonDateTask', 3750),
],
'event.tools.range.complete.count': [
('CommonDateTask', 5057),
],
'event.tools.range.complete.fraction': [
('CommonDateTask', 5057. / (5057 + 7)),
],
}
)
class RangeHourlyBaseTest(unittest.TestCase):
maxDiff = None
def setUp(self):
# yucky to create separate callbacks; would be nicer if the callback
# received an instance of a subclass of Event, so one callback could
# accumulate all types
@RangeHourlyBase.event_handler(RangeEvent.DELAY)
def callback_delay(*args):
self.events.setdefault(RangeEvent.DELAY, []).append(args)
@RangeHourlyBase.event_handler(RangeEvent.COMPLETE_COUNT)
def callback_complete_count(*args):
self.events.setdefault(RangeEvent.COMPLETE_COUNT, []).append(args)
@RangeHourlyBase.event_handler(RangeEvent.COMPLETE_FRACTION)
def callback_complete_fraction(*args):
self.events.setdefault(RangeEvent.COMPLETE_FRACTION, []).append(args)
self.events = {}
def test_consistent_formatting(self):
task = RangeHourlyBase(of=CommonDateHourTask,
start=datetime.datetime(2016, 1, 1))
self.assertEqual(task._format_range([datetime.datetime(2016, 1, 2, 13), datetime.datetime(2016, 2, 29, 23)]), '[2016-01-02T13, 2016-02-29T23]')
def _empty_subcase(self, kwargs, expected_events):
calls = []
class RangeHourlyDerived(RangeHourlyBase):
def missing_datetimes(a, b, c):
args = [a, b, c]
calls.append(args)
return args[-1][:5]
task = RangeHourlyDerived(of=CommonDateHourTask,
**kwargs)
self.assertEqual(task.requires(), [])
self.assertEqual(calls, [])
self.assertEqual(task.requires(), [])
self.assertEqual(calls, []) # subsequent requires() should return the cached result, never call missing_datetimes
self.assertEqual(self.events, expected_events)
self.assertTrue(task.complete())
def test_start_after_hours_forward(self):
# nothing to do because start is later
self._empty_subcase(
{
'now': datetime_to_epoch(datetime.datetime(2000, 1, 1, 4)),
'start': datetime.datetime(2014, 3, 20, 17),
'hours_back': 4,
'hours_forward': 20,
},
{
'event.tools.range.delay': [
('CommonDateHourTask', 0),
],
'event.tools.range.complete.count': [
('CommonDateHourTask', 0),
],
'event.tools.range.complete.fraction': [
('CommonDateHourTask', 1.),
],
}
)
def _nonempty_subcase(self, kwargs, expected_finite_datetimes_range, expected_requires, expected_events):
calls = []
class RangeHourlyDerived(RangeHourlyBase):
def missing_datetimes(a, b, c):
args = [a, b, c]
calls.append(args)
return args[-1][:7]
task = RangeHourlyDerived(of=CommonDateHourTask,
**kwargs)
self.assertEqual(list(map(str, task.requires())), expected_requires)
self.assertEqual(calls[0][1], CommonDateHourTask)
self.assertEqual((min(calls[0][2]), max(calls[0][2])), expected_finite_datetimes_range)
self.assertEqual(list(map(str, task.requires())), expected_requires)
self.assertEqual(len(calls), 1) # subsequent requires() should return the cached result, not call missing_datetimes again
self.assertEqual(self.events, expected_events)
self.assertFalse(task.complete())
def test_start_long_before_hours_back(self):
self._nonempty_subcase(
{
'now': datetime_to_epoch(datetime.datetime(2000, 1, 1, 4)),
'start': datetime.datetime(1960, 3, 2, 1),
'hours_back': 5,
'hours_forward': 20,
},
(datetime.datetime(1999, 12, 31, 23), datetime.datetime(2000, 1, 1, 23)),
[
'CommonDateHourTask(dh=1999-12-31T23)',
'CommonDateHourTask(dh=2000-01-01T00)',
'CommonDateHourTask(dh=2000-01-01T01)',
'CommonDateHourTask(dh=2000-01-01T02)',
'CommonDateHourTask(dh=2000-01-01T03)',
'CommonDateHourTask(dh=2000-01-01T04)',
'CommonDateHourTask(dh=2000-01-01T05)',
],
{
'event.tools.range.delay': [
('CommonDateHourTask', 25), # because of short hours_back we're oblivious to those 40 preceding years
],
'event.tools.range.complete.count': [
('CommonDateHourTask', 349192),
],
'event.tools.range.complete.fraction': [
('CommonDateHourTask', 349192. / (349192 + 7)),
],
}
)
def test_start_after_long_hours_back(self):
self._nonempty_subcase(
{
'now': datetime_to_epoch(datetime.datetime(2014, 10, 22, 12, 4, 29)),
'start': datetime.datetime(2014, 3, 20, 17),
'task_limit': 4,
'hours_back': 365 * 24,
},
(datetime.datetime(2014, 3, 20, 17), datetime.datetime(2014, 10, 22, 12)),
[
'CommonDateHourTask(dh=2014-03-20T17)',
'CommonDateHourTask(dh=2014-03-20T18)',
'CommonDateHourTask(dh=2014-03-20T19)',
'CommonDateHourTask(dh=2014-03-20T20)',
],
{
'event.tools.range.delay': [
('CommonDateHourTask', 5180),
],
'event.tools.range.complete.count': [
('CommonDateHourTask', 5173),
],
'event.tools.range.complete.fraction': [
('CommonDateHourTask', 5173. / (5173 + 7)),
],
}
)
def test_start_long_before_long_hours_back_and_with_long_hours_forward(self):
self._nonempty_subcase(
{
'now': datetime_to_epoch(datetime.datetime(2017, 10, 22, 12, 4, 29)),
'start': datetime.datetime(2011, 3, 20, 17),
'task_limit': 4,
'hours_back': 3 * 365 * 24,
'hours_forward': 3 * 365 * 24,
},
(datetime.datetime(2014, 10, 23, 13), datetime.datetime(2020, 10, 21, 12)),
[
'CommonDateHourTask(dh=2014-10-23T13)',
'CommonDateHourTask(dh=2014-10-23T14)',
'CommonDateHourTask(dh=2014-10-23T15)',
'CommonDateHourTask(dh=2014-10-23T16)',
],
{
'event.tools.range.delay': [
('CommonDateHourTask', 52560),
],
'event.tools.range.complete.count': [
('CommonDateHourTask', 84061),
],
'event.tools.range.complete.fraction': [
('CommonDateHourTask', 84061. / (84061 + 7)),
],
}
)
class RangeByMinutesBaseTest(unittest.TestCase):
maxDiff = None
def setUp(self):
# yucky to create separate callbacks; would be nicer if the callback
# received an instance of a subclass of Event, so one callback could
# accumulate all types
@RangeByMinutesBase.event_handler(RangeEvent.DELAY)
def callback_delay(*args):
self.events.setdefault(RangeEvent.DELAY, []).append(args)
@RangeByMinutesBase.event_handler(RangeEvent.COMPLETE_COUNT)
def callback_complete_count(*args):
self.events.setdefault(RangeEvent.COMPLETE_COUNT, []).append(args)
@RangeByMinutesBase.event_handler(RangeEvent.COMPLETE_FRACTION)
def callback_complete_fraction(*args):
self.events.setdefault(RangeEvent.COMPLETE_FRACTION, []).append(args)
self.events = {}
def test_consistent_formatting(self):
task = RangeByMinutesBase(of=CommonDateMinuteTask,
start=datetime.datetime(2016, 1, 1, 13),
minutes_interval=5)
self.assertEqual(task._format_range(
[datetime.datetime(2016, 1, 2, 13, 10), datetime.datetime(2016, 2, 29, 23, 20)]),
'[2016-01-02T1310, 2016-02-29T2320]')
def _empty_subcase(self, kwargs, expected_events):
calls = []
class RangeByMinutesDerived(RangeByMinutesBase):
def missing_datetimes(a, b, c):
args = [a, b, c]
calls.append(args)
return args[-1][:5]
task = RangeByMinutesDerived(of=CommonDateMinuteTask, **kwargs)
self.assertEqual(task.requires(), [])
self.assertEqual(calls, [])
self.assertEqual(task.requires(), [])
self.assertEqual(calls, []) # subsequent requires() should return the cached result, never call missing_datetimes
self.assertEqual(self.events, expected_events)
self.assertTrue(task.complete())
def test_start_after_minutes_forward(self):
# nothing to do because start is later
self._empty_subcase(
{
'now': datetime_to_epoch(datetime.datetime(2000, 1, 1, 4)),
'start': datetime.datetime(2014, 3, 20, 17, 10),
'minutes_back': 4,
'minutes_forward': 20,
'minutes_interval': 5,
},
{
'event.tools.range.delay': [
('CommonDateMinuteTask', 0),
],
'event.tools.range.complete.count': [
('CommonDateMinuteTask', 0),
],
'event.tools.range.complete.fraction': [
('CommonDateMinuteTask', 1.),
],
}
)
def _nonempty_subcase(self, kwargs, expected_finite_datetimes_range, expected_requires, expected_events):
calls = []
class RangeByMinutesDerived(RangeByMinutesBase):
def missing_datetimes(a, b, c):
args = [a, b, c]
calls.append(args)
return args[-1][:7]
task = RangeByMinutesDerived(of=CommonDateMinuteTask, **kwargs)
self.assertEqual(list(map(str, task.requires())), expected_requires)
self.assertEqual(calls[0][1], CommonDateMinuteTask)
self.assertEqual((min(calls[0][2]), max(calls[0][2])), expected_finite_datetimes_range)
self.assertEqual(list(map(str, task.requires())), expected_requires)
self.assertEqual(len(calls), 1) # subsequent requires() should return the cached result, not call missing_datetimes again
self.assertEqual(self.events, expected_events)
self.assertFalse(task.complete())
def test_negative_interval(self):
class SomeByMinutesTask(luigi.Task):
d = luigi.DateMinuteParameter()
def output(self):
return MockTarget(self.d.strftime('/data/2014/p/v/z/%Y_/_%m-_-%doctor/20/%HZ%MOOO'))
task = RangeByMinutes(now=datetime_to_epoch(datetime.datetime(2016, 4, 1)),
of=SomeByMinutesTask,
start=datetime.datetime(2014, 3, 20, 17),
minutes_interval=-1)
self.assertRaises(luigi.parameter.ParameterException, task.requires)
def test_non_dividing_interval(self):
class SomeByMinutesTask(luigi.Task):
d = luigi.DateMinuteParameter()
def output(self):
return MockTarget(self.d.strftime('/data/2014/p/v/z/%Y_/_%m-_-%doctor/20/%HZ%MOOO'))
task = RangeByMinutes(now=datetime_to_epoch(datetime.datetime(2016, 4, 1)),
of=SomeByMinutesTask,
start=datetime.datetime(2014, 3, 20, 17),
minutes_interval=8)
self.assertRaises(luigi.parameter.ParameterException, task.requires)
def test_start_and_minutes_period(self):
self._nonempty_subcase(
{
'now': datetime_to_epoch(datetime.datetime(2016, 9, 1, 12, 0, 0)),
'start': datetime.datetime(2016, 9, 1, 11, 0, 0),
'minutes_back': 24 * 60,
'minutes_forward': 0,
'minutes_interval': 3,
},
(datetime.datetime(2016, 9, 1, 11, 0), datetime.datetime(2016, 9, 1, 11, 57, 0)),
[
'CommonDateMinuteTask(dh=2016-09-01T1100)',
'CommonDateMinuteTask(dh=2016-09-01T1103)',
'CommonDateMinuteTask(dh=2016-09-01T1106)',
'CommonDateMinuteTask(dh=2016-09-01T1109)',
'CommonDateMinuteTask(dh=2016-09-01T1112)',
'CommonDateMinuteTask(dh=2016-09-01T1115)',
'CommonDateMinuteTask(dh=2016-09-01T1118)',
],
{
'event.tools.range.delay': [
('CommonDateMinuteTask', 20), # First missing is the 20th
],
'event.tools.range.complete.count': [
('CommonDateMinuteTask', 13), # 20 intervals - 7 missing
],
'event.tools.range.complete.fraction': [
('CommonDateMinuteTask', 13. / (13 + 7)), # (exptected - missing) / expected
],
}
)
def test_start_long_before_minutes_back(self):
self._nonempty_subcase(
{
'now': datetime_to_epoch(datetime.datetime(2000, 1, 1, 0, 3, 0)),
'start': datetime.datetime(1960, 1, 1, 0, 0, 0),
'minutes_back': 5,
'minutes_forward': 20,
'minutes_interval': 5,
},
(datetime.datetime(2000, 1, 1, 0, 0), datetime.datetime(2000, 1, 1, 0, 20, 0)),
[
'CommonDateMinuteTask(dh=2000-01-01T0000)',
'CommonDateMinuteTask(dh=2000-01-01T0005)',
'CommonDateMinuteTask(dh=2000-01-01T0010)',
'CommonDateMinuteTask(dh=2000-01-01T0015)',
'CommonDateMinuteTask(dh=2000-01-01T0020)',
],
{
'event.tools.range.delay': [
('CommonDateMinuteTask', 5), # because of short minutes_back we're oblivious to those 40 preceding years
],
'event.tools.range.complete.count': [
('CommonDateMinuteTask', 4207680), # expected intervals - missing.
],
'event.tools.range.complete.fraction': [
('CommonDateMinuteTask', 4207680. / 4207685), # (expected - missing) / expected
],
}
)
def test_start_after_long_minutes_back(self):
self._nonempty_subcase(
{
'now': datetime_to_epoch(datetime.datetime(2014, 3, 20, 18, 4, 29)),
'start': datetime.datetime(2014, 3, 20, 17, 10),
'task_limit': 4,
'minutes_back': 365 * 24 * 60,
'minutes_interval': 5,
},
(datetime.datetime(2014, 3, 20, 17, 10, 0), datetime.datetime(2014, 3, 20, 18, 0, 0)),
[
'CommonDateMinuteTask(dh=2014-03-20T1710)',
'CommonDateMinuteTask(dh=2014-03-20T1715)',
'CommonDateMinuteTask(dh=2014-03-20T1720)',
'CommonDateMinuteTask(dh=2014-03-20T1725)',
],
{
'event.tools.range.delay': [
('CommonDateMinuteTask', 11),
],
'event.tools.range.complete.count': [
('CommonDateMinuteTask', 4),
],
'event.tools.range.complete.fraction': [
('CommonDateMinuteTask', 4. / 11),
],
}
)
def test_start_long_before_long_minutes_back_and_with_long_minutes_forward(self):
self._nonempty_subcase(
{
'now': datetime_to_epoch(datetime.datetime(2017, 3, 22, 20, 4, 29)),
'start': datetime.datetime(2011, 3, 20, 17, 10, 0),
'task_limit': 4,
'minutes_back': 365 * 24 * 60,
'minutes_forward': 365 * 24 * 60,
'minutes_interval': 5,
},
(datetime.datetime(2016, 3, 22, 20, 5), datetime.datetime(2018, 3, 22, 20, 0)),
[
'CommonDateMinuteTask(dh=2016-03-22T2005)',
'CommonDateMinuteTask(dh=2016-03-22T2010)',
'CommonDateMinuteTask(dh=2016-03-22T2015)',
'CommonDateMinuteTask(dh=2016-03-22T2020)',
],
{
'event.tools.range.delay': [
('CommonDateMinuteTask', 210240),
],
'event.tools.range.complete.count': [
('CommonDateMinuteTask', 737020),
],
'event.tools.range.complete.fraction': [
('CommonDateMinuteTask', 737020. / (737020 + 7)),
],
}
)
class FilesystemInferenceTest(unittest.TestCase):
def _test_filesystems_and_globs(self, datetime_to_task, datetime_to_re, expected):
actual = list(_get_filesystems_and_globs(datetime_to_task, datetime_to_re))
self.assertEqual(len(actual), len(expected))
for (actual_filesystem, actual_glob), (expected_filesystem, expected_glob) in zip(actual, expected):
self.assertTrue(isinstance(actual_filesystem, expected_filesystem))
self.assertEqual(actual_glob, expected_glob)
def test_date_glob_successfully_inferred(self):
self._test_filesystems_and_globs(
lambda d: CommonDateTask(d),
lambda d: d.strftime('(%Y).*(%m).*(%d)'),
[
(MockFileSystem, '/n2000y01a05n/[0-9][0-9][0-9][0-9]_[0-9][0-9]-_-[0-9][0-9]aww/21mm01dara21'),
]
)
def test_datehour_glob_successfully_inferred(self):
self._test_filesystems_and_globs(
lambda d: CommonDateHourTask(d),
lambda d: d.strftime('(%Y).*(%m).*(%d).*(%H)'),
[
(MockFileSystem, '/n2000y01a05n/[0-9][0-9][0-9][0-9]_[0-9][0-9]-_-[0-9][0-9]aww/21mm[0-9][0-9]dara21'),
]
)
def test_dateminute_glob_successfully_inferred(self):
self._test_filesystems_and_globs(
lambda d: CommonDateMinuteTask(d),
lambda d: d.strftime('(%Y).*(%m).*(%d).*(%H).*(%M)'),
[
(MockFileSystem, '/n2000y01a05n/[0-9][0-9][0-9][0-9]_[0-9][0-9]-_-[0-9][0-9]aww/21mm[0-9][0-9][0-9][0-9]dara21'),
]
)
def test_wrapped_datehour_globs_successfully_inferred(self):
self._test_filesystems_and_globs(
lambda d: CommonWrapperTask(d),
lambda d: d.strftime('(%Y).*(%m).*(%d).*(%H)'),
[
(MockFileSystem, 'TaskA/[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]'),
(MockFileSystem, 'TaskB/no/worries[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]'),
]
)
def test_inconsistent_output_datehour_glob_not_inferred(self):
class InconsistentlyOutputtingDateHourTask(luigi.Task):
dh = luigi.DateHourParameter()
def output(self):
base = self.dh.strftime('/even/%Y%m%d%H')
if self.dh.hour % 2 == 0:
return MockTarget(base)
else:
return {
'spi': MockTarget(base + '/something.spi'),
'spl': MockTarget(base + '/something.spl'),
}
def test_raise_not_implemented():
list(_get_filesystems_and_globs(
lambda d: InconsistentlyOutputtingDateHourTask(d),
lambda d: d.strftime('(%Y).*(%m).*(%d).*(%H)')))
self.assertRaises(NotImplementedError, test_raise_not_implemented)
def test_wrapped_inconsistent_datehour_globs_not_inferred(self):
class InconsistentlyParameterizedWrapperTask(luigi.WrapperTask):
dh = luigi.DateHourParameter()
def requires(self):
yield TaskA(dh=self.dh - datetime.timedelta(days=1))
yield TaskB(dh=self.dh, complicator='no/worries')
def test_raise_not_implemented():
list(_get_filesystems_and_globs(
lambda d: InconsistentlyParameterizedWrapperTask(d),
lambda d: d.strftime('(%Y).*(%m).*(%d).*(%H)')))
self.assertRaises(NotImplementedError, test_raise_not_implemented)
class RangeDailyTest(unittest.TestCase):
def test_bulk_complete_correctly_interfaced(self):
class BulkCompleteDailyTask(luigi.Task):
d = luigi.DateParameter()
@classmethod
def bulk_complete(self, parameter_tuples):
return list(parameter_tuples)[:-2]
def output(self):
raise RuntimeError("Shouldn't get called while resolving deps via bulk_complete")
task = RangeDaily(now=datetime_to_epoch(datetime.datetime(2015, 12, 1)),
of=BulkCompleteDailyTask,
start=datetime.date(2015, 11, 1),
stop=datetime.date(2015, 12, 1))
expected = [
'BulkCompleteDailyTask(d=2015-11-29)',
'BulkCompleteDailyTask(d=2015-11-30)',
]
actual = [str(t) for t in task.requires()]
self.assertEqual(actual, expected)
def test_bulk_complete_of_params(self):
class BulkCompleteDailyTask(luigi.Task):
non_positional_arbitrary_argument = luigi.Parameter(default="whatever", positional=False, significant=False)
d = luigi.DateParameter()
arbitrary_argument = luigi.BoolParameter()
@classmethod
def bulk_complete(cls, parameter_tuples):
ptuples = list(parameter_tuples)
for t in map(cls, ptuples):
assert t.arbitrary_argument
return ptuples[:-2]
def output(self):
raise RuntimeError("Shouldn't get called while resolving deps via bulk_complete")
task = RangeDaily(now=datetime_to_epoch(datetime.datetime(2015, 12, 1)),
of=BulkCompleteDailyTask,
of_params=dict(arbitrary_argument=True),
start=datetime.date(2015, 11, 1),
stop=datetime.date(2015, 12, 1))
expected = [
'BulkCompleteDailyTask(d=2015-11-29, arbitrary_argument=True)',
'BulkCompleteDailyTask(d=2015-11-30, arbitrary_argument=True)',
]
actual = [str(t) for t in task.requires()]
self.assertEqual(actual, expected)
@mock.patch('luigi.mock.MockFileSystem.listdir',
new=mock_listdir([
'/data/2014/p/v/z/2014_/_03-_-21octor/20/ZOOO',
'/data/2014/p/v/z/2014_/_03-_-23octor/20/ZOOO',
'/data/2014/p/v/z/2014_/_03-_-24octor/20/ZOOO',
]))
@mock.patch('luigi.mock.MockFileSystem.exists',
new=mock_exists_always_true)
def test_missing_tasks_correctly_required(self):
class SomeDailyTask(luigi.Task):
d = luigi.DateParameter()
def output(self):
return MockTarget(self.d.strftime('/data/2014/p/v/z/%Y_/_%m-_-%doctor/20/ZOOO'))
task = RangeDaily(now=datetime_to_epoch(datetime.datetime(2016, 4, 1)),
of=SomeDailyTask,
start=datetime.date(2014, 3, 20),
task_limit=3,
days_back=3 * 365)
expected = [
'SomeDailyTask(d=2014-03-20)',
'SomeDailyTask(d=2014-03-22)',
'SomeDailyTask(d=2014-03-25)',
]
actual = [str(t) for t in task.requires()]
self.assertEqual(actual, expected)
class RangeHourlyTest(unittest.TestCase):
# fishy to mock the mock, but MockFileSystem doesn't support globs yet
@mock.patch('luigi.mock.MockFileSystem.listdir', new=mock_listdir(mock_contents))
@mock.patch('luigi.mock.MockFileSystem.exists',
new=mock_exists_always_true)
def test_missing_tasks_correctly_required(self):
for task_path in task_a_paths:
MockTarget(task_path)
# this test takes a few seconds. Since stop is not defined,
# finite_datetimes constitute many years to consider
task = RangeHourly(now=datetime_to_epoch(datetime.datetime(2016, 4, 1)),
of=TaskA,
start=datetime.datetime(2014, 3, 20, 17),
task_limit=3,
hours_back=3 * 365 * 24)
actual = [str(t) for t in task.requires()]
self.assertEqual(actual, expected_a)
@mock.patch('luigi.mock.MockFileSystem.listdir', new=mock_listdir(mock_contents))
@mock.patch('luigi.mock.MockFileSystem.exists',
new=mock_exists_always_true)
def test_missing_wrapper_tasks_correctly_required(self):
task = RangeHourly(
now=datetime_to_epoch(datetime.datetime(2040, 4, 1)),
of=CommonWrapperTask,
start=datetime.datetime(2014, 3, 20, 23),
stop=datetime.datetime(2014, 3, 21, 6),
hours_back=30 * 365 * 24)
actual = [str(t) for t in task.requires()]
self.assertEqual(actual, expected_wrapper)
def test_bulk_complete_correctly_interfaced(self):
class BulkCompleteHourlyTask(luigi.Task):
dh = luigi.DateHourParameter()
@classmethod
def bulk_complete(cls, parameter_tuples):
return parameter_tuples[:-2]
def output(self):
raise RuntimeError("Shouldn't get called while resolving deps via bulk_complete")
task = RangeHourly(now=datetime_to_epoch(datetime.datetime(2015, 12, 1)),
of=BulkCompleteHourlyTask,
start=datetime.datetime(2015, 11, 1),
stop=datetime.datetime(2015, 12, 1))
expected = [
'BulkCompleteHourlyTask(dh=2015-11-30T22)',
'BulkCompleteHourlyTask(dh=2015-11-30T23)',
]
actual = [str(t) for t in task.requires()]
self.assertEqual(actual, expected)
def test_bulk_complete_of_params(self):
class BulkCompleteHourlyTask(luigi.Task):
non_positional_arbitrary_argument = luigi.Parameter(default="whatever", positional=False, significant=False)
dh = luigi.DateHourParameter()
arbitrary_argument = luigi.BoolParameter()
@classmethod
def bulk_complete(cls, parameter_tuples):
for t in map(cls, parameter_tuples):
assert t.arbitrary_argument
return parameter_tuples[:-2]
def output(self):
raise RuntimeError("Shouldn't get called while resolving deps via bulk_complete")
task = RangeHourly(now=datetime_to_epoch(datetime.datetime(2015, 12, 1)),
of=BulkCompleteHourlyTask,
of_params=dict(arbitrary_argument=True),
start=datetime.datetime(2015, 11, 1),
stop=datetime.datetime(2015, 12, 1))
expected = [
'BulkCompleteHourlyTask(dh=2015-11-30T22, arbitrary_argument=True)',
'BulkCompleteHourlyTask(dh=2015-11-30T23, arbitrary_argument=True)',
]
actual = [str(t) for t in task.requires()]
self.assertEqual(actual, expected)
@mock.patch('luigi.mock.MockFileSystem.exists',
new=mock_exists_always_false)
def test_missing_directory(self):
task = RangeHourly(now=datetime_to_epoch(
datetime.datetime(2014, 4, 1)),
of=TaskC,