-
Notifications
You must be signed in to change notification settings - Fork 86
Expand file tree
/
Copy pathtest_placeholders.py
More file actions
366 lines (294 loc) · 10.7 KB
/
Copy pathtest_placeholders.py
File metadata and controls
366 lines (294 loc) · 10.7 KB
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
# Copyright 2019 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://www.apache.org/licenses/LICENSE-2.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 __future__ import absolute_import
import pytest
import json
from stepfunctions.inputs import ExecutionInput, MapItemIndex, MapItemValue, StepInput
def test_placeholder_creation_with_subscript_operator():
step_input = StepInput()
placeholder_variable = step_input["A"]
assert placeholder_variable.name == "A"
assert placeholder_variable.type is None
def test_placeholder_creation_with_type():
workflow_input = ExecutionInput()
placeholder_variable = workflow_input["A"]["b"].get("C", float)
assert placeholder_variable.name == "C"
assert placeholder_variable.type == float
def test_placeholder_creation_with_int_key():
workflow_input = ExecutionInput()
placeholder_variable = workflow_input["A"][0]
assert placeholder_variable.name == 0
assert placeholder_variable.type == None
def test_placeholder_creation_with_invalid_key():
step_input = StepInput()
with pytest.raises(ValueError):
step_input["A"][1.3]
with pytest.raises(ValueError):
step_input["A"].get(1.2, str)
def test_placeholder_creation_failure_with_type():
workflow_input = ExecutionInput()
placeholder_variable = workflow_input["A"]["b"].get("C", float)
with pytest.raises(ValueError):
workflow_input["A"]["b"].get("C", int)
def test_placeholder_path():
workflow_input = ExecutionInput()
placeholder_variable = workflow_input["A"]["b"]["C"]
expected_path = ["A", "b", "C"]
assert placeholder_variable._get_path() == expected_path
def test_placeholder_contains():
step_input = StepInput()
var_one = step_input["Key01"]
var_two = step_input["Key02"]["Key03"]
var_three = step_input["Key01"]["Key04"]
var_four = step_input["Key05"]
step_input_two = StepInput()
var_five = step_input_two["Key07"]
assert step_input.contains(var_three) == True
assert step_input.contains(var_five) == False
assert step_input_two.contains(var_three) == False
def test_placeholder_schema_as_dict():
workflow_input = ExecutionInput()
workflow_input["A"]["b"].get("C", float)
workflow_input["Message"]
workflow_input["Key01"]["Key02"]
workflow_input["Key03"]
workflow_input["Key03"]["Key04"]
expected_schema = {
"A": {
"b": {
"C": float
}
},
"Message": str,
"Key01": {
"Key02": str
},
"Key03": {
"Key04": str
}
}
assert workflow_input.get_schema_as_dict() == expected_schema
def test_placeholder_schema_as_json():
step_input = StepInput()
step_input["Response"].get("StatusCode", int)
step_input["Hello"]["World"]
step_input["A"]
step_input["Hello"]["World"].get("Test", str)
expected_schema = {
"Response": {
"StatusCode": "int"
},
"Hello": {
"World": {
"Test": "str"
}
},
"A": "str"
}
assert step_input.get_schema_as_json() == json.dumps(expected_schema)
def test_placeholder_is_empty():
workflow_input = ExecutionInput()
placeholder_variable = workflow_input["A"]["B"]["C"]
assert placeholder_variable._is_empty() == True
workflow_input["A"]["B"]["C"]["D"]
assert placeholder_variable._is_empty() == False
def test_placeholder_make_immutable():
workflow_input = ExecutionInput()
workflow_input["A"]["b"].get("C", float)
workflow_input["Message"]
workflow_input["Key01"]["Key02"]
workflow_input["Key03"]
workflow_input["Key03"]["Key04"]
assert check_immutable(workflow_input) == False
workflow_input._make_immutable()
assert check_immutable(workflow_input) == True
def test_placeholder_with_schema():
test_schema = {
"A": {
"B":{
"C": int
}
},
"Request": {
"Status": str
},
"Hello": float
}
workflow_input = ExecutionInput(schema=test_schema)
assert workflow_input.get_schema_as_dict() == test_schema
assert workflow_input.immutable == True
with pytest.raises(ValueError):
workflow_input["A"]["B"]["D"]
with pytest.raises(ValueError):
workflow_input["A"]["B"].get("C", float)
def test_workflow_input_jsonpath():
workflow_input = ExecutionInput()
placeholder_variable = workflow_input["A"]["b"].get("C", float)
assert placeholder_variable.to_jsonpath() == "$$.Execution.Input['A']['b']['C']"
def test_step_input_jsonpath():
step_input = StepInput()
placeholder_variable = step_input["A"]["b"].get(0, float)
assert placeholder_variable.to_jsonpath() == "$['A']['b'][0]"
# UTILS
def check_immutable(placeholder):
if placeholder.immutable is True:
if placeholder._is_empty():
return True
else:
for k, v in placeholder.store.items():
return check_immutable(v)
else:
return False
def test_map_item_value_creation_with_subscript_operator():
map_item_placeholder = MapItemValue()
map_item_placeholder = map_item_placeholder["A"]
assert map_item_placeholder.name == "A"
assert map_item_placeholder.type is None
def test_map_item_index_creation_with_subscript_operator():
map_item_placeholder = MapItemIndex()
with pytest.raises(AttributeError):
map_item_placeholder["A"]
assert not map_item_placeholder.get_schema_as_dict()
assert not map_item_placeholder.immutable
def test_map_item_value_creation_with_type():
map_item_placeholder = MapItemValue()
map_item_variable = map_item_placeholder["A"]["b"].get("C", float)
assert map_item_variable.name == "C"
assert map_item_variable.type == float
def test_map_item_value_creation_with_int_key():
map_item_placeholder = MapItemValue()
map_item_variable = map_item_placeholder["A"][0]
assert map_item_variable.name == 0
assert map_item_variable.type is None
def test_map_item_value_creation_with_invalid_key():
map_item_placeholder = MapItemValue()
with pytest.raises(ValueError):
map_item_placeholder["A"][1.3]
with pytest.raises(ValueError):
map_item_placeholder["A"].get(1.2, str)
def test_map_item_value_creation_failure_with_type():
map_item_placeholder = MapItemValue()
map_item_placeholder["A"]["b"].get("C", float)
with pytest.raises(ValueError):
map_item_placeholder["A"]["b"].get("C", int)
def test_map_item_value_path():
map_item_placeholder = MapItemValue()
placeholder_variable = map_item_placeholder["A"]["b"]["C"]
expected_path = ["A", "b", "C"]
assert placeholder_variable._get_path() == expected_path
def test_map_item_value_contains():
map_item_placeholder = MapItemValue()
var_three = map_item_placeholder["Key01"]["Key04"]
map_item_placeholder_two = StepInput()
var_five = map_item_placeholder_two["Key07"]
assert map_item_placeholder.contains(var_three) == True
assert map_item_placeholder.contains(var_five) == False
assert map_item_placeholder_two.contains(var_three) == False
assert map_item_placeholder_two.contains(var_five) == True
def test_map_item_value_schema_as_dict():
map_item_placeholder = MapItemValue()
map_item_placeholder["A"]["b"].get("C", float)
map_item_placeholder["Message"]
map_item_placeholder["Key01"]["Key02"]
map_item_placeholder["Key03"]
map_item_placeholder["Key03"]["Key04"]
expected_schema = {
"A": {
"b": {
"C": float
}
},
"Message": str,
"Key01": {
"Key02": str
},
"Key03": {
"Key04": str
}
}
assert map_item_placeholder.get_schema_as_dict() == expected_schema
def test_map_item_value_schema_as_json():
map_item_placeholder = MapItemValue()
map_item_placeholder["Response"].get("StatusCode", int)
map_item_placeholder["Hello"]["World"]
map_item_placeholder["A"]
map_item_placeholder["Hello"]["World"].get("Test", str)
expected_schema = {
"Response": {
"StatusCode": "int"
},
"Hello": {
"World": {
"Test": "str"
}
},
"A": "str"
}
assert map_item_placeholder.get_schema_as_json() == json.dumps(expected_schema)
def test_map_item_value_is_empty():
workflow_input = MapItemValue()
placeholder_variable = workflow_input["A"]["B"]["C"]
assert placeholder_variable._is_empty() == True
workflow_input["A"]["B"]["C"]["D"]
assert placeholder_variable._is_empty() == False
def test_map_item_value_make_immutable():
workflow_input = MapItemValue()
workflow_input["A"]["b"].get("C", float)
workflow_input["Message"]
workflow_input["Key01"]["Key02"]
workflow_input["Key03"]
workflow_input["Key03"]["Key04"]
assert check_immutable(workflow_input) == False
workflow_input._make_immutable()
assert check_immutable(workflow_input) == True
def test_map_item_value_with_schema():
test_schema = {
"A": {
"B": {
"C": int
}
},
"Request": {
"Status": str
},
"Hello": float
}
workflow_input = MapItemValue(schema=test_schema)
assert workflow_input.get_schema_as_dict() == test_schema
assert workflow_input.immutable == True
assert workflow_input['A']['B'].get("C", int)
with pytest.raises(ValueError):
workflow_input["A"]["B"]["D"]
with pytest.raises(ValueError):
workflow_input["A"]["B"].get("C", float)
def test_map_item_index_with_schema():
test_schema = {
"A": {
"B": {
"C": int
}
},
"Request": {
"Status": str
},
"Hello": float
}
with pytest.raises(AttributeError):
workflow_input = MapItemIndex(schema=test_schema)
def test_map_item_value_jsonpath():
map_item_value = MapItemValue()
map_item_value_variable = map_item_value["A"]["b"].get(0, float)
assert map_item_value_variable.to_jsonpath() == "$$.Map.Item.Value['A']['b'][0]"
def test_map_item_index_jsonpath():
map_item_index = MapItemIndex()
assert map_item_index.to_jsonpath() == "$$.Map.Item.Index"