| 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 |
import pytest |
| 18 |
|
| 19 |
from cylc.flow.cycling.integer import ( |
| 20 |
IntegerSequence, |
| 21 |
IntegerPoint, |
| 22 |
IntegerInterval, |
| 23 |
IntervalParsingError, |
| 24 |
SequenceParsingError, |
| 25 |
) |
| 26 |
from cylc.flow.cycling.iso8601 import ( |
| 27 |
ISO8601Point, |
| 28 |
ISO8601Interval, |
| 29 |
) |
| 30 |
from cylc.flow.exceptions import CyclerTypeError |
| 31 |
|
| 32 |
|
| 33 |
def test_exclusions_simple(): |
| 34 |
"""Test the generation of points for integer sequences with exclusions. |
| 35 |
""" |
| 36 |
sequence = IntegerSequence('R/P1!3', 1, 5) |
| 37 |
output = [] |
| 38 |
point = sequence.get_start_point() |
| 39 |
while point: |
| 40 |
output.append(point) |
| 41 |
point = sequence.get_next_point(point) |
| 42 |
assert [int(out) for out in output] == [1, 2, 4, 5] |
| 43 |
|
| 44 |
|
| 45 |
def test_multiple_exclusions_simple(): |
| 46 |
"""Tests the multiple exclusion syntax for integer notation""" |
| 47 |
sequence = IntegerSequence('R/P1!(2,3,7)', 1, 10) |
| 48 |
output = [] |
| 49 |
point = sequence.get_start_point() |
| 50 |
while point: |
| 51 |
output.append(point) |
| 52 |
point = sequence.get_next_point(point) |
| 53 |
assert [int(out) for out in output] == [1, 4, 5, 6, 8, 9, 10] |
| 54 |
|
| 55 |
# duplicate excluded points should be ignored |
| 56 |
sequence1 = IntegerSequence('R/P1!(2,2,2)', 1, 10) |
| 57 |
sequence2 = IntegerSequence('R/P1!(2)', 1, 10) |
| 58 |
assert ( |
| 59 |
sequence1.exclusions.exclusion_points |
| 60 |
) == sequence2.exclusions.exclusion_points |
| 61 |
|
| 62 |
|
| 63 |
def test_multiple_exclusions_integer_sequence(): |
| 64 |
"""Tests the multiple exclusion syntax for integer notation""" |
| 65 |
sequence = IntegerSequence('P1 ! P2', 1, 10) |
| 66 |
output = [] |
| 67 |
point = sequence.get_start_point() |
| 68 |
while point: |
| 69 |
output.append(point) |
| 70 |
point = sequence.get_next_point(point) |
| 71 |
assert [int(out) for out in output] == [2, 4, 6, 8, 10] |
| 72 |
assert sequence.exclusions[0] == IntegerSequence('P2', 1, 10) |
| 73 |
|
| 74 |
|
| 75 |
def test_multiple_exclusions_integer_sequence2(): |
| 76 |
"""Tests the multiple exclusion syntax for integer notation""" |
| 77 |
sequence = IntegerSequence('P1 ! +P1/P2', 1, 10) |
| 78 |
output = [] |
| 79 |
point = sequence.get_start_point() |
| 80 |
while point: |
| 81 |
output.append(point) |
| 82 |
point = sequence.get_next_point(point) |
| 83 |
assert [int(out) for out in output] == [1, 3, 5, 7, 9] |
| 84 |
|
| 85 |
|
| 86 |
def test_multiple_exclusions_integer_sequence3(): |
| 87 |
"""Tests the multiple exclusion syntax for integer notation""" |
| 88 |
sequence = IntegerSequence('P1 ! (P2, 6, 8) ', 1, 10) |
| 89 |
output = [] |
| 90 |
point = sequence.get_start_point() |
| 91 |
while point: |
| 92 |
output.append(point) |
| 93 |
point = sequence.get_next_point(point) |
| 94 |
assert [int(out) for out in output] == [2, 4, 10] |
| 95 |
|
| 96 |
|
| 97 |
def test_multiple_exclusions_integer_sequence_weird_valid_formatting(): |
| 98 |
"""Tests the multiple exclusion syntax for integer notation""" |
| 99 |
sequence = IntegerSequence('P1 !(P2, 6,8) ', 1, 10) |
| 100 |
output = [] |
| 101 |
point = sequence.get_start_point() |
| 102 |
while point: |
| 103 |
output.append(point) |
| 104 |
point = sequence.get_next_point(point) |
| 105 |
assert [int(out) for out in output] == [2, 4, 10] |
| 106 |
|
| 107 |
|
| 108 |
def test_multiple_exclusions_integer_sequence_invalid_formatting(): |
| 109 |
"""Tests the multiple exclusion syntax for integer notation""" |
| 110 |
with pytest.raises(Exception): |
| 111 |
IntegerSequence('P1 !(6,8), P2 ', 1, 10) |
| 112 |
|
| 113 |
|
| 114 |
def test_multiple_exclusions_extensive(): |
| 115 |
"""Tests IntegerSequence methods for sequences with multi-exclusions""" |
| 116 |
points = [IntegerPoint(i) for i in range(10)] |
| 117 |
sequence = IntegerSequence('R/P1!(2,3,7)', 1, 10) |
| 118 |
assert not sequence.is_on_sequence(points[3]) |
| 119 |
assert not sequence.is_valid(points[3]) |
| 120 |
assert sequence.get_prev_point(points[3]) == points[1] |
| 121 |
assert sequence.get_prev_point(points[4]) == points[1] |
| 122 |
assert sequence.get_nearest_prev_point(points[3]) == points[1] |
| 123 |
assert sequence.get_nearest_prev_point(points[4]) == points[1] |
| 124 |
assert sequence.get_next_point(points[3]) == points[4] |
| 125 |
assert sequence.get_next_point(points[2]) == points[4] |
| 126 |
assert sequence.get_next_point_on_sequence(points[3]) == points[4] |
| 127 |
assert sequence.get_next_point_on_sequence(points[6]) == points[8] |
| 128 |
|
| 129 |
sequence = IntegerSequence('R/P1!(1,3,4)', 1, 10) |
| 130 |
assert sequence.get_first_point(points[1]) == points[2] |
| 131 |
assert sequence.get_first_point(points[0]) == points[2] |
| 132 |
assert sequence.get_start_point() == points[2] |
| 133 |
|
| 134 |
sequence = IntegerSequence('R/P1!(8,9,10)', 1, 10) |
| 135 |
assert sequence.get_stop_point() == points[7] |
| 136 |
|
| 137 |
|
| 138 |
def test_exclusions_extensive(): |
| 139 |
"""Test IntegerSequence methods for sequences with exclusions.""" |
| 140 |
point_0 = IntegerPoint(0) |
| 141 |
point_1 = IntegerPoint(1) |
| 142 |
point_2 = IntegerPoint(2) |
| 143 |
point_3 = IntegerPoint(3) |
| 144 |
point_4 = IntegerPoint(4) |
| 145 |
|
| 146 |
sequence = IntegerSequence('R/P1!3', 1, 5) |
| 147 |
assert not sequence.is_on_sequence(point_3) |
| 148 |
assert not sequence.is_valid(point_3) |
| 149 |
assert sequence.get_prev_point(point_3) == point_2 |
| 150 |
assert sequence.get_prev_point(point_4) == point_2 |
| 151 |
assert sequence.get_nearest_prev_point(point_3) == point_2 |
| 152 |
assert sequence.get_nearest_prev_point(point_3) == point_2 |
| 153 |
assert sequence.get_next_point(point_3) == point_4 |
| 154 |
assert sequence.get_next_point(point_2) == point_4 |
| 155 |
assert sequence.get_next_point_on_sequence(point_3) == point_4 |
| 156 |
assert sequence.get_next_point_on_sequence(point_2) == point_4 |
| 157 |
|
| 158 |
sequence = IntegerSequence('R/P1!1', 1, 5) |
| 159 |
assert sequence.get_first_point(point_1) == point_2 |
| 160 |
assert sequence.get_first_point(point_0) == point_2 |
| 161 |
assert sequence.get_start_point() == point_2 |
| 162 |
|
| 163 |
sequence = IntegerSequence('R/P1!5', 1, 5) |
| 164 |
assert sequence.get_stop_point() == point_4 |
| 165 |
|
| 166 |
|
| 167 |
def test_simple(): |
| 168 |
"""Run some simple tests for integer cycling.""" |
| 169 |
sequence = IntegerSequence('R/1/P3', 1, 10) |
| 170 |
start = sequence.p_start |
| 171 |
stop = sequence.p_stop |
| 172 |
|
| 173 |
# Test point generation forwards. |
| 174 |
point = start |
| 175 |
output = [] |
| 176 |
while point and stop and point <= stop: |
| 177 |
output.append(point) |
| 178 |
point = sequence.get_next_point(point) |
| 179 |
assert [int(out) for out in output] == [1, 4, 7, 10] |
| 180 |
|
| 181 |
# Test point generation backwards. |
| 182 |
point = stop |
| 183 |
output = [] |
| 184 |
while point and start and point >= start: |
| 185 |
output.append(point) |
| 186 |
point = sequence.get_prev_point(point) |
| 187 |
assert [int(out) for out in output] == [10, 7, 4, 1] |
| 188 |
|
| 189 |
# Test sequence comparison |
| 190 |
sequence1 = IntegerSequence('R/1/P2', 1, 10) |
| 191 |
sequence2 = IntegerSequence('R/1/P2', 1, 10) |
| 192 |
assert sequence1 == sequence2 |
| 193 |
sequence2.set_offset(IntegerInterval('-P2')) |
| 194 |
assert sequence1 == sequence2 |
| 195 |
sequence2.set_offset(IntegerInterval('-P1')) |
| 196 |
assert sequence1 != sequence2 |
| 197 |
|
| 198 |
|
| 199 |
def test_interval_parsing_error(): |
| 200 |
"""It should reject invalid intervals.""" |
| 201 |
with pytest.raises(IntervalParsingError): |
| 202 |
IntegerInterval(42) |
| 203 |
with pytest.raises(IntervalParsingError): |
| 204 |
IntegerInterval('forty two') |
| 205 |
|
| 206 |
|
| 207 |
def test_sequence_parsing_error(): |
| 208 |
with pytest.raises(SequenceParsingError): |
| 209 |
IntegerSequence('zz0+za', 1) |
| 210 |
|
| 211 |
|
| 212 |
def test_interval_arithmetic(): |
| 213 |
"""It should do basic maths on integer intervals.""" |
| 214 |
a = IntegerInterval('P2') |
| 215 |
b = IntegerInterval('P3') |
| 216 |
p = IntegerPoint('3') |
| 217 |
|
| 218 |
assert a + b == IntegerInterval('P5') |
| 219 |
assert a + p == IntegerPoint('5') |
| 220 |
assert a - b == IntegerInterval('-P1') |
| 221 |
assert a - p == IntegerPoint('-1') |
| 222 |
|
| 223 |
assert abs(IntegerInterval('-P2')) == IntegerInterval('P2') |
| 224 |
|
| 225 |
assert a + IntegerInterval.get_null_offset() == a |
| 226 |
|
| 227 |
with pytest.raises(CyclerTypeError): |
| 228 |
IntegerPoint(1000) - ISO8601Point('1000') |
| 229 |
|
| 230 |
with pytest.raises(CyclerTypeError): |
| 231 |
IntegerPoint(1000) + ISO8601Point('1000') |
| 232 |
|
| 233 |
with pytest.raises(CyclerTypeError): |
| 234 |
IntegerInterval('P1') - ISO8601Interval('P1Y') |
| 235 |
|
| 236 |
with pytest.raises(CyclerTypeError): |
| 237 |
IntegerInterval('P1') + ISO8601Interval('P1Y') |
| 238 |
|
| 239 |
|
| 240 |
def test_async_expr(): |
| 241 |
"""The async expression should run once and only once.""" |
| 242 |
point = IntegerPoint('5') |
| 243 |
sequence = IntegerSequence(IntegerSequence.get_async_expr(point), 1, 10) |
| 244 |
assert sequence.get_next_point(IntegerPoint('1')) == point |
| 245 |
assert sequence.get_next_point(IntegerPoint('5')) is None |
| 246 |
|
| 247 |
|
| 248 |
def test_point_comparisons(): |
| 249 |
# basic comparisons |
| 250 |
assert IntegerPoint(1) == IntegerPoint(1) |
| 251 |
assert IntegerPoint(1) < IntegerPoint(2) |
| 252 |
assert IntegerPoint(1) <= IntegerPoint(2) |
| 253 |
assert IntegerPoint(2) >= IntegerPoint(1) |
| 254 |
assert IntegerPoint(1) != IntegerPoint(2) |
| 255 |
assert IntegerInterval('P1') == IntegerInterval('P1') |
| 256 |
assert IntegerInterval('P1') < IntegerInterval('P2') |
| 257 |
assert IntegerInterval('P1') <= IntegerInterval('P2') |
| 258 |
assert IntegerInterval('P2') >= IntegerInterval('P1') |
| 259 |
assert IntegerInterval('P1') != IntegerInterval('P2') |
| 260 |
|
| 261 |
# None comparisons work counter intuitively |
| 262 |
# (reason unknown) |
| 263 |
assert IntegerPoint(1) < None |
| 264 |
assert None > IntegerPoint(1) |
| 265 |
assert IntegerInterval('P1') < None |
| 266 |
assert None > IntegerInterval('P1') |
| 267 |
|
| 268 |
# compare against other PointBase implementations |
| 269 |
assert ISO8601Point('1000') > IntegerPoint(1000) |
| 270 |
assert IntegerPoint(1000) < ISO8601Point('1000') |
| 271 |
assert ISO8601Interval('P1Y') > IntegerInterval('P1') |
| 272 |
assert IntegerInterval('P1') < ISO8601Interval('P1Y') |
| 273 |
|
| 274 |
|
| 275 |
def test_string_representations(): |
| 276 |
p = IntegerPoint(1) |
| 277 |
assert str(p) == '1' |
| 278 |
assert repr(p) == '1' |
| 279 |
|
| 280 |
i = IntegerInterval('P1') |
| 281 |
assert str(i) == 'P1' |
| 282 |
assert repr(i) == '<IntegerInterval P1>' |