File: test_model.py

package info (click to toggle)
python-boto3 1.26.27%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 7,880 kB
  • sloc: python: 12,629; makefile: 128
file content (448 lines) | stat: -rw-r--r-- 14,421 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
# Copyright 2014 Amazon.com, Inc. or its affiliates. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the 'License'). You
# may not use this file except in compliance with the License. A copy of
# the License is located at
#
# https://aws.amazon.com/apache2.0/
#
# or in the 'license' file accompanying this file. This file is
# distributed on an 'AS IS' BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
# ANY KIND, either express or implied. See the License for the specific
# language governing permissions and limitations under the License.

from botocore.model import DenormalizedStructureBuilder

from boto3.resources.model import Action, Collection, ResourceModel, Waiter
from tests import BaseTestCase


class TestModels(BaseTestCase):
    def test_resource_name(self):
        model = ResourceModel('test', {}, {})

        assert model.name == 'test'

    def test_resource_shape(self):
        model = ResourceModel('test', {'shape': 'Frob'}, {})

        assert model.shape == 'Frob'

    def test_resource_identifiers(self):
        model = ResourceModel(
            'test',
            {
                'identifiers': [
                    {'name': 'one'},
                    {'name': 'two', 'memberName': 'three'},
                ]
            },
            {},
        )

        assert model.identifiers[0].name == 'one'
        assert model.identifiers[1].name == 'two'
        assert model.identifiers[1].member_name == 'three'

    def test_resource_action_raw(self):
        model = ResourceModel(
            'test',
            {
                'actions': {
                    'GetFrobs': {
                        'request': {
                            'operation': 'GetFrobsOperation',
                            'params': [
                                {
                                    'target': 'FrobId',
                                    'source': 'identifier',
                                    'name': 'Id',
                                }
                            ],
                        },
                        'path': 'Container.Frobs[]',
                    }
                }
            },
            {},
        )

        assert isinstance(model.actions, list)
        assert len(model.actions) == 1

        action = model.actions[0]
        assert isinstance(action, Action)
        assert action.request.operation == 'GetFrobsOperation'
        assert isinstance(action.request.params, list)
        assert len(action.request.params) == 1
        assert action.request.params[0].target == 'FrobId'
        assert action.request.params[0].source == 'identifier'
        assert action.request.params[0].name == 'Id'
        assert action.path == 'Container.Frobs[]'

    def test_resource_action_response_resource(self):
        model = ResourceModel(
            'test',
            {
                'actions': {
                    'GetFrobs': {
                        'resource': {
                            'type': 'Frob',
                            'path': 'Container.Frobs[]',
                        }
                    }
                }
            },
            {'Frob': {}},
        )

        action = model.actions[0]
        assert action.resource.type == 'Frob'
        assert action.resource.path == 'Container.Frobs[]'
        assert isinstance(action.resource.model, ResourceModel)
        assert action.resource.model.name == 'Frob'

    def test_resource_load_action(self):
        model = ResourceModel(
            'test',
            {'load': {'request': {'operation': 'GetFrobInfo'}, 'path': '$'}},
            {},
        )

        assert isinstance(model.load, Action)
        assert model.load.request.operation == 'GetFrobInfo'
        assert model.load.path == '$'

    def test_resource_batch_action(self):
        model = ResourceModel(
            'test',
            {
                'batchActions': {
                    'Delete': {
                        'request': {
                            'operation': 'DeleteObjects',
                            'params': [
                                {
                                    'target': 'Bucket',
                                    'sourceType': 'identifier',
                                    'source': 'BucketName',
                                }
                            ],
                        }
                    }
                }
            },
            {},
        )

        assert isinstance(model.batch_actions, list)

        action = model.batch_actions[0]
        assert isinstance(action, Action)
        assert action.request.operation == 'DeleteObjects'
        assert action.request.params[0].target == 'Bucket'

    def test_sub_resources(self):
        model = ResourceModel(
            'test',
            {
                'has': {
                    'RedFrob': {
                        'resource': {
                            'type': 'Frob',
                            'identifiers': [
                                {'target': 'Id', 'source': 'input'}
                            ],
                        }
                    },
                    'GreenFrob': {
                        'resource': {
                            'type': 'Frob',
                            'identifiers': [
                                {'target': 'Id', 'source': 'input'}
                            ],
                        }
                    },
                }
            },
            {'Frob': {}},
        )

        assert isinstance(model.subresources, list)
        assert len(model.subresources) == 2

        action = model.subresources[0]
        resource = action.resource

        assert action.name in ['RedFrob', 'GreenFrob']
        assert resource.identifiers[0].target == 'Id'
        assert resource.identifiers[0].source == 'input'
        assert resource.type == 'Frob'

    def test_resource_references(self):
        model_def = {
            'has': {
                'Frob': {
                    'resource': {
                        'type': 'Frob',
                        'identifiers': [
                            {
                                'target': 'Id',
                                'source': 'data',
                                'path': 'FrobId',
                            }
                        ],
                    }
                }
            }
        }
        resource_defs = {'Frob': {}}
        model = ResourceModel('test', model_def, resource_defs)

        assert isinstance(model.references, list)
        assert len(model.references) == 1

        ref = model.references[0]
        assert ref.name == 'frob'
        assert ref.resource.type == 'Frob'
        assert ref.resource.identifiers[0].target == 'Id'
        assert ref.resource.identifiers[0].source == 'data'
        assert ref.resource.identifiers[0].path == 'FrobId'

    def test_resource_collections(self):
        model = ResourceModel(
            'test',
            {
                'hasMany': {
                    'Frobs': {
                        'request': {'operation': 'GetFrobList'},
                        'resource': {'type': 'Frob', 'path': 'FrobList[]'},
                    }
                }
            },
            {'Frob': {}},
        )

        assert isinstance(model.collections, list)
        assert len(model.collections) == 1
        assert isinstance(model.collections[0], Collection)
        assert model.collections[0].request.operation == 'GetFrobList'
        assert model.collections[0].resource.type == 'Frob'
        assert model.collections[0].resource.model.name == 'Frob'
        assert model.collections[0].resource.path == 'FrobList[]'

    def test_waiter(self):
        model = ResourceModel(
            'test',
            {
                'waiters': {
                    'Exists': {
                        'waiterName': 'ObjectExists',
                        'params': [
                            {
                                'target': 'Bucket',
                                'sourceType': 'identifier',
                                'source': 'BucketName',
                            }
                        ],
                    }
                }
            },
            {},
        )

        assert isinstance(model.waiters, list)

        waiter = model.waiters[0]
        assert isinstance(waiter, Waiter)
        assert waiter.name == 'wait_until_exists'
        assert waiter.waiter_name == 'ObjectExists'
        assert waiter.params[0].target == 'Bucket'


class TestRenaming(BaseTestCase):
    def test_multiple(self):
        # This tests a bunch of different renames working together
        model = ResourceModel(
            'test',
            {
                'identifiers': [{'name': 'Foo'}],
                'actions': {'Foo': {}},
                'has': {
                    'Foo': {
                        'resource': {
                            'type': 'Frob',
                            'identifiers': [
                                {
                                    'target': 'Id',
                                    'source': 'data',
                                    'path': 'FrobId',
                                }
                            ],
                        }
                    }
                },
                'hasMany': {'Foo': {}},
                'waiters': {'Foo': {}},
            },
            {'Frob': {}},
        )

        shape = (
            DenormalizedStructureBuilder()
            .with_members(
                {
                    'Foo': {
                        'type': 'string',
                    },
                    'Bar': {'type': 'string'},
                }
            )
            .build_model()
        )

        model.load_rename_map(shape)

        assert model.identifiers[0].name == 'foo'
        assert model.actions[0].name == 'foo_action'
        assert model.references[0].name == 'foo_reference'
        assert model.collections[0].name == 'foo_collection'
        assert model.waiters[0].name == 'wait_until_foo'

        # If an identifier and an attribute share the same name, then
        # the attribute is essentially hidden.
        assert 'foo_attribute' not in model.get_attributes(shape)

        # Other attributes need to be there, though
        assert 'bar' in model.get_attributes(shape)

    # The rest of the tests below ensure the correct order of precedence
    # for the various categories of attributes/properties/methods on the
    # resource model.
    def test_meta_beats_identifier(self):
        model = ResourceModel('test', {'identifiers': [{'name': 'Meta'}]}, {})

        model.load_rename_map()

        assert model.identifiers[0].name == 'meta_identifier'

    def test_load_beats_identifier(self):
        model = ResourceModel(
            'test',
            {
                'identifiers': [{'name': 'Load'}],
                'load': {'request': {'operation': 'GetFrobs'}},
            },
            {},
        )

        model.load_rename_map()

        assert model.load
        assert model.identifiers[0].name == 'load_identifier'

    def test_identifier_beats_action(self):
        model = ResourceModel(
            'test',
            {
                'identifiers': [{'name': 'foo'}],
                'actions': {'Foo': {'request': {'operation': 'GetFoo'}}},
            },
            {},
        )

        model.load_rename_map()

        assert model.identifiers[0].name == 'foo'
        assert model.actions[0].name == 'foo_action'

    def test_action_beats_reference(self):
        model = ResourceModel(
            'test',
            {
                'actions': {'Foo': {'request': {'operation': 'GetFoo'}}},
                'has': {
                    'Foo': {
                        'resource': {
                            'type': 'Frob',
                            'identifiers': [
                                {
                                    'target': 'Id',
                                    'source': 'data',
                                    'path': 'FrobId',
                                }
                            ],
                        }
                    }
                },
            },
            {'Frob': {}},
        )

        model.load_rename_map()

        assert model.actions[0].name == 'foo'
        assert model.references[0].name == 'foo_reference'

    def test_reference_beats_collection(self):
        model = ResourceModel(
            'test',
            {
                'has': {
                    'Foo': {
                        'resource': {
                            'type': 'Frob',
                            'identifiers': [
                                {
                                    'target': 'Id',
                                    'source': 'data',
                                    'path': 'FrobId',
                                }
                            ],
                        }
                    }
                },
                'hasMany': {'Foo': {'resource': {'type': 'Frob'}}},
            },
            {'Frob': {}},
        )

        model.load_rename_map()

        assert model.references[0].name == 'foo'
        assert model.collections[0].name == 'foo_collection'

    def test_collection_beats_waiter(self):
        model = ResourceModel(
            'test',
            {
                'hasMany': {'WaitUntilFoo': {'resource': {'type': 'Frob'}}},
                'waiters': {'Foo': {}},
            },
            {'Frob': {}},
        )

        model.load_rename_map()

        assert model.collections[0].name == 'wait_until_foo'
        assert model.waiters[0].name == 'wait_until_foo_waiter'

    def test_waiter_beats_attribute(self):
        model = ResourceModel('test', {'waiters': {'Foo': {}}}, {'Frob': {}})

        shape = (
            DenormalizedStructureBuilder()
            .with_members(
                {
                    'WaitUntilFoo': {
                        'type': 'string',
                    }
                }
            )
            .build_model()
        )

        model.load_rename_map(shape)

        assert model.waiters[0].name == 'wait_until_foo'
        assert 'wait_until_foo_attribute' in model.get_attributes(shape)