Skip to content

Commit

Permalink
Add better testing
Browse files Browse the repository at this point in the history
  • Loading branch information
kddejong committed Aug 9, 2024
1 parent 71958f5 commit 362b5f7
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 3 deletions.
4 changes: 2 additions & 2 deletions src/cfnlint/context/_mappings.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def create_from_dict(cls, instance: Any) -> Mappings:
for k, v in instance.items():
if k == "Fn::Transform":
is_transform = True
else:
elif isinstance(k, str):
result[k] = Map.create_from_dict(v)
return cls(result, is_transform)
except (ValueError, AttributeError) as e:
Expand Down Expand Up @@ -95,6 +95,6 @@ def create_from_dict(cls, instance: Any) -> Map:
for k, v in instance.items():
if k == "Fn::Transform":
is_transform = True
else:
elif isinstance(k, str):
keys[k] = _MappingSecondaryKey.create_from_dict(v)
return cls(keys, is_transform)
9 changes: 8 additions & 1 deletion test/unit/module/jsonschema/test_resolvers_cfn.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,8 @@ def test_invalid_functions(name, instance, response):
ValidationError(
(
"'bar' is not one of ['foo', "
"'transformFirstKey', 'transformSecondKey']"
"'transformFirstKey', 'transformSecondKey', "
"'integers']"
),
path=deque(["Fn::FindInMap", 0]),
),
Expand Down Expand Up @@ -306,6 +307,11 @@ def test_invalid_functions(name, instance, response):
)
],
),
(
"Valid FindInMap with integer types",
{"Fn::FindInMap": ["integers", 1, 2]},
[("Value", deque(["Mappings", "integers", "1", "2"]), None)],
),
(
"Valid FindInMap with a bad second key and default",
{"Fn::FindInMap": ["foo", "first", "third", {"DefaultValue": "default"}]},
Expand Down Expand Up @@ -340,6 +346,7 @@ def test_valid_functions(name, instance, response):
"foo": {"first": {"second": "bar"}},
"transformFirstKey": {"Fn::Transform": {"second": "bar"}},
"transformSecondKey": {"first": {"Fn::Transform": "bar"}},
"integers": {"1": {"2": "Value"}},
}
)
)
Expand Down
30 changes: 30 additions & 0 deletions test/unit/rules/functions/test_basefn.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,36 @@ def rule():
)
],
),
(
"Errors with context error",
{"Fn::Sub": "Bar"},
{"anyOf": [{"enum": ["Foo"]}]},
[
ValidationError(
message=(
"{'Fn::Sub': 'Bar'} is not valid "
"under any of the given schemas "
"when '' is resolved"
),
path=deque(["Fn::Sub"]),
validator="",
schema_path=deque(["anyOf"]),
rule=_ChildRule(),
context=[
ValidationError(
message=(
"{'Fn::Sub': 'Bar'} is not one of "
"['Foo'] when '' is resolved"
),
path=deque([]),
validator="enum",
schema_path=deque([0, "enum"]),
rule=_ChildRule(),
)
],
)
],
),
],
)
def test_resolve(name, instance, schema, expected, validator, rule):
Expand Down

0 comments on commit 362b5f7

Please sign in to comment.