Skip to content

Commit fcc3c79

Browse files
authored
chore: more unified schema tests (#2798)
1 parent 1128ff2 commit fcc3c79

File tree

1 file changed

+124
-0
lines changed

1 file changed

+124
-0
lines changed

tests/schema/test_validate_schema.py

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from unittest import TestCase
99
from jsonschema import validate
1010
from jsonschema.exceptions import ValidationError
11+
from jsonschema.validators import Draft4Validator
1112
from parameterized import parameterized
1213

1314
from samtranslator.yaml_helper import yaml_parse
@@ -117,10 +118,133 @@ def test_validate_unified_schema(self, testcase):
117118

118119
@parameterized.expand(
119120
[
121+
(PROJECT_ROOT.joinpath("tests/translator/input/error_api_invalid_auth.yaml"),),
122+
(PROJECT_ROOT.joinpath("tests/translator/input/error_function_invalid_event_type.yaml"),),
120123
(PROJECT_ROOT.joinpath("tests/translator/input/schema_validation_ec2_not_valid.yaml"),),
121124
]
122125
)
123126
def test_validate_unified_schema_error(self, testcase):
124127
obj = yaml_parse(Path(testcase).read_bytes())
125128
with pytest.raises(ValidationError):
126129
validate(obj, schema=UNIFIED_SCHEMA)
130+
131+
@parameterized.expand(
132+
[
133+
[
134+
# Valid (SAM)
135+
{
136+
"Transform": "AWS::Serverless-2016-10-31",
137+
"Resources": {
138+
"Foo": {
139+
"Type": "AWS::Serverless::SimpleTable",
140+
},
141+
},
142+
}
143+
],
144+
[
145+
# Valid (CFN)
146+
{
147+
"Resources": {
148+
"Foo": {
149+
"Type": "AWS::S3::Bucket",
150+
},
151+
},
152+
},
153+
],
154+
],
155+
)
156+
def test_sanity_valid(self, template):
157+
assert UNIFIED_SCHEMA["$schema"] == "http://json-schema.org/draft-04/schema#"
158+
Draft4Validator(UNIFIED_SCHEMA).validate(template)
159+
validate(template, schema=UNIFIED_SCHEMA)
160+
161+
@parameterized.expand(
162+
[
163+
[
164+
# Unknown property (SAM)
165+
{
166+
"Transform": "AWS::Serverless-2016-10-31",
167+
"Resources": {
168+
"Foo": {
169+
"Type": "AWS::Serverless::SimpleTable",
170+
"Properties": {
171+
"UnknownProperty": True,
172+
},
173+
},
174+
},
175+
},
176+
],
177+
[
178+
# Missing property (SAM)
179+
{
180+
"Transform": "AWS::Serverless-2016-10-31",
181+
"Resources": {
182+
"Foo": {
183+
"Type": "AWS::Serverless::SimpleTable",
184+
"Properties": {
185+
"PrimaryKey": {
186+
"Name": "Foo",
187+
},
188+
},
189+
},
190+
},
191+
},
192+
],
193+
[
194+
# Wrong value type (SAM)
195+
{
196+
"Transform": "AWS::Serverless-2016-10-31",
197+
"Resources": {
198+
"Foo": {
199+
"Type": "AWS::Serverless::Function",
200+
"Properties": {
201+
"InlineCode": "foo",
202+
"Handler": "bar",
203+
"Runtime": "node16.x",
204+
"Events": 1337,
205+
},
206+
},
207+
},
208+
},
209+
],
210+
[
211+
# Unknown property (CFN)
212+
{
213+
"Resources": {
214+
"Foo": {
215+
"Type": "AWS::S3::Bucket",
216+
"Properties": {
217+
"UnknownProperty": True,
218+
},
219+
},
220+
},
221+
},
222+
],
223+
[
224+
# Missing property (CFN)
225+
{
226+
"Resources": {
227+
"Foo": {
228+
"Type": "AWS::ResourceGroups::Group",
229+
}
230+
}
231+
}
232+
],
233+
[
234+
# Wrong value type (CFN)
235+
{
236+
"Resources": {
237+
"Foo": {
238+
"Type": "AWS::ResourceGroups::Group",
239+
"Properties": {
240+
"Name": 1337,
241+
},
242+
}
243+
}
244+
}
245+
],
246+
],
247+
)
248+
def test_sanity_invalid(self, template):
249+
with pytest.raises(ValidationError):
250+
validate(template, schema=UNIFIED_SCHEMA)

0 commit comments

Comments
 (0)