python-boto3/tests/unit/docs/__init__.py

347 lines
12 KiB
Python
Raw Normal View History

2015-11-27 23:25:33 +01:00
# 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
#
2021-09-22 18:34:33 +02:00
# https://aws.amazon.com/apache2.0/
2015-11-27 23:25:33 +01:00
#
# 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.
import json
2022-05-26 01:13:54 +02:00
import os
2015-11-27 23:25:33 +01:00
import shutil
2022-05-26 01:13:54 +02:00
import tempfile
2015-11-27 23:25:33 +01:00
import botocore.session
from botocore.compat import OrderedDict
from botocore.docs.bcdoc.restdoc import DocumentStructure
2022-05-26 01:13:54 +02:00
from botocore.loaders import Loader
2015-11-27 23:25:33 +01:00
from boto3.session import Session
2022-05-26 01:13:54 +02:00
from tests import unittest
2015-11-27 23:25:33 +01:00
class BaseDocsTest(unittest.TestCase):
def setUp(self):
self.root_dir = tempfile.mkdtemp()
self.version_dirs = os.path.join(
2022-05-26 01:13:54 +02:00
self.root_dir, 'myservice', '2014-01-01'
)
2015-11-27 23:25:33 +01:00
os.makedirs(self.version_dirs)
self.model_file = os.path.join(self.version_dirs, 'service-2.json')
self.waiter_model_file = os.path.join(
2022-05-26 01:13:54 +02:00
self.version_dirs, 'waiters-2.json'
)
2015-11-27 23:25:33 +01:00
self.paginator_model_file = os.path.join(
2022-05-26 01:13:54 +02:00
self.version_dirs, 'paginators-1.json'
)
2015-11-27 23:25:33 +01:00
self.resource_model_file = os.path.join(
2022-05-26 01:13:54 +02:00
self.version_dirs, 'resources-1.json'
)
2016-05-22 04:03:29 +02:00
self.example_model_file = os.path.join(
2022-05-26 01:13:54 +02:00
self.version_dirs, 'examples-1.json'
)
2015-11-27 23:25:33 +01:00
self.json_model = {}
self.waiter_json_model = {}
self.paginator_json_model = {}
self.resource_json_model = {}
self._setup_models()
self.doc_name = 'MyDoc'
self.doc_structure = DocumentStructure(self.doc_name)
2016-05-22 04:03:29 +02:00
self.setup_client_and_resource()
def tearDown(self):
shutil.rmtree(self.root_dir)
def setup_client_and_resource(self):
self._write_models()
2015-11-27 23:25:33 +01:00
self.loader = Loader(extra_search_paths=[self.root_dir])
self.botocore_session = botocore.session.get_session()
self.botocore_session.register_component('data_loader', self.loader)
self.session = Session(
2022-05-26 01:13:54 +02:00
botocore_session=self.botocore_session, region_name='us-east-1'
)
2015-11-27 23:25:33 +01:00
self.client = self.session.client('myservice', 'us-east-1')
self.resource = self.session.resource('myservice', 'us-east-1')
def _setup_models(self):
self.json_model = {
'metadata': {
'apiVersion': '2014-01-01',
'endpointPrefix': 'myservice',
'signatureVersion': 'v4',
'serviceFullName': 'AWS MyService',
2019-01-28 22:47:44 +01:00
'protocol': 'query',
'serviceId': 'MyService',
2015-11-27 23:25:33 +01:00
},
'operations': {
'SampleOperation': {
'name': 'SampleOperation',
'input': {'shape': 'SampleOperationInputOutput'},
2022-05-26 01:13:54 +02:00
'output': {'shape': 'SampleOperationInputOutput'},
2015-11-27 23:25:33 +01:00
}
},
'shapes': {
'SampleOperationInputOutput': {
'type': 'structure',
2022-05-26 01:13:54 +02:00
'members': OrderedDict(
[
(
'Foo',
{
'shape': 'String',
'documentation': 'Documents Foo',
},
),
(
'Bar',
{
'shape': 'String',
'documentation': 'Documents Bar',
},
),
]
),
2015-11-27 23:25:33 +01:00
},
2022-05-26 01:13:54 +02:00
'String': {'type': 'string'},
},
2015-11-27 23:25:33 +01:00
}
2016-05-22 04:03:29 +02:00
self.example_json_model = {
"version": 1,
"examples": {
2022-05-26 01:13:54 +02:00
"SampleOperation": [
{
"id": "sample-id",
"title": "sample-title",
"description": "Sample Description.",
"input": OrderedDict(
[
("Foo", "bar"),
]
),
"comments": {
"input": {"Foo": "biz"},
2016-05-22 04:03:29 +02:00
},
}
2022-05-26 01:13:54 +02:00
]
},
2016-05-22 04:03:29 +02:00
}
2015-11-27 23:25:33 +01:00
self.waiter_json_model = {
"version": 2,
"waiters": {
"SampleOperationComplete": {
"delay": 15,
"operation": "SampleOperation",
"maxAttempts": 40,
"acceptors": [
2022-05-26 01:13:54 +02:00
{
"expected": "complete",
"matcher": "pathAll",
"state": "success",
"argument": "Biz",
},
{
"expected": "failed",
"matcher": "pathAny",
"state": "failure",
"argument": "Biz",
},
],
2015-11-27 23:25:33 +01:00
}
2022-05-26 01:13:54 +02:00
},
2015-11-27 23:25:33 +01:00
}
self.paginator_json_model = {
"pagination": {
"SampleOperation": {
"input_token": "NextResult",
"output_token": "NextResult",
"limit_key": "MaxResults",
2022-05-26 01:13:54 +02:00
"result_key": "Biz",
2015-11-27 23:25:33 +01:00
}
}
}
self.resource_json_model = {
"service": {
2022-05-26 01:13:54 +02:00
"actions": OrderedDict(
[
(
"SampleOperation",
{"request": {"operation": "SampleOperation"}},
),
(
"SampleListReturnOperation",
{
"request": {"operation": "SampleOperation"},
"resource": {
"type": "Sample",
"identifiers": [
{
"target": "Name",
"source": "response",
"path": "Samples[].Name",
}
],
"path": "Samples[]",
},
},
),
]
),
2015-11-27 23:25:33 +01:00
"has": {
"Sample": {
"resource": {
"type": "Sample",
"identifiers": [
{"target": "Name", "source": "input"}
2022-05-26 01:13:54 +02:00
],
2015-11-27 23:25:33 +01:00
}
}
},
"hasMany": {
"Samples": {
"request": {"operation": "SampleOperation"},
"resource": {
"type": "Sample",
"identifiers": [
2022-05-26 01:13:54 +02:00
{
"target": "Name",
"source": "response",
"path": "Samples[].Foo",
}
],
},
2015-11-27 23:25:33 +01:00
}
2022-05-26 01:13:54 +02:00
},
2015-11-27 23:25:33 +01:00
},
"resources": {
"Sample": {
2022-05-26 01:13:54 +02:00
"identifiers": [{"name": "Name", "memberName": "Foo"}],
2015-11-27 23:25:33 +01:00
"shape": "SampleOperationInputOutput",
"load": {
"request": {
"operation": "SampleOperation",
"params": [
2022-05-26 01:13:54 +02:00
{
"target": "Foo",
"source": "identifier",
"name": "Name",
}
],
2015-11-27 23:25:33 +01:00
}
},
"actions": {
"Operate": {
"request": {
"operation": "SampleOperation",
"params": [
2022-05-26 01:13:54 +02:00
{
"target": "Foo",
"source": "identifier",
"name": "Name",
}
],
2015-11-27 23:25:33 +01:00
}
}
},
"batchActions": {
"Operate": {
"request": {
"operation": "SampleOperation",
"params": [
2022-05-26 01:13:54 +02:00
{
"target": "Samples[].Foo",
"source": "identifier",
"name": "Name",
}
],
2015-11-27 23:25:33 +01:00
}
}
},
"has": {
"RelatedSample": {
"resource": {
"type": "Sample",
"identifiers": [
2022-05-26 01:13:54 +02:00
{
"target": "Name",
"source": "data",
"path": "Foo",
}
],
2015-11-27 23:25:33 +01:00
}
}
},
"waiters": {
"Complete": {
"waiterName": "SampleOperationComplete",
"params": [
2022-05-26 01:13:54 +02:00
{
"target": "Foo",
"source": "identifier",
"name": "Name",
}
],
2015-11-27 23:25:33 +01:00
}
2022-05-26 01:13:54 +02:00
},
2015-11-27 23:25:33 +01:00
}
2022-05-26 01:13:54 +02:00
},
2015-11-27 23:25:33 +01:00
}
def _write_models(self):
with open(self.resource_model_file, 'w') as f:
json.dump(self.resource_json_model, f)
with open(self.waiter_model_file, 'w') as f:
json.dump(self.waiter_json_model, f)
with open(self.paginator_model_file, 'w') as f:
json.dump(self.paginator_json_model, f)
with open(self.model_file, 'w') as f:
json.dump(self.json_model, f)
2016-05-22 04:03:29 +02:00
with open(self.example_model_file, 'w') as f:
json.dump(self.example_json_model, f)
2015-11-27 23:25:33 +01:00
def add_shape(self, shape):
shape_name = list(shape.keys())[0]
self.json_model['shapes'][shape_name] = shape[shape_name]
2022-05-26 01:13:54 +02:00
def add_shape_to_params(
self, param_name, shape_name, documentation=None, is_required=False
):
2015-11-27 23:25:33 +01:00
params_shape = self.json_model['shapes']['SampleOperationInputOutput']
member = {'shape': shape_name}
if documentation is not None:
member['documentation'] = documentation
params_shape['members'][param_name] = member
if is_required:
required_list = params_shape.get('required', [])
required_list.append(param_name)
params_shape['required'] = required_list
def assert_contains_lines_in_order(self, lines, contents=None):
if contents is None:
contents = self.doc_structure.flush_structure().decode('utf-8')
for line in lines:
2021-10-04 19:51:32 +02:00
assert line in contents
2015-11-27 23:25:33 +01:00
beginning = contents.find(line)
2022-05-26 01:13:54 +02:00
contents = contents[(beginning + len(line)) :]
2015-11-27 23:25:33 +01:00
def assert_not_contains_lines(self, lines):
contents = self.doc_structure.flush_structure().decode('utf-8')
for line in lines:
2021-10-04 19:51:32 +02:00
assert line not in contents