From 362b5f71a86eb1b9cf0fdb43699a1781ee1be8ea Mon Sep 17 00:00:00 2001 From: Kevin DeJong Date: Fri, 9 Aug 2024 10:46:44 -0700 Subject: [PATCH] Add better testing --- src/cfnlint/context/_mappings.py | 4 +-- .../module/jsonschema/test_resolvers_cfn.py | 9 +++++- test/unit/rules/functions/test_basefn.py | 30 +++++++++++++++++++ 3 files changed, 40 insertions(+), 3 deletions(-) diff --git a/src/cfnlint/context/_mappings.py b/src/cfnlint/context/_mappings.py index b01fca50bb..25bc71fc7f 100644 --- a/src/cfnlint/context/_mappings.py +++ b/src/cfnlint/context/_mappings.py @@ -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: @@ -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) diff --git a/test/unit/module/jsonschema/test_resolvers_cfn.py b/test/unit/module/jsonschema/test_resolvers_cfn.py index 278d551dd9..ec98e2bcfe 100644 --- a/test/unit/module/jsonschema/test_resolvers_cfn.py +++ b/test/unit/module/jsonschema/test_resolvers_cfn.py @@ -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]), ), @@ -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"}]}, @@ -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"}}, } ) ) diff --git a/test/unit/rules/functions/test_basefn.py b/test/unit/rules/functions/test_basefn.py index 255b46652a..6363f47f06 100644 --- a/test/unit/rules/functions/test_basefn.py +++ b/test/unit/rules/functions/test_basefn.py @@ -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):