Path: blob/develop/tests/unit/customizations/test_flatten.py
1567 views
# Copyright 2013 Amazon.com, Inc. or its affiliates. All Rights Reserved.1#2# Licensed under the Apache License, Version 2.0 (the "License"). You3# may not use this file except in compliance with the License. A copy of4# the License is located at5#6# http://aws.amazon.com/apache2.0/7#8# or in the "license" file accompanying this file. This file is9# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF10# ANY KIND, either express or implied. See the License for the specific11# language governing permissions and limitations under the License.12from awscli.testutils import mock, unittest1314from awscli.arguments import CLIArgument15from awscli.customizations import utils16from awscli.customizations.flatten import FlattenedArgument, FlattenArguments171819def _hydrate(params, container, cli_type, key, value):20"""21An example to hydrate a complex structure with custom value logic. In this22case we create a nested structure and divide the value by 100.23"""24params['bag'] = {25'ArgumentBaz': {26'SomeValueAbc': value / 100.027}28}293031FLATTEN_CONFIG = {32'command-name': {33'original-argument': {34"keep": False,35"flatten": {36"ArgumentFoo": {37"name": "foo"38},39"ArgumentBar": {40"name": "bar",41"help_text": "Some help text",42"required": True,43"hydrate_value": lambda x: x.upper()44}45}46},47'another-original-argument': {48"keep": True,49"flatten": {50"ArgumentBaz.SomeValue": {51"name": "baz",52"hydrate": _hydrate53}54}55}56}57}5859class TestFlattenedArgument(unittest.TestCase):60def test_basic_argument(self):61container = mock.Mock()62container.argument_model.name = 'bag'63kwargs = {64'container': container,65'prop': 'ArgumentFoo'66}67kwargs['container'].py_name = 'bag'68kwargs.update(FLATTEN_CONFIG['command-name']['original-argument']69['flatten']['ArgumentFoo'])70arg = FlattenedArgument(**kwargs)7172self.assertEqual('foo', arg.name)73self.assertEqual('', arg.documentation)74self.assertEqual(False, arg.required)7576params = {}77arg.add_to_params(params, 'value')78self.assertEqual('value', params['bag']['ArgumentFoo'])7980def test_hydrate_value_argument(self):81container = mock.Mock()82container.argument_model.name = 'bag'83kwargs = {84'container': container,85'prop': 'ArgumentBar'86}87kwargs['container'].py_name = 'bag'88kwargs['container'].cli_type_name = 'list'89kwargs.update(FLATTEN_CONFIG['command-name']['original-argument']90['flatten']['ArgumentBar'])91arg = FlattenedArgument(**kwargs)9293self.assertEqual('bar', arg.name)94self.assertEqual('Some help text', arg.documentation)95self.assertEqual(True, arg.required)9697params = {}98arg.add_to_params(params, 'value')99self.assertEqual('VALUE', params['bag'][0]['ArgumentBar'])100101def test_hydrate_function_argument(self):102container = mock.Mock()103container.argument_model.name = 'bag'104kwargs = {105'container': container,106'prop': 'ArgumentBaz:SomeValue'107}108kwargs['container'].py_name = 'bag'109kwargs.update(FLATTEN_CONFIG['command-name']110['another-original-argument']111['flatten']['ArgumentBaz.SomeValue'])112arg = FlattenedArgument(**kwargs)113114self.assertEqual('baz', arg.name)115self.assertEqual('', arg.documentation)116self.assertEqual(False, arg.required)117118params = {}119arg.add_to_params(params, 1020)120self.assertEqual(10.2, params['bag']['ArgumentBaz']['SomeValueAbc'])121122123class TestFlattenCommands(unittest.TestCase):124def test_flatten_register(self):125cli = mock.Mock()126127flatten = FlattenArguments('my-service', FLATTEN_CONFIG)128flatten.register(cli)129130cli.register.assert_called_with(\131'building-argument-table.my-service.command-name',132flatten.flatten_args)133134def test_flatten_modify_args(self):135# Mock operation, arguments, and members for a service136command = mock.Mock()137command.name = 'command-name'138139argument_model1 = mock.Mock()140argument_model1.required_members = []141142member_foo = mock.Mock()143member_foo.name = 'ArgumentFoo'144member_foo.documentation = 'Original docs'145member_foo.required_members = []146147member_bar = mock.Mock()148member_bar.name = 'ArgumentBar'149member_bar.documentation = 'More docs'150member_bar.required_members = []151152argument_model1.members = {153'ArgumentFoo': member_foo,154'ArgumentBar': member_bar155}156157argument_model2 = mock.Mock()158argument_model2.required_members = []159160member_baz = mock.Mock()161member_baz.name = 'ArgumentBaz'162member_baz.documentation = ''163member_baz.required_members = []164165member_some_value = mock.Mock()166member_some_value.name = 'SomeValue'167member_some_value.documenation = ''168member_some_value.required_members = []169170member_baz.members = {171'SomeValue': member_some_value172}173174argument_model2.members = {175'ArgumentBaz': member_baz176}177178cli_argument1 = mock.Mock(spec=CLIArgument)179cli_argument1.argument_model = argument_model1180181cli_argument2 = mock.Mock(spec=CLIArgument)182cli_argument2.argument_model = argument_model2183184argument_table = {185'original-argument': cli_argument1,186'another-original-argument': cli_argument2187}188189# Create the flattened argument table190cli = mock.Mock()191flatten = FlattenArguments('my-service', FLATTEN_CONFIG)192flatten.flatten_args(command, argument_table)193194# Make sure new arguments and any with keep=True are there195self.assertIn('foo', argument_table)196self.assertIn('bar', argument_table)197self.assertNotIn('original-argument', argument_table)198self.assertIn('baz', argument_table)199self.assertIn('another-original-argument', argument_table)200201# Make sure the new arguments are the class we expect202self.assertIsInstance(argument_table['foo'], FlattenedArgument)203self.assertIsInstance(argument_table['bar'], FlattenedArgument)204self.assertIsInstance(argument_table['baz'], FlattenedArgument)205self.assertNotIsInstance(argument_table['another-original-argument'],206FlattenedArgument)207208# Make sure original required trait can be overridden209self.assertEqual(False, argument_table['foo'].required)210self.assertEqual(True, argument_table['bar'].required)211212# Make sure docs can be overridden and get the defaults213self.assertEqual('Original docs', argument_table['foo'].documentation)214self.assertEqual('Some help text', argument_table['bar'].documentation)215216217