Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
aws
GitHub Repository: aws/aws-cli
Path: blob/develop/tests/unit/customizations/test_flatten.py
1567 views
1
# Copyright 2013 Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
#
3
# Licensed under the Apache License, Version 2.0 (the "License"). You
4
# may not use this file except in compliance with the License. A copy of
5
# the License is located at
6
#
7
# http://aws.amazon.com/apache2.0/
8
#
9
# or in the "license" file accompanying this file. This file is
10
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
11
# ANY KIND, either express or implied. See the License for the specific
12
# language governing permissions and limitations under the License.
13
from awscli.testutils import mock, unittest
14
15
from awscli.arguments import CLIArgument
16
from awscli.customizations import utils
17
from awscli.customizations.flatten import FlattenedArgument, FlattenArguments
18
19
20
def _hydrate(params, container, cli_type, key, value):
21
"""
22
An example to hydrate a complex structure with custom value logic. In this
23
case we create a nested structure and divide the value by 100.
24
"""
25
params['bag'] = {
26
'ArgumentBaz': {
27
'SomeValueAbc': value / 100.0
28
}
29
}
30
31
32
FLATTEN_CONFIG = {
33
'command-name': {
34
'original-argument': {
35
"keep": False,
36
"flatten": {
37
"ArgumentFoo": {
38
"name": "foo"
39
},
40
"ArgumentBar": {
41
"name": "bar",
42
"help_text": "Some help text",
43
"required": True,
44
"hydrate_value": lambda x: x.upper()
45
}
46
}
47
},
48
'another-original-argument': {
49
"keep": True,
50
"flatten": {
51
"ArgumentBaz.SomeValue": {
52
"name": "baz",
53
"hydrate": _hydrate
54
}
55
}
56
}
57
}
58
}
59
60
class TestFlattenedArgument(unittest.TestCase):
61
def test_basic_argument(self):
62
container = mock.Mock()
63
container.argument_model.name = 'bag'
64
kwargs = {
65
'container': container,
66
'prop': 'ArgumentFoo'
67
}
68
kwargs['container'].py_name = 'bag'
69
kwargs.update(FLATTEN_CONFIG['command-name']['original-argument']
70
['flatten']['ArgumentFoo'])
71
arg = FlattenedArgument(**kwargs)
72
73
self.assertEqual('foo', arg.name)
74
self.assertEqual('', arg.documentation)
75
self.assertEqual(False, arg.required)
76
77
params = {}
78
arg.add_to_params(params, 'value')
79
self.assertEqual('value', params['bag']['ArgumentFoo'])
80
81
def test_hydrate_value_argument(self):
82
container = mock.Mock()
83
container.argument_model.name = 'bag'
84
kwargs = {
85
'container': container,
86
'prop': 'ArgumentBar'
87
}
88
kwargs['container'].py_name = 'bag'
89
kwargs['container'].cli_type_name = 'list'
90
kwargs.update(FLATTEN_CONFIG['command-name']['original-argument']
91
['flatten']['ArgumentBar'])
92
arg = FlattenedArgument(**kwargs)
93
94
self.assertEqual('bar', arg.name)
95
self.assertEqual('Some help text', arg.documentation)
96
self.assertEqual(True, arg.required)
97
98
params = {}
99
arg.add_to_params(params, 'value')
100
self.assertEqual('VALUE', params['bag'][0]['ArgumentBar'])
101
102
def test_hydrate_function_argument(self):
103
container = mock.Mock()
104
container.argument_model.name = 'bag'
105
kwargs = {
106
'container': container,
107
'prop': 'ArgumentBaz:SomeValue'
108
}
109
kwargs['container'].py_name = 'bag'
110
kwargs.update(FLATTEN_CONFIG['command-name']
111
['another-original-argument']
112
['flatten']['ArgumentBaz.SomeValue'])
113
arg = FlattenedArgument(**kwargs)
114
115
self.assertEqual('baz', arg.name)
116
self.assertEqual('', arg.documentation)
117
self.assertEqual(False, arg.required)
118
119
params = {}
120
arg.add_to_params(params, 1020)
121
self.assertEqual(10.2, params['bag']['ArgumentBaz']['SomeValueAbc'])
122
123
124
class TestFlattenCommands(unittest.TestCase):
125
def test_flatten_register(self):
126
cli = mock.Mock()
127
128
flatten = FlattenArguments('my-service', FLATTEN_CONFIG)
129
flatten.register(cli)
130
131
cli.register.assert_called_with(\
132
'building-argument-table.my-service.command-name',
133
flatten.flatten_args)
134
135
def test_flatten_modify_args(self):
136
# Mock operation, arguments, and members for a service
137
command = mock.Mock()
138
command.name = 'command-name'
139
140
argument_model1 = mock.Mock()
141
argument_model1.required_members = []
142
143
member_foo = mock.Mock()
144
member_foo.name = 'ArgumentFoo'
145
member_foo.documentation = 'Original docs'
146
member_foo.required_members = []
147
148
member_bar = mock.Mock()
149
member_bar.name = 'ArgumentBar'
150
member_bar.documentation = 'More docs'
151
member_bar.required_members = []
152
153
argument_model1.members = {
154
'ArgumentFoo': member_foo,
155
'ArgumentBar': member_bar
156
}
157
158
argument_model2 = mock.Mock()
159
argument_model2.required_members = []
160
161
member_baz = mock.Mock()
162
member_baz.name = 'ArgumentBaz'
163
member_baz.documentation = ''
164
member_baz.required_members = []
165
166
member_some_value = mock.Mock()
167
member_some_value.name = 'SomeValue'
168
member_some_value.documenation = ''
169
member_some_value.required_members = []
170
171
member_baz.members = {
172
'SomeValue': member_some_value
173
}
174
175
argument_model2.members = {
176
'ArgumentBaz': member_baz
177
}
178
179
cli_argument1 = mock.Mock(spec=CLIArgument)
180
cli_argument1.argument_model = argument_model1
181
182
cli_argument2 = mock.Mock(spec=CLIArgument)
183
cli_argument2.argument_model = argument_model2
184
185
argument_table = {
186
'original-argument': cli_argument1,
187
'another-original-argument': cli_argument2
188
}
189
190
# Create the flattened argument table
191
cli = mock.Mock()
192
flatten = FlattenArguments('my-service', FLATTEN_CONFIG)
193
flatten.flatten_args(command, argument_table)
194
195
# Make sure new arguments and any with keep=True are there
196
self.assertIn('foo', argument_table)
197
self.assertIn('bar', argument_table)
198
self.assertNotIn('original-argument', argument_table)
199
self.assertIn('baz', argument_table)
200
self.assertIn('another-original-argument', argument_table)
201
202
# Make sure the new arguments are the class we expect
203
self.assertIsInstance(argument_table['foo'], FlattenedArgument)
204
self.assertIsInstance(argument_table['bar'], FlattenedArgument)
205
self.assertIsInstance(argument_table['baz'], FlattenedArgument)
206
self.assertNotIsInstance(argument_table['another-original-argument'],
207
FlattenedArgument)
208
209
# Make sure original required trait can be overridden
210
self.assertEqual(False, argument_table['foo'].required)
211
self.assertEqual(True, argument_table['bar'].required)
212
213
# Make sure docs can be overridden and get the defaults
214
self.assertEqual('Original docs', argument_table['foo'].documentation)
215
self.assertEqual('Some help text', argument_table['bar'].documentation)
216
217