| 1 |
# THIS FILE IS PART OF THE CYLC WORKFLOW ENGINE. |
| 2 |
# Copyright (C) NIWA & British Crown (Met Office) & Contributors. |
| 3 |
# |
| 4 |
# This program is free software: you can redistribute it and/or modify |
| 5 |
# it under the terms of the GNU General Public License as published by |
| 6 |
# the Free Software Foundation, either version 3 of the License, or |
| 7 |
# (at your option) any later version. |
| 8 |
# |
| 9 |
# This program is distributed in the hope that it will be useful, |
| 10 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 |
# GNU General Public License for more details. |
| 13 |
# |
| 14 |
# You should have received a copy of the GNU General Public License |
| 15 |
# along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 |
|
| 17 |
from datetime import datetime |
| 18 |
|
| 19 |
import pytest |
| 20 |
from pytest import param |
| 21 |
|
| 22 |
from cylc.flow.cycling.iso8601 import ( |
| 23 |
ISO8601Interval, |
| 24 |
ISO8601Point, |
| 25 |
ISO8601Sequence, |
| 26 |
ingest_time, |
| 27 |
) |
| 28 |
from cylc.flow.cycling.loader import ISO8601_CYCLING_TYPE |
| 29 |
|
| 30 |
|
| 31 |
def test_exclusions_simple(set_cycling_type): |
| 32 |
"""Test the generation of points for sequences with exclusions.""" |
| 33 |
set_cycling_type(ISO8601_CYCLING_TYPE, "Z") |
| 34 |
sequence = ISO8601Sequence("PT1H!20000101T02Z", "20000101T00Z") |
| 35 |
|
| 36 |
output = [] |
| 37 |
point = sequence.get_start_point() |
| 38 |
count = 0 |
| 39 |
while point and count < 4: |
| 40 |
output.append(point) |
| 41 |
point = sequence.get_next_point(point) |
| 42 |
count += 1 |
| 43 |
output = [str(out) for out in output] |
| 44 |
assert output == [ |
| 45 |
"20000101T0000Z", |
| 46 |
"20000101T0100Z", |
| 47 |
"20000101T0300Z", |
| 48 |
"20000101T0400Z", |
| 49 |
] |
| 50 |
|
| 51 |
|
| 52 |
def test_exclusions_offset(set_cycling_type): |
| 53 |
"""Test the generation of points for sequences with exclusions |
| 54 |
that have an offset on the end""" |
| 55 |
set_cycling_type(ISO8601_CYCLING_TYPE, "Z") |
| 56 |
sequence = ISO8601Sequence("PT1H!20000101T00Z+PT1H", "20000101T00Z") |
| 57 |
|
| 58 |
output = [] |
| 59 |
point = sequence.get_start_point() |
| 60 |
count = 0 |
| 61 |
while point and count < 4: |
| 62 |
output.append(point) |
| 63 |
point = sequence.get_next_point(point) |
| 64 |
count += 1 |
| 65 |
output = [str(out) for out in output] |
| 66 |
assert output == [ |
| 67 |
"20000101T0000Z", |
| 68 |
"20000101T0200Z", |
| 69 |
"20000101T0300Z", |
| 70 |
"20000101T0400Z", |
| 71 |
] |
| 72 |
|
| 73 |
|
| 74 |
def test_multiple_exclusions_complex1(set_cycling_type): |
| 75 |
"""Tests sequences that have multiple exclusions and a more |
| 76 |
complicated format""" |
| 77 |
set_cycling_type(ISO8601_CYCLING_TYPE, "Z") |
| 78 |
|
| 79 |
# A sequence that specifies a dep start time |
| 80 |
sequence = ISO8601Sequence( |
| 81 |
"20000101T01Z/PT1H!20000101T02Z", "20000101T01Z" |
| 82 |
) |
| 83 |
|
| 84 |
output = [] |
| 85 |
point = sequence.get_start_point() |
| 86 |
count = 0 |
| 87 |
# We are going to make four sequence points |
| 88 |
while point and count < 4: |
| 89 |
output.append(point) |
| 90 |
point = sequence.get_next_point(point) |
| 91 |
count += 1 |
| 92 |
output = [str(out) for out in output] |
| 93 |
# We should expect one of the hours to be excluded: T02 |
| 94 |
assert output == [ |
| 95 |
"20000101T0100Z", |
| 96 |
"20000101T0300Z", |
| 97 |
"20000101T0400Z", |
| 98 |
"20000101T0500Z", |
| 99 |
] |
| 100 |
|
| 101 |
|
| 102 |
def test_multiple_exclusions_complex2(set_cycling_type): |
| 103 |
"""Tests sequences that have multiple exclusions and a more |
| 104 |
complicated format""" |
| 105 |
set_cycling_type(ISO8601_CYCLING_TYPE, "Z") |
| 106 |
|
| 107 |
# A sequence that specifies a dep start time |
| 108 |
sequence = ISO8601Sequence( |
| 109 |
"20000101T01Z/PT1H!" "(20000101T02Z,20000101T03Z)", |
| 110 |
"20000101T00Z", |
| 111 |
"20000101T05Z", |
| 112 |
) |
| 113 |
|
| 114 |
output = [] |
| 115 |
point = sequence.get_start_point() |
| 116 |
count = 0 |
| 117 |
# We are going to make four sequence points |
| 118 |
while point and count < 3: |
| 119 |
output.append(point) |
| 120 |
point = sequence.get_next_point(point) |
| 121 |
count += 1 |
| 122 |
output = [str(out) for out in output] |
| 123 |
# We should expect two of the hours to be excluded: T02, T03 |
| 124 |
assert output == [ |
| 125 |
"20000101T0100Z", |
| 126 |
"20000101T0400Z", |
| 127 |
"20000101T0500Z", |
| 128 |
] |
| 129 |
|
| 130 |
|
| 131 |
def test_multiple_exclusions_simple(set_cycling_type): |
| 132 |
"""Tests generation of points for sequences with multiple exclusions""" |
| 133 |
set_cycling_type(ISO8601_CYCLING_TYPE, "Z") |
| 134 |
sequence = ISO8601Sequence( |
| 135 |
"PT1H!(20000101T02Z,20000101T03Z)", "20000101T00Z" |
| 136 |
) |
| 137 |
|
| 138 |
output = [] |
| 139 |
point = sequence.get_start_point() |
| 140 |
count = 0 |
| 141 |
# We are going to make four sequence points |
| 142 |
while point and count < 4: |
| 143 |
output.append(point) |
| 144 |
point = sequence.get_next_point(point) |
| 145 |
count += 1 |
| 146 |
output = [str(out) for out in output] |
| 147 |
# We should expect two of the hours to be excluded: T02 and T03 |
| 148 |
assert output == [ |
| 149 |
"20000101T0000Z", |
| 150 |
"20000101T0100Z", |
| 151 |
"20000101T0400Z", |
| 152 |
"20000101T0500Z", |
| 153 |
] |
| 154 |
|
| 155 |
|
| 156 |
def test_advanced_exclusions_partial_datetime1(set_cycling_type): |
| 157 |
"""Advanced exclusions refers to exclusions that are not just |
| 158 |
simple points but could be time periods or recurrences such as |
| 159 |
'!T06' or similar""" |
| 160 |
set_cycling_type(ISO8601_CYCLING_TYPE, "Z") |
| 161 |
# Run 3-hourly but not at 06:00 (from the ICP) |
| 162 |
sequence = ISO8601Sequence("PT3H!T06", "20000101T00Z") |
| 163 |
|
| 164 |
output = [] |
| 165 |
point = sequence.get_start_point() |
| 166 |
count = 0 |
| 167 |
# We are going to make ten sequence points |
| 168 |
while point and count < 10: |
| 169 |
output.append(point) |
| 170 |
point = sequence.get_next_point(point) |
| 171 |
count += 1 |
| 172 |
output = [str(out) for out in output] |
| 173 |
# We should expect every T06 to be excluded |
| 174 |
assert output == [ |
| 175 |
"20000101T0000Z", |
| 176 |
"20000101T0300Z", |
| 177 |
"20000101T0900Z", |
| 178 |
"20000101T1200Z", |
| 179 |
"20000101T1500Z", |
| 180 |
"20000101T1800Z", |
| 181 |
"20000101T2100Z", |
| 182 |
"20000102T0000Z", |
| 183 |
"20000102T0300Z", |
| 184 |
"20000102T0900Z", |
| 185 |
] |
| 186 |
|
| 187 |
|
| 188 |
def test_advanced_exclusions_partial_datetime2(set_cycling_type): |
| 189 |
"""Advanced exclusions refers to exclusions that are not just |
| 190 |
simple points but could be time periods or recurrences such as |
| 191 |
'!T06' or similar""" |
| 192 |
set_cycling_type(ISO8601_CYCLING_TYPE, "Z") |
| 193 |
# Run hourly but not at 00:00, 06:00, 12:00, 18:00 |
| 194 |
sequence = ISO8601Sequence("T-00!(T00, T06, T12, T18)", "20000101T00Z") |
| 195 |
|
| 196 |
output = [] |
| 197 |
point = sequence.get_start_point() |
| 198 |
count = 0 |
| 199 |
# We are going to make 18 sequence points |
| 200 |
while point and count < 18: |
| 201 |
output.append(point) |
| 202 |
point = sequence.get_next_point(point) |
| 203 |
count += 1 |
| 204 |
output = [str(out) for out in output] |
| 205 |
# We should expect T00, T06, T12, and T18 to be excluded |
| 206 |
assert output == [ |
| 207 |
"20000101T0100Z", |
| 208 |
"20000101T0200Z", |
| 209 |
"20000101T0300Z", |
| 210 |
"20000101T0400Z", |
| 211 |
"20000101T0500Z", |
| 212 |
"20000101T0700Z", |
| 213 |
"20000101T0800Z", |
| 214 |
"20000101T0900Z", |
| 215 |
"20000101T1000Z", |
| 216 |
"20000101T1100Z", |
| 217 |
"20000101T1300Z", |
| 218 |
"20000101T1400Z", |
| 219 |
"20000101T1500Z", |
| 220 |
"20000101T1600Z", |
| 221 |
"20000101T1700Z", |
| 222 |
"20000101T1900Z", |
| 223 |
"20000101T2000Z", |
| 224 |
"20000101T2100Z", |
| 225 |
] |
| 226 |
|
| 227 |
|
| 228 |
def test_advanced_exclusions_partial_datetime3(set_cycling_type): |
| 229 |
"""Advanced exclusions refers to exclusions that are not just |
| 230 |
simple points but could be time periods or recurrences such as |
| 231 |
'!T06' or similar""" |
| 232 |
set_cycling_type(ISO8601_CYCLING_TYPE, "Z") |
| 233 |
# Run 5 minutely but not at 15 minutes past the hour from ICP |
| 234 |
sequence = ISO8601Sequence("PT5M!T-15", "20000101T00Z") |
| 235 |
|
| 236 |
output = [] |
| 237 |
point = sequence.get_start_point() |
| 238 |
count = 0 |
| 239 |
# We are going to make 15 sequence points |
| 240 |
while point and count < 15: |
| 241 |
output.append(point) |
| 242 |
point = sequence.get_next_point(point) |
| 243 |
count += 1 |
| 244 |
output = [str(out) for out in output] |
| 245 |
# We should expect xx:15 (15 minutes past the hour) to be excluded |
| 246 |
assert output == [ |
| 247 |
"20000101T0000Z", |
| 248 |
"20000101T0005Z", |
| 249 |
"20000101T0010Z", |
| 250 |
"20000101T0020Z", |
| 251 |
"20000101T0025Z", |
| 252 |
"20000101T0030Z", |
| 253 |
"20000101T0035Z", |
| 254 |
"20000101T0040Z", |
| 255 |
"20000101T0045Z", |
| 256 |
"20000101T0050Z", |
| 257 |
"20000101T0055Z", |
| 258 |
"20000101T0100Z", |
| 259 |
"20000101T0105Z", |
| 260 |
"20000101T0110Z", |
| 261 |
"20000101T0120Z", |
| 262 |
] |
| 263 |
|
| 264 |
|
| 265 |
def test_advanced_exclusions_partial_datetime4(set_cycling_type): |
| 266 |
"""Advanced exclusions refers to exclusions that are not just |
| 267 |
simple points but could be time periods or recurrences such as |
| 268 |
'!T06' or similar""" |
| 269 |
set_cycling_type(ISO8601_CYCLING_TYPE, "Z") |
| 270 |
# Run daily at 00:00 except on Mondays |
| 271 |
sequence = ISO8601Sequence("T00!W-1T00", "20170422T00Z") |
| 272 |
|
| 273 |
output = [] |
| 274 |
point = sequence.get_start_point() |
| 275 |
count = 0 |
| 276 |
# We are going to make 19 sequence points |
| 277 |
while point and count < 9: |
| 278 |
output.append(point) |
| 279 |
point = sequence.get_next_point(point) |
| 280 |
count += 1 |
| 281 |
output = [str(out) for out in output] |
| 282 |
# We should expect Monday 24th April and Monday 1st May |
| 283 |
# to be excluded. |
| 284 |
assert output == [ |
| 285 |
"20170422T0000Z", |
| 286 |
"20170423T0000Z", |
| 287 |
"20170425T0000Z", |
| 288 |
"20170426T0000Z", |
| 289 |
"20170427T0000Z", |
| 290 |
"20170428T0000Z", |
| 291 |
"20170429T0000Z", |
| 292 |
"20170430T0000Z", |
| 293 |
"20170502T0000Z", |
| 294 |
] |
| 295 |
|
| 296 |
|
| 297 |
def test_exclusions_to_string(set_cycling_type): |
| 298 |
set_cycling_type(ISO8601_CYCLING_TYPE, "Z") |
| 299 |
# Check that exclusions are not included where they should not be. |
| 300 |
basic = ISO8601Sequence("PT1H", "2000", "2001") |
| 301 |
assert "!" not in str(basic) |
| 302 |
|
| 303 |
# Check that exclusions are parsable. |
| 304 |
sequence = ISO8601Sequence("PT1H!(20000101T10Z, PT6H)", "2000", "2001") |
| 305 |
sequence2 = ISO8601Sequence(str(sequence), "2000", "2001") |
| 306 |
assert sequence == sequence2 |
| 307 |
|
| 308 |
|
| 309 |
def test_advanced_exclusions_sequences1(set_cycling_type): |
| 310 |
"""Advanced exclusions refers to exclusions that are not just |
| 311 |
simple points but could be time periods or recurrences such as |
| 312 |
'!T06' or similar""" |
| 313 |
set_cycling_type(ISO8601_CYCLING_TYPE, "Z") |
| 314 |
# Run hourly from the ICP but not 3-hourly |
| 315 |
sequence = ISO8601Sequence("PT1H!PT3H", "20000101T01Z") |
| 316 |
|
| 317 |
output = [] |
| 318 |
point = sequence.get_start_point() |
| 319 |
count = 0 |
| 320 |
# We are going to make six sequence points |
| 321 |
while point and count < 6: |
| 322 |
output.append(point) |
| 323 |
point = sequence.get_next_point(point) |
| 324 |
count += 1 |
| 325 |
output = [str(out) for out in output] |
| 326 |
# We should expect to see hourly from ICP but not 3 hourly |
| 327 |
assert output == [ |
| 328 |
"20000101T0200Z", |
| 329 |
"20000101T0300Z", |
| 330 |
"20000101T0500Z", |
| 331 |
"20000101T0600Z", |
| 332 |
"20000101T0800Z", |
| 333 |
"20000101T0900Z", |
| 334 |
] |
| 335 |
|
| 336 |
|
| 337 |
def test_advanced_exclusions_sequences2(set_cycling_type): |
| 338 |
"""Advanced exclusions refers to exclusions that are not just |
| 339 |
simple points but could be time periods or recurrences such as |
| 340 |
'!T06' or similar""" |
| 341 |
set_cycling_type(ISO8601_CYCLING_TYPE, "Z") |
| 342 |
# Run hourly on the hour but not 3 hourly on the hour |
| 343 |
sequence = ISO8601Sequence("T-00!T-00/PT3H", "20000101T00Z") |
| 344 |
|
| 345 |
output = [] |
| 346 |
point = sequence.get_start_point() |
| 347 |
count = 0 |
| 348 |
# We are going to make six sequence points |
| 349 |
while point and count < 6: |
| 350 |
output.append(point) |
| 351 |
point = sequence.get_next_point(point) |
| 352 |
count += 1 |
| 353 |
output = [str(out) for out in output] |
| 354 |
|
| 355 |
assert output == [ |
| 356 |
"20000101T0100Z", |
| 357 |
"20000101T0200Z", |
| 358 |
"20000101T0400Z", |
| 359 |
"20000101T0500Z", |
| 360 |
"20000101T0700Z", |
| 361 |
"20000101T0800Z", |
| 362 |
] |
| 363 |
|
| 364 |
|
| 365 |
def test_advanced_exclusions_sequences3(set_cycling_type): |
| 366 |
"""Advanced exclusions refers to exclusions that are not just |
| 367 |
simple points but could be time periods or recurrences such as |
| 368 |
'!T06' or similar""" |
| 369 |
set_cycling_type(ISO8601_CYCLING_TYPE, "Z") |
| 370 |
# Run daily at 12:00 except every 3rd day |
| 371 |
sequence = ISO8601Sequence("T12!P3D", "20000101T12Z") |
| 372 |
|
| 373 |
output = [] |
| 374 |
point = sequence.get_start_point() |
| 375 |
count = 0 |
| 376 |
# We are going to make six sequence points |
| 377 |
while point and count < 6: |
| 378 |
output.append(point) |
| 379 |
point = sequence.get_next_point(point) |
| 380 |
count += 1 |
| 381 |
output = [str(out) for out in output] |
| 382 |
|
| 383 |
assert output == [ |
| 384 |
"20000102T1200Z", |
| 385 |
"20000103T1200Z", |
| 386 |
"20000105T1200Z", |
| 387 |
"20000106T1200Z", |
| 388 |
"20000108T1200Z", |
| 389 |
"20000109T1200Z", |
| 390 |
] |
| 391 |
|
| 392 |
|
| 393 |
def test_advanced_exclusions_sequences4(set_cycling_type): |
| 394 |
"""Advanced exclusions refers to exclusions that are not just |
| 395 |
simple points but could be time periods or recurrences such as |
| 396 |
'!T06' or similar""" |
| 397 |
set_cycling_type(ISO8601_CYCLING_TYPE, "Z") |
| 398 |
# Run every hour from 01:00 excluding every 3rd hour. |
| 399 |
sequence = ISO8601Sequence("T01/PT1H!+PT3H/PT3H", "20000101T01Z") |
| 400 |
|
| 401 |
output = [] |
| 402 |
point = sequence.get_start_point() |
| 403 |
count = 0 |
| 404 |
# We are going to make six sequence points |
| 405 |
while point and count < 6: |
| 406 |
output.append(point) |
| 407 |
point = sequence.get_next_point(point) |
| 408 |
count += 1 |
| 409 |
output = [str(out) for out in output] |
| 410 |
|
| 411 |
assert output == [ |
| 412 |
"20000101T0100Z", |
| 413 |
"20000101T0200Z", |
| 414 |
"20000101T0300Z", |
| 415 |
"20000101T0500Z", |
| 416 |
"20000101T0600Z", |
| 417 |
"20000101T0800Z", |
| 418 |
] |
| 419 |
|
| 420 |
|
| 421 |
def test_advanced_exclusions_sequences5(set_cycling_type): |
| 422 |
"""Advanced exclusions refers to exclusions that are not just |
| 423 |
simple points but could be time periods or recurrences such as |
| 424 |
'!T06' or similar""" |
| 425 |
set_cycling_type(ISO8601_CYCLING_TYPE, "Z") |
| 426 |
# Run every hour from 01:00 excluding every 3rd hour. |
| 427 |
sequence = ISO8601Sequence("T-00 ! 2000", "20000101T00Z") |
| 428 |
|
| 429 |
output = [] |
| 430 |
point = sequence.get_start_point() |
| 431 |
count = 0 |
| 432 |
# We are going to make six sequence points |
| 433 |
while point and count < 6: |
| 434 |
output.append(point) |
| 435 |
point = sequence.get_next_point(point) |
| 436 |
count += 1 |
| 437 |
output = [str(out) for out in output] |
| 438 |
|
| 439 |
assert output == [ |
| 440 |
"20000101T0100Z", |
| 441 |
"20000101T0200Z", |
| 442 |
"20000101T0300Z", |
| 443 |
"20000101T0400Z", |
| 444 |
"20000101T0500Z", |
| 445 |
"20000101T0600Z", |
| 446 |
] |
| 447 |
|
| 448 |
|
| 449 |
def test_advanced_exclusions_sequences_mix_points_sequences(set_cycling_type): |
| 450 |
"""Advanced exclusions refers to exclusions that are not just |
| 451 |
simple points but could be time periods or recurrences such as |
| 452 |
'!T06' or similar""" |
| 453 |
set_cycling_type(ISO8601_CYCLING_TYPE, "Z") |
| 454 |
# Run every hour from 01:00 excluding every 3rd hour. |
| 455 |
sequence = ISO8601Sequence("T-00 ! (2000, PT2H)", "20000101T00Z") |
| 456 |
|
| 457 |
output = [] |
| 458 |
point = sequence.get_start_point() |
| 459 |
count = 0 |
| 460 |
# We are going to make six sequence points |
| 461 |
while point and count < 6: |
| 462 |
output.append(point) |
| 463 |
point = sequence.get_next_point(point) |
| 464 |
count += 1 |
| 465 |
output = [str(out) for out in output] |
| 466 |
|
| 467 |
assert output == [ |
| 468 |
"20000101T0100Z", |
| 469 |
"20000101T0300Z", |
| 470 |
"20000101T0500Z", |
| 471 |
"20000101T0700Z", |
| 472 |
"20000101T0900Z", |
| 473 |
"20000101T1100Z", |
| 474 |
] |
| 475 |
|
| 476 |
|
| 477 |
def test_advanced_exclusions_sequences_implied_start_point(set_cycling_type): |
| 478 |
"""Advanced exclusions refers to exclusions that are not just |
| 479 |
simple points but could be time periods or recurrences such as |
| 480 |
'!T06' or similar""" |
| 481 |
set_cycling_type(ISO8601_CYCLING_TYPE, "Z") |
| 482 |
# Run every hour from 01:00 excluding every 3rd hour. |
| 483 |
sequence = ISO8601Sequence("T05/PT1H!PT3H", "20000101T00Z") |
| 484 |
|
| 485 |
output = [] |
| 486 |
point = sequence.get_start_point() |
| 487 |
count = 0 |
| 488 |
# We are going to make six sequence points |
| 489 |
while point and count < 6: |
| 490 |
output.append(point) |
| 491 |
point = sequence.get_next_point(point) |
| 492 |
count += 1 |
| 493 |
output = [str(out) for out in output] |
| 494 |
|
| 495 |
assert output == [ |
| 496 |
"20000101T0600Z", |
| 497 |
"20000101T0700Z", |
| 498 |
"20000101T0900Z", |
| 499 |
"20000101T1000Z", |
| 500 |
"20000101T1200Z", |
| 501 |
"20000101T1300Z", |
| 502 |
] |
| 503 |
|
| 504 |
|
| 505 |
def test_point_init(): |
| 506 |
"""It should error if inited with anything other than a string.""" |
| 507 |
ISO8601Point('1000') |
| 508 |
with pytest.raises(TypeError): |
| 509 |
ISO8601Point(1000) |
| 510 |
with pytest.raises(TypeError): |
| 511 |
ISO8601Interval(1000) |
| 512 |
|
| 513 |
|
| 514 |
def test_exclusions_sequences_points(set_cycling_type): |
| 515 |
"""Test ISO8601Sequence methods for sequences with exclusions""" |
| 516 |
set_cycling_type(ISO8601_CYCLING_TYPE, "Z") |
| 517 |
# Run every hour from 01:00 excluding every 3rd hour |
| 518 |
sequence = ISO8601Sequence("T01/PT1H!PT3H", "20000101T01Z") |
| 519 |
|
| 520 |
point_0 = ISO8601Point("20000101T00Z") |
| 521 |
point_1 = ISO8601Point("20000101T01Z") |
| 522 |
point_2 = ISO8601Point("20000101T02Z") |
| 523 |
point_3 = ISO8601Point("20000101T03Z") |
| 524 |
point_4 = ISO8601Point("20000101T04Z") |
| 525 |
|
| 526 |
assert point_0 not in sequence.exclusions |
| 527 |
assert point_1 in sequence.exclusions |
| 528 |
assert sequence.is_on_sequence(point_2) |
| 529 |
assert sequence.is_on_sequence(point_3) |
| 530 |
assert not sequence.is_on_sequence(point_4) |
| 531 |
assert point_4 in sequence.exclusions |
| 532 |
|
| 533 |
|
| 534 |
def test_exclusions_extensive(set_cycling_type): |
| 535 |
"""Test ISO8601Sequence methods for sequences with exclusions""" |
| 536 |
set_cycling_type(ISO8601_CYCLING_TYPE, "+05") |
| 537 |
sequence = ISO8601Sequence( |
| 538 |
"PT1H!20000101T02+05", "20000101T00", "20000101T05" |
| 539 |
) |
| 540 |
|
| 541 |
point_0 = ISO8601Point("20000101T0000+05") |
| 542 |
point_1 = ISO8601Point("20000101T0100+05") |
| 543 |
point_2 = ISO8601Point("20000101T0200+05") # The excluded point. |
| 544 |
point_3 = ISO8601Point("20000101T0300+05") |
| 545 |
|
| 546 |
assert not sequence.is_on_sequence(point_2) |
| 547 |
assert not sequence.is_valid(point_2) |
| 548 |
assert sequence.get_prev_point(point_2) == point_1 |
| 549 |
assert sequence.get_prev_point(point_3) == point_1 |
| 550 |
assert sequence.get_prev_point(point_2) == point_1 |
| 551 |
assert sequence.get_nearest_prev_point(point_3) == point_1 |
| 552 |
assert sequence.get_next_point(point_1) == point_3 |
| 553 |
assert sequence.get_next_point(point_2) == point_3 |
| 554 |
|
| 555 |
sequence = ISO8601Sequence("PT1H!20000101T00+05", "20000101T00+05") |
| 556 |
assert sequence.get_first_point(point_0) == point_1 |
| 557 |
assert sequence.get_start_point() == point_1 |
| 558 |
|
| 559 |
|
| 560 |
def test_multiple_exclusions_extensive(set_cycling_type): |
| 561 |
"""Test ISO8601Sequence methods for sequences with multiple exclusions""" |
| 562 |
set_cycling_type(ISO8601_CYCLING_TYPE, "+05") |
| 563 |
sequence = ISO8601Sequence( |
| 564 |
"PT1H!(20000101T02,20000101T03)", "20000101T00", "20000101T06" |
| 565 |
) |
| 566 |
|
| 567 |
point_0 = ISO8601Point("20000101T0000+05") |
| 568 |
point_1 = ISO8601Point("20000101T0100+05") |
| 569 |
point_2 = ISO8601Point("20000101T0200+05") # First excluded point |
| 570 |
point_3 = ISO8601Point("20000101T0300+05") # Second excluded point |
| 571 |
point_4 = ISO8601Point("20000101T0400+05") |
| 572 |
|
| 573 |
# Check the excluded points are not on the sequence |
| 574 |
assert not sequence.is_on_sequence(point_2) |
| 575 |
assert not sequence.is_on_sequence(point_3) |
| 576 |
assert not sequence.is_valid(point_2) # Should be excluded |
| 577 |
assert not sequence.is_valid(point_3) # Should be excluded |
| 578 |
# Check that we can correctly retrieve previous points |
| 579 |
assert sequence.get_prev_point(point_2) == point_1 |
| 580 |
# Should skip two excluded points |
| 581 |
assert sequence.get_prev_point(point_4) == point_1 |
| 582 |
assert sequence.get_prev_point(point_2) == point_1 |
| 583 |
assert sequence.get_nearest_prev_point(point_4) == point_1 |
| 584 |
assert sequence.get_next_point(point_1) == point_4 |
| 585 |
assert sequence.get_next_point(point_3) == point_4 |
| 586 |
|
| 587 |
sequence = ISO8601Sequence("PT1H!20000101T00+05", "20000101T00") |
| 588 |
# Check that the first point is after 00. |
| 589 |
assert sequence.get_first_point(point_0) == point_1 |
| 590 |
assert sequence.get_start_point() == point_1 |
| 591 |
|
| 592 |
# Check a longer list of exclusions |
| 593 |
# Also note you can change the format of the exclusion list |
| 594 |
# (removing the parentheses) |
| 595 |
sequence = ISO8601Sequence( |
| 596 |
"PT1H!(20000101T02+05, 20000101T03+05," "20000101T04+05)", |
| 597 |
"20000101T00", |
| 598 |
"20000101T06", |
| 599 |
) |
| 600 |
assert sequence.get_prev_point(point_3) == point_1 |
| 601 |
assert sequence.get_prev_point(point_4) == point_1 |
| 602 |
|
| 603 |
|
| 604 |
def test_get_nearest_prev_point_exclusions(set_cycling_type): |
| 605 |
"""Another test for get_nearest_prev_point() for sequences with exclusions. |
| 606 |
|
| 607 |
https://github.com/cylc/cylc-flow/issues/7268 |
| 608 |
""" |
| 609 |
set_cycling_type(ISO8601_CYCLING_TYPE) |
| 610 |
P = ISO8601Point |
| 611 |
seq = ISO8601Sequence('T00 ! 2025-01-01T00Z', '2025-01-01T00Z') |
| 612 |
assert seq.get_nearest_prev_point(P('2025-01-01T09Z')) is None |
| 613 |
assert seq.get_nearest_prev_point(P('2025-01-02T09Z')) == P( |
| 614 |
'2025-01-02T00Z' |
| 615 |
) |
| 616 |
|
| 617 |
|
| 618 |
def test_exclusion_zero_duration_warning(set_cycling_type, caplog, log_filter): |
| 619 |
"""It should not log zero-duration warnings for exclusion points. |
| 620 |
|
| 621 |
Exclusions may either be sequences or points. We first attempt to parse |
| 622 |
them as sequences, if this fails, we attempt to parse them as points. |
| 623 |
|
| 624 |
The zero-duration recurrence warning would be logged if we attempted to |
| 625 |
parse a point as a sequence. To avoid spurious warnings this should be |
| 626 |
turned off for exclusion parsing. |
| 627 |
|
| 628 |
""" |
| 629 |
# parsing a point as a sequences causes a zero-duration warning |
| 630 |
set_cycling_type(ISO8601_CYCLING_TYPE, "+05") |
| 631 |
with pytest.raises(Exception): |
| 632 |
ISO8601Sequence('3000', '2999') |
| 633 |
assert log_filter(contains='zero-duration') |
| 634 |
|
| 635 |
# parsing a point in an exclusion should not |
| 636 |
caplog.clear() |
| 637 |
ISO8601Sequence('P1Y ! 3000', '2999') |
| 638 |
assert not log_filter(contains='zero-duration') |
| 639 |
|
| 640 |
|
| 641 |
def test_simple(set_cycling_type): |
| 642 |
"""Run some simple tests for date-time cycling.""" |
| 643 |
set_cycling_type(ISO8601_CYCLING_TYPE, "Z") |
| 644 |
p_start = ISO8601Point("20100808T00") |
| 645 |
p_stop = ISO8601Point("20100808T02") |
| 646 |
i = ISO8601Interval("PT6H") |
| 647 |
assert p_start - i == ISO8601Point("20100807T18") |
| 648 |
assert p_stop + i == ISO8601Point("20100808T08") |
| 649 |
|
| 650 |
sequence = ISO8601Sequence( |
| 651 |
"PT10M", |
| 652 |
str(p_start), |
| 653 |
str(p_stop), |
| 654 |
) |
| 655 |
sequence.set_offset(-ISO8601Interval("PT10M")) |
| 656 |
point = sequence.get_next_point(ISO8601Point("20100808T0000")) |
| 657 |
assert point == ISO8601Point("20100808T0010") |
| 658 |
output = [] |
| 659 |
|
| 660 |
# Test point generation forwards. |
| 661 |
while point and point < p_stop: |
| 662 |
output.append(point) |
| 663 |
assert sequence.is_on_sequence(point) |
| 664 |
point = sequence.get_next_point(point) |
| 665 |
assert [str(out) for out in output] == [ |
| 666 |
"20100808T0010Z", |
| 667 |
"20100808T0020Z", |
| 668 |
"20100808T0030Z", |
| 669 |
"20100808T0040Z", |
| 670 |
"20100808T0050Z", |
| 671 |
"20100808T0100Z", |
| 672 |
"20100808T0110Z", |
| 673 |
"20100808T0120Z", |
| 674 |
"20100808T0130Z", |
| 675 |
"20100808T0140Z", |
| 676 |
"20100808T0150Z", |
| 677 |
] |
| 678 |
|
| 679 |
assert point == ISO8601Point("20100808T0200") |
| 680 |
|
| 681 |
# Test point generation backwards. |
| 682 |
output = [] |
| 683 |
while point and point >= p_start: |
| 684 |
output.append(point) |
| 685 |
assert sequence.is_on_sequence(point) |
| 686 |
point = sequence.get_prev_point(point) |
| 687 |
assert [str(out) for out in output] == [ |
| 688 |
"20100808T0200Z", |
| 689 |
"20100808T0150Z", |
| 690 |
"20100808T0140Z", |
| 691 |
"20100808T0130Z", |
| 692 |
"20100808T0120Z", |
| 693 |
"20100808T0110Z", |
| 694 |
"20100808T0100Z", |
| 695 |
"20100808T0050Z", |
| 696 |
"20100808T0040Z", |
| 697 |
"20100808T0030Z", |
| 698 |
"20100808T0020Z", |
| 699 |
"20100808T0010Z", |
| 700 |
"20100808T0000Z", |
| 701 |
] |
| 702 |
assert not sequence.is_on_sequence(ISO8601Point("20100809T0005")) |
| 703 |
|
| 704 |
|
| 705 |
@pytest.mark.parametrize( |
| 706 |
'value, expected', [ |
| 707 |
('next(T2100Z)', '20100808T2100Z'), |
| 708 |
('next(T00)', '20100809T0000Z'), |
| 709 |
('next(T-15)', '20100808T1615Z'), |
| 710 |
('next(T-45)', '20100808T1545Z'), |
| 711 |
('next(-10)', '21100101T0000Z'), |
| 712 |
('next(-1008)', '21100801T0000Z'), |
| 713 |
('next(--10)', '20101001T0000Z'), |
| 714 |
('next(--0325)', '20110325T0000Z'), |
| 715 |
('next(---10)', '20100810T0000Z'), |
| 716 |
('next(---05T1200Z)', '20100905T1200Z'), |
| 717 |
param('next(--08-08)', '20110808T0000Z', marks=pytest.mark.xfail), |
| 718 |
('next(T15)', '20100809T1500Z'), |
| 719 |
('next(T-41)', '20100808T1541Z'), |
| 720 |
] |
| 721 |
) |
| 722 |
def test_next_simple(value: str, expected: str, set_cycling_type): |
| 723 |
"""Test the generation of CP using 'next' from single input.""" |
| 724 |
set_cycling_type(ISO8601_CYCLING_TYPE, "Z") |
| 725 |
my_now = "2010-08-08T15:41Z" |
| 726 |
assert ingest_time(value, my_now) == expected |
| 727 |
|
| 728 |
|
| 729 |
@pytest.mark.parametrize( |
| 730 |
'value, expected', [ |
| 731 |
('previous(T2100Z)', '20100807T2100Z'), |
| 732 |
('previous(T00)', '20100808T0000Z'), |
| 733 |
('previous(T-15)', '20100808T1515Z'), |
| 734 |
('previous(T-45)', '20100808T1445Z'), |
| 735 |
('previous(-10)', '20100101T0000Z'), |
| 736 |
('previous(-1008)', '20100801T0000Z'), |
| 737 |
('previous(--10)', '20091001T0000Z'), |
| 738 |
('previous(--0325)', '20100325T0000Z'), |
| 739 |
('previous(---10)', '20100710T0000Z'), |
| 740 |
('previous(---05T1200Z)', '20100805T1200Z'), |
| 741 |
param('previous(--08-08)', '20100808T0000Z', marks=pytest.mark.xfail), |
| 742 |
('previous(T15)', '20100808T1500Z'), |
| 743 |
('previous(T-41)', '20100808T1441Z'), |
| 744 |
] |
| 745 |
) |
| 746 |
def test_previous_simple(value: str, expected: str, set_cycling_type): |
| 747 |
"""Test the generation of CP using 'previous' from single input.""" |
| 748 |
set_cycling_type(ISO8601_CYCLING_TYPE, "Z") |
| 749 |
my_now = "2010-08-08T15:41Z" |
| 750 |
assert ingest_time(value, my_now) == expected |
| 751 |
|
| 752 |
|
| 753 |
def test_sequence(set_cycling_type): |
| 754 |
"""Test the generation of CP from list input.""" |
| 755 |
set_cycling_type(ISO8601_CYCLING_TYPE, "Z") |
| 756 |
my_now = "20100808T1540Z" |
| 757 |
sequence = ( |
| 758 |
"next(T-00;T-15;T-30;T-45)", # 20100808T1545Z |
| 759 |
"previous(T-00;T-15;T-30;T-45)", # 20100808T1530Z |
| 760 |
"next(T00;T06;T12;T18)", # 20100808T1800Z |
| 761 |
"previous(T00;T06;T12;T18)", |
| 762 |
) # 20100808T1200Z |
| 763 |
|
| 764 |
output = [] |
| 765 |
|
| 766 |
for point in sequence: |
| 767 |
output.append(ingest_time(point, my_now)) |
| 768 |
assert output == [ |
| 769 |
"20100808T1545Z", |
| 770 |
"20100808T1530Z", |
| 771 |
"20100808T1800Z", |
| 772 |
"20100808T1200Z", |
| 773 |
] |
| 774 |
|
| 775 |
|
| 776 |
def test_offset_simple(set_cycling_type): |
| 777 |
"""Test the generation of offset CP.""" |
| 778 |
set_cycling_type(ISO8601_CYCLING_TYPE, "Z") |
| 779 |
my_now = "20100808T1540Z" |
| 780 |
sequence = ( |
| 781 |
"PT15M", # 20100808T1555Z |
| 782 |
"-PT30M", # 20100808T1510Z |
| 783 |
"PT1H", # 20100808T1640Z |
| 784 |
"-PT18H", # 20100807T2140Z |
| 785 |
"P3D", # 20100811T1540Z |
| 786 |
"-P2W", # 20100725T1540Z |
| 787 |
"P6M", # 20110208T1540Z |
| 788 |
"-P1M", # 20100708T1540Z |
| 789 |
"P1Y", # 20110808T1540Z |
| 790 |
"-P5Y", # 20050808T1540Z |
| 791 |
) |
| 792 |
|
| 793 |
output = [] |
| 794 |
|
| 795 |
for point in sequence: |
| 796 |
output.append(ingest_time(point, my_now)) |
| 797 |
assert output == [ |
| 798 |
"20100808T1555Z", |
| 799 |
"20100808T1510Z", |
| 800 |
"20100808T1640Z", |
| 801 |
"20100807T2140Z", |
| 802 |
"20100811T1540Z", |
| 803 |
"20100725T1540Z", |
| 804 |
"20110208T1540Z", |
| 805 |
"20100708T1540Z", |
| 806 |
"20110808T1540Z", |
| 807 |
"20050808T1540Z", |
| 808 |
] |
| 809 |
|
| 810 |
|
| 811 |
def test_offset(set_cycling_type): |
| 812 |
"""Test the generation of offset CP with 'next' and 'previous'.""" |
| 813 |
set_cycling_type(ISO8601_CYCLING_TYPE, "Z") |
| 814 |
my_now = "20100808T1540Z" |
| 815 |
sequence = ( |
| 816 |
"next(T06) +P1D", # 20100810T0600Z |
| 817 |
"previous(T-30) -PT12H", # 20100808T0330Z |
| 818 |
"next(T00;T06;T12;T18) -P1W", # 20100801T1800Z |
| 819 |
"previous(T00;T06;T12;T18) +PT1H", # 20100808T1300Z |
| 820 |
) |
| 821 |
|
| 822 |
output = [] |
| 823 |
|
| 824 |
for point in sequence: |
| 825 |
output.append(ingest_time(point, my_now)) |
| 826 |
assert output == [ |
| 827 |
"20100810T0600Z", |
| 828 |
"20100808T0330Z", |
| 829 |
"20100801T1800Z", |
| 830 |
"20100808T1300Z", |
| 831 |
] |
| 832 |
|
| 833 |
|
| 834 |
def test_weeks_days(set_cycling_type): |
| 835 |
"""Test the generation of CP with day-of-week, |
| 836 |
ordinal day, and week (with day-of-week specified).""" |
| 837 |
set_cycling_type(ISO8601_CYCLING_TYPE, "Z") |
| 838 |
my_now = "20100808T1540Z" |
| 839 |
sequence = ( |
| 840 |
"next(-W-1)", # 20100809T0000Z |
| 841 |
"previous(-W-4)", # 20100805T0000Z |
| 842 |
"next(-010)", # 20110110T0000Z |
| 843 |
"previous(-101)", # 20100411T0000Z |
| 844 |
"next(-W40-1)", # 20101004T0000Z |
| 845 |
"previous(-W05-1)", # 20100201T0000Z |
| 846 |
"next(-W05-5)", # 20110204T0000Z |
| 847 |
"previous(-W40-4)", # 20091001T0000Z |
| 848 |
) |
| 849 |
|
| 850 |
output = [] |
| 851 |
|
| 852 |
for point in sequence: |
| 853 |
output.append(ingest_time(point, my_now)) |
| 854 |
assert output == [ |
| 855 |
"20100809T0000Z", |
| 856 |
"20100805T0000Z", |
| 857 |
"20110110T0000Z", |
| 858 |
"20100411T0000Z", |
| 859 |
"20101004T0000Z", |
| 860 |
"20100201T0000Z", |
| 861 |
"20110204T0000Z", |
| 862 |
"20091001T0000Z", |
| 863 |
] |
| 864 |
|
| 865 |
|
| 866 |
@pytest.mark.parametrize( |
| 867 |
'value, expected', [ |
| 868 |
('next(T-00)', '20180314T1600Z'), |
| 869 |
('previous(T-00)', '20180314T1500Z'), |
| 870 |
('next(T-00; T-15; T-30; T-45)', '20180314T1515Z'), |
| 871 |
('previous(T-00; T-15; T-30; T-45)', '20180314T1500Z'), |
| 872 |
('next(T00)', '20180315T0000Z'), |
| 873 |
('previous(T00)', '20180314T0000Z'), |
| 874 |
('next(T06:30Z)', '20180315T0630Z'), |
| 875 |
('previous(T06:30) -P1D', '20180313T0630Z'), |
| 876 |
('next(T00; T06; T12; T18)', '20180314T1800Z'), |
| 877 |
('previous(T00; T06; T12; T18)', '20180314T1200Z'), |
| 878 |
('next(T00; T06; T12; T18)+P1W', '20180321T1800Z'), |
| 879 |
('PT1H', '20180314T1612Z'), |
| 880 |
('-P1M', '20180214T1512Z'), |
| 881 |
('next(-00)', '21000101T0000Z'), |
| 882 |
('previous(--01)', '20180101T0000Z'), |
| 883 |
('next(---01)', '20180401T0000Z'), |
| 884 |
('previous(--1225)', '20171225T0000Z'), |
| 885 |
('next(-2006)', '20200601T0000Z'), |
| 886 |
('previous(-W101)', '20180305T0000Z'), |
| 887 |
('next(-W-1; -W-3; -W-5)', '20180314T0000Z'), |
| 888 |
('next(-001; -091; -181; -271)', '20180401T0000Z'), |
| 889 |
('previous(-365T12Z)', '20171231T1200Z'), |
| 890 |
] |
| 891 |
) |
| 892 |
def test_user_guide_examples(value: str, expected: str, set_cycling_type): |
| 893 |
"""Test the offset CP examples in the Cylc user guide. |
| 894 |
|
| 895 |
https://cylc.github.io/cylc-doc/stable/html/user-guide/writing-workflows/scheduling.html |
| 896 |
""" |
| 897 |
set_cycling_type(ISO8601_CYCLING_TYPE, "Z") |
| 898 |
my_now = "2018-03-14T15:12Z" |
| 899 |
assert ingest_time(value, my_now) == expected |
| 900 |
|
| 901 |
|
| 902 |
def test_next_simple_no_now(set_cycling_type): |
| 903 |
"""Test the generation of CP using 'next' with no value for `now`.""" |
| 904 |
set_cycling_type(ISO8601_CYCLING_TYPE, "Z") |
| 905 |
my_now = None |
| 906 |
point = "next(T00Z)+P1D" |
| 907 |
output = ingest_time(point, my_now) |
| 908 |
|
| 909 |
current_time = datetime.utcnow() |
| 910 |
# my_now is None, but ingest_time will have used a similar time, and |
| 911 |
# the returned value must be after current_time |
| 912 |
output_time = datetime.strptime(output, "%Y%m%dT%H%MZ") |
| 913 |
assert current_time < output_time |
| 914 |
|
| 915 |
|
| 916 |
def test_integer_cycling_is_returned(set_cycling_type): |
| 917 |
"""Test that when integer points are given, the same value is |
| 918 |
returned.""" |
| 919 |
set_cycling_type(ISO8601_CYCLING_TYPE, "Z") |
| 920 |
integer_point = "1" |
| 921 |
assert integer_point, ingest_time(integer_point is None) |
| 922 |
|
| 923 |
|
| 924 |
def test_expanded_dates_are_returned(set_cycling_type): |
| 925 |
"""Test that when expanded dates are given, the same value is |
| 926 |
returned.""" |
| 927 |
set_cycling_type(ISO8601_CYCLING_TYPE, "Z") |
| 928 |
expanded_date = "+0100400101T0000Z" |
| 929 |
assert expanded_date, ingest_time(expanded_date is None) |
| 930 |
|
| 931 |
|
| 932 |
def test_timepoint_truncated(set_cycling_type): |
| 933 |
"""Test that when a timepoint is given, and is truncated, then the |
| 934 |
value is added to `now`.""" |
| 935 |
set_cycling_type(ISO8601_CYCLING_TYPE, "Z") |
| 936 |
my_now = "2018-03-14T15:12Z" |
| 937 |
timepoint_truncated = "T15:00Z" # 20180315T1500Z |
| 938 |
output = ingest_time(timepoint_truncated, my_now) |
| 939 |
assert output == "20180315T1500Z" |
| 940 |
|
| 941 |
|
| 942 |
def test_timepoint(set_cycling_type): |
| 943 |
"""Test that when a timepoint is given, and is not truncated, the |
| 944 |
same value is returned.""" |
| 945 |
set_cycling_type(ISO8601_CYCLING_TYPE, "Z") |
| 946 |
my_now = "2018-03-14T15:12Z" |
| 947 |
timepoint_truncated = "19951231T0630" # 19951231T0630 |
| 948 |
output = ingest_time(timepoint_truncated, my_now) |
| 949 |
assert output == "19951231T0630" |
| 950 |
|
| 951 |
|
| 952 |
@pytest.mark.parametrize( |
| 953 |
"_input, errortext", |
| 954 |
( |
| 955 |
("next (T-00, T-30)", "T-00;T-30"), |
| 956 |
("next (wildebeest)", "Invalid ISO 8601 date"), |
| 957 |
), |
| 958 |
) |
| 959 |
def test_validate_fails_comma_sep_offset_list( |
| 960 |
_input, errortext, set_cycling_type |
| 961 |
): |
| 962 |
"""It raises an exception if validating a list separated by commas""" |
| 963 |
set_cycling_type(ISO8601_CYCLING_TYPE, "Z") |
| 964 |
with pytest.raises(Exception, match=errortext): |
| 965 |
ingest_time(_input) |