Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
aws
GitHub Repository: aws/aws-cli
Path: blob/develop/tests/unit/test_schema.py
1566 views
1
# Copyright 2014 Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
#
3
# Licensed under the Apache License, Version 2.0 (the "License"). You
4
# may not use this file except in compliance with the License. A copy of
5
# the License is located at
6
#
7
# http://aws.amazon.com/apache2.0/
8
#
9
# or in the "license" file accompanying this file. This file is
10
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
11
# ANY KIND, either express or implied. See the License for the specific
12
# language governing permissions and limitations under the License.
13
import pprint
14
15
from botocore.compat import OrderedDict
16
17
from awscli.testutils import unittest
18
from awscli.schema import ParameterRequiredError, SchemaTransformer
19
from awscli.schema import ShapeNameGenerator
20
21
22
MISSING_TYPE = {
23
"type": "object",
24
"properties": {
25
"Foo": {
26
"description": "I am a foo"
27
}
28
}
29
}
30
31
32
class TestSchemaTransformer(unittest.TestCase):
33
34
maxDiff = None
35
36
def test_missing_top_level_type_raises_exception(self):
37
transformer = SchemaTransformer()
38
with self.assertRaises(ParameterRequiredError):
39
transformer.transform({})
40
41
def test_missing_type_raises_exception(self):
42
transformer = SchemaTransformer()
43
44
with self.assertRaises(ParameterRequiredError):
45
transformer.transform({
46
'type': 'object',
47
'properties': {
48
'Foo': {
49
'description': 'foo',
50
}
51
}
52
})
53
54
def assert_schema_transforms_to(self, schema, transforms_to):
55
transformer = SchemaTransformer()
56
actual = transformer.transform(schema)
57
if actual != transforms_to:
58
self.fail("Transform failed.\n\nExpected:\n%s\n\nActual:\n%s\n" % (
59
pprint.pformat(transforms_to), pprint.pformat(actual)))
60
61
def test_transforms_list_of_single_string(self):
62
schema = {
63
'type': 'array',
64
'items': {
65
'type': 'string'
66
}
67
}
68
transforms_to = {
69
'InputShape': {
70
'type': 'list',
71
'member': {'shape': 'StringType1'}
72
},
73
'StringType1': {'type': 'string'}
74
}
75
self.assert_schema_transforms_to(schema, transforms_to)
76
77
def test_transform_list_of_structures(self):
78
schema = {
79
"type": "array",
80
"items": {
81
"type": "object",
82
"properties": {
83
"arg1": {
84
"type": "string",
85
},
86
"arg2": {
87
"type": "integer",
88
}
89
}
90
}
91
}
92
transforms_to = {
93
'InputShape': {
94
'type': 'list',
95
'member': {
96
'shape': 'StructureType1'
97
}
98
},
99
'StructureType1': {
100
'type': 'structure',
101
'members': {
102
'arg1': {
103
'shape': 'StringType1',
104
},
105
'arg2': {
106
'shape': 'IntegerType1',
107
},
108
}
109
},
110
'StringType1': {'type': 'string'},
111
'IntegerType1': {'type': 'integer'},
112
}
113
self.assert_schema_transforms_to(schema, transforms_to)
114
115
def test_transform_required_members_on_structure(self):
116
pass
117
118
def test_transforms_string(self):
119
self.assert_schema_transforms_to(
120
schema={
121
'type': 'string'
122
},
123
transforms_to={
124
'InputShape': {'type': 'string'}
125
}
126
)
127
128
def test_transforms_boolean(self):
129
self.assert_schema_transforms_to(
130
schema={
131
'type': 'boolean'
132
},
133
transforms_to={
134
'InputShape': {'type': 'boolean'}
135
}
136
)
137
138
def test_transforms_integer(self):
139
self.assert_schema_transforms_to(
140
schema={
141
'type': 'integer'
142
},
143
transforms_to={
144
'InputShape': {'type': 'integer'}
145
}
146
)
147
148
def test_transforms_structure(self):
149
self.assert_schema_transforms_to(
150
schema={
151
"type": "object",
152
"properties": OrderedDict([
153
("A", {"type": "string"}),
154
("B", {"type": "string"}),
155
]),
156
},
157
transforms_to={
158
'InputShape': {
159
'type': 'structure',
160
'members': {
161
'A': {'shape': 'StringType1'},
162
'B': {'shape': 'StringType2'},
163
}
164
},
165
'StringType1': {'type': 'string'},
166
'StringType2': {'type': 'string'},
167
}
168
)
169
170
def test_transforms_map(self):
171
self.assert_schema_transforms_to(
172
schema={
173
"type": "map",
174
"key": {"type": "string"},
175
"value": {"type": "string"}
176
},
177
transforms_to={
178
'InputShape': {
179
"type": "map",
180
"key": {"shape": "StringType1"},
181
"value": {"shape": "StringType2"}
182
},
183
'StringType1': {'type': 'string'},
184
'StringType2': {'type': 'string'},
185
}
186
)
187
188
def test_description_on_shape_type(self):
189
self.assert_schema_transforms_to(
190
schema={
191
'type': 'string',
192
'description': 'a description'
193
},
194
transforms_to={
195
'InputShape': {
196
'type': 'string',
197
'documentation': 'a description'
198
}
199
}
200
)
201
202
def test_enum_on_shape_type(self):
203
self.assert_schema_transforms_to(
204
schema={
205
'type': 'string',
206
'enum': ['a', 'b'],
207
},
208
transforms_to={
209
'InputShape': {
210
'type': 'string',
211
'enum': ['a', 'b']
212
}
213
}
214
)
215
216
def test_description_on_shape_ref(self):
217
self.assert_schema_transforms_to(
218
schema={
219
'type': 'object',
220
'description': 'object description',
221
'properties': {
222
'A': {
223
'type': 'string',
224
'description': 'string description',
225
},
226
}
227
},
228
transforms_to={
229
'InputShape': {
230
'type': 'structure',
231
'documentation': 'object description',
232
'members': {
233
'A': {'shape': 'StringType1'},
234
}
235
},
236
'StringType1': {
237
'documentation': 'string description',
238
'type': 'string'
239
}
240
}
241
)
242
243
def test_required_members_on_structure(self):
244
# This case is interesting because we actually
245
# don't support a 'required' key on a member shape ref.
246
# Now, all the required members are added as a key on the
247
# parent structure shape.
248
self.assert_schema_transforms_to(
249
schema={
250
'type': 'object',
251
'properties': {
252
'A': {'type': 'string', 'required': True},
253
}
254
},
255
transforms_to={
256
'InputShape': {
257
'type': 'structure',
258
# This 'required' key is the change here.
259
'required': ['A'],
260
'members': {
261
'A': {'shape': 'StringType1'},
262
}
263
},
264
'StringType1': {'type': 'string'},
265
}
266
)
267
268
def test_nested_structure(self):
269
self.assert_schema_transforms_to(
270
schema={
271
'type': 'object',
272
'properties': {
273
'A': {
274
'type': 'object',
275
'properties': {
276
'B': {
277
'type': 'object',
278
'properties': {
279
'C': {'type': 'string'}
280
}
281
}
282
}
283
},
284
}
285
},
286
transforms_to={
287
'InputShape': {
288
'type': 'structure',
289
'members': {
290
'A': {'shape': 'StructureType1'},
291
}
292
},
293
'StructureType1': {
294
'type': 'structure',
295
'members': {
296
'B': {'shape': 'StructureType2'}
297
}
298
},
299
'StructureType2': {
300
'type': 'structure',
301
'members': {
302
'C': {'shape': 'StringType1'}
303
}
304
},
305
'StringType1': {
306
'type': 'string',
307
}
308
}
309
)
310
311
312
class TestShapeNameGenerator(unittest.TestCase):
313
def test_generate_name_types(self):
314
namer = ShapeNameGenerator()
315
self.assertEqual(namer.new_shape_name('string'), 'StringType1')
316
self.assertEqual(namer.new_shape_name('list'), 'ListType1')
317
self.assertEqual(namer.new_shape_name('structure'), 'StructureType1')
318
319
def test_generate_type_multiple_times(self):
320
namer = ShapeNameGenerator()
321
self.assertEqual(namer.new_shape_name('string'), 'StringType1')
322
self.assertEqual(namer.new_shape_name('string'), 'StringType2')
323
324