python-botocore/tests/functional/docs/test_shared_example_config.py

151 lines
5.2 KiB
Python
Raw Normal View History

2017-06-27 11:52:19 +02:00
# Copyright 2017 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.
2021-10-04 18:33:37 +02:00
import pytest
2017-06-27 11:52:19 +02:00
import botocore.session
from botocore.model import OperationNotFoundError
from botocore.utils import parse_timestamp
2021-10-04 18:33:37 +02:00
def _shared_example_configs():
2017-06-27 11:52:19 +02:00
session = botocore.session.Session()
loader = session.get_component('data_loader')
services = loader.list_available_services('examples-1')
for service in services:
service_model = session.get_service_model(service)
example_config = loader.load_service_model(
service, 'examples-1', service_model.api_version
)
examples = example_config.get("examples", {})
for operation, operation_examples in examples.items():
for example in operation_examples:
2021-10-04 18:33:37 +02:00
yield operation, example, service_model
2017-06-27 11:52:19 +02:00
2021-10-04 18:33:37 +02:00
@pytest.mark.parametrize(
2022-05-26 00:10:07 +02:00
"operation_name, example_config, service_model", _shared_example_configs()
2021-10-04 18:33:37 +02:00
)
2022-05-26 00:10:07 +02:00
def test_lint_shared_example_configs(
operation_name, example_config, service_model
):
2017-06-27 11:52:19 +02:00
# The operation should actually exist
assert_operation_exists(service_model, operation_name)
operation_model = service_model.operation_model(operation_name)
2022-05-26 00:10:07 +02:00
assert_valid_values(
service_model.service_name, operation_model, example_config
)
2017-06-27 11:52:19 +02:00
def assert_valid_values(service_name, operation_model, example_config):
example_input = example_config.get('input')
input_shape = operation_model.input_shape
example_id = example_config['id']
if input_shape is None and example_input:
raise AssertionError(
"Input found in example for %s from %s with id %s, but no input "
2022-05-26 00:10:07 +02:00
"shape is defined."
% (operation_model.name, service_name, example_id)
)
2017-06-27 11:52:19 +02:00
example_output = example_config.get('output')
output_shape = operation_model.output_shape
if output_shape is None and example_output:
raise AssertionError(
"Output found in example for %s from %s with id %s, but no output "
2022-05-26 00:10:07 +02:00
"shape is defined."
% (operation_model.name, service_name, example_id)
)
2017-06-27 11:52:19 +02:00
try:
if example_input is not None and input_shape is not None:
_assert_valid_values(
2022-05-26 00:10:07 +02:00
input_shape, example_input, [input_shape.name]
)
2017-06-27 11:52:19 +02:00
if example_output is not None and output_shape is not None:
_assert_valid_values(
2022-05-26 00:10:07 +02:00
output_shape, example_output, [output_shape.name]
)
2017-06-27 11:52:19 +02:00
except AssertionError as e:
raise AssertionError(
2022-05-26 00:10:07 +02:00
"Invalid value in example for {} from {} with id {}: {}".format(
2017-06-27 11:52:19 +02:00
operation_model.name, service_name, example_id, e
2022-05-26 00:10:07 +02:00
)
)
2017-06-27 11:52:19 +02:00
def _assert_valid_values(shape, example_value, path):
if shape.type_name == 'timestamp':
_assert_valid_timestamp(example_value, path)
elif shape.type_name == 'structure':
_assert_valid_structure_values(shape, example_value, path)
elif shape.type_name == 'list':
_assert_valid_list_values(shape, example_value, path)
elif shape.type_name == 'map':
_assert_valid_map_values(shape, example_value, path)
def _assert_valid_structure_values(shape, example_dict, path):
2022-05-26 00:10:07 +02:00
invalid_members = [
k for k in example_dict.keys() if k not in shape.members
]
2017-06-27 11:52:19 +02:00
if invalid_members:
dotted_path = '.'.join(path)
raise AssertionError(
2022-05-26 00:10:07 +02:00
"Invalid members found for {}: {}".format(
dotted_path, invalid_members
)
2017-06-27 11:52:19 +02:00
)
for member_name, example_value in example_dict.items():
member = shape.members[member_name]
_assert_valid_values(member, example_value, path + [member_name])
def _assert_valid_list_values(shape, example_values, path):
member = shape.member
for i, value in enumerate(example_values):
2022-05-26 00:10:07 +02:00
name = f"{path[-1]}[{i}]"
2017-06-27 11:52:19 +02:00
_assert_valid_values(member, value, path[:-1] + [name])
def _assert_valid_map_values(shape, example_value, path):
for key, value in example_value.items():
2022-05-26 00:10:07 +02:00
name = f'{path[-1]}["{key}"]'
2017-06-27 11:52:19 +02:00
_assert_valid_values(shape.value, value, path[:-1] + [name])
def _assert_valid_timestamp(timestamp, path):
try:
parse_timestamp(timestamp).timetuple()
except Exception as e:
dotted_path = '.'.join(path)
2022-05-26 00:10:07 +02:00
raise AssertionError(
'Failed to parse timestamp {} for {}: {}'.format(
timestamp, dotted_path, e
)
)
2017-06-27 11:52:19 +02:00
def assert_operation_exists(service_model, operation_name):
try:
service_model.operation_model(operation_name)
except OperationNotFoundError:
raise AssertionError(
2022-05-26 00:10:07 +02:00
"Examples found in {} for operation {} that does not exist.".format(
service_model.service_name, operation_name
)
)