| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
import json |
| 4 |
from unittest import mock |
| 5 |
|
| 6 |
import pytest |
| 7 |
|
| 8 |
from cfgv import apply_defaults |
| 9 |
from cfgv import Array |
| 10 |
from cfgv import check_and |
| 11 |
from cfgv import check_any |
| 12 |
from cfgv import check_array |
| 13 |
from cfgv import check_bool |
| 14 |
from cfgv import check_int |
| 15 |
from cfgv import check_one_of |
| 16 |
from cfgv import check_regex |
| 17 |
from cfgv import check_string |
| 18 |
from cfgv import check_type |
| 19 |
from cfgv import Conditional |
| 20 |
from cfgv import ConditionalOptional |
| 21 |
from cfgv import ConditionalRecurse |
| 22 |
from cfgv import In |
| 23 |
from cfgv import KeyValueMap |
| 24 |
from cfgv import load_from_filename |
| 25 |
from cfgv import Map |
| 26 |
from cfgv import MISSING |
| 27 |
from cfgv import NoAdditionalKeys |
| 28 |
from cfgv import Not |
| 29 |
from cfgv import NotIn |
| 30 |
from cfgv import Optional |
| 31 |
from cfgv import OptionalNoDefault |
| 32 |
from cfgv import OptionalRecurse |
| 33 |
from cfgv import remove_defaults |
| 34 |
from cfgv import Required |
| 35 |
from cfgv import RequiredRecurse |
| 36 |
from cfgv import validate |
| 37 |
from cfgv import ValidationError |
| 38 |
from cfgv import WarnAdditionalKeys |
| 39 |
|
| 40 |
|
| 41 |
def _assert_exception_trace(e, trace): |
| 42 |
parts = [] |
| 43 |
while e.ctx is not None: |
| 44 |
parts.append(e.ctx) |
| 45 |
e = e.error_msg |
| 46 |
parts.append(e.error_msg) |
| 47 |
assert tuple(parts) == trace |
| 48 |
|
| 49 |
|
| 50 |
def test_ValidationError_simple_str(): |
| 51 |
assert str(ValidationError('error msg')) == ( |
| 52 |
'\n' |
| 53 |
'=====> error msg' |
| 54 |
) |
| 55 |
|
| 56 |
|
| 57 |
def test_ValidationError_nested(): |
| 58 |
error = ValidationError( |
| 59 |
ValidationError( |
| 60 |
ValidationError('error msg'), |
| 61 |
ctx='At line 1', |
| 62 |
), |
| 63 |
ctx='In file foo', |
| 64 |
) |
| 65 |
assert str(error) == ( |
| 66 |
'\n' |
| 67 |
'==> In file foo\n' |
| 68 |
'==> At line 1\n' |
| 69 |
'=====> error msg' |
| 70 |
) |
| 71 |
|
| 72 |
|
| 73 |
def test_check_one_of(): |
| 74 |
with pytest.raises(ValidationError) as excinfo: |
| 75 |
check_one_of((1, 2))(3) |
| 76 |
assert excinfo.value.error_msg == 'Expected one of 1, 2 but got: 3' |
| 77 |
|
| 78 |
|
| 79 |
def test_check_one_of_ok(): |
| 80 |
check_one_of((1, 2))(2) |
| 81 |
|
| 82 |
|
| 83 |
def test_check_regex(): |
| 84 |
with pytest.raises(ValidationError) as excinfo: |
| 85 |
check_regex('(') |
| 86 |
assert excinfo.value.error_msg == "'(' is not a valid python regex" |
| 87 |
|
| 88 |
|
| 89 |
def test_check_regex_ok(): |
| 90 |
check_regex('^$') |
| 91 |
|
| 92 |
|
| 93 |
def test_check_array_failed_inner_check(): |
| 94 |
check = check_array(check_bool) |
| 95 |
with pytest.raises(ValidationError) as excinfo: |
| 96 |
check([True, False, 5]) |
| 97 |
_assert_exception_trace( |
| 98 |
excinfo.value, ('At index 2', 'Expected bool got int'), |
| 99 |
) |
| 100 |
|
| 101 |
|
| 102 |
def test_check_array_ok(): |
| 103 |
check_array(check_bool)([True, False]) |
| 104 |
|
| 105 |
|
| 106 |
def test_check_and(): |
| 107 |
check = check_and(check_type(str), check_regex) |
| 108 |
with pytest.raises(ValidationError) as excinfo: |
| 109 |
check(True) |
| 110 |
assert excinfo.value.error_msg == 'Expected str got bool' |
| 111 |
with pytest.raises(ValidationError) as excinfo: |
| 112 |
check('(') |
| 113 |
assert excinfo.value.error_msg == "'(' is not a valid python regex" |
| 114 |
|
| 115 |
|
| 116 |
def test_check_and_ok(): |
| 117 |
check = check_and(check_type(str), check_regex) |
| 118 |
check('^$') |
| 119 |
|
| 120 |
|
| 121 |
@pytest.mark.parametrize( |
| 122 |
('val', 'expected'), |
| 123 |
(('bar', True), ('foo', False), (MISSING, False)), |
| 124 |
) |
| 125 |
def test_not(val, expected): |
| 126 |
compared = Not('foo') |
| 127 |
assert (val == compared) is expected |
| 128 |
assert (compared == val) is expected |
| 129 |
|
| 130 |
|
| 131 |
@pytest.mark.parametrize( |
| 132 |
('values', 'expected'), |
| 133 |
(('bar', True), ('foo', False), (MISSING, False)), |
| 134 |
) |
| 135 |
def test_not_in(values, expected): |
| 136 |
compared = NotIn('baz', 'foo') |
| 137 |
assert (values == compared) is expected |
| 138 |
assert (compared == values) is expected |
| 139 |
|
| 140 |
|
| 141 |
@pytest.mark.parametrize( |
| 142 |
('values', 'expected'), |
| 143 |
(('bar', False), ('foo', True), ('baz', True), (MISSING, False)), |
| 144 |
) |
| 145 |
def test_in(values, expected): |
| 146 |
compared = In('baz', 'foo') |
| 147 |
assert (values == compared) is expected |
| 148 |
assert (compared == values) is expected |
| 149 |
|
| 150 |
|
| 151 |
trivial_array_schema = Array(Map('foo', 'id')) |
| 152 |
trivial_array_schema_nonempty = Array(Map('foo', 'id'), allow_empty=False) |
| 153 |
|
| 154 |
|
| 155 |
def test_validate_top_level_array_not_an_array(): |
| 156 |
with pytest.raises(ValidationError) as excinfo: |
| 157 |
validate({}, trivial_array_schema) |
| 158 |
assert excinfo.value.error_msg == "Expected array but got 'dict'" |
| 159 |
|
| 160 |
|
| 161 |
def test_validate_top_level_array_no_objects(): |
| 162 |
with pytest.raises(ValidationError) as excinfo: |
| 163 |
validate([], trivial_array_schema_nonempty) |
| 164 |
assert excinfo.value.error_msg == "Expected at least 1 'foo'" |
| 165 |
|
| 166 |
|
| 167 |
def test_trivial_array_schema_ok_empty(): |
| 168 |
validate([], trivial_array_schema) |
| 169 |
|
| 170 |
|
| 171 |
@pytest.mark.parametrize('v', (({},), [{}])) |
| 172 |
def test_ok_both_types(v): |
| 173 |
validate(v, trivial_array_schema) |
| 174 |
|
| 175 |
|
| 176 |
map_required = Map('foo', 'key', Required('key', check_bool)) |
| 177 |
map_optional = Map('foo', 'key', Optional('key', check_bool, False)) |
| 178 |
map_no_default = Map('foo', 'key', OptionalNoDefault('key', check_bool)) |
| 179 |
map_no_id_key = Map('foo', None, Required('key', check_bool)) |
| 180 |
|
| 181 |
|
| 182 |
def test_map_wrong_type(): |
| 183 |
with pytest.raises(ValidationError) as excinfo: |
| 184 |
validate([], map_required) |
| 185 |
assert excinfo.value.error_msg == 'Expected a foo map but got a list' |
| 186 |
|
| 187 |
|
| 188 |
def test_required_missing_key(): |
| 189 |
with pytest.raises(ValidationError) as excinfo: |
| 190 |
validate({}, map_required) |
| 191 |
expected = ('At foo(key=MISSING)', 'Missing required key: key') |
| 192 |
_assert_exception_trace(excinfo.value, expected) |
| 193 |
|
| 194 |
|
| 195 |
@pytest.mark.parametrize( |
| 196 |
'schema', (map_required, map_optional, map_no_default), |
| 197 |
) |
| 198 |
def test_map_value_wrong_type(schema): |
| 199 |
with pytest.raises(ValidationError) as excinfo: |
| 200 |
validate({'key': 5}, schema) |
| 201 |
expected = ('At foo(key=5)', 'At key: key', 'Expected bool got int') |
| 202 |
_assert_exception_trace(excinfo.value, expected) |
| 203 |
|
| 204 |
|
| 205 |
@pytest.mark.parametrize( |
| 206 |
'schema', (map_required, map_optional, map_no_default), |
| 207 |
) |
| 208 |
def test_map_value_correct_type(schema): |
| 209 |
validate({'key': True}, schema) |
| 210 |
|
| 211 |
|
| 212 |
@pytest.mark.parametrize('schema', (map_optional, map_no_default)) |
| 213 |
def test_optional_key_missing(schema): |
| 214 |
validate({}, schema) |
| 215 |
|
| 216 |
|
| 217 |
def test_error_message_no_id_key(): |
| 218 |
with pytest.raises(ValidationError) as excinfo: |
| 219 |
validate({'key': 5}, map_no_id_key) |
| 220 |
expected = ('At foo()', 'At key: key', 'Expected bool got int') |
| 221 |
_assert_exception_trace(excinfo.value, expected) |
| 222 |
|
| 223 |
|
| 224 |
map_conditional = Map( |
| 225 |
'foo', 'key', |
| 226 |
Conditional( |
| 227 |
'key2', check_bool, condition_key='key', condition_value=True, |
| 228 |
), |
| 229 |
) |
| 230 |
map_conditional_not = Map( |
| 231 |
'foo', 'key', |
| 232 |
Conditional( |
| 233 |
'key2', check_bool, condition_key='key', condition_value=Not(False), |
| 234 |
), |
| 235 |
) |
| 236 |
map_conditional_absent = Map( |
| 237 |
'foo', 'key', |
| 238 |
Conditional( |
| 239 |
'key2', check_bool, |
| 240 |
condition_key='key', condition_value=True, ensure_absent=True, |
| 241 |
), |
| 242 |
) |
| 243 |
map_conditional_absent_not = Map( |
| 244 |
'foo', 'key', |
| 245 |
Conditional( |
| 246 |
'key2', check_bool, |
| 247 |
condition_key='key', condition_value=Not(True), ensure_absent=True, |
| 248 |
), |
| 249 |
) |
| 250 |
map_conditional_absent_not_in = Map( |
| 251 |
'foo', 'key', |
| 252 |
Conditional( |
| 253 |
'key2', check_bool, |
| 254 |
condition_key='key', condition_value=NotIn(1, 2), ensure_absent=True, |
| 255 |
), |
| 256 |
) |
| 257 |
map_conditional_absent_in = Map( |
| 258 |
'foo', 'key', |
| 259 |
Conditional( |
| 260 |
'key2', check_bool, |
| 261 |
condition_key='key', condition_value=In(1, 2), ensure_absent=True, |
| 262 |
), |
| 263 |
) |
| 264 |
|
| 265 |
|
| 266 |
@pytest.mark.parametrize('schema', (map_conditional, map_conditional_not)) |
| 267 |
@pytest.mark.parametrize( |
| 268 |
'v', |
| 269 |
( |
| 270 |
# Conditional check passes, key2 is checked and passes |
| 271 |
{'key': True, 'key2': True}, |
| 272 |
# Conditional check fails, key2 is not checked |
| 273 |
{'key': False, 'key2': 'ohai'}, |
| 274 |
), |
| 275 |
) |
| 276 |
def test_ok_conditional_schemas(v, schema): |
| 277 |
validate(v, schema) |
| 278 |
|
| 279 |
|
| 280 |
@pytest.mark.parametrize('schema', (map_conditional, map_conditional_not)) |
| 281 |
def test_not_ok_conditional_schemas(schema): |
| 282 |
with pytest.raises(ValidationError) as excinfo: |
| 283 |
validate({'key': True, 'key2': 5}, schema) |
| 284 |
expected = ('At foo(key=True)', 'At key: key2', 'Expected bool got int') |
| 285 |
_assert_exception_trace(excinfo.value, expected) |
| 286 |
|
| 287 |
|
| 288 |
def test_ensure_absent_conditional(): |
| 289 |
with pytest.raises(ValidationError) as excinfo: |
| 290 |
validate({'key': False, 'key2': True}, map_conditional_absent) |
| 291 |
expected = ( |
| 292 |
'At foo(key=False)', |
| 293 |
'Expected key2 to be absent when key is not True, ' |
| 294 |
'found key2: True', |
| 295 |
) |
| 296 |
_assert_exception_trace(excinfo.value, expected) |
| 297 |
|
| 298 |
|
| 299 |
def test_ensure_absent_conditional_not(): |
| 300 |
with pytest.raises(ValidationError) as excinfo: |
| 301 |
validate({'key': True, 'key2': True}, map_conditional_absent_not) |
| 302 |
expected = ( |
| 303 |
'At foo(key=True)', |
| 304 |
'Expected key2 to be absent when key is True, ' |
| 305 |
'found key2: True', |
| 306 |
) |
| 307 |
_assert_exception_trace(excinfo.value, expected) |
| 308 |
|
| 309 |
|
| 310 |
def test_ensure_absent_conditional_not_in(): |
| 311 |
with pytest.raises(ValidationError) as excinfo: |
| 312 |
validate({'key': 1, 'key2': True}, map_conditional_absent_not_in) |
| 313 |
expected = ( |
| 314 |
'At foo(key=1)', |
| 315 |
'Expected key2 to be absent when key is any of (1, 2), ' |
| 316 |
'found key2: True', |
| 317 |
) |
| 318 |
_assert_exception_trace(excinfo.value, expected) |
| 319 |
|
| 320 |
|
| 321 |
def test_ensure_absent_conditional_in(): |
| 322 |
with pytest.raises(ValidationError) as excinfo: |
| 323 |
validate({'key': 3, 'key2': True}, map_conditional_absent_in) |
| 324 |
expected = ( |
| 325 |
'At foo(key=3)', |
| 326 |
'Expected key2 to be absent when key is not any of (1, 2), ' |
| 327 |
'found key2: True', |
| 328 |
) |
| 329 |
_assert_exception_trace(excinfo.value, expected) |
| 330 |
|
| 331 |
|
| 332 |
def test_no_error_conditional_absent(): |
| 333 |
validate({}, map_conditional_absent) |
| 334 |
validate({}, map_conditional_absent_not) |
| 335 |
validate({'key2': True}, map_conditional_absent) |
| 336 |
validate({'key2': True}, map_conditional_absent_not) |
| 337 |
|
| 338 |
|
| 339 |
def test_apply_defaults_copies_object(): |
| 340 |
val = {} |
| 341 |
ret = apply_defaults(val, map_optional) |
| 342 |
assert ret is not val |
| 343 |
|
| 344 |
|
| 345 |
def test_apply_defaults_sets_default(): |
| 346 |
ret = apply_defaults({}, map_optional) |
| 347 |
assert ret == {'key': False} |
| 348 |
|
| 349 |
|
| 350 |
def test_apply_defaults_does_not_change_non_default(): |
| 351 |
ret = apply_defaults({'key': True}, map_optional) |
| 352 |
assert ret == {'key': True} |
| 353 |
|
| 354 |
|
| 355 |
def test_apply_defaults_does_nothing_on_non_optional(): |
| 356 |
ret = apply_defaults({}, map_required) |
| 357 |
assert ret == {} |
| 358 |
|
| 359 |
|
| 360 |
def test_apply_defaults_map_in_list(): |
| 361 |
ret = apply_defaults([{}], Array(map_optional)) |
| 362 |
assert ret == [{'key': False}] |
| 363 |
|
| 364 |
|
| 365 |
def test_remove_defaults_copies_object(): |
| 366 |
val = {'key': False} |
| 367 |
ret = remove_defaults(val, map_optional) |
| 368 |
assert ret is not val |
| 369 |
|
| 370 |
|
| 371 |
def test_remove_defaults_removes_defaults(): |
| 372 |
ret = remove_defaults({'key': False}, map_optional) |
| 373 |
assert ret == {} |
| 374 |
|
| 375 |
|
| 376 |
def test_remove_defaults_nothing_to_remove(): |
| 377 |
ret = remove_defaults({}, map_optional) |
| 378 |
assert ret == {} |
| 379 |
|
| 380 |
|
| 381 |
def test_remove_defaults_does_not_change_non_default(): |
| 382 |
ret = remove_defaults({'key': True}, map_optional) |
| 383 |
assert ret == {'key': True} |
| 384 |
|
| 385 |
|
| 386 |
def test_remove_defaults_map_in_list(): |
| 387 |
ret = remove_defaults([{'key': False}], Array(map_optional)) |
| 388 |
assert ret == [{}] |
| 389 |
|
| 390 |
|
| 391 |
def test_remove_defaults_does_nothing_on_non_optional(): |
| 392 |
ret = remove_defaults({'key': True}, map_required) |
| 393 |
assert ret == {'key': True} |
| 394 |
|
| 395 |
|
| 396 |
nested_schema_required = Map( |
| 397 |
'Repository', 'repo', |
| 398 |
Required('repo', check_any), |
| 399 |
RequiredRecurse('hooks', Array(map_required)), |
| 400 |
) |
| 401 |
nested_schema_optional = Map( |
| 402 |
'Repository', 'repo', |
| 403 |
Required('repo', check_any), |
| 404 |
RequiredRecurse('hooks', Array(map_optional)), |
| 405 |
) |
| 406 |
|
| 407 |
|
| 408 |
def test_validate_failure_nested(): |
| 409 |
with pytest.raises(ValidationError) as excinfo: |
| 410 |
validate({'repo': 1, 'hooks': [{}]}, nested_schema_required) |
| 411 |
expected = ( |
| 412 |
'At Repository(repo=1)', |
| 413 |
'At key: hooks', |
| 414 |
'At foo(key=MISSING)', |
| 415 |
'Missing required key: key', |
| 416 |
) |
| 417 |
_assert_exception_trace(excinfo.value, expected) |
| 418 |
|
| 419 |
|
| 420 |
def test_apply_defaults_nested(): |
| 421 |
val = {'repo': 'repo1', 'hooks': [{}]} |
| 422 |
ret = apply_defaults(val, nested_schema_optional) |
| 423 |
assert ret == {'repo': 'repo1', 'hooks': [{'key': False}]} |
| 424 |
|
| 425 |
|
| 426 |
def test_remove_defaults_nested(): |
| 427 |
val = {'repo': 'repo1', 'hooks': [{'key': False}]} |
| 428 |
ret = remove_defaults(val, nested_schema_optional) |
| 429 |
assert ret == {'repo': 'repo1', 'hooks': [{}]} |
| 430 |
|
| 431 |
|
| 432 |
link = Map('Link', 'key', Required('key', check_bool)) |
| 433 |
optional_nested_schema = Map( |
| 434 |
'Config', None, |
| 435 |
OptionalRecurse('links', Array(link), []), |
| 436 |
) |
| 437 |
|
| 438 |
|
| 439 |
def test_validate_failure_optional_recurse(): |
| 440 |
with pytest.raises(ValidationError) as excinfo: |
| 441 |
validate({'links': [{}]}, optional_nested_schema) |
| 442 |
expected = ( |
| 443 |
'At Config()', |
| 444 |
'At key: links', |
| 445 |
'At Link(key=MISSING)', |
| 446 |
'Missing required key: key', |
| 447 |
) |
| 448 |
_assert_exception_trace(excinfo.value, expected) |
| 449 |
|
| 450 |
|
| 451 |
def test_optional_recurse_ok_missing(): |
| 452 |
validate({}, optional_nested_schema) |
| 453 |
|
| 454 |
|
| 455 |
def test_apply_defaults_optional_recurse_missing(): |
| 456 |
ret = apply_defaults({}, optional_nested_schema) |
| 457 |
assert ret == {'links': []} |
| 458 |
|
| 459 |
|
| 460 |
def test_apply_defaults_optional_recurse_already_present(): |
| 461 |
ret = apply_defaults({'links': [{'key': True}]}, optional_nested_schema) |
| 462 |
assert ret == {'links': [{'key': True}]} |
| 463 |
|
| 464 |
|
| 465 |
def test_remove_defaults_optional_recurse_not_present(): |
| 466 |
assert remove_defaults({}, optional_nested_schema) == {} |
| 467 |
|
| 468 |
|
| 469 |
def test_remove_defaults_optional_recurse_present_at_default(): |
| 470 |
assert remove_defaults({'links': []}, optional_nested_schema) == {} |
| 471 |
|
| 472 |
|
| 473 |
def test_remove_defaults_optional_recurse_non_default(): |
| 474 |
ret = remove_defaults({'links': [{'key': True}]}, optional_nested_schema) |
| 475 |
assert ret == {'links': [{'key': True}]} |
| 476 |
|
| 477 |
|
| 478 |
builder_opts = Map('BuilderOpts', None, Optional('noop', check_bool, True)) |
| 479 |
optional_nested_optional_schema = Map( |
| 480 |
'Config', None, |
| 481 |
OptionalRecurse('builder', builder_opts, {}), |
| 482 |
) |
| 483 |
|
| 484 |
|
| 485 |
def test_optional_optional_apply_defaults(): |
| 486 |
ret = apply_defaults({}, optional_nested_optional_schema) |
| 487 |
assert ret == {'builder': {'noop': True}} |
| 488 |
|
| 489 |
|
| 490 |
def test_optional_optional_remove_defaults(): |
| 491 |
val = {'builder': {'noop': True}} |
| 492 |
ret = remove_defaults(val, optional_nested_optional_schema) |
| 493 |
assert ret == {} |
| 494 |
|
| 495 |
|
| 496 |
params1_schema = Map('Params1', None, Required('p1', check_bool)) |
| 497 |
params2_schema = Map('Params2', None, Required('p2', check_bool)) |
| 498 |
conditional_nested_schema = Map( |
| 499 |
'Config', None, |
| 500 |
Required('type', check_any), |
| 501 |
ConditionalRecurse('params', params1_schema, 'type', 'type1'), |
| 502 |
ConditionalRecurse('params', params2_schema, 'type', 'type2'), |
| 503 |
) |
| 504 |
|
| 505 |
|
| 506 |
@pytest.mark.parametrize( |
| 507 |
'val', |
| 508 |
( |
| 509 |
{'type': 'type3'}, # matches no condition |
| 510 |
{'type': 'type1', 'params': {'p1': True}}, |
| 511 |
{'type': 'type2', 'params': {'p2': True}}, |
| 512 |
), |
| 513 |
) |
| 514 |
def test_conditional_recurse_ok(val): |
| 515 |
validate(val, conditional_nested_schema) |
| 516 |
|
| 517 |
|
| 518 |
def test_conditional_recurse_error(): |
| 519 |
with pytest.raises(ValidationError) as excinfo: |
| 520 |
val = {'type': 'type1', 'params': {'p2': True}} |
| 521 |
validate(val, conditional_nested_schema) |
| 522 |
expected = ( |
| 523 |
'At Config()', |
| 524 |
'At key: params', |
| 525 |
'At Params1()', |
| 526 |
'Missing required key: p1', |
| 527 |
) |
| 528 |
_assert_exception_trace(excinfo.value, expected) |
| 529 |
|
| 530 |
|
| 531 |
class Error(Exception): |
| 532 |
pass |
| 533 |
|
| 534 |
|
| 535 |
def test_load_from_filename_file_does_not_exist(): |
| 536 |
with pytest.raises(Error) as excinfo: |
| 537 |
load_from_filename('does_not_exist', map_required, json.loads, Error) |
| 538 |
assert excinfo.value.args[0].error_msg == 'does_not_exist is not a file' |
| 539 |
|
| 540 |
|
| 541 |
def test_load_from_filename_not_a_file(tmpdir): |
| 542 |
with tmpdir.as_cwd(): |
| 543 |
tmpdir.join('f').ensure_dir() |
| 544 |
with pytest.raises(Error) as excinfo: |
| 545 |
load_from_filename('f', map_required, json.loads, Error) |
| 546 |
assert excinfo.value.args[0].error_msg == 'f is not a file' |
| 547 |
|
| 548 |
|
| 549 |
def test_load_from_filename_unicode_error(tmp_path): |
| 550 |
f = tmp_path.joinpath('f') |
| 551 |
f.write_bytes(b'\x98\xae\xfe') |
| 552 |
|
| 553 |
with pytest.raises(Error) as excinfo: |
| 554 |
load_from_filename(f, map_required, json.loads, Error) |
| 555 |
expected = (f'File {f}', mock.ANY) |
| 556 |
_assert_exception_trace(excinfo.value.args[0], expected) |
| 557 |
|
| 558 |
|
| 559 |
def test_load_from_filename_fails_load_strategy(tmpdir): |
| 560 |
f = tmpdir.join('foo.notjson') |
| 561 |
f.write('totes not json') |
| 562 |
with pytest.raises(Error) as excinfo: |
| 563 |
load_from_filename(f.strpath, map_required, json.loads, Error) |
| 564 |
# ANY is json's error message |
| 565 |
expected = (f'File {f.strpath}', mock.ANY) |
| 566 |
_assert_exception_trace(excinfo.value.args[0], expected) |
| 567 |
|
| 568 |
|
| 569 |
def test_load_from_filename_validation_error(tmpdir): |
| 570 |
f = tmpdir.join('foo.json') |
| 571 |
f.write('{}') |
| 572 |
with pytest.raises(Error) as excinfo: |
| 573 |
load_from_filename(f.strpath, map_required, json.loads, Error) |
| 574 |
expected = ( |
| 575 |
f'File {f.strpath}', |
| 576 |
'At foo(key=MISSING)', |
| 577 |
'Missing required key: key', |
| 578 |
) |
| 579 |
_assert_exception_trace(excinfo.value.args[0], expected) |
| 580 |
|
| 581 |
|
| 582 |
def test_load_from_filename_applies_defaults(tmpdir): |
| 583 |
f = tmpdir.join('foo.json') |
| 584 |
f.write('{}') |
| 585 |
ret = load_from_filename(f.strpath, map_optional, json.loads, Error) |
| 586 |
assert ret == {'key': False} |
| 587 |
|
| 588 |
|
| 589 |
def test_load_from_filename_custom_display_no_file(tmp_path): |
| 590 |
with pytest.raises(ValidationError) as excinfo: |
| 591 |
load_from_filename( |
| 592 |
tmp_path.joinpath('cfg.json'), |
| 593 |
map_required, |
| 594 |
json.loads, |
| 595 |
display_filename='cfg.json', |
| 596 |
) |
| 597 |
_assert_exception_trace(excinfo.value.args[0], ('cfg.json is not a file',)) |
| 598 |
|
| 599 |
|
| 600 |
def test_load_from_filename_custom_display_error(tmp_path): |
| 601 |
f = tmp_path.joinpath('cfg.json') |
| 602 |
f.write_text('{}') |
| 603 |
with pytest.raises(ValidationError) as excinfo: |
| 604 |
load_from_filename( |
| 605 |
f, |
| 606 |
map_required, |
| 607 |
json.loads, |
| 608 |
display_filename='cfg.json', |
| 609 |
) |
| 610 |
expected = ( |
| 611 |
'File cfg.json', |
| 612 |
'At foo(key=MISSING)', |
| 613 |
'Missing required key: key', |
| 614 |
) |
| 615 |
_assert_exception_trace(excinfo.value.args[0], expected) |
| 616 |
|
| 617 |
|
| 618 |
conditional_recurse = Map( |
| 619 |
'Map', None, |
| 620 |
|
| 621 |
Required('t', check_bool), |
| 622 |
ConditionalRecurse( |
| 623 |
'v', Map('Inner', 'k', Optional('k', check_bool, True)), |
| 624 |
't', True, |
| 625 |
), |
| 626 |
ConditionalRecurse( |
| 627 |
'v', Map('Inner', 'k', Optional('k', check_bool, False)), |
| 628 |
't', False, |
| 629 |
), |
| 630 |
) |
| 631 |
|
| 632 |
|
| 633 |
@pytest.mark.parametrize('tvalue', (True, False)) |
| 634 |
def test_conditional_recurse_apply_defaults(tvalue): |
| 635 |
val = {'t': tvalue, 'v': {}} |
| 636 |
ret = apply_defaults(val, conditional_recurse) |
| 637 |
assert ret == {'t': tvalue, 'v': {'k': tvalue}} |
| 638 |
|
| 639 |
val = {'t': tvalue, 'v': {'k': not tvalue}} |
| 640 |
ret = apply_defaults(val, conditional_recurse) |
| 641 |
assert ret == {'t': tvalue, 'v': {'k': not tvalue}} |
| 642 |
|
| 643 |
|
| 644 |
@pytest.mark.parametrize('tvalue', (True, False)) |
| 645 |
def test_conditional_recurse_remove_defaults(tvalue): |
| 646 |
val = {'t': tvalue, 'v': {'k': tvalue}} |
| 647 |
ret = remove_defaults(val, conditional_recurse) |
| 648 |
assert ret == {'t': tvalue, 'v': {}} |
| 649 |
|
| 650 |
val = {'t': tvalue, 'v': {'k': not tvalue}} |
| 651 |
ret = remove_defaults(val, conditional_recurse) |
| 652 |
assert ret == {'t': tvalue, 'v': {'k': not tvalue}} |
| 653 |
|
| 654 |
|
| 655 |
conditional_optional = Map( |
| 656 |
'Map', None, |
| 657 |
|
| 658 |
Required('t', check_bool), |
| 659 |
ConditionalOptional('v', check_bool, True, 't', True), |
| 660 |
ConditionalOptional('v', check_bool, False, 't', False), |
| 661 |
) |
| 662 |
|
| 663 |
|
| 664 |
@pytest.mark.parametrize('tvalue', (True, False)) |
| 665 |
def test_conditional_optional_check(tvalue): |
| 666 |
with pytest.raises(ValidationError) as excinfo: |
| 667 |
validate({'t': tvalue, 'v': 2}, conditional_optional) |
| 668 |
expected = ( |
| 669 |
'At Map()', |
| 670 |
'At key: v', |
| 671 |
'Expected bool got int', |
| 672 |
) |
| 673 |
_assert_exception_trace(excinfo.value, expected) |
| 674 |
|
| 675 |
validate({'t': tvalue, 'v': tvalue}, conditional_optional) |
| 676 |
|
| 677 |
|
| 678 |
@pytest.mark.parametrize('tvalue', (True, False)) |
| 679 |
def test_conditional_optional_apply_default(tvalue): |
| 680 |
ret = apply_defaults({'t': tvalue}, conditional_optional) |
| 681 |
assert ret == {'t': tvalue, 'v': tvalue} |
| 682 |
|
| 683 |
|
| 684 |
@pytest.mark.parametrize('tvalue', (True, False)) |
| 685 |
def test_conditional_optional_remove_default(tvalue): |
| 686 |
ret = remove_defaults({'t': tvalue, 'v': tvalue}, conditional_optional) |
| 687 |
assert ret == {'t': tvalue} |
| 688 |
ret = remove_defaults({'t': tvalue, 'v': not tvalue}, conditional_optional) |
| 689 |
assert ret == {'t': tvalue, 'v': not tvalue} |
| 690 |
|
| 691 |
|
| 692 |
no_additional_keys = Map( |
| 693 |
'Map', None, |
| 694 |
Required(True, check_bool), |
| 695 |
NoAdditionalKeys((True,)), |
| 696 |
) |
| 697 |
|
| 698 |
|
| 699 |
def test_no_additional_keys(): |
| 700 |
with pytest.raises(ValidationError) as excinfo: |
| 701 |
validate({True: True, False: False}, no_additional_keys) |
| 702 |
expected = ( |
| 703 |
'At Map()', |
| 704 |
'Additional keys found: False. Only these keys are allowed: True', |
| 705 |
) |
| 706 |
_assert_exception_trace(excinfo.value, expected) |
| 707 |
|
| 708 |
validate({True: True}, no_additional_keys) |
| 709 |
|
| 710 |
|
| 711 |
@pytest.fixture |
| 712 |
def warn_additional_keys(): |
| 713 |
ret = mock.Mock() |
| 714 |
|
| 715 |
def callback(extra, keys, dct): |
| 716 |
return ret.record(extra, keys, dct) |
| 717 |
|
| 718 |
ret.schema = Map( |
| 719 |
'Map', None, |
| 720 |
Required(True, check_bool), |
| 721 |
WarnAdditionalKeys((True,), callback), |
| 722 |
) |
| 723 |
yield ret |
| 724 |
|
| 725 |
|
| 726 |
def test_warn_additional_keys_when_has_extra_keys(warn_additional_keys): |
| 727 |
validate({True: True, False: False}, warn_additional_keys.schema) |
| 728 |
assert warn_additional_keys.record.called |
| 729 |
|
| 730 |
|
| 731 |
def test_warn_additional_keys_when_no_extra_keys(warn_additional_keys): |
| 732 |
validate({True: True}, warn_additional_keys.schema) |
| 733 |
assert not warn_additional_keys.record.called |
| 734 |
|
| 735 |
|
| 736 |
key_value_map_schema = KeyValueMap( |
| 737 |
'Container', |
| 738 |
check_string, |
| 739 |
Map( |
| 740 |
'Object', 'name', |
| 741 |
Required('name', check_string), |
| 742 |
Optional('setting', check_bool, False), |
| 743 |
), |
| 744 |
) |
| 745 |
key_value_map_ints_schema = KeyValueMap( |
| 746 |
'Container', |
| 747 |
check_int, |
| 748 |
Array(Map('Object', 'nane', Required('name', check_string))), |
| 749 |
) |
| 750 |
|
| 751 |
|
| 752 |
def test_key_value_map_schema_ok(): |
| 753 |
validate( |
| 754 |
{'hello': {'name': 'hello'}, 'world': {'name': 'world'}}, |
| 755 |
key_value_map_schema, |
| 756 |
) |
| 757 |
validate( |
| 758 |
{1: [{'name': 'hello'}], 2: [{'name': 'world'}]}, |
| 759 |
key_value_map_ints_schema, |
| 760 |
) |
| 761 |
|
| 762 |
|
| 763 |
def test_key_value_map_apply_defaults(): |
| 764 |
orig = {'hello': {'name': 'hello'}} |
| 765 |
ret = apply_defaults(orig, key_value_map_schema) |
| 766 |
assert orig == {'hello': {'name': 'hello'}} |
| 767 |
assert ret == {'hello': {'name': 'hello', 'setting': False}} |
| 768 |
|
| 769 |
|
| 770 |
def test_key_value_map_remove_defaults(): |
| 771 |
orig = {'hello': {'name': 'hello', 'setting': False}} |
| 772 |
ret = remove_defaults(orig, key_value_map_schema) |
| 773 |
assert orig == {'hello': {'name': 'hello', 'setting': False}} |
| 774 |
assert ret == {'hello': {'name': 'hello'}} |
| 775 |
|
| 776 |
|
| 777 |
def test_key_value_map_not_a_map(): |
| 778 |
with pytest.raises(ValidationError) as excinfo: |
| 779 |
validate([], key_value_map_schema) |
| 780 |
expected = ( |
| 781 |
'Expected a Container map but got a list', |
| 782 |
) |
| 783 |
_assert_exception_trace(excinfo.value, expected) |
| 784 |
|
| 785 |
|
| 786 |
def test_key_value_map_wrong_key_type(): |
| 787 |
with pytest.raises(ValidationError) as excinfo: |
| 788 |
val = {1: {'name': 'hello'}} |
| 789 |
validate(val, key_value_map_schema) |
| 790 |
expected = ( |
| 791 |
'At Container()', |
| 792 |
'For key: 1', |
| 793 |
'Expected string got int', |
| 794 |
) |
| 795 |
_assert_exception_trace(excinfo.value, expected) |
| 796 |
|
| 797 |
|
| 798 |
def test_key_value_map_error_in_child_schema(): |
| 799 |
with pytest.raises(ValidationError) as excinfo: |
| 800 |
val = {'hello': {'name': 1}} |
| 801 |
validate(val, key_value_map_schema) |
| 802 |
expected = ( |
| 803 |
'At Container()', |
| 804 |
'At key: hello', |
| 805 |
'At Object(name=1)', |
| 806 |
'At key: name', |
| 807 |
'Expected string got int', |
| 808 |
) |
| 809 |
_assert_exception_trace(excinfo.value, expected) |