|
| 1 | +""" |
| 2 | +Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | +SPDX-License-Identifier: MIT-0 |
| 4 | +""" |
| 5 | + |
| 6 | +from collections import deque |
| 7 | + |
| 8 | +import pytest |
| 9 | + |
| 10 | +from cfnlint.jsonschema import ValidationError |
| 11 | +from cfnlint.rules.transforms.Configuration import Configuration |
| 12 | + |
| 13 | + |
| 14 | +@pytest.fixture(scope="module") |
| 15 | +def rule(): |
| 16 | + rule = Configuration() |
| 17 | + yield rule |
| 18 | + |
| 19 | + |
| 20 | +@pytest.mark.parametrize( |
| 21 | + "name,instance,expected", |
| 22 | + [ |
| 23 | + ( |
| 24 | + "Empty list is ok", |
| 25 | + [], |
| 26 | + [], |
| 27 | + ), |
| 28 | + ( |
| 29 | + "String is ok", |
| 30 | + "Foo", |
| 31 | + [], |
| 32 | + ), |
| 33 | + ( |
| 34 | + "List is ok", |
| 35 | + ["Foo", "Bar"], |
| 36 | + [], |
| 37 | + ), |
| 38 | + ( |
| 39 | + "Object is ok", |
| 40 | + {"Name": "Foo", "Parameters": {"Bar": "Test"}}, |
| 41 | + [], |
| 42 | + ), |
| 43 | + ( |
| 44 | + "Array of objects is ok", |
| 45 | + [ |
| 46 | + {"Name": "Foo", "Parameters": {"Bar": "Test"}}, |
| 47 | + "Foo", |
| 48 | + ], |
| 49 | + [], |
| 50 | + ), |
| 51 | + ( |
| 52 | + "Missing required Name", |
| 53 | + {"Parameters": {"Bar": "Test"}}, |
| 54 | + [ |
| 55 | + ValidationError( |
| 56 | + "'Name' is a required property", |
| 57 | + validator="required", |
| 58 | + rule=Configuration(), |
| 59 | + path=deque([]), |
| 60 | + schema_path=deque(["required"]), |
| 61 | + ) |
| 62 | + ], |
| 63 | + ), |
| 64 | + ( |
| 65 | + "No additional property names are allowed", |
| 66 | + {"Name": "Foo", "Foo": "Bar", "Parameters": {"Bar": "Test"}}, |
| 67 | + [ |
| 68 | + ValidationError( |
| 69 | + "Additional properties are not allowed ('Foo' was unexpected)", |
| 70 | + validator="additionalProperties", |
| 71 | + rule=Configuration(), |
| 72 | + path=deque(["Foo"]), |
| 73 | + schema_path=deque(["additionalProperties"]), |
| 74 | + ) |
| 75 | + ], |
| 76 | + ), |
| 77 | + ( |
| 78 | + "Null is not ok", |
| 79 | + None, |
| 80 | + [ |
| 81 | + ValidationError( |
| 82 | + "None is not of type 'string', 'array', 'object'", |
| 83 | + validator="type", |
| 84 | + rule=Configuration(), |
| 85 | + path=deque([]), |
| 86 | + schema_path=deque(["type"]), |
| 87 | + ) |
| 88 | + ], |
| 89 | + ), |
| 90 | + ], |
| 91 | +) |
| 92 | +def test_validate(name, instance, expected, rule, validator): |
| 93 | + errs = list(rule.validate(validator, {}, instance, {})) |
| 94 | + |
| 95 | + assert errs == expected, f"Test {name!r} got {errs!r}" |
0 commit comments