File: test_method.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 (322 lines) | stat: -rw-r--r-- 12,154 bytes parent folder | download
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
# Copyright 2015 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.docs.utils import DocumentedShape
from botocore.hooks import HierarchicalEmitter

from boto3.docs.method import document_model_driven_resource_method
from boto3.resources.model import ResponseResource
from tests.unit.docs import BaseDocsTest


class TestDocumentModelDrivenResourceMethod(BaseDocsTest):
    def setUp(self):
        super().setUp()
        self.event_emitter = HierarchicalEmitter()
        self.service_model = self.client.meta.service_model
        self.operation_model = self.service_model.operation_model(
            'SampleOperation'
        )
        self.service_resource_model = self.resource.meta.resource_model

    def test_default(self):
        document_model_driven_resource_method(
            self.doc_structure,
            'foo',
            self.operation_model,
            event_emitter=self.event_emitter,
            method_description='This describes the foo method.',
            example_prefix='response = myservice.foo',
            resource_action_model=self.service_resource_model.actions[0],
        )
        self.assert_contains_lines_in_order(
            [
                '.. py:method:: foo(**kwargs)',
                '  This describes the foo method.',
                '  **Request Syntax** ',
                '  ::',
                '    response = myservice.foo(',
                "        Foo='string',",
                "        Bar='string'",
                '    )',
                '  :type Foo: string',
                '  :param Foo: Documents Foo',
                '  :type Bar: string',
                '  :param Bar: Documents Bar',
                '  :rtype: dict',
                '  :returns: ',
                '    **Response Syntax** ',
                '    ::',
                '      {',
                "          'Foo': 'string',",
                "          'Bar': 'string'",
                '      }',
                '    **Response Structure** ',
                '    - *(dict) --* ',
                '      - **Foo** *(string) --* Documents Foo',
                '      - **Bar** *(string) --* Documents Bar',
            ]
        )

    def test_returns_resource(self):
        resource_action = self.service_resource_model.actions[0]
        # Override the return type of the action to be a resource
        # instead of the regular dictionary.
        resource_action.resource = ResponseResource(
            {
                'type': 'Sample',
                'identifiers': [
                    {
                        'target': 'Name',
                        'source': 'requestParameter',
                        'path': 'Foo',
                    }
                ],
            },
            self.resource_json_model,
        )
        document_model_driven_resource_method(
            self.doc_structure,
            'foo',
            self.operation_model,
            event_emitter=self.event_emitter,
            method_description='This describes the foo method.',
            example_prefix='sample = myservice.foo',
            resource_action_model=resource_action,
        )
        self.assert_contains_lines_in_order(
            [
                '.. py:method:: foo(**kwargs)',
                '  This describes the foo method.',
                '  **Request Syntax** ',
                '  ::',
                '    sample = myservice.foo(',
                "        Foo='string',",
                "        Bar='string'",
                '    )',
                '  :type Foo: string',
                '  :param Foo: Documents Foo',
                '  :type Bar: string',
                '  :param Bar: Documents Bar',
                '  :rtype: :py:class:`myservice.Sample`',
                '  :returns: Sample resource',
            ]
        )

    def test_returns_list_of_resource(self):
        resource_action = self.service_resource_model.actions[1]
        document_model_driven_resource_method(
            self.doc_structure,
            'foo',
            self.operation_model,
            event_emitter=self.event_emitter,
            method_description='This describes the foo method.',
            example_prefix='samples = myservice.foo',
            resource_action_model=resource_action,
        )
        self.assert_contains_lines_in_order(
            [
                '.. py:method:: foo(**kwargs)',
                '  This describes the foo method.',
                '  **Request Syntax** ',
                '  ::',
                '    samples = myservice.foo(',
                "        Foo='string',",
                "        Bar='string'",
                '    )',
                '  :type Foo: string',
                '  :param Foo: Documents Foo',
                '  :type Bar: string',
                '  :param Bar: Documents Bar',
                '  :rtype: list(:py:class:`myservice.Sample`)',
                '  :returns: A list of Sample resource',
            ]
        )

    def test_include_input(self):
        include_params = [
            DocumentedShape(
                name='Biz', type_name='string', documentation='biz docs'
            )
        ]
        document_model_driven_resource_method(
            self.doc_structure,
            'foo',
            self.operation_model,
            event_emitter=self.event_emitter,
            method_description='This describes the foo method.',
            example_prefix='response = myservice.foo',
            include_input=include_params,
            resource_action_model=self.service_resource_model.actions[0],
        )
        self.assert_contains_lines_in_order(
            [
                '.. py:method:: foo(**kwargs)',
                '  This describes the foo method.',
                '  **Request Syntax** ',
                '  ::',
                '    response = myservice.foo(',
                "        Foo='string',",
                "        Bar='string',",
                "        Biz='string'",
                '    )',
                '  :type Foo: string',
                '  :param Foo: Documents Foo',
                '  :type Bar: string',
                '  :param Bar: Documents Bar',
                '  :type Biz: string',
                '  :param Biz: biz docs',
                '  :rtype: dict',
                '  :returns: ',
                '    **Response Syntax** ',
                '    ::',
                '      {',
                "          'Foo': 'string',",
                "          'Bar': 'string'",
                '      }',
                '    **Response Structure** ',
                '    - *(dict) --* ',
                '      - **Foo** *(string) --* Documents Foo',
                '      - **Bar** *(string) --* Documents Bar',
            ]
        )

    def test_include_output(self):
        include_params = [
            DocumentedShape(
                name='Biz', type_name='string', documentation='biz docs'
            )
        ]
        document_model_driven_resource_method(
            self.doc_structure,
            'foo',
            self.operation_model,
            event_emitter=self.event_emitter,
            method_description='This describes the foo method.',
            example_prefix='response = myservice.foo',
            include_output=include_params,
            resource_action_model=self.service_resource_model.actions[0],
        )
        self.assert_contains_lines_in_order(
            [
                '.. py:method:: foo(**kwargs)',
                '  This describes the foo method.',
                '  **Request Syntax** ',
                '  ::',
                '    response = myservice.foo(',
                "        Foo='string',",
                "        Bar='string'",
                '    )',
                '  :type Foo: string',
                '  :param Foo: Documents Foo',
                '  :type Bar: string',
                '  :param Bar: Documents Bar',
                '  :rtype: dict',
                '  :returns: ',
                '    **Response Syntax** ',
                '    ::',
                '      {',
                "          'Foo': 'string',",
                "          'Bar': 'string',",
                "          'Biz': 'string'",
                '      }',
                '    **Response Structure** ',
                '    - *(dict) --* ',
                '      - **Foo** *(string) --* Documents Foo',
                '      - **Bar** *(string) --* Documents Bar',
                '      - **Biz** *(string) --* biz docs',
            ]
        )

    def test_exclude_input(self):
        document_model_driven_resource_method(
            self.doc_structure,
            'foo',
            self.operation_model,
            event_emitter=self.event_emitter,
            method_description='This describes the foo method.',
            example_prefix='response = myservice.foo',
            exclude_input=['Bar'],
            resource_action_model=self.service_resource_model.actions[0],
        )
        self.assert_contains_lines_in_order(
            [
                '.. py:method:: foo(**kwargs)',
                '  This describes the foo method.',
                '  **Request Syntax** ',
                '  ::',
                '    response = myservice.foo(',
                "        Foo='string',",
                '    )',
                '  :type Foo: string',
                '  :param Foo: Documents Foo',
                '  :rtype: dict',
                '  :returns: ',
                '    **Response Syntax** ',
                '    ::',
                '      {',
                "          'Foo': 'string',",
                "          'Bar': 'string'",
                '      }',
                '    **Response Structure** ',
                '    - *(dict) --* ',
                '      - **Foo** *(string) --* Documents Foo',
                '      - **Bar** *(string) --* Documents Bar',
            ]
        )
        self.assert_not_contains_lines(
            [':param Bar: string', 'Bar=\'string\'']
        )

    def test_exclude_output(self):
        document_model_driven_resource_method(
            self.doc_structure,
            'foo',
            self.operation_model,
            event_emitter=self.event_emitter,
            method_description='This describes the foo method.',
            example_prefix='response = myservice.foo',
            exclude_output=['Bar'],
            resource_action_model=self.service_resource_model.actions[0],
        )
        self.assert_contains_lines_in_order(
            [
                '.. py:method:: foo(**kwargs)',
                '  This describes the foo method.',
                '  **Request Syntax** ',
                '  ::',
                '    response = myservice.foo(',
                "        Foo='string',",
                "        Bar='string'",
                '    )',
                '  :type Foo: string',
                '  :param Foo: Documents Foo',
                '  :type Bar: string',
                '  :param Bar: Documents Bar',
                '  :rtype: dict',
                '  :returns: ',
                '    **Response Syntax** ',
                '    ::',
                '      {',
                "          'Foo': 'string'",
                '      }',
                '    **Response Structure** ',
                '    - *(dict) --* ',
                '      - **Foo** *(string) --* Documents Foo',
            ]
        )
        self.assert_not_contains_lines(
            [
                '\'Bar\': \'string\'',
                '- **Bar** *(string) --*',
            ]
        )