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
|
# 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
#
# http://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 tests import unittest
from tests.unit.docs import BaseDocsTest
from botocore.hooks import HierarchicalEmitter
from botocore.docs.method import document_model_driven_signature
from botocore.docs.method import document_custom_signature
from botocore.docs.method import document_custom_method
from botocore.docs.method import document_model_driven_method
from botocore.docs.method import get_instance_public_methods
from botocore.docs.utils import DocumentedShape
class TestGetInstanceMethods(unittest.TestCase):
class MySampleClass(object):
def _internal_method(self):
pass
def public_method(self):
pass
def test_get_instance_methods(self):
instance = self.MySampleClass()
instance_methods = get_instance_public_methods(instance)
self.assertEqual(len(instance_methods), 1)
self.assertIn('public_method', instance_methods)
self.assertEqual(
instance.public_method, instance_methods['public_method'])
class TestDocumentModelDrivenSignature(BaseDocsTest):
def setUp(self):
super(TestDocumentModelDrivenSignature, self).setUp()
self.add_shape_to_params('Foo', 'String')
self.add_shape_to_params('Bar', 'String', is_required=True)
self.add_shape_to_params('Baz', 'String')
def test_document_signature(self):
document_model_driven_signature(
self.doc_structure, 'my_method', self.operation_model)
self.assert_contains_line(
'.. py:method:: my_method(**kwargs)')
def test_document_signature_exclude_all_kwargs(self):
exclude_params = ['Foo', 'Bar', 'Baz']
document_model_driven_signature(
self.doc_structure, 'my_method', self.operation_model,
exclude=exclude_params)
self.assert_contains_line(
'.. py:method:: my_method()')
def test_document_signature_exclude_and_include(self):
exclude_params = ['Foo', 'Bar', 'Baz']
include_params = [
DocumentedShape(
name='Biz', type_name='integer', documentation='biz docs')
]
document_model_driven_signature(
self.doc_structure, 'my_method', self.operation_model,
include=include_params, exclude=exclude_params)
self.assert_contains_line(
'.. py:method:: my_method(**kwargs)')
class TestDocumentCustomSignature(BaseDocsTest):
def sample_method(self, foo, bar='bar', baz=None):
pass
def test_document_signature(self):
document_custom_signature(
self.doc_structure, 'my_method', self.sample_method)
self.assert_contains_line(
'.. py:method:: my_method(foo, bar=\'bar\', baz=None)')
class TestDocumentCustomMethod(BaseDocsTest):
def custom_method(self, foo):
"""This is a custom method
:type foo: string
:param foo: The foo parameter
"""
pass
def test_document_custom_signature(self):
document_custom_method(
self.doc_structure, 'my_method', self.custom_method)
self.assert_contains_lines_in_order([
'.. py:method:: my_method(foo)',
' This is a custom method',
' :type foo: string',
' :param foo: The foo parameter'
])
class TestDocumentModelDrivenMethod(BaseDocsTest):
def setUp(self):
super(TestDocumentModelDrivenMethod, self).setUp()
self.event_emitter = HierarchicalEmitter()
self.add_shape_to_params('Bar', 'String')
def test_default(self):
document_model_driven_method(
self.doc_structure, 'foo', self.operation_model,
event_emitter=self.event_emitter,
method_description='This describes the foo method.',
example_prefix='response = client.foo'
)
cross_ref_link = (
'See also: `AWS API Documentation '
'<https://docs.aws.amazon.com/goto/WebAPI'
'/myservice-2014-01-01/SampleOperation>'
)
self.assert_contains_lines_in_order([
'.. py:method:: foo(**kwargs)',
' This describes the foo method.',
cross_ref_link,
' **Request Syntax**',
' ::',
' response = client.foo(',
' Bar=\'string\'',
' )',
' :type Bar: string',
' :param Bar:',
' :rtype: dict',
' :returns:',
' **Response Syntax**',
' ::',
' {',
' \'Bar\': \'string\'',
' }',
' **Response Structure**',
' - *(dict) --*',
' - **Bar** *(string) --*'
])
def test_no_input_output_shape(self):
del self.json_model['operations']['SampleOperation']['input']
del self.json_model['operations']['SampleOperation']['output']
document_model_driven_method(
self.doc_structure, 'foo', self.operation_model,
event_emitter=self.event_emitter,
method_description='This describes the foo method.',
example_prefix='response = client.foo'
)
self.assert_contains_lines_in_order([
'.. py:method:: foo()',
' This describes the foo method.',
' **Request Syntax**',
' ::',
' response = client.foo()',
' :returns: None',
])
def test_include_input(self):
include_params = [
DocumentedShape(
name='Biz', type_name='string', documentation='biz docs')
]
document_model_driven_method(
self.doc_structure, 'foo', self.operation_model,
event_emitter=self.event_emitter,
method_description='This describes the foo method.',
example_prefix='response = client.foo',
include_input=include_params
)
self.assert_contains_lines_in_order([
'.. py:method:: foo(**kwargs)',
' This describes the foo method.',
' **Request Syntax**',
' ::',
' response = client.foo(',
' Bar=\'string\',',
' Biz=\'string\'',
' )',
' :type Bar: string',
' :param Bar:',
' :type Biz: string',
' :param Biz: biz docs',
' :rtype: dict',
' :returns:',
' **Response Syntax**',
' ::',
' {',
' \'Bar\': \'string\'',
' }',
' **Response Structure**',
' - *(dict) --*',
' - **Bar** *(string) --*'
])
def test_include_output(self):
include_params = [
DocumentedShape(
name='Biz', type_name='string', documentation='biz docs')
]
document_model_driven_method(
self.doc_structure, 'foo', self.operation_model,
event_emitter=self.event_emitter,
method_description='This describes the foo method.',
example_prefix='response = client.foo',
include_output=include_params
)
self.assert_contains_lines_in_order([
'.. py:method:: foo(**kwargs)',
' This describes the foo method.',
' **Request Syntax**',
' ::',
' response = client.foo(',
' Bar=\'string\'',
' )',
' :type Bar: string',
' :param Bar:',
' :rtype: dict',
' :returns:',
' **Response Syntax**',
' ::',
' {',
' \'Bar\': \'string\'',
' \'Biz\': \'string\'',
' }',
' **Response Structure**',
' - *(dict) --*',
' - **Bar** *(string) --*',
' - **Biz** *(string) --*'
])
def test_exclude_input(self):
self.add_shape_to_params('Biz', 'String')
document_model_driven_method(
self.doc_structure, 'foo', self.operation_model,
event_emitter=self.event_emitter,
method_description='This describes the foo method.',
example_prefix='response = client.foo',
exclude_input=['Bar']
)
self.assert_contains_lines_in_order([
'.. py:method:: foo(**kwargs)',
' This describes the foo method.',
' **Request Syntax**',
' ::',
' response = client.foo(',
' Biz=\'string\'',
' )',
' :type Biz: string',
' :param Biz:',
' :rtype: dict',
' :returns:',
' **Response Syntax**',
' ::',
' {',
' \'Bar\': \'string\'',
' \'Biz\': \'string\'',
' }',
' **Response Structure**',
' - *(dict) --*',
' - **Bar** *(string) --*',
' - **Biz** *(string) --*'
])
self.assert_not_contains_lines([
':param Bar: string',
'Bar=\'string\''
])
def test_exclude_output(self):
self.add_shape_to_params('Biz', 'String')
document_model_driven_method(
self.doc_structure, 'foo', self.operation_model,
event_emitter=self.event_emitter,
method_description='This describes the foo method.',
example_prefix='response = client.foo',
exclude_output=['Bar']
)
self.assert_contains_lines_in_order([
'.. py:method:: foo(**kwargs)',
' This describes the foo method.',
' **Request Syntax**',
' ::',
' response = client.foo(',
' Bar=\'string\'',
' Biz=\'string\'',
' )',
' :type Biz: string',
' :param Biz:',
' :rtype: dict',
' :returns:',
' **Response Syntax**',
' ::',
' {',
' \'Biz\': \'string\'',
' }',
' **Response Structure**',
' - *(dict) --*',
' - **Biz** *(string) --*'
])
self.assert_not_contains_lines([
'\'Bar\': \'string\'',
'- **Bar** *(string) --*',
])
def test_streaming_body_in_output(self):
self.add_shape_to_params('Body', 'Blob')
self.json_model['shapes']['Blob'] = {'type': 'blob'}
self.json_model['shapes']['SampleOperationInputOutput']['payload'] = \
'Body'
document_model_driven_method(
self.doc_structure, 'foo', self.operation_model,
event_emitter=self.event_emitter,
method_description='This describes the foo method.',
example_prefix='response = client.foo'
)
self.assert_contains_line('**Body** (:class:`.StreamingBody`)')
def test_event_stream_body_in_output(self):
self.add_shape_to_params('Payload', 'EventStream')
self.json_model['shapes']['SampleOperationInputOutput']['payload'] = \
'Payload'
self.json_model['shapes']['EventStream'] = {
'type': 'structure',
'eventstream': True,
'members': {'Event': {'shape': 'Event'}}
}
self.json_model['shapes']['Event'] = {
'type': 'structure',
'event': True,
'members': {
'Fields': {
'shape': 'EventFields',
'eventpayload': True,
}
}
}
self.json_model['shapes']['EventFields'] = {
'type': 'structure',
'members': {
'Field': {'shape': 'EventField'}
}
}
self.json_model['shapes']['EventField'] = {'type': 'blob'}
document_model_driven_method(
self.doc_structure, 'foo', self.operation_model,
event_emitter=self.event_emitter,
method_description='This describes the foo method.',
example_prefix='response = client.foo'
)
self.assert_contains_lines_in_order([
"this operation contains an :class:`.EventStream`",
"'Payload': EventStream({",
"'Event': {",
"'Fields': {",
"'Field': b'bytes'",
"**Payload** (:class:`.EventStream`)",
"**Event** *(dict)",
"**Fields** *(dict)",
"**Field** *(bytes)",
])
def test_streaming_body_in_input(self):
del self.json_model['operations']['SampleOperation']['output']
self.add_shape_to_params('Body', 'Blob')
self.json_model['shapes']['Blob'] = {'type': 'blob'}
self.json_model['shapes']['SampleOperationInputOutput']['payload'] = \
'Body'
document_model_driven_method(
self.doc_structure, 'foo', self.operation_model,
event_emitter=self.event_emitter,
method_description='This describes the foo method.',
example_prefix='response = client.foo'
)
# The line in the example
self.assert_contains_line('Body=b\'bytes\'|file')
# The line in the parameter description
self.assert_contains_line(
':type Body: bytes or seekable file-like object')
def test_deprecated(self):
self.json_model['operations']['SampleOperation']['deprecated'] = True
document_model_driven_method(
self.doc_structure, 'foo', self.operation_model,
event_emitter=self.event_emitter,
method_description='This describes the foo method.',
example_prefix='response = client.foo'
)
# The line in the example
self.assert_contains_lines_in_order([
' .. danger::',
' This operation is deprecated and may not function as '
'expected. This operation should not be used going forward and is '
'only kept for the purpose of backwards compatiblity.'
])
|