forked from cylc/cylc-flow
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_iso8601.py
More file actions
954 lines (827 loc) · 30.7 KB
/
test_iso8601.py
File metadata and controls
954 lines (827 loc) · 30.7 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
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
# THIS FILE IS PART OF THE CYLC WORKFLOW ENGINE.
# Copyright (C) NIWA & British Crown (Met Office) & Contributors.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from datetime import (
datetime,
timezone,
)
import pytest
from pytest import param
from cylc.flow.cycling.iso8601 import (
ISO8601Interval,
ISO8601Point,
ISO8601Sequence,
ingest_time,
)
from cylc.flow.cycling.loader import ISO8601_CYCLING_TYPE
def test_exclusions_simple(set_cycling_type):
"""Test the generation of points for sequences with exclusions."""
set_cycling_type(ISO8601_CYCLING_TYPE, "Z")
sequence = ISO8601Sequence("PT1H!20000101T02Z", "20000101T00Z")
output = []
point = sequence.get_start_point()
count = 0
while point and count < 4:
output.append(point)
point = sequence.get_next_point(point)
count += 1
output = [str(out) for out in output]
assert output == [
"20000101T0000Z",
"20000101T0100Z",
"20000101T0300Z",
"20000101T0400Z",
]
def test_exclusions_offset(set_cycling_type):
"""Test the generation of points for sequences with exclusions
that have an offset on the end"""
set_cycling_type(ISO8601_CYCLING_TYPE, "Z")
sequence = ISO8601Sequence("PT1H!20000101T00Z+PT1H", "20000101T00Z")
output = []
point = sequence.get_start_point()
count = 0
while point and count < 4:
output.append(point)
point = sequence.get_next_point(point)
count += 1
output = [str(out) for out in output]
assert output == [
"20000101T0000Z",
"20000101T0200Z",
"20000101T0300Z",
"20000101T0400Z",
]
def test_multiple_exclusions_complex1(set_cycling_type):
"""Tests sequences that have multiple exclusions and a more
complicated format"""
set_cycling_type(ISO8601_CYCLING_TYPE, "Z")
# A sequence that specifies a dep start time
sequence = ISO8601Sequence(
"20000101T01Z/PT1H!20000101T02Z", "20000101T01Z"
)
output = []
point = sequence.get_start_point()
count = 0
# We are going to make four sequence points
while point and count < 4:
output.append(point)
point = sequence.get_next_point(point)
count += 1
output = [str(out) for out in output]
# We should expect one of the hours to be excluded: T02
assert output == [
"20000101T0100Z",
"20000101T0300Z",
"20000101T0400Z",
"20000101T0500Z",
]
def test_multiple_exclusions_complex2(set_cycling_type):
"""Tests sequences that have multiple exclusions and a more
complicated format"""
set_cycling_type(ISO8601_CYCLING_TYPE, "Z")
# A sequence that specifies a dep start time
sequence = ISO8601Sequence(
"20000101T01Z/PT1H!" "(20000101T02Z,20000101T03Z)",
"20000101T00Z",
"20000101T05Z",
)
output = []
point = sequence.get_start_point()
count = 0
# We are going to make four sequence points
while point and count < 3:
output.append(point)
point = sequence.get_next_point(point)
count += 1
output = [str(out) for out in output]
# We should expect two of the hours to be excluded: T02, T03
assert output == [
"20000101T0100Z",
"20000101T0400Z",
"20000101T0500Z",
]
def test_multiple_exclusions_simple(set_cycling_type):
"""Tests generation of points for sequences with multiple exclusions"""
set_cycling_type(ISO8601_CYCLING_TYPE, "Z")
sequence = ISO8601Sequence(
"PT1H!(20000101T02Z,20000101T03Z)", "20000101T00Z"
)
output = []
point = sequence.get_start_point()
count = 0
# We are going to make four sequence points
while point and count < 4:
output.append(point)
point = sequence.get_next_point(point)
count += 1
output = [str(out) for out in output]
# We should expect two of the hours to be excluded: T02 and T03
assert output == [
"20000101T0000Z",
"20000101T0100Z",
"20000101T0400Z",
"20000101T0500Z",
]
def test_advanced_exclusions_partial_datetime1(set_cycling_type):
"""Advanced exclusions refers to exclusions that are not just
simple points but could be time periods or recurrences such as
'!T06' or similar"""
set_cycling_type(ISO8601_CYCLING_TYPE, "Z")
# Run 3-hourly but not at 06:00 (from the ICP)
sequence = ISO8601Sequence("PT3H!T06", "20000101T00Z")
output = []
point = sequence.get_start_point()
count = 0
# We are going to make ten sequence points
while point and count < 10:
output.append(point)
point = sequence.get_next_point(point)
count += 1
output = [str(out) for out in output]
# We should expect every T06 to be excluded
assert output == [
"20000101T0000Z",
"20000101T0300Z",
"20000101T0900Z",
"20000101T1200Z",
"20000101T1500Z",
"20000101T1800Z",
"20000101T2100Z",
"20000102T0000Z",
"20000102T0300Z",
"20000102T0900Z",
]
def test_advanced_exclusions_partial_datetime2(set_cycling_type):
"""Advanced exclusions refers to exclusions that are not just
simple points but could be time periods or recurrences such as
'!T06' or similar"""
set_cycling_type(ISO8601_CYCLING_TYPE, "Z")
# Run hourly but not at 00:00, 06:00, 12:00, 18:00
sequence = ISO8601Sequence("T-00!(T00, T06, T12, T18)", "20000101T00Z")
output = []
point = sequence.get_start_point()
count = 0
# We are going to make 18 sequence points
while point and count < 18:
output.append(point)
point = sequence.get_next_point(point)
count += 1
output = [str(out) for out in output]
# We should expect T00, T06, T12, and T18 to be excluded
assert output == [
"20000101T0100Z",
"20000101T0200Z",
"20000101T0300Z",
"20000101T0400Z",
"20000101T0500Z",
"20000101T0700Z",
"20000101T0800Z",
"20000101T0900Z",
"20000101T1000Z",
"20000101T1100Z",
"20000101T1300Z",
"20000101T1400Z",
"20000101T1500Z",
"20000101T1600Z",
"20000101T1700Z",
"20000101T1900Z",
"20000101T2000Z",
"20000101T2100Z",
]
def test_advanced_exclusions_partial_datetime3(set_cycling_type):
"""Advanced exclusions refers to exclusions that are not just
simple points but could be time periods or recurrences such as
'!T06' or similar"""
set_cycling_type(ISO8601_CYCLING_TYPE, "Z")
# Run 5 minutely but not at 15 minutes past the hour from ICP
sequence = ISO8601Sequence("PT5M!T-15", "20000101T00Z")
output = []
point = sequence.get_start_point()
count = 0
# We are going to make 15 sequence points
while point and count < 15:
output.append(point)
point = sequence.get_next_point(point)
count += 1
output = [str(out) for out in output]
# We should expect xx:15 (15 minutes past the hour) to be excluded
assert output == [
"20000101T0000Z",
"20000101T0005Z",
"20000101T0010Z",
"20000101T0020Z",
"20000101T0025Z",
"20000101T0030Z",
"20000101T0035Z",
"20000101T0040Z",
"20000101T0045Z",
"20000101T0050Z",
"20000101T0055Z",
"20000101T0100Z",
"20000101T0105Z",
"20000101T0110Z",
"20000101T0120Z",
]
def test_advanced_exclusions_partial_datetime4(set_cycling_type):
"""Advanced exclusions refers to exclusions that are not just
simple points but could be time periods or recurrences such as
'!T06' or similar"""
set_cycling_type(ISO8601_CYCLING_TYPE, "Z")
# Run daily at 00:00 except on Mondays
sequence = ISO8601Sequence("T00!W-1T00", "20170422T00Z")
output = []
point = sequence.get_start_point()
count = 0
# We are going to make 19 sequence points
while point and count < 9:
output.append(point)
point = sequence.get_next_point(point)
count += 1
output = [str(out) for out in output]
# We should expect Monday 24th April and Monday 1st May
# to be excluded.
assert output == [
"20170422T0000Z",
"20170423T0000Z",
"20170425T0000Z",
"20170426T0000Z",
"20170427T0000Z",
"20170428T0000Z",
"20170429T0000Z",
"20170430T0000Z",
"20170502T0000Z",
]
def test_exclusions_to_string(set_cycling_type):
set_cycling_type(ISO8601_CYCLING_TYPE, "Z")
# Check that exclusions are not included where they should not be.
basic = ISO8601Sequence("PT1H", "2000", "2001")
assert "!" not in str(basic)
# Check that exclusions are parsable.
sequence = ISO8601Sequence("PT1H!(20000101T10Z, PT6H)", "2000", "2001")
sequence2 = ISO8601Sequence(str(sequence), "2000", "2001")
assert sequence == sequence2
def test_advanced_exclusions_sequences1(set_cycling_type):
"""Advanced exclusions refers to exclusions that are not just
simple points but could be time periods or recurrences such as
'!T06' or similar"""
set_cycling_type(ISO8601_CYCLING_TYPE, "Z")
# Run hourly from the ICP but not 3-hourly
sequence = ISO8601Sequence("PT1H!PT3H", "20000101T01Z")
output = []
point = sequence.get_start_point()
count = 0
# We are going to make six sequence points
while point and count < 6:
output.append(point)
point = sequence.get_next_point(point)
count += 1
output = [str(out) for out in output]
# We should expect to see hourly from ICP but not 3 hourly
assert output == [
"20000101T0200Z",
"20000101T0300Z",
"20000101T0500Z",
"20000101T0600Z",
"20000101T0800Z",
"20000101T0900Z",
]
def test_advanced_exclusions_sequences2(set_cycling_type):
"""Advanced exclusions refers to exclusions that are not just
simple points but could be time periods or recurrences such as
'!T06' or similar"""
set_cycling_type(ISO8601_CYCLING_TYPE, "Z")
# Run hourly on the hour but not 3 hourly on the hour
sequence = ISO8601Sequence("T-00!T-00/PT3H", "20000101T00Z")
output = []
point = sequence.get_start_point()
count = 0
# We are going to make six sequence points
while point and count < 6:
output.append(point)
point = sequence.get_next_point(point)
count += 1
output = [str(out) for out in output]
assert output == [
"20000101T0100Z",
"20000101T0200Z",
"20000101T0400Z",
"20000101T0500Z",
"20000101T0700Z",
"20000101T0800Z",
]
def test_advanced_exclusions_sequences3(set_cycling_type):
"""Advanced exclusions refers to exclusions that are not just
simple points but could be time periods or recurrences such as
'!T06' or similar"""
set_cycling_type(ISO8601_CYCLING_TYPE, "Z")
# Run daily at 12:00 except every 3rd day
sequence = ISO8601Sequence("T12!P3D", "20000101T12Z")
output = []
point = sequence.get_start_point()
count = 0
# We are going to make six sequence points
while point and count < 6:
output.append(point)
point = sequence.get_next_point(point)
count += 1
output = [str(out) for out in output]
assert output == [
"20000102T1200Z",
"20000103T1200Z",
"20000105T1200Z",
"20000106T1200Z",
"20000108T1200Z",
"20000109T1200Z",
]
def test_advanced_exclusions_sequences4(set_cycling_type):
"""Advanced exclusions refers to exclusions that are not just
simple points but could be time periods or recurrences such as
'!T06' or similar"""
set_cycling_type(ISO8601_CYCLING_TYPE, "Z")
# Run every hour from 01:00 excluding every 3rd hour.
sequence = ISO8601Sequence("T01/PT1H!+PT3H/PT3H", "20000101T01Z")
output = []
point = sequence.get_start_point()
count = 0
# We are going to make six sequence points
while point and count < 6:
output.append(point)
point = sequence.get_next_point(point)
count += 1
output = [str(out) for out in output]
assert output == [
"20000101T0100Z",
"20000101T0200Z",
"20000101T0300Z",
"20000101T0500Z",
"20000101T0600Z",
"20000101T0800Z",
]
def test_advanced_exclusions_sequences5(set_cycling_type):
"""Advanced exclusions refers to exclusions that are not just
simple points but could be time periods or recurrences such as
'!T06' or similar"""
set_cycling_type(ISO8601_CYCLING_TYPE, "Z")
# Run every hour from 01:00 excluding every 3rd hour.
sequence = ISO8601Sequence("T-00 ! 2000", "20000101T00Z")
output = []
point = sequence.get_start_point()
count = 0
# We are going to make six sequence points
while point and count < 6:
output.append(point)
point = sequence.get_next_point(point)
count += 1
output = [str(out) for out in output]
assert output == [
"20000101T0100Z",
"20000101T0200Z",
"20000101T0300Z",
"20000101T0400Z",
"20000101T0500Z",
"20000101T0600Z",
]
def test_advanced_exclusions_sequences_mix_points_sequences(set_cycling_type):
"""Advanced exclusions refers to exclusions that are not just
simple points but could be time periods or recurrences such as
'!T06' or similar"""
set_cycling_type(ISO8601_CYCLING_TYPE, "Z")
# Run every hour from 01:00 excluding every 3rd hour.
sequence = ISO8601Sequence("T-00 ! (2000, PT2H)", "20000101T00Z")
output = []
point = sequence.get_start_point()
count = 0
# We are going to make six sequence points
while point and count < 6:
output.append(point)
point = sequence.get_next_point(point)
count += 1
output = [str(out) for out in output]
assert output == [
"20000101T0100Z",
"20000101T0300Z",
"20000101T0500Z",
"20000101T0700Z",
"20000101T0900Z",
"20000101T1100Z",
]
def test_advanced_exclusions_sequences_implied_start_point(set_cycling_type):
"""Advanced exclusions refers to exclusions that are not just
simple points but could be time periods or recurrences such as
'!T06' or similar"""
set_cycling_type(ISO8601_CYCLING_TYPE, "Z")
# Run every hour from 01:00 excluding every 3rd hour.
sequence = ISO8601Sequence("T05/PT1H!PT3H", "20000101T00Z")
output = []
point = sequence.get_start_point()
count = 0
# We are going to make six sequence points
while point and count < 6:
output.append(point)
point = sequence.get_next_point(point)
count += 1
output = [str(out) for out in output]
assert output == [
"20000101T0600Z",
"20000101T0700Z",
"20000101T0900Z",
"20000101T1000Z",
"20000101T1200Z",
"20000101T1300Z",
]
def test_point_init():
"""It should error if inited with anything other than a string."""
ISO8601Point('1000')
with pytest.raises(TypeError):
ISO8601Point(1000)
with pytest.raises(TypeError):
ISO8601Interval(1000)
def test_exclusions_sequences_points(set_cycling_type):
"""Test ISO8601Sequence methods for sequences with exclusions"""
set_cycling_type(ISO8601_CYCLING_TYPE, "Z")
# Run every hour from 01:00 excluding every 3rd hour
sequence = ISO8601Sequence("T01/PT1H!PT3H", "20000101T01Z")
point_0 = ISO8601Point("20000101T00Z")
point_1 = ISO8601Point("20000101T01Z")
point_2 = ISO8601Point("20000101T02Z")
point_3 = ISO8601Point("20000101T03Z")
point_4 = ISO8601Point("20000101T04Z")
assert point_0 not in sequence.exclusions
assert point_1 in sequence.exclusions
assert sequence.is_on_sequence(point_2)
assert sequence.is_on_sequence(point_3)
assert not sequence.is_on_sequence(point_4)
assert point_4 in sequence.exclusions
def test_exclusions_extensive(set_cycling_type):
"""Test ISO8601Sequence methods for sequences with exclusions"""
set_cycling_type(ISO8601_CYCLING_TYPE, "+05")
sequence = ISO8601Sequence(
"PT1H!20000101T02+05", "20000101T00", "20000101T05"
)
point_0 = ISO8601Point("20000101T0000+05")
point_1 = ISO8601Point("20000101T0100+05")
point_2 = ISO8601Point("20000101T0200+05") # The excluded point.
point_3 = ISO8601Point("20000101T0300+05")
assert not sequence.is_on_sequence(point_2)
assert not sequence.is_valid(point_2)
assert sequence.get_prev_point(point_2) == point_1
assert sequence.get_prev_point(point_3) == point_1
assert sequence.get_prev_point(point_2) == point_1
assert sequence.get_nearest_prev_point(point_3) == point_1
assert sequence.get_next_point(point_1) == point_3
assert sequence.get_next_point(point_2) == point_3
sequence = ISO8601Sequence("PT1H!20000101T00+05", "20000101T00+05")
assert sequence.get_first_point(point_0) == point_1
assert sequence.get_start_point() == point_1
def test_multiple_exclusions_extensive(set_cycling_type):
"""Test ISO8601Sequence methods for sequences with multiple exclusions"""
set_cycling_type(ISO8601_CYCLING_TYPE, "+05")
sequence = ISO8601Sequence(
"PT1H!(20000101T02,20000101T03)", "20000101T00", "20000101T06"
)
point_0 = ISO8601Point("20000101T0000+05")
point_1 = ISO8601Point("20000101T0100+05")
point_2 = ISO8601Point("20000101T0200+05") # First excluded point
point_3 = ISO8601Point("20000101T0300+05") # Second excluded point
point_4 = ISO8601Point("20000101T0400+05")
# Check the excluded points are not on the sequence
assert not sequence.is_on_sequence(point_2)
assert not sequence.is_on_sequence(point_3)
assert not sequence.is_valid(point_2) # Should be excluded
assert not sequence.is_valid(point_3) # Should be excluded
# Check that we can correctly retrieve previous points
assert sequence.get_prev_point(point_2) == point_1
# Should skip two excluded points
assert sequence.get_prev_point(point_4) == point_1
assert sequence.get_prev_point(point_2) == point_1
assert sequence.get_nearest_prev_point(point_4) == point_1
assert sequence.get_next_point(point_1) == point_4
assert sequence.get_next_point(point_3) == point_4
sequence = ISO8601Sequence("PT1H!20000101T00+05", "20000101T00")
# Check that the first point is after 00.
assert sequence.get_first_point(point_0) == point_1
assert sequence.get_start_point() == point_1
# Check a longer list of exclusions
# Also note you can change the format of the exclusion list
# (removing the parentheses)
sequence = ISO8601Sequence(
"PT1H!(20000101T02+05, 20000101T03+05," "20000101T04+05)",
"20000101T00",
"20000101T06",
)
assert sequence.get_prev_point(point_3) == point_1
assert sequence.get_prev_point(point_4) == point_1
def test_exclusion_zero_duration_warning(set_cycling_type, caplog, log_filter):
"""It should not log zero-duration warnings for exclusion points.
Exclusions may either be sequences or points. We first attempt to parse
them as sequences, if this fails, we attempt to parse them as points.
The zero-duration recurrence warning would be logged if we attempted to
parse a point as a sequence. To avoid spurious warnings this should be
turned off for exclusion parsing.
"""
# parsing a point as a sequences causes a zero-duration warning
set_cycling_type(ISO8601_CYCLING_TYPE, "+05")
with pytest.raises(Exception):
ISO8601Sequence('3000', '2999')
assert log_filter(contains='zero-duration')
# parsing a point in an exclusion should not
caplog.clear()
ISO8601Sequence('P1Y ! 3000', '2999')
assert not log_filter(contains='zero-duration')
def test_simple(set_cycling_type):
"""Run some simple tests for date-time cycling."""
set_cycling_type(ISO8601_CYCLING_TYPE, "Z")
p_start = ISO8601Point("20100808T00")
p_stop = ISO8601Point("20100808T02")
i = ISO8601Interval("PT6H")
assert p_start - i == ISO8601Point("20100807T18")
assert p_stop + i == ISO8601Point("20100808T08")
sequence = ISO8601Sequence(
"PT10M",
str(p_start),
str(p_stop),
)
sequence.set_offset(-ISO8601Interval("PT10M"))
point = sequence.get_next_point(ISO8601Point("20100808T0000"))
assert point == ISO8601Point("20100808T0010")
output = []
# Test point generation forwards.
while point and point < p_stop:
output.append(point)
assert sequence.is_on_sequence(point)
point = sequence.get_next_point(point)
assert [str(out) for out in output] == [
"20100808T0010Z",
"20100808T0020Z",
"20100808T0030Z",
"20100808T0040Z",
"20100808T0050Z",
"20100808T0100Z",
"20100808T0110Z",
"20100808T0120Z",
"20100808T0130Z",
"20100808T0140Z",
"20100808T0150Z",
]
assert point == ISO8601Point("20100808T0200")
# Test point generation backwards.
output = []
while point and point >= p_start:
output.append(point)
assert sequence.is_on_sequence(point)
point = sequence.get_prev_point(point)
assert [str(out) for out in output] == [
"20100808T0200Z",
"20100808T0150Z",
"20100808T0140Z",
"20100808T0130Z",
"20100808T0120Z",
"20100808T0110Z",
"20100808T0100Z",
"20100808T0050Z",
"20100808T0040Z",
"20100808T0030Z",
"20100808T0020Z",
"20100808T0010Z",
"20100808T0000Z",
]
assert not sequence.is_on_sequence(ISO8601Point("20100809T0005"))
@pytest.mark.parametrize(
'value, expected', [
('next(T2100Z)', '20100808T2100Z'),
('next(T00)', '20100809T0000Z'),
('next(T-15)', '20100808T1615Z'),
('next(T-45)', '20100808T1545Z'),
('next(-10)', '21100101T0000Z'),
('next(-1008)', '21100801T0000Z'),
('next(--10)', '20101001T0000Z'),
('next(--0325)', '20110325T0000Z'),
('next(---10)', '20100810T0000Z'),
('next(---05T1200Z)', '20100905T1200Z'),
param('next(--08-08)', '20110808T0000Z', marks=pytest.mark.xfail),
('next(T15)', '20100809T1500Z'),
('next(T-41)', '20100808T1541Z'),
]
)
def test_next_simple(value: str, expected: str, set_cycling_type):
"""Test the generation of CP using 'next' from single input."""
set_cycling_type(ISO8601_CYCLING_TYPE, "Z")
my_now = "2010-08-08T15:41Z"
assert ingest_time(value, my_now) == expected
@pytest.mark.parametrize(
'value, expected', [
('previous(T2100Z)', '20100807T2100Z'),
('previous(T00)', '20100808T0000Z'),
('previous(T-15)', '20100808T1515Z'),
('previous(T-45)', '20100808T1445Z'),
('previous(-10)', '20100101T0000Z'),
('previous(-1008)', '20100801T0000Z'),
('previous(--10)', '20091001T0000Z'),
('previous(--0325)', '20100325T0000Z'),
('previous(---10)', '20100710T0000Z'),
('previous(---05T1200Z)', '20100805T1200Z'),
param('previous(--08-08)', '20100808T0000Z', marks=pytest.mark.xfail),
('previous(T15)', '20100808T1500Z'),
('previous(T-41)', '20100808T1441Z'),
]
)
def test_previous_simple(value: str, expected: str, set_cycling_type):
"""Test the generation of CP using 'previous' from single input."""
set_cycling_type(ISO8601_CYCLING_TYPE, "Z")
my_now = "2010-08-08T15:41Z"
assert ingest_time(value, my_now) == expected
def test_sequence(set_cycling_type):
"""Test the generation of CP from list input."""
set_cycling_type(ISO8601_CYCLING_TYPE, "Z")
my_now = "20100808T1540Z"
sequence = (
"next(T-00;T-15;T-30;T-45)", # 20100808T1545Z
"previous(T-00;T-15;T-30;T-45)", # 20100808T1530Z
"next(T00;T06;T12;T18)", # 20100808T1800Z
"previous(T00;T06;T12;T18)",
) # 20100808T1200Z
output = []
for point in sequence:
output.append(ingest_time(point, my_now))
assert output == [
"20100808T1545Z",
"20100808T1530Z",
"20100808T1800Z",
"20100808T1200Z",
]
def test_offset_simple(set_cycling_type):
"""Test the generation of offset CP."""
set_cycling_type(ISO8601_CYCLING_TYPE, "Z")
my_now = "20100808T1540Z"
sequence = (
"PT15M", # 20100808T1555Z
"-PT30M", # 20100808T1510Z
"PT1H", # 20100808T1640Z
"-PT18H", # 20100807T2140Z
"P3D", # 20100811T1540Z
"-P2W", # 20100725T1540Z
"P6M", # 20110208T1540Z
"-P1M", # 20100708T1540Z
"P1Y", # 20110808T1540Z
"-P5Y", # 20050808T1540Z
)
output = []
for point in sequence:
output.append(ingest_time(point, my_now))
assert output == [
"20100808T1555Z",
"20100808T1510Z",
"20100808T1640Z",
"20100807T2140Z",
"20100811T1540Z",
"20100725T1540Z",
"20110208T1540Z",
"20100708T1540Z",
"20110808T1540Z",
"20050808T1540Z",
]
def test_offset(set_cycling_type):
"""Test the generation of offset CP with 'next' and 'previous'."""
set_cycling_type(ISO8601_CYCLING_TYPE, "Z")
my_now = "20100808T1540Z"
sequence = (
"next(T06) +P1D", # 20100810T0600Z
"previous(T-30) -PT12H", # 20100808T0330Z
"next(T00;T06;T12;T18) -P1W", # 20100801T1800Z
"previous(T00;T06;T12;T18) +PT1H", # 20100808T1300Z
)
output = []
for point in sequence:
output.append(ingest_time(point, my_now))
assert output == [
"20100810T0600Z",
"20100808T0330Z",
"20100801T1800Z",
"20100808T1300Z",
]
def test_weeks_days(set_cycling_type):
"""Test the generation of CP with day-of-week,
ordinal day, and week (with day-of-week specified)."""
set_cycling_type(ISO8601_CYCLING_TYPE, "Z")
my_now = "20100808T1540Z"
sequence = (
"next(-W-1)", # 20100809T0000Z
"previous(-W-4)", # 20100805T0000Z
"next(-010)", # 20110110T0000Z
"previous(-101)", # 20100411T0000Z
"next(-W40-1)", # 20101004T0000Z
"previous(-W05-1)", # 20100201T0000Z
"next(-W05-5)", # 20110204T0000Z
"previous(-W40-4)", # 20091001T0000Z
)
output = []
for point in sequence:
output.append(ingest_time(point, my_now))
assert output == [
"20100809T0000Z",
"20100805T0000Z",
"20110110T0000Z",
"20100411T0000Z",
"20101004T0000Z",
"20100201T0000Z",
"20110204T0000Z",
"20091001T0000Z",
]
@pytest.mark.parametrize(
'value, expected', [
('next(T-00)', '20180314T1600Z'),
('previous(T-00)', '20180314T1500Z'),
('next(T-00; T-15; T-30; T-45)', '20180314T1515Z'),
('previous(T-00; T-15; T-30; T-45)', '20180314T1500Z'),
('next(T00)', '20180315T0000Z'),
('previous(T00)', '20180314T0000Z'),
('next(T06:30Z)', '20180315T0630Z'),
('previous(T06:30) -P1D', '20180313T0630Z'),
('next(T00; T06; T12; T18)', '20180314T1800Z'),
('previous(T00; T06; T12; T18)', '20180314T1200Z'),
('next(T00; T06; T12; T18)+P1W', '20180321T1800Z'),
('PT1H', '20180314T1612Z'),
('-P1M', '20180214T1512Z'),
('next(-00)', '21000101T0000Z'),
('previous(--01)', '20180101T0000Z'),
('next(---01)', '20180401T0000Z'),
('previous(--1225)', '20171225T0000Z'),
('next(-2006)', '20200601T0000Z'),
('previous(-W101)', '20180305T0000Z'),
('next(-W-1; -W-3; -W-5)', '20180314T0000Z'),
('next(-001; -091; -181; -271)', '20180401T0000Z'),
('previous(-365T12Z)', '20171231T1200Z'),
]
)
def test_user_guide_examples(value: str, expected: str, set_cycling_type):
"""Test the offset CP examples in the Cylc user guide.
https://cylc.github.io/cylc-doc/stable/html/user-guide/writing-workflows/scheduling.html
"""
set_cycling_type(ISO8601_CYCLING_TYPE, "Z")
my_now = "2018-03-14T15:12Z"
assert ingest_time(value, my_now) == expected
def test_next_simple_no_now(set_cycling_type):
"""Test the generation of CP using 'next' with no value for `now`."""
set_cycling_type(ISO8601_CYCLING_TYPE, "Z")
my_now = None
point = "next(T00Z)+P1D"
output = ingest_time(point, my_now)
current_time = datetime.now(timezone.utc)
# my_now is None, but ingest_time will have used a similar time, and
# the returned value must be after current_time
output_time = datetime.strptime(output, "%Y%m%dT%H%M%z")
assert current_time < output_time
def test_integer_cycling_is_returned(set_cycling_type):
"""Test that when integer points are given, the same value is
returned."""
set_cycling_type(ISO8601_CYCLING_TYPE, "Z")
integer_point = "1"
assert integer_point, ingest_time(integer_point is None)
def test_expanded_dates_are_returned(set_cycling_type):
"""Test that when expanded dates are given, the same value is
returned."""
set_cycling_type(ISO8601_CYCLING_TYPE, "Z")
expanded_date = "+0100400101T0000Z"
assert expanded_date, ingest_time(expanded_date is None)
def test_timepoint_truncated(set_cycling_type):
"""Test that when a timepoint is given, and is truncated, then the
value is added to `now`."""
set_cycling_type(ISO8601_CYCLING_TYPE, "Z")
my_now = "2018-03-14T15:12Z"
timepoint_truncated = "T15:00Z" # 20180315T1500Z
output = ingest_time(timepoint_truncated, my_now)
assert output == "20180315T1500Z"
def test_timepoint(set_cycling_type):
"""Test that when a timepoint is given, and is not truncated, the
same value is returned."""
set_cycling_type(ISO8601_CYCLING_TYPE, "Z")
my_now = "2018-03-14T15:12Z"
timepoint_truncated = "19951231T0630" # 19951231T0630
output = ingest_time(timepoint_truncated, my_now)
assert output == "19951231T0630"
@pytest.mark.parametrize(
"_input, errortext",
(
("next (T-00, T-30)", "T-00;T-30"),
("next (wildebeest)", "Invalid ISO 8601 date"),
),
)
def test_validate_fails_comma_sep_offset_list(
_input, errortext, set_cycling_type
):
"""It raises an exception if validating a list separated by commas"""
set_cycling_type(ISO8601_CYCLING_TYPE, "Z")
with pytest.raises(Exception, match=errortext):
ingest_time(_input)