| 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 |
"""Unit Tests for cylc.flow.parsec.validate.ParsecValidator.coerce methods.""" |
| 17 |
|
| 18 |
import logging |
| 19 |
from typing import List |
| 20 |
|
| 21 |
from cylc.flow import CYLC_LOG |
| 22 |
import pytest |
| 23 |
from pytest import approx, param |
| 24 |
|
| 25 |
from cylc.flow.parsec.config import ConfigNode as Conf |
| 26 |
from cylc.flow.parsec.OrderedDict import OrderedDictWithDefaults |
| 27 |
from cylc.flow.parsec.exceptions import IllegalValueError |
| 28 |
from cylc.flow.parsec.validate import ( |
| 29 |
BroadcastConfigValidator, |
| 30 |
CylcConfigValidator as VDR, |
| 31 |
DurationFloat, |
| 32 |
ListValueError, |
| 33 |
IllegalItemError, |
| 34 |
ParsecValidator, |
| 35 |
parsec_validate |
| 36 |
) |
| 37 |
|
| 38 |
|
| 39 |
@pytest.fixture |
| 40 |
def sample_spec(): |
| 41 |
with Conf('myconf') as myconf: |
| 42 |
with Conf('section1'): |
| 43 |
Conf('value1', default='') |
| 44 |
Conf('value2', default='what?') |
| 45 |
with Conf('section2'): |
| 46 |
Conf('enabled', VDR.V_BOOLEAN) |
| 47 |
with Conf('section3'): |
| 48 |
Conf('title', default='default', options=['1', '2']) |
| 49 |
Conf( |
| 50 |
'amounts', |
| 51 |
VDR.V_INTEGER_LIST, |
| 52 |
default=[1, 2, 3], |
| 53 |
# options=[[1, 2, 3]] |
| 54 |
) |
| 55 |
with Conf('entries'): |
| 56 |
Conf('key') |
| 57 |
Conf('value') |
| 58 |
with Conf('<whatever>'): |
| 59 |
Conf('section300000', default='') |
| 60 |
Conf('ids', VDR.V_INTEGER_LIST) |
| 61 |
return myconf |
| 62 |
|
| 63 |
|
| 64 |
def validator_invalid_values(): |
| 65 |
""" |
| 66 |
Data provider or invalid values for parsec validator. All values must not |
| 67 |
be null (covered elsewhere), and not dict's. |
| 68 |
|
| 69 |
Possible invalid scenarios must include: |
| 70 |
|
| 71 |
- cfg[key] is a list AND a value is not in list of the possible values |
| 72 |
- OR |
| 73 |
- cfg[key] is not a list AND cfg[key] not in the list of possible values |
| 74 |
|
| 75 |
:return: a list with sets of tuples for the test parameters |
| 76 |
:rtype: list |
| 77 |
""" |
| 78 |
|
| 79 |
values = [] |
| 80 |
|
| 81 |
# variables reused throughout |
| 82 |
spec = None |
| 83 |
msg = None |
| 84 |
|
| 85 |
# set 1 (t, f, f, t) |
| 86 |
with Conf('base') as spec: |
| 87 |
Conf('value', VDR.V_INTEGER_LIST, default=1, options=[1, 2, 3, 4]) |
| 88 |
cfg = OrderedDictWithDefaults() |
| 89 |
cfg['value'] = "1, 2, 3" |
| 90 |
msg = None |
| 91 |
values.append((spec, cfg, msg)) |
| 92 |
|
| 93 |
# set 2 (t, t, f, t) |
| 94 |
with Conf('base') as spec: |
| 95 |
Conf('value', VDR.V_INTEGER_LIST, default=1, options=[1, 2, 3, 4]) |
| 96 |
cfg = OrderedDictWithDefaults() |
| 97 |
cfg['value'] = "1, 2, 5" |
| 98 |
msg = '(type=option) value = 5' |
| 99 |
values.append((spec, cfg, msg)) |
| 100 |
|
| 101 |
# set 3 (f, f, t, f) |
| 102 |
with Conf('base') as spec: |
| 103 |
Conf('value', VDR.V_INTEGER, default=1, options=[2, 3, 4]) |
| 104 |
cfg = OrderedDictWithDefaults() |
| 105 |
cfg['value'] = "2" |
| 106 |
msg = None |
| 107 |
values.append((spec, cfg, msg)) |
| 108 |
|
| 109 |
# set 4 (f, f, t, t) |
| 110 |
with Conf('base') as spec: |
| 111 |
Conf('value', VDR.V_INTEGER, default=1, options=[1, 2, 3, 4]) |
| 112 |
cfg = OrderedDictWithDefaults() |
| 113 |
cfg['value'] = "5" |
| 114 |
msg = '(type=option) value = 5' |
| 115 |
values.append((spec, cfg, msg)) |
| 116 |
|
| 117 |
return values |
| 118 |
|
| 119 |
|
| 120 |
@pytest.fixture |
| 121 |
def strip_and_unquote_list(): |
| 122 |
return [ |
| 123 |
[ |
| 124 |
'"a,b", c, "d e"', # input |
| 125 |
["a,b", "c", "d e"] # expected |
| 126 |
], |
| 127 |
[ |
| 128 |
'foo bar baz', # input |
| 129 |
["foo bar baz"] # expected |
| 130 |
], |
| 131 |
[ |
| 132 |
'"a", \'b\', c', # input |
| 133 |
["a", "b", "c"] # expected |
| 134 |
], |
| 135 |
[ |
| 136 |
'a b c, d e f', # input |
| 137 |
["a b c", "d e f"] # expected |
| 138 |
], |
| 139 |
] |
| 140 |
|
| 141 |
|
| 142 |
def test_list_value_error(): |
| 143 |
keys = ['a,', 'b', 'c'] |
| 144 |
value = 'a sample value' |
| 145 |
error = ListValueError(keys, value, "who cares") |
| 146 |
output = str(error) |
| 147 |
expected = '(type=list) [a,][b]c = a sample value - (who cares)' |
| 148 |
assert expected == output |
| 149 |
|
| 150 |
|
| 151 |
def test_list_value_error_with_exception(): |
| 152 |
keys = ['a,', 'b', 'c'] |
| 153 |
value = 'a sample value' |
| 154 |
exc = Exception('test') |
| 155 |
error = ListValueError(keys, value, "who cares", exc) |
| 156 |
output = str(error) |
| 157 |
expected = '(type=list) [a,][b]c = a sample value - (test: who cares)' |
| 158 |
assert expected == output |
| 159 |
|
| 160 |
|
| 161 |
def test_illegal_value_error(): |
| 162 |
value_type = 'ClassA' |
| 163 |
keys = ['a,', 'b', 'c'] |
| 164 |
value = 'a sample value' |
| 165 |
error = IllegalValueError(value_type, keys, value) |
| 166 |
output = str(error) |
| 167 |
expected = "(type=ClassA) [a,][b]c = a sample value" |
| 168 |
assert expected == output |
| 169 |
|
| 170 |
|
| 171 |
def test_illegal_value_error_with_exception(): |
| 172 |
value_type = 'ClassA' |
| 173 |
keys = ['a,', 'b', 'c'] |
| 174 |
value = 'a sample value' |
| 175 |
exc = Exception('test') |
| 176 |
error = IllegalValueError(value_type, keys, value, exc) |
| 177 |
output = str(error) |
| 178 |
expected = "(type=ClassA) [a,][b]c = a sample value - (test)" |
| 179 |
assert expected == output |
| 180 |
|
| 181 |
|
| 182 |
def test_illegal_item_error(): |
| 183 |
keys = ['a,', 'b', 'c'] |
| 184 |
key = 'a sample value' |
| 185 |
error = IllegalItemError(keys, key) |
| 186 |
output = str(error) |
| 187 |
expected = "[a,][b][c]a sample value" |
| 188 |
assert expected == output |
| 189 |
|
| 190 |
|
| 191 |
def test_illegal_item_error_message(): |
| 192 |
keys = ['a,', 'b', 'c'] |
| 193 |
key = 'a sample value' |
| 194 |
message = "invalid" |
| 195 |
error = IllegalItemError(keys, key, message) |
| 196 |
output = str(error) |
| 197 |
expected = "[a,][b][c]a sample value - (invalid)" |
| 198 |
assert expected == output |
| 199 |
|
| 200 |
|
| 201 |
def test_parsec_validator_invalid_key(sample_spec): |
| 202 |
parsec_validator = ParsecValidator() |
| 203 |
cfg = OrderedDictWithDefaults() |
| 204 |
cfg['section1'] = OrderedDictWithDefaults() |
| 205 |
cfg['section1']['value1'] = '1' |
| 206 |
cfg['section1']['value2'] = '2' |
| 207 |
cfg['section22'] = 'abc' |
| 208 |
with pytest.raises(IllegalItemError): |
| 209 |
parsec_validator.validate(cfg, sample_spec) |
| 210 |
|
| 211 |
|
| 212 |
def test_parsec_validator_invalid_key_no_spec(sample_spec): |
| 213 |
parsec_validator = ParsecValidator() |
| 214 |
cfg = OrderedDictWithDefaults() |
| 215 |
cfg['section1'] = OrderedDictWithDefaults() |
| 216 |
cfg['section1']['value1'] = '1' |
| 217 |
cfg['section1']['value2'] = '2' |
| 218 |
cfg['section22'] = 'abc' |
| 219 |
# remove the user-defined section from the spec |
| 220 |
sample_spec._children = { |
| 221 |
key: value |
| 222 |
for key, value in sample_spec._children.items() |
| 223 |
if key != '__MANY__' |
| 224 |
} |
| 225 |
with pytest.raises(IllegalItemError): |
| 226 |
parsec_validator.validate(cfg, sample_spec) |
| 227 |
|
| 228 |
|
| 229 |
def test_parsec_validator_invalid_key_with_many_spaces(sample_spec): |
| 230 |
parsec_validator = ParsecValidator() |
| 231 |
cfg = OrderedDictWithDefaults() |
| 232 |
cfg['section1'] = OrderedDictWithDefaults() |
| 233 |
cfg['section1']['value1'] = '1' |
| 234 |
cfg['section1']['value2'] = '2' |
| 235 |
cfg['section 3000000'] = 'test' |
| 236 |
with pytest.raises(IllegalItemError) as cm: |
| 237 |
parsec_validator.validate(cfg, sample_spec) |
| 238 |
assert str(cm.exception) == "section 3000000 - (consecutive spaces)" |
| 239 |
|
| 240 |
|
| 241 |
@pytest.mark.parametrize('spec, cfg, msg', validator_invalid_values()) |
| 242 |
def test_parsec_validator_invalid_key_with_many_invalid_values( |
| 243 |
spec, cfg, msg |
| 244 |
): |
| 245 |
parsec_validator = ParsecValidator() |
| 246 |
if msg is not None: |
| 247 |
with pytest.raises(IllegalValueError) as cm: |
| 248 |
parsec_validator.validate(cfg, spec) |
| 249 |
assert msg == str(cm.value) |
| 250 |
else: |
| 251 |
# cylc.flow.parsec_validator.validate(cfg, spec) |
| 252 |
# let's use the alias `parsec_validate` here |
| 253 |
parsec_validate(cfg, spec) |
| 254 |
# TBD assertIsNotNone when 2.6+ |
| 255 |
assert parsec_validator is not None |
| 256 |
|
| 257 |
|
| 258 |
def test_parsec_validator_warn_options(caplog): |
| 259 |
"""Test the "warn_options" option. |
| 260 |
|
| 261 |
This should turn invalid option errors into warnings. |
| 262 |
""" |
| 263 |
with Conf('base') as spec: |
| 264 |
Conf( |
| 265 |
'foo', |
| 266 |
VDR.V_STRING_LIST, |
| 267 |
default=1, |
| 268 |
options=['a', 'b'], |
| 269 |
warn_options=True, |
| 270 |
) |
| 271 |
|
| 272 |
parsec_validator = ParsecValidator() |
| 273 |
caplog.set_level(logging.WARNING, CYLC_LOG) |
| 274 |
parsec_validator.validate({'foo': 'b, c'}, spec) |
| 275 |
|
| 276 |
# there should be one (and only one) warning |
| 277 |
assert caplog.messages == [ |
| 278 |
'(type=option) foo = c\nInvalid items have been removed' |
| 279 |
] |
| 280 |
|
| 281 |
|
| 282 |
def test_parsec_validator_invalid_key_with_many_1(sample_spec): |
| 283 |
parsec_validator = ParsecValidator() |
| 284 |
cfg = OrderedDictWithDefaults() |
| 285 |
cfg['section1'] = OrderedDictWithDefaults() |
| 286 |
cfg['section1']['value1'] = '1' |
| 287 |
cfg['section1']['value2'] = '2' |
| 288 |
cfg['section3000000'] = OrderedDictWithDefaults() |
| 289 |
parsec_validator.validate(cfg, sample_spec) |
| 290 |
# TBD assertIsNotNone when 2.6+ |
| 291 |
assert parsec_validator is not None |
| 292 |
|
| 293 |
|
| 294 |
def test_parsec_validator_invalid_key_with_many_2(sample_spec): |
| 295 |
parsec_validator = ParsecValidator() |
| 296 |
cfg = OrderedDictWithDefaults() |
| 297 |
cfg['section3'] = OrderedDictWithDefaults() |
| 298 |
cfg['section3']['title'] = '1' |
| 299 |
cfg['section3']['entries'] = OrderedDictWithDefaults() |
| 300 |
cfg['section3']['entries']['key'] = 'name' |
| 301 |
cfg['section3']['entries']['value'] = "1, 2, 3, 4" |
| 302 |
parsec_validator.validate(cfg, sample_spec) |
| 303 |
# TBD assertIsNotNone when 2.6+ |
| 304 |
assert parsec_validator is not None |
| 305 |
|
| 306 |
|
| 307 |
def test_parsec_validator(sample_spec): |
| 308 |
parsec_validator = ParsecValidator() |
| 309 |
cfg = OrderedDictWithDefaults() |
| 310 |
cfg['section1'] = OrderedDictWithDefaults() |
| 311 |
cfg['section1']['value1'] = '1' |
| 312 |
cfg['section1']['value2'] = '2' |
| 313 |
cfg['section3'] = OrderedDictWithDefaults() |
| 314 |
cfg['section3']['title'] = None |
| 315 |
parsec_validator.validate(cfg, sample_spec) |
| 316 |
# TBD assertIsNotNone when 2.6+ |
| 317 |
assert parsec_validator is not None |
| 318 |
|
| 319 |
# --- static methods |
| 320 |
|
| 321 |
|
| 322 |
def test_coerce_none_fails(): |
| 323 |
with pytest.raises(AttributeError): |
| 324 |
ParsecValidator.coerce_boolean(None, []) |
| 325 |
with pytest.raises(AttributeError): |
| 326 |
ParsecValidator.coerce_float(None, []) |
| 327 |
with pytest.raises(AttributeError): |
| 328 |
ParsecValidator.coerce_int(None, []) |
| 329 |
|
| 330 |
|
| 331 |
def test_coerce_boolean(): |
| 332 |
"""Test coerce_boolean.""" |
| 333 |
validator = ParsecValidator() |
| 334 |
# The good |
| 335 |
for value, result in [ |
| 336 |
('True', True), |
| 337 |
(' True ', True), |
| 338 |
('"True"', True), |
| 339 |
("'True'", True), |
| 340 |
('true', True), |
| 341 |
(' true ', True), |
| 342 |
('"true"', True), |
| 343 |
("'true'", True), |
| 344 |
('False', False), |
| 345 |
(' False ', False), |
| 346 |
('"False"', False), |
| 347 |
("'False'", False), |
| 348 |
('false', False), |
| 349 |
(' false ', False), |
| 350 |
('"false"', False), |
| 351 |
("'false'", False), |
| 352 |
('', None), |
| 353 |
(' ', None) |
| 354 |
]: |
| 355 |
assert validator.coerce_boolean(value, ['whatever']) == result |
| 356 |
# The bad |
| 357 |
for value in [ |
| 358 |
'None', ' Who cares? ', '3.14', '[]', '[True]', 'True, False' |
| 359 |
]: |
| 360 |
with pytest.raises(IllegalValueError): |
| 361 |
validator.coerce_boolean(value, ['whatever']) |
| 362 |
|
| 363 |
|
| 364 |
@pytest.mark.parametrize( |
| 365 |
'value, expected', |
| 366 |
[ |
| 367 |
('3', 3.0), |
| 368 |
('9.80', 9.80), |
| 369 |
('3.141592654', 3.141592654), |
| 370 |
('"3.141592654"', 3.141592654), |
| 371 |
("'3.141592654'", 3.141592654), |
| 372 |
('-3', -3.0), |
| 373 |
('-3.1', -3.1), |
| 374 |
('0', 0.0), |
| 375 |
('-0', -0.0), |
| 376 |
('0.0', 0.0), |
| 377 |
('1e20', 1.0e20), |
| 378 |
('6.02e23', 6.02e23), |
| 379 |
('-1.6021765e-19', -1.6021765e-19), |
| 380 |
('6.62607004e-34', 6.62607004e-34), |
| 381 |
] |
| 382 |
) |
| 383 |
def test_coerce_float(value: str, expected: float): |
| 384 |
"""Test coerce_float.""" |
| 385 |
assert ( |
| 386 |
ParsecValidator.coerce_float(value, ['whatever']) == approx(expected) |
| 387 |
) |
| 388 |
|
| 389 |
|
| 390 |
def test_coerce_float__empty(): |
| 391 |
# not a number |
| 392 |
assert ParsecValidator.coerce_float('', ['whatever']) is None |
| 393 |
|
| 394 |
|
| 395 |
@pytest.mark.parametrize( |
| 396 |
'value', |
| 397 |
['None', ' Who cares? ', 'True', '[]', '[3.14]', '3.14, 2.72'] |
| 398 |
) |
| 399 |
def test_coerce_float__bad(value: str): |
| 400 |
with pytest.raises(IllegalValueError): |
| 401 |
ParsecValidator.coerce_float(value, ['whatever']) |
| 402 |
|
| 403 |
|
| 404 |
@pytest.mark.parametrize( |
| 405 |
'value, expected', |
| 406 |
[ |
| 407 |
('', []), |
| 408 |
('3', [3.0]), |
| 409 |
('2*3.141592654', [3.141592654, 3.141592654]), |
| 410 |
('12*8, 8*12.0', [8.0] * 12 + [12.0] * 8), |
| 411 |
('-3, -2, -1, -0.0, 1.0', [-3.0, -2.0, -1.0, -0.0, 1.0]), |
| 412 |
('6.02e23, -1.6021765e-19, 6.62607004e-34', |
| 413 |
[6.02e23, -1.6021765e-19, 6.62607004e-34]), |
| 414 |
] |
| 415 |
) |
| 416 |
def test_coerce_float_list(value: str, expected: List[float]): |
| 417 |
"""Test coerce_float_list.""" |
| 418 |
items = ParsecValidator.coerce_float_list(value, ['whatever']) |
| 419 |
assert items == approx(expected) |
| 420 |
|
| 421 |
|
| 422 |
@pytest.mark.parametrize( |
| 423 |
'value', |
| 424 |
['None', 'e, i, e, i, o', '[]', '[3.14]', 'pi, 2.72', '2*True'] |
| 425 |
) |
| 426 |
def test_coerce_float_list__bad(value: str): |
| 427 |
with pytest.raises(IllegalValueError): |
| 428 |
ParsecValidator.coerce_float_list(value, ['whatever']) |
| 429 |
|
| 430 |
|
| 431 |
@pytest.mark.parametrize( |
| 432 |
'value, expected', |
| 433 |
[ |
| 434 |
('0', 0), |
| 435 |
('3', 3), |
| 436 |
('-3', -3), |
| 437 |
('-0', -0), |
| 438 |
('653456', 653456), |
| 439 |
('-8362583645365', -8362583645365) |
| 440 |
] |
| 441 |
) |
| 442 |
def test_coerce_int(value: str, expected: int): |
| 443 |
"""Test coerce_int.""" |
| 444 |
assert ParsecValidator.coerce_int(value, ['whatever']) == expected |
| 445 |
|
| 446 |
|
| 447 |
def test_coerce_int__empty(): |
| 448 |
assert ParsecValidator.coerce_int('', ['whatever']) is None # not a number |
| 449 |
|
| 450 |
|
| 451 |
@pytest.mark.parametrize( |
| 452 |
'value', |
| 453 |
['None', ' Who cares? ', 'True', '4.8', '[]', '[3]', '60*60'] |
| 454 |
) |
| 455 |
def test_coerce_int__bad(value: str): |
| 456 |
with pytest.raises(IllegalValueError): |
| 457 |
ParsecValidator.coerce_int(value, ['whatever']) |
| 458 |
|
| 459 |
|
| 460 |
def test_coerce_int_list(): |
| 461 |
"""Test coerce_int_list.""" |
| 462 |
validator = ParsecValidator() |
| 463 |
# The good |
| 464 |
for value, results in [ |
| 465 |
('', []), |
| 466 |
('3', [3]), |
| 467 |
('1..10, 11..20..2', |
| 468 |
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19]), |
| 469 |
('18 .. 24', [18, 19, 20, 21, 22, 23, 24]), |
| 470 |
('18 .. 24 .. 3', [18, 21, 24]), |
| 471 |
('-10..10..3', [-10, -7, -4, -1, 2, 5, 8]), |
| 472 |
('10*3, 4*-6', [3] * 10 + [-6] * 4), |
| 473 |
('10*128, -78..-72, 2048', |
| 474 |
[128] * 10 + [-78, -77, -76, -75, -74, -73, -72, 2048]) |
| 475 |
]: |
| 476 |
assert validator.coerce_int_list(value, ['whatever']) == results |
| 477 |
# The bad |
| 478 |
for value in [ |
| 479 |
'None', 'e, i, e, i, o', '[]', '1..3, x', 'one..ten' |
| 480 |
]: |
| 481 |
with pytest.raises(IllegalValueError): |
| 482 |
validator.coerce_int_list(value, ['whatever']) |
| 483 |
|
| 484 |
|
| 485 |
@pytest.mark.parametrize( |
| 486 |
'value, expected', |
| 487 |
[ |
| 488 |
('', ''), |
| 489 |
('Hello World!', 'Hello World!'), |
| 490 |
('"Hello World!"', 'Hello World!'), |
| 491 |
('"Hello Cylc\'s World!"', 'Hello Cylc\'s World!'), |
| 492 |
("'Hello World!'", 'Hello World!'), |
| 493 |
('0', '0'), |
| 494 |
('My list is:\nfoo, bar, baz\n', 'My list is:\nfoo, bar, baz'), |
| 495 |
(' Hello:\n foo\n bar\n baz\n', |
| 496 |
'Hello:\nfoo\nbar\nbaz'), |
| 497 |
(' Hello:\n foo\n Greet\n baz\n', |
| 498 |
'Hello:\n foo\nGreet\n baz'), |
| 499 |
('False', 'False'), |
| 500 |
('None', 'None'), |
| 501 |
(['a', 'b'], 'a\nb'), |
| 502 |
('abc#def', 'abc'), |
| 503 |
] |
| 504 |
) |
| 505 |
def test_coerce_str(value: str, expected: str): |
| 506 |
"""Test coerce_str.""" |
| 507 |
validator = ParsecValidator() |
| 508 |
# The good |
| 509 |
assert validator.coerce_str(value, ['whatever']) == expected |
| 510 |
|
| 511 |
|
| 512 |
def test_coerce_str_list(): |
| 513 |
"""Test coerce_str_list.""" |
| 514 |
validator = ParsecValidator() |
| 515 |
# The good |
| 516 |
for value, results in [ |
| 517 |
('', []), |
| 518 |
('Hello', ['Hello']), |
| 519 |
('"Hello"', ['Hello']), |
| 520 |
('1', ['1']), |
| 521 |
('Mercury, Venus, Earth, Mars', |
| 522 |
['Mercury', 'Venus', 'Earth', 'Mars']), |
| 523 |
('Mercury, Venus, Earth, Mars,\n"Jupiter",\n"Saturn"\n', |
| 524 |
['Mercury', 'Venus', 'Earth', 'Mars', 'Jupiter', 'Saturn']), |
| 525 |
('New Zealand, United Kingdom', |
| 526 |
['New Zealand', 'United Kingdom']) |
| 527 |
]: |
| 528 |
assert validator.coerce_str_list(value, ['whatever']) == results |
| 529 |
|
| 530 |
|
| 531 |
@pytest.mark.parametrize('value, expected', [ |
| 532 |
param( |
| 533 |
"'a'", |
| 534 |
'a', |
| 535 |
id="single quotes" |
| 536 |
), |
| 537 |
param( |
| 538 |
'"\'a\'"', |
| 539 |
"'a'", |
| 540 |
id="single quotes inside double quotes" |
| 541 |
), |
| 542 |
param( |
| 543 |
'" a b" # comment', |
| 544 |
' a b', |
| 545 |
id="comment outside" |
| 546 |
), |
| 547 |
param( |
| 548 |
'"""bene\ngesserit"""', |
| 549 |
'bene\ngesserit', |
| 550 |
id="multiline double quotes" |
| 551 |
), |
| 552 |
param( |
| 553 |
"'''kwisatz\n haderach'''", |
| 554 |
'kwisatz\n haderach', |
| 555 |
id="multiline single quotes" |
| 556 |
), |
| 557 |
param( |
| 558 |
'"""a\nb""" # comment', |
| 559 |
'a\nb', |
| 560 |
id="multiline with comment outside" |
| 561 |
), |
| 562 |
]) |
| 563 |
def test_unquote(value: str, expected: str): |
| 564 |
"""Test strip_and_unquote.""" |
| 565 |
assert ParsecValidator._unquote(['a'], value) == expected |
| 566 |
|
| 567 |
|
| 568 |
@pytest.mark.parametrize('value', [ |
| 569 |
'"""', |
| 570 |
"'''", |
| 571 |
"'don't do this'", |
| 572 |
]) |
| 573 |
def test_strip_and_unquote__bad(value: str): |
| 574 |
with pytest.raises(IllegalValueError): |
| 575 |
ParsecValidator.strip_and_unquote(['a'], value) |
| 576 |
|
| 577 |
|
| 578 |
def test_strip_and_unquote_list_parsec(): |
| 579 |
"""Test strip_and_unquote_list using ParsecValidator.""" |
| 580 |
for value, results in [ |
| 581 |
('"a"\n"b"', ['a', 'b']), |
| 582 |
('"a", "b"', ['a', 'b']), |
| 583 |
('"a", "b"', ['a', 'b']), |
| 584 |
('"c" # d', ['c']), |
| 585 |
('"a", "b", "c" # d', ['a', 'b', 'c']), |
| 586 |
('"a"\n"b"\n"c" # d', ['a', 'b', 'c']), |
| 587 |
("'a', 'b'", ['a', 'b']), |
| 588 |
("'c' #d", ['c']), |
| 589 |
("'a', 'b', 'c' # d", ['a', 'b', 'c']), |
| 590 |
("'a'\n'b'\n'c' # d", ['a', 'b', 'c']), |
| 591 |
('a, b, c,', ['a', 'b', 'c']), |
| 592 |
('a, b, c # d', ['a', 'b', 'c']), |
| 593 |
('a, b, c\n"d"', ['a', 'b', 'd']), |
| 594 |
('a, b, c\n"d" # e', ['a', 'b', '"d"']) |
| 595 |
]: |
| 596 |
assert results == ParsecValidator.strip_and_unquote_list( |
| 597 |
['a'], value) |
| 598 |
|
| 599 |
|
| 600 |
def test_strip_and_unquote_list_cylc(strip_and_unquote_list): |
| 601 |
"""Test strip_and_unquote_list using CylcConfigValidator.""" |
| 602 |
validator = VDR() |
| 603 |
for values in strip_and_unquote_list: |
| 604 |
value = values[0] |
| 605 |
expected = values[1] |
| 606 |
output = validator.strip_and_unquote_list(keys=[], value=value) |
| 607 |
assert expected == output |
| 608 |
|
| 609 |
|
| 610 |
def test_strip_and_unquote_list_multiparam(): |
| 611 |
with pytest.raises(ListValueError): |
| 612 |
ParsecValidator.strip_and_unquote_list( |
| 613 |
['a'], 'a, b, c<a,b>' |
| 614 |
) |
| 615 |
|
| 616 |
|
| 617 |
def test_coerce_cycle_point(): |
| 618 |
"""Test coerce_cycle_point.""" |
| 619 |
validator = VDR() |
| 620 |
# The good |
| 621 |
for value, result in [ |
| 622 |
('', None), |
| 623 |
('3', '3'), |
| 624 |
('2018', '2018'), |
| 625 |
('20181225T12Z', '20181225T12Z'), |
| 626 |
('2018-12-25T12:00+11:00', '2018-12-25T12:00+11:00')]: |
| 627 |
assert validator.coerce_cycle_point(value, ['whatever']) == result |
| 628 |
# The bad |
| 629 |
for value in [ |
| 630 |
'None', ' Who cares? ', 'True', '1, 2', '20781340E10']: |
| 631 |
with pytest.raises(IllegalValueError): |
| 632 |
validator.coerce_cycle_point(value, ['whatever']) |
| 633 |
|
| 634 |
|
| 635 |
def test_coerce_cycle_point_format(): |
| 636 |
"""Test coerce_cycle_point_format.""" |
| 637 |
validator = VDR() |
| 638 |
# The good |
| 639 |
for value, result in [ |
| 640 |
('', None), |
| 641 |
('%Y%m%dT%H%M%z', '%Y%m%dT%H%M%z'), |
| 642 |
('CCYYMMDDThhmmZ', 'CCYYMMDDThhmmZ'), |
| 643 |
('XCCYYMMDDThhmmZ', 'XCCYYMMDDThhmmZ')]: |
| 644 |
assert ( |
| 645 |
validator.coerce_cycle_point_format(value, ['whatever']) |
| 646 |
== result |
| 647 |
) |
| 648 |
# The bad |
| 649 |
# '/' and ':' not allowed in cylc cycle points (they are used in paths). |
| 650 |
for value in ['%i%j', 'Y/M/D', '%Y-%m-%dT%H:%MZ']: |
| 651 |
with pytest.raises(IllegalValueError): |
| 652 |
validator.coerce_cycle_point_format(value, ['whatever']) |
| 653 |
|
| 654 |
|
| 655 |
def test_coerce_cycle_point_time_zone(): |
| 656 |
"""Test coerce_cycle_point_time_zone.""" |
| 657 |
validator = VDR() |
| 658 |
# The good |
| 659 |
for value, result in [ |
| 660 |
('', None), |
| 661 |
('Z', 'Z'), |
| 662 |
('+0000', '+0000'), |
| 663 |
('+0100', '+0100'), |
| 664 |
('+1300', '+1300'), |
| 665 |
('-0630', '-0630')]: |
| 666 |
assert ( |
| 667 |
validator.coerce_cycle_point_time_zone(value, ['whatever']) |
| 668 |
== result |
| 669 |
) |
| 670 |
# The bad |
| 671 |
for value in ['None', 'Big Bang Time', 'Standard Galaxy Time']: |
| 672 |
with pytest.raises(IllegalValueError): |
| 673 |
validator.coerce_cycle_point_time_zone(value, ['whatever']) |
| 674 |
|
| 675 |
|
| 676 |
def test_coerce_interval(): |
| 677 |
"""Test coerce_interval.""" |
| 678 |
validator = VDR() |
| 679 |
# The good |
| 680 |
for value, result in [ |
| 681 |
('', None), |
| 682 |
('P3D', DurationFloat(259200)), |
| 683 |
('PT10M10S', DurationFloat(610))]: |
| 684 |
assert validator.coerce_interval(value, ['whatever']) == result |
| 685 |
# The bad |
| 686 |
for value in ['None', '5 days', '20', '-12']: |
| 687 |
with pytest.raises(IllegalValueError): |
| 688 |
validator.coerce_interval(value, ['whatever']) |
| 689 |
|
| 690 |
|
| 691 |
@pytest.mark.parametrize( |
| 692 |
'value, expected', |
| 693 |
[ |
| 694 |
('', []), |
| 695 |
('P3D', [DurationFloat(259200)]), |
| 696 |
('P3D, PT10M10S', [DurationFloat(259200), DurationFloat(610)]), |
| 697 |
('25*PT30M,10*PT1H', |
| 698 |
[DurationFloat(1800)] * 25 + [DurationFloat(3600)] * 10) |
| 699 |
] |
| 700 |
) |
| 701 |
def test_coerce_interval_list(value: str, expected: List[DurationFloat]): |
| 702 |
"""Test coerce_interval_list.""" |
| 703 |
assert VDR.coerce_interval_list(value, ['whatever']) == approx(expected) |
| 704 |
|
| 705 |
|
| 706 |
@pytest.mark.parametrize( |
| 707 |
'value', |
| 708 |
['None', '5 days', '20', 'PT10S, -12'] |
| 709 |
) |
| 710 |
def test_coerce_interval_list__bad(value: str): |
| 711 |
with pytest.raises(IllegalValueError): |
| 712 |
VDR.coerce_interval_list(value, ['whatever']) |
| 713 |
|
| 714 |
|
| 715 |
def test_coerce_parameter_list(): |
| 716 |
"""Test coerce_parameter_list.""" |
| 717 |
validator = VDR() |
| 718 |
# The good |
| 719 |
for value, result in [ |
| 720 |
('', []), |
| 721 |
('planet', ['planet']), |
| 722 |
('planet, star, galaxy', ['planet', 'star', 'galaxy']), |
| 723 |
('1..5, 21..25', [1, 2, 3, 4, 5, 21, 22, 23, 24, 25]), |
| 724 |
('-15, -10, -5, -1..1', [-15, -10, -5, -1, 0, 1])]: |
| 725 |
assert validator.coerce_parameter_list(value, ['whatever']) == result |
| 726 |
# The bad |
| 727 |
for value in ['foo/bar', 'p1, 1..10', '2..3, 4, p', 'x:,']: |
| 728 |
with pytest.raises(IllegalValueError): |
| 729 |
validator.coerce_parameter_list(value, ['whatever']) |
| 730 |
|
| 731 |
|
| 732 |
def test_coerce_xtrigger(): |
| 733 |
"""Test coerce_xtrigger.""" |
| 734 |
validator = VDR() |
| 735 |
# The good |
| 736 |
for value, result in [ |
| 737 |
('foo(x="bar")', 'foo(x=bar)'), |
| 738 |
('foo(x, y, z="zebra")', 'foo(x, y, z=zebra)')]: |
| 739 |
assert ( |
| 740 |
validator.coerce_xtrigger(value, ['whatever']).get_signature() |
| 741 |
== result |
| 742 |
) |
| 743 |
# The bad |
| 744 |
for value in [ |
| 745 |
'', 'foo(', 'foo)', 'foo,bar']: |
| 746 |
with pytest.raises(IllegalValueError): |
| 747 |
validator.coerce_xtrigger(value, ['whatever']) |
| 748 |
|
| 749 |
|
| 750 |
def test_type_help_examples(): |
| 751 |
types = { |
| 752 |
**ParsecValidator.V_TYPE_HELP, |
| 753 |
**VDR.V_TYPE_HELP |
| 754 |
} |
| 755 |
|
| 756 |
validator = VDR() |
| 757 |
|
| 758 |
for vdr, info in types.items(): |
| 759 |
coercer = validator.coercers[vdr] |
| 760 |
if len(info) > 2: |
| 761 |
for example in info[2]: |
| 762 |
try: |
| 763 |
coercer(example, [None]) |
| 764 |
except Exception: |
| 765 |
raise Exception( |
| 766 |
f'Example "{example}" failed for type "{vdr}"' |
| 767 |
) |
| 768 |
|
| 769 |
|
| 770 |
@pytest.mark.parametrize( |
| 771 |
'value, expected', |
| 772 |
[ |
| 773 |
param( |
| 774 |
""" |
| 775 |
a="don't have a cow" |
| 776 |
a=${a#*have} |
| 777 |
echo "$a" # let's see what happens |
| 778 |
""", |
| 779 |
"a=\"don't have a cow\"\na=${a#*have}\necho \"$a\"" |
| 780 |
" # let's see what happens", |
| 781 |
id="multiline", |
| 782 |
), |
| 783 |
param('"sleep 30 # ja!" ', 'sleep 30 # ja!', id="quoted"), |
| 784 |
], |
| 785 |
) |
| 786 |
def test_broadcast_coerce_str(value: str, expected: str): |
| 787 |
assert BroadcastConfigValidator.coerce_str(value, ['whatever']) == expected |