File: test_model.py

package info (click to toggle)
python-botocore 1.37.9%2Brepack-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 121,768 kB
  • sloc: python: 73,696; xml: 14,880; javascript: 181; makefile: 155
file content (1221 lines) | stat: -rw-r--r-- 48,316 bytes parent folder | download
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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
import pytest

from botocore import model
from botocore.compat import OrderedDict
from tests import unittest


@pytest.mark.parametrize("property_name", ['api_version', 'protocol'])
def test_missing_model_attribute_raises_exception(property_name):
    service_model = model.ServiceModel({'metadata': {'endpointPrefix': 'foo'}})
    with pytest.raises(model.UndefinedModelAttributeError):
        getattr(service_model, property_name)


class TestServiceId(unittest.TestCase):
    def test_hypenize_replaces_spaces(self):
        self.assertEqual(
            model.ServiceId('my service').hyphenize(), 'my-service'
        )

    def test_hyphenize_lower_cases(self):
        self.assertEqual(model.ServiceId('MyService').hyphenize(), 'myservice')


class TestServiceModel(unittest.TestCase):
    def setUp(self):
        self.model = {
            'metadata': {
                'protocol': 'query',
                'endpointPrefix': 'endpoint-prefix',
                'serviceId': 'MyService',
            },
            'documentation': 'Documentation value',
            'operations': {},
            'shapes': {'StringShape': {'type': 'string'}},
        }
        self.error_shapes = {
            'ExceptionOne': {
                'exception': True,
                'type': 'structure',
                'members': {},
            },
            'ExceptionTwo': {
                'exception': True,
                'type': 'structure',
                'members': {},
                'error': {'code': 'FooCode'},
            },
        }
        self.service_model = model.ServiceModel(self.model)

    def test_metadata_available(self):
        # You should be able to access the metadata in a service description
        # through the service model object.
        self.assertEqual(self.service_model.metadata.get('protocol'), 'query')

    def test_service_name_can_be_overriden(self):
        service_model = model.ServiceModel(
            self.model, service_name='myservice'
        )
        self.assertEqual(service_model.service_name, 'myservice')

    def test_service_name_defaults_to_endpoint_prefix(self):
        self.assertEqual(self.service_model.service_name, 'endpoint-prefix')

    def test_service_id(self):
        self.assertEqual(self.service_model.service_id, 'MyService')

    def test_hyphenize_service_id(self):
        self.assertEqual(
            self.service_model.service_id.hyphenize(), 'myservice'
        )

    def test_service_id_does_not_exist(self):
        service_model = {
            'metadata': {
                'protocol': 'query',
                'endpointPrefix': 'endpoint-prefix',
            },
            'documentation': 'Documentation value',
            'operations': {},
            'shapes': {'StringShape': {'type': 'string'}},
        }
        service_name = 'myservice'
        service_model = model.ServiceModel(service_model, service_name)
        with self.assertRaisesRegex(
            model.UndefinedModelAttributeError, service_name
        ):
            service_model.service_id

    def test_operation_does_not_exist(self):
        with self.assertRaises(model.OperationNotFoundError):
            self.service_model.operation_model('NoExistOperation')

    def test_signing_name_defaults_to_endpoint_prefix(self):
        self.assertEqual(self.service_model.signing_name, 'endpoint-prefix')

    def test_documentation_exposed_as_property(self):
        self.assertEqual(
            self.service_model.documentation, 'Documentation value'
        )

    def test_shape_names(self):
        self.assertEqual(self.service_model.shape_names, ['StringShape'])

    def test_repr_has_service_name(self):
        self.assertEqual(
            repr(self.service_model), 'ServiceModel(endpoint-prefix)'
        )

    def test_shape_for_error_code(self):
        self.model['shapes'].update(self.error_shapes)
        self.service_model = model.ServiceModel(self.model)
        shape = self.service_model.shape_for_error_code('ExceptionOne')
        self.assertEqual(shape.name, 'ExceptionOne')
        shape = self.service_model.shape_for_error_code('FooCode')
        self.assertEqual(shape.name, 'ExceptionTwo')

    def test_error_shapes(self):
        self.model['shapes'].update(self.error_shapes)
        self.service_model = model.ServiceModel(self.model)
        error_shapes = self.service_model.error_shapes
        error_shape_names = [shape.name for shape in error_shapes]
        self.assertEqual(len(error_shape_names), 2)
        self.assertIn('ExceptionOne', error_shape_names)
        self.assertIn('ExceptionTwo', error_shape_names)

    def test_client_context_params(self):
        service_model = model.ServiceModel(
            {
                'metadata': {
                    'protocol': 'query',
                    'endpointPrefix': 'endpoint-prefix',
                    'serviceId': 'MyServiceWithClientContextParams',
                },
                'documentation': 'Documentation value',
                'operations': {},
                'shapes': {},
                'clientContextParams': {
                    'stringClientContextParam': {
                        'type': 'string',
                        'documentation': 'str-valued',
                    },
                    'booleanClientContextParam': {
                        'type': 'boolean',
                        'documentation': 'bool-valued',
                    },
                },
            }
        )
        self.assertEqual(len(service_model.client_context_parameters), 2)
        client_ctx_param1 = service_model.client_context_parameters[0]
        client_ctx_param2 = service_model.client_context_parameters[1]
        self.assertEqual(client_ctx_param1.name, 'stringClientContextParam')
        self.assertEqual(client_ctx_param1.type, 'string')
        self.assertEqual(client_ctx_param1.documentation, 'str-valued')
        self.assertEqual(client_ctx_param2.name, 'booleanClientContextParam')

    def test_client_context_params_absent(self):
        self.assertIsInstance(
            self.service_model.client_context_parameters, list
        )
        self.assertEqual(len(self.service_model.client_context_parameters), 0)


class TestOperationModelFromService(unittest.TestCase):
    def setUp(self):
        self.model = {
            'metadata': {'protocol': 'query', 'endpointPrefix': 'foo'},
            'documentation': '',
            'operations': {
                'OperationName': {
                    'http': {
                        'method': 'POST',
                        'requestUri': '/',
                    },
                    'name': 'OperationName',
                    'input': {'shape': 'OperationNameRequest'},
                    'output': {
                        'shape': 'OperationNameResponse',
                    },
                    'errors': [{'shape': 'NoSuchResourceException'}],
                    'documentation': 'Docs for OperationName',
                    'authtype': 'v4',
                    'auth': ['aws.auth#sigv4'],
                },
                'OperationTwo': {
                    'http': {
                        'method': 'POST',
                        'requestUri': '/',
                    },
                    'name': 'OperationTwo',
                    'input': {'shape': 'OperationNameRequest'},
                    'output': {
                        'shape': 'OperationNameResponse',
                    },
                    'errors': [{'shape': 'NoSuchResourceException'}],
                    'documentation': 'Docs for OperationTwo',
                },
                'PayloadOperation': {
                    'http': {
                        'method': 'POST',
                        'requestUri': '/',
                    },
                    'name': 'PayloadOperation',
                    'input': {'shape': 'PayloadOperationRequest'},
                    'output': {
                        'shape': 'PayloadOperationResponse',
                    },
                    'errors': [{'shape': 'NoSuchResourceException'}],
                    'documentation': 'Docs for PayloadOperation',
                    'requestcompression': {'encodings': ['gzip']},
                },
                'NoBodyOperation': {
                    'http': {
                        'method': 'GET',
                        'requestUri': '/',
                    },
                    'name': 'NoBodyOperation',
                    'input': {'shape': 'NoBodyOperationRequest'},
                    'output': {
                        'shape': 'OperationNameResponse',
                    },
                    'errors': [{'shape': 'NoSuchResourceException'}],
                    'documentation': 'Docs for NoBodyOperation',
                },
                'ContextParamOperation': {
                    'http': {
                        'method': 'POST',
                        'requestUri': '/',
                    },
                    'name': 'ContextParamOperation',
                    'input': {'shape': 'ContextParamOperationRequest'},
                    'output': {'shape': 'OperationNameResponse'},
                    'errors': [{'shape': 'NoSuchResourceException'}],
                    'documentation': 'Docs for ContextParamOperation',
                    'staticContextParams': {
                        'stringStaticContextParam': {
                            'value': 'Static Context Param Value',
                        },
                        'booleanStaticContextParam': {
                            'value': True,
                        },
                    },
                },
            },
            'shapes': {
                'OperationNameRequest': {
                    'type': 'structure',
                    'members': {
                        'Arg1': {'shape': 'stringType'},
                        'Arg2': {'shape': 'stringType'},
                    },
                },
                'OperationNameResponse': {
                    'type': 'structure',
                    'members': {
                        'String': {
                            'shape': 'stringType',
                        }
                    },
                },
                'PayloadOperationRequest': {
                    'type': 'structure',
                    'members': {
                        'Arg1': {'shape': 'TestConfig'},
                        'Arg2': {'shape': 'stringType'},
                    },
                    'payload': 'Arg1',
                },
                'PayloadOperationResponse': {
                    'type': 'structure',
                    'members': {
                        'String': {
                            'shape': 'stringType',
                        }
                    },
                    'payload': 'String',
                },
                'NoBodyOperationRequest': {
                    'type': 'structure',
                    'members': {
                        'data': {
                            'location': 'header',
                            'locationName': 'x-amz-data',
                            'shape': 'stringType',
                        }
                    },
                },
                'ContextParamOperationRequest': {
                    'type': 'structure',
                    'members': {
                        'ContextParamArg': {
                            'shape': 'stringType',
                            'contextParam': {
                                'name': 'contextParamName',
                            },
                        }
                    },
                    'payload': 'ContextParamArg',
                },
                'NoSuchResourceException': {
                    'type': 'structure',
                    'members': {},
                },
                'stringType': {
                    'type': 'string',
                },
                'TestConfig': {
                    'type': 'structure',
                    'members': {'timeout': {'shape': 'stringType'}},
                },
            },
        }
        self.service_model = model.ServiceModel(self.model)

    def test_wire_name_always_matches_model(self):
        service_model = model.ServiceModel(self.model)
        operation = model.OperationModel(
            self.model['operations']['OperationName'], service_model, 'Foo'
        )
        self.assertEqual(operation.name, 'Foo')
        self.assertEqual(operation.wire_name, 'OperationName')

    def test_operation_name_in_repr(self):
        service_model = model.ServiceModel(self.model)
        operation = service_model.operation_model('OperationName')
        self.assertIn('OperationName', repr(operation))

    def test_name_and_wire_name_defaults_to_same_value(self):
        service_model = model.ServiceModel(self.model)
        operation = model.OperationModel(
            self.model['operations']['OperationName'], service_model
        )
        self.assertEqual(operation.name, 'OperationName')
        self.assertEqual(operation.wire_name, 'OperationName')

    def test_name_from_service(self):
        service_model = model.ServiceModel(self.model)
        operation = service_model.operation_model('OperationName')
        self.assertEqual(operation.name, 'OperationName')

    def test_name_from_service_model_when_differs_from_name(self):
        self.model['operations']['Foo'] = self.model['operations'][
            'OperationName'
        ]
        service_model = model.ServiceModel(self.model)
        operation = service_model.operation_model('Foo')
        self.assertEqual(operation.name, 'Foo')

    def test_operation_input_model(self):
        service_model = model.ServiceModel(self.model)
        operation = service_model.operation_model('OperationName')
        self.assertEqual(operation.name, 'OperationName')
        # Operations should also have a reference to the top level metadata.
        self.assertEqual(operation.metadata['protocol'], 'query')
        self.assertEqual(operation.http['method'], 'POST')
        self.assertEqual(operation.http['requestUri'], '/')
        shape = operation.input_shape
        self.assertEqual(shape.name, 'OperationNameRequest')
        self.assertEqual(list(sorted(shape.members)), ['Arg1', 'Arg2'])

    def test_has_documentation_property(self):
        service_model = model.ServiceModel(self.model)
        operation = service_model.operation_model('OperationName')
        self.assertEqual(operation.documentation, 'Docs for OperationName')

    def test_service_model_available_from_operation_model(self):
        service_model = model.ServiceModel(self.model)
        operation = service_model.operation_model('OperationName')
        # This is an identity comparison because we don't implement
        # __eq__, so we may need to change this in the future.
        self.assertEqual(operation.service_model, service_model)

    def test_operation_output_model(self):
        service_model = model.ServiceModel(self.model)
        operation = service_model.operation_model('OperationName')
        output = operation.output_shape
        self.assertEqual(list(output.members), ['String'])
        self.assertFalse(operation.has_streaming_output)

    def test_operation_shape_not_required(self):
        # It's ok if there's no output shape. We'll just get a return value of
        # None.
        del self.model['operations']['OperationName']['output']
        service_model = model.ServiceModel(self.model)
        operation = service_model.operation_model('OperationName')
        output_shape = operation.output_shape
        self.assertIsNone(output_shape)

    def test_error_shapes(self):
        service_model = model.ServiceModel(self.model)
        operation = service_model.operation_model('OperationName')
        # OperationName only has a NoSuchResourceException
        self.assertEqual(len(operation.error_shapes), 1)
        self.assertEqual(
            operation.error_shapes[0].name, 'NoSuchResourceException'
        )

    def test_has_auth(self):
        operation = self.service_model.operation_model('OperationName')
        self.assertEqual(operation.auth, ["aws.auth#sigv4"])

    def test_auth_not_set(self):
        operation = self.service_model.operation_model('OperationTwo')
        self.assertIsNone(operation.auth)

    def test_has_resolved_auth_type(self):
        operation = self.service_model.operation_model('OperationName')
        self.assertEqual(operation.resolved_auth_type, 'v4')

    def test_resolved_auth_type_not_set(self):
        operation = self.service_model.operation_model('OperationTwo')
        self.assertIsNone(operation.resolved_auth_type)

    def test_has_auth_type(self):
        operation = self.service_model.operation_model('OperationName')
        self.assertEqual(operation.auth_type, 'v4')

    def test_auth_type_not_set(self):
        operation = self.service_model.operation_model('OperationTwo')
        self.assertIsNone(operation.auth_type)

    def test_deprecated_present(self):
        self.model['operations']['OperationName']['deprecated'] = True
        service_model = model.ServiceModel(self.model)
        operation_name = service_model.operation_model('OperationName')
        self.assertTrue(operation_name.deprecated)

    def test_deprecated_present_false(self):
        self.model['operations']['OperationName']['deprecated'] = False
        service_model = model.ServiceModel(self.model)
        operation_name = service_model.operation_model('OperationName')
        self.assertFalse(operation_name.deprecated)

    def test_deprecated_absent(self):
        service_model = model.ServiceModel(self.model)
        operation_two = service_model.operation_model('OperationTwo')
        self.assertFalse(operation_two.deprecated)

    def test_endpoint_operation_present(self):
        self.model['operations']['OperationName']['endpointoperation'] = True
        service_model = model.ServiceModel(self.model)
        operation_name = service_model.operation_model('OperationName')
        self.assertTrue(operation_name.is_endpoint_discovery_operation)

    def test_endpoint_operation_present_false(self):
        self.model['operations']['OperationName']['endpointoperation'] = False
        service_model = model.ServiceModel(self.model)
        operation_name = service_model.operation_model('OperationName')
        self.assertFalse(operation_name.is_endpoint_discovery_operation)

    def test_endpoint_operation_absent(self):
        operation_two = self.service_model.operation_model('OperationName')
        self.assertFalse(operation_two.is_endpoint_discovery_operation)

    def test_endpoint_discovery_required(self):
        operation = self.model['operations']['OperationName']
        operation['endpointdiscovery'] = {'required': True}
        service_model = model.ServiceModel(self.model)
        self.assertTrue(service_model.endpoint_discovery_required)

    def test_endpoint_discovery_required_false(self):
        self.model['operations']['OperationName']['endpointdiscovery'] = {}
        service_model = model.ServiceModel(self.model)
        self.assertFalse(service_model.endpoint_discovery_required)

    def test_endpoint_discovery_required_no_value(self):
        operation = self.model['operations']['OperationName']
        self.assertTrue(operation.get('endpointdiscovery') is None)
        service_model = model.ServiceModel(self.model)
        self.assertFalse(service_model.endpoint_discovery_required)

    def test_endpoint_discovery_present(self):
        operation = self.model['operations']['OperationName']
        operation['endpointdiscovery'] = {'required': True}
        service_model = model.ServiceModel(self.model)
        operation_name = service_model.operation_model('OperationName')
        self.assertTrue(operation_name.endpoint_discovery.get('required'))

    def test_endpoint_discovery_absent(self):
        operation_name = self.service_model.operation_model('OperationName')
        self.assertIsNone(operation_name.endpoint_discovery)

    def test_http_checksum_absent(self):
        operation_name = self.service_model.operation_model('OperationName')
        self.assertEqual(operation_name.http_checksum, {})

    def test_http_checksum_present(self):
        operation = self.model['operations']['OperationName']
        operation['httpChecksum'] = {
            "requestChecksumRequired": True,
            "requestAlgorithmMember": "ChecksumAlgorithm",
            "requestValidationModeMember": "ChecksumMode",
            "responseAlgorithms": ["crc32", "crc32c", "sha256", "sha1"],
        }
        service_model = model.ServiceModel(self.model)
        operation_model = service_model.operation_model('OperationName')
        http_checksum = operation_model.http_checksum
        self.assertEqual(
            http_checksum["requestChecksumRequired"],
            True,
        )
        self.assertEqual(
            http_checksum["requestAlgorithmMember"],
            "ChecksumAlgorithm",
        )
        self.assertEqual(
            http_checksum["requestValidationModeMember"],
            "ChecksumMode",
        )
        self.assertEqual(
            http_checksum["responseAlgorithms"],
            ["crc32", "crc32c", "sha256", "sha1"],
        )

    def test_context_parameter_present(self):
        service_model = model.ServiceModel(self.model)
        operation = service_model.operation_model('ContextParamOperation')
        self.assertEqual(len(operation.context_parameters), 1)
        context_param = operation.context_parameters[0]
        self.assertEqual(context_param.name, 'contextParamName')
        self.assertEqual(context_param.member_name, 'ContextParamArg')

    def test_context_parameter_absent(self):
        service_model = model.ServiceModel(self.model)
        operation = service_model.operation_model('OperationTwo')
        self.assertIsInstance(operation.context_parameters, list)
        self.assertEqual(len(operation.context_parameters), 0)

    def test_static_context_parameter_present(self):
        service_model = model.ServiceModel(self.model)
        operation = service_model.operation_model('ContextParamOperation')
        self.assertEqual(len(operation.static_context_parameters), 2)
        static_ctx_param1 = operation.static_context_parameters[0]
        static_ctx_param2 = operation.static_context_parameters[1]
        self.assertEqual(static_ctx_param1.name, 'stringStaticContextParam')
        self.assertEqual(static_ctx_param1.value, 'Static Context Param Value')
        self.assertEqual(static_ctx_param2.name, 'booleanStaticContextParam')
        self.assertEqual(static_ctx_param2.value, True)

    def test_static_context_parameter_absent(self):
        service_model = model.ServiceModel(self.model)
        operation = service_model.operation_model('OperationTwo')
        self.assertIsInstance(operation.static_context_parameters, list)
        self.assertEqual(len(operation.static_context_parameters), 0)

    def test_request_compression(self):
        service_model = model.ServiceModel(self.model)
        operation = service_model.operation_model('PayloadOperation')
        self.assertEqual(
            operation.request_compression, {'encodings': ['gzip']}
        )


class TestOperationModelEventStreamTypes(unittest.TestCase):
    def setUp(self):
        super().setUp()
        self.model = {
            'metadata': {'protocol': 'rest-xml', 'endpointPrefix': 'foo'},
            'documentation': '',
            'operations': {
                'OperationName': {
                    'http': {
                        'method': 'POST',
                        'requestUri': '/',
                    },
                    'name': 'OperationName',
                    'input': {'shape': 'OperationRequest'},
                    'output': {'shape': 'OperationResponse'},
                }
            },
            'shapes': {
                'NormalStructure': {
                    'type': 'structure',
                    'members': {'Input': {'shape': 'StringType'}},
                },
                'OperationRequest': {
                    'type': 'structure',
                    'members': {
                        'String': {'shape': 'StringType'},
                        "Body": {'shape': 'EventStreamStructure'},
                    },
                    'payload': 'Body',
                },
                'OperationResponse': {
                    'type': 'structure',
                    'members': {
                        'String': {'shape': 'StringType'},
                        "Body": {'shape': 'EventStreamStructure'},
                    },
                    'payload': 'Body',
                },
                'StringType': {'type': 'string'},
                'BlobType': {'type': 'blob'},
                'EventStreamStructure': {
                    'eventstream': True,
                    'type': 'structure',
                    'members': {
                        'EventA': {'shape': 'EventAStructure'},
                        'EventB': {'shape': 'EventBStructure'},
                    },
                },
                'EventAStructure': {
                    'event': True,
                    'type': 'structure',
                    'members': {
                        'Payload': {'shape': 'BlobType', 'eventpayload': True},
                        'Header': {'shape': 'StringType', 'eventheader': True},
                    },
                },
                'EventBStructure': {
                    'event': True,
                    'type': 'structure',
                    'members': {'Records': {'shape': 'StringType'}},
                },
            },
        }

    def update_operation(self, **kwargs):
        operation = self.model['operations']['OperationName']
        operation.update(kwargs)

    def test_event_stream_input_for_operation(self):
        service_model = model.ServiceModel(self.model)
        operation = service_model.operation_model('OperationName')
        self.assertTrue(operation.has_event_stream_input)
        event_stream_input = operation.get_event_stream_input()
        self.assertEqual(event_stream_input.name, 'EventStreamStructure')

    def test_no_event_stream_input_for_operation(self):
        self.update_operation(input={'shape': 'NormalStructure'})
        service_model = model.ServiceModel(self.model)
        operation = service_model.operation_model('OperationName')
        self.assertFalse(operation.has_event_stream_input)
        self.assertEqual(operation.get_event_stream_input(), None)

    def test_event_stream_output_for_operation(self):
        service_model = model.ServiceModel(self.model)
        operation = service_model.operation_model('OperationName')
        self.assertTrue(operation.has_event_stream_output)
        output = operation.get_event_stream_output()
        self.assertEqual(output.name, 'EventStreamStructure')

    def test_no_event_stream_output_for_operation(self):
        self.update_operation(output={'shape': 'NormalStructure'})
        service_model = model.ServiceModel(self.model)
        operation = service_model.operation_model('OperationName')
        self.assertFalse(operation.has_event_stream_output)
        self.assertEqual(operation.get_event_stream_output(), None)

    def test_no_output_shape(self):
        self.update_operation(output=None)
        del self.model['operations']['OperationName']['output']
        service_model = model.ServiceModel(self.model)
        operation = service_model.operation_model('OperationName')
        self.assertFalse(operation.has_event_stream_output)
        self.assertEqual(operation.get_event_stream_output(), None)


class TestOperationModelStreamingTypes(unittest.TestCase):
    def setUp(self):
        super().setUp()
        self.model = {
            'metadata': {'protocol': 'query', 'endpointPrefix': 'foo'},
            'documentation': '',
            'operations': {
                'OperationName': {
                    'name': 'OperationName',
                    'input': {
                        'shape': 'OperationRequest',
                    },
                    'output': {
                        'shape': 'OperationResponse',
                    },
                }
            },
            'shapes': {
                'OperationRequest': {
                    'type': 'structure',
                    'members': {
                        'String': {
                            'shape': 'stringType',
                        },
                        "Body": {
                            'shape': 'blobType',
                        },
                    },
                    'payload': 'Body',
                },
                'OperationResponse': {
                    'type': 'structure',
                    'members': {
                        'String': {
                            'shape': 'stringType',
                        },
                        "Body": {
                            'shape': 'blobType',
                        },
                    },
                    'payload': 'Body',
                },
                'stringType': {
                    'type': 'string',
                },
                'blobType': {'type': 'blob'},
            },
        }

    def remove_payload(self, type):
        self.model['shapes']['Operation' + type].pop('payload')

    def test_streaming_input_for_operation(self):
        service_model = model.ServiceModel(self.model)
        operation = service_model.operation_model('OperationName')
        self.assertTrue(operation.has_streaming_input)
        self.assertEqual(operation.get_streaming_input().name, 'blobType')

    def test_not_streaming_input_for_operation(self):
        self.remove_payload('Request')
        service_model = model.ServiceModel(self.model)
        operation = service_model.operation_model('OperationName')
        self.assertFalse(operation.has_streaming_input)
        self.assertEqual(operation.get_streaming_input(), None)

    def test_streaming_output_for_operation(self):
        service_model = model.ServiceModel(self.model)
        operation = service_model.operation_model('OperationName')
        self.assertTrue(operation.has_streaming_output)
        self.assertEqual(operation.get_streaming_output().name, 'blobType')

    def test_not_streaming_output_for_operation(self):
        self.remove_payload('Response')
        service_model = model.ServiceModel(self.model)
        operation = service_model.operation_model('OperationName')
        self.assertFalse(operation.has_streaming_output)
        self.assertEqual(operation.get_streaming_output(), None)


class TestDeepMerge(unittest.TestCase):
    def setUp(self):
        self.shapes = {
            'SetQueueAttributes': {
                'type': 'structure',
                'members': {
                    'MapExample': {
                        'shape': 'StrToStrMap',
                        'locationName': 'Attribute',
                    },
                },
            },
            'SetQueueAttributes2': {
                'type': 'structure',
                'members': {
                    'MapExample': {
                        'shape': 'StrToStrMap',
                        'locationName': 'Attribute2',
                    },
                },
            },
            'StrToStrMap': {
                'type': 'map',
                'key': {'shape': 'StringType', 'locationName': 'Name'},
                'value': {'shape': 'StringType', 'locationName': 'Value'},
                'flattened': True,
                'name': 'NotAttribute',
            },
            'StringType': {'type': 'string'},
        }
        self.shape_resolver = model.ShapeResolver(self.shapes)

    def test_deep_merge(self):
        shape = self.shape_resolver.get_shape_by_name('SetQueueAttributes')
        map_merged = shape.members['MapExample']
        # map_merged has a serialization as a member trait as well as
        # in the StrToStrMap.
        # The member trait should have precedence.
        self.assertEqual(
            map_merged.serialization,
            # member beats the definition.
            {
                'name': 'Attribute',
                # From the definition.
                'flattened': True,
            },
        )
        # Ensure we don't merge/mutate the original dicts.
        self.assertEqual(map_merged.key.serialization['name'], 'Name')
        self.assertEqual(map_merged.value.serialization['name'], 'Value')
        self.assertEqual(map_merged.key.serialization['name'], 'Name')

    def test_merges_copy_dict(self):
        shape = self.shape_resolver.get_shape_by_name('SetQueueAttributes')
        map_merged = shape.members['MapExample']
        self.assertEqual(map_merged.serialization.get('name'), 'Attribute')

        shape2 = self.shape_resolver.get_shape_by_name('SetQueueAttributes2')
        map_merged2 = shape2.members['MapExample']
        self.assertEqual(map_merged2.serialization.get('name'), 'Attribute2')


class TestShapeResolver(unittest.TestCase):
    def test_get_shape_by_name(self):
        shape_map = {
            'Foo': {
                'type': 'structure',
                'members': {
                    'Bar': {'shape': 'StringType'},
                    'Baz': {'shape': 'StringType'},
                },
            },
            "StringType": {"type": "string"},
        }
        resolver = model.ShapeResolver(shape_map)
        shape = resolver.get_shape_by_name('Foo')
        self.assertEqual(shape.name, 'Foo')
        self.assertEqual(shape.type_name, 'structure')

    def test_resolve_shape_reference(self):
        shape_map = {
            'Foo': {
                'type': 'structure',
                'members': {
                    'Bar': {'shape': 'StringType'},
                    'Baz': {'shape': 'StringType'},
                },
            },
            "StringType": {"type": "string"},
        }
        resolver = model.ShapeResolver(shape_map)
        shape = resolver.resolve_shape_ref({'shape': 'StringType'})
        self.assertEqual(shape.name, 'StringType')
        self.assertEqual(shape.type_name, 'string')

    def test_resolve_shape_references_with_member_traits(self):
        shape_map = {
            'Foo': {
                'type': 'structure',
                'members': {
                    'Bar': {'shape': 'StringType'},
                    'Baz': {'shape': 'StringType', 'locationName': 'other'},
                },
            },
            "StringType": {"type": "string"},
        }
        resolver = model.ShapeResolver(shape_map)
        shape = resolver.resolve_shape_ref(
            {'shape': 'StringType', 'locationName': 'other'}
        )
        self.assertEqual(shape.serialization['name'], 'other')
        self.assertEqual(shape.name, 'StringType')

    def test_serialization_cache(self):
        shape_map = {
            'Foo': {
                'type': 'structure',
                'members': {
                    'Baz': {'shape': 'StringType', 'locationName': 'other'},
                },
            },
            "StringType": {"type": "string"},
        }
        resolver = model.ShapeResolver(shape_map)
        shape = resolver.resolve_shape_ref(
            {'shape': 'StringType', 'locationName': 'other'}
        )
        self.assertEqual(shape.serialization['name'], 'other')
        # serialization is computed on demand, and a cache is kept.
        # This is just verifying that trying to access serialization again
        # gives the same result.  We don't actually care that it's cached,
        # we just care that the cache doesn't mess with correctness.
        self.assertEqual(shape.serialization['name'], 'other')

    def test_shape_overrides(self):
        shape_map = {
            "StringType": {
                "type": "string",
                "documentation": "Original documentation",
            }
        }
        resolver = model.ShapeResolver(shape_map)
        shape = resolver.get_shape_by_name('StringType')
        self.assertEqual(shape.documentation, 'Original documentation')

        shape = resolver.resolve_shape_ref(
            {'shape': 'StringType', 'documentation': 'override'}
        )
        self.assertEqual(shape.documentation, 'override')

    def test_shape_type_structure(self):
        shapes = {
            'ChangePasswordRequest': {
                'type': 'structure',
                'members': {
                    'OldPassword': {'shape': 'passwordType'},
                    'NewPassword': {'shape': 'passwordType'},
                },
            },
            'passwordType': {
                "type": "string",
            },
        }
        resolver = model.ShapeResolver(shapes)
        shape = resolver.get_shape_by_name('ChangePasswordRequest')
        self.assertEqual(shape.type_name, 'structure')
        self.assertEqual(shape.name, 'ChangePasswordRequest')
        self.assertEqual(
            list(sorted(shape.members)), ['NewPassword', 'OldPassword']
        )
        self.assertEqual(shape.members['OldPassword'].name, 'passwordType')
        self.assertEqual(shape.members['OldPassword'].type_name, 'string')
        self.assertEqual(shape.error_code, None)

    def test_exception_error_code(self):
        shapes = {
            'FooException': {
                'exception': True,
                'type': 'structure',
                'members': {},
            }
        }
        # Test without explicit error code
        resolver = model.ShapeResolver(shapes)
        shape = resolver.get_shape_by_name('FooException')
        self.assertTrue(shape.metadata['exception'])
        self.assertEqual(shape.error_code, 'FooException')
        # Test with explicit error code
        shapes['FooException']['error'] = {'code': 'ExceptionCode'}
        resolver = model.ShapeResolver(shapes)
        shape = resolver.get_shape_by_name('FooException')
        self.assertTrue(shape.metadata['exception'])
        self.assertEqual(shape.error_code, 'ExceptionCode')

    def test_shape_metadata(self):
        shapes = {
            'ChangePasswordRequest': {
                'type': 'structure',
                'required': ['OldPassword', 'NewPassword'],
                'members': {
                    'OldPassword': {'shape': 'passwordType'},
                    'NewPassword': {'shape': 'passwordType'},
                },
            },
            'passwordType': {
                "type": "string",
                "min": 1,
                "max": 128,
                "pattern": ".*",
                "sensitive": True,
            },
        }
        resolver = model.ShapeResolver(shapes)
        shape = resolver.get_shape_by_name('ChangePasswordRequest')
        self.assertEqual(
            shape.metadata['required'], ['OldPassword', 'NewPassword']
        )
        member = shape.members['OldPassword']
        self.assertEqual(member.metadata['min'], 1)
        self.assertEqual(member.metadata['max'], 128)
        self.assertEqual(member.metadata['pattern'], '.*')
        self.assertEqual(member.metadata['sensitive'], True)

    def test_error_shape_metadata(self):
        shapes = {
            'ResourceNotFoundException': {
                'type': 'structure',
                'members': {
                    'message': {
                        'shape': 'ErrorMessage',
                    }
                },
                'exception': True,
                'retryable': {'throttling': True},
            }
        }
        resolver = model.ShapeResolver(shapes)
        shape = resolver.get_shape_by_name('ResourceNotFoundException')
        self.assertEqual(
            shape.metadata,
            {'exception': True, 'retryable': {'throttling': True}},
        )

    def test_shape_list(self):
        shapes = {
            'mfaDeviceListType': {
                "type": "list",
                "member": {"shape": "MFADevice"},
            },
            'MFADevice': {
                'type': 'structure',
                'members': {'UserName': {'shape': 'userNameType'}},
            },
            'userNameType': {'type': 'string'},
        }
        resolver = model.ShapeResolver(shapes)
        shape = resolver.get_shape_by_name('mfaDeviceListType')
        self.assertEqual(shape.member.type_name, 'structure')
        self.assertEqual(shape.member.name, 'MFADevice')
        self.assertEqual(list(shape.member.members), ['UserName'])

    def test_shape_does_not_exist(self):
        resolver = model.ShapeResolver({})
        with self.assertRaises(model.NoShapeFoundError):
            resolver.get_shape_by_name('NoExistShape')

    def test_missing_type_key(self):
        shapes = {'UnknownType': {'NotTheTypeKey': 'someUnknownType'}}
        resolver = model.ShapeResolver(shapes)
        with self.assertRaises(model.InvalidShapeError):
            resolver.get_shape_by_name('UnknownType')

    def test_bad_shape_ref(self):
        # This is an example of a denormalized model,
        # which should raise an exception.
        shapes = {
            'Struct': {
                'type': 'structure',
                'members': {
                    'A': {'type': 'string'},
                    'B': {'type': 'string'},
                },
            }
        }
        resolver = model.ShapeResolver(shapes)
        with self.assertRaises(model.InvalidShapeReferenceError):
            struct = resolver.get_shape_by_name('Struct')
            # Resolving the members will fail because
            # the 'A' and 'B' members are not shape refs.
            struct.members

    def test_shape_name_in_repr(self):
        shapes = {
            'StringType': {
                'type': 'string',
            }
        }
        resolver = model.ShapeResolver(shapes)
        self.assertIn(
            'StringType', repr(resolver.get_shape_by_name('StringType'))
        )


class TestBuilders(unittest.TestCase):
    def test_structure_shape_builder_with_scalar_types(self):
        b = model.DenormalizedStructureBuilder()
        shape = b.with_members(
            {
                'A': {'type': 'string'},
                'B': {'type': 'integer'},
            }
        ).build_model()
        self.assertIsInstance(shape, model.StructureShape)
        self.assertEqual(sorted(list(shape.members)), ['A', 'B'])
        self.assertEqual(shape.members['A'].type_name, 'string')
        self.assertEqual(shape.members['B'].type_name, 'integer')

    def test_structure_shape_with_structure_type(self):
        b = model.DenormalizedStructureBuilder()
        shape = b.with_members(
            {
                'A': {
                    'type': 'structure',
                    'members': {
                        'A-1': {'type': 'string'},
                    },
                },
            }
        ).build_model()
        self.assertIsInstance(shape, model.StructureShape)
        self.assertEqual(list(shape.members), ['A'])
        self.assertEqual(shape.members['A'].type_name, 'structure')
        self.assertEqual(list(shape.members['A'].members), ['A-1'])

    def test_structure_shape_with_list(self):
        b = model.DenormalizedStructureBuilder()
        shape = b.with_members(
            {
                'A': {'type': 'list', 'member': {'type': 'string'}},
            }
        ).build_model()
        self.assertIsInstance(shape.members['A'], model.ListShape)
        self.assertEqual(shape.members['A'].member.type_name, 'string')

    def test_structure_shape_with_map_type(self):
        b = model.DenormalizedStructureBuilder()
        shape = b.with_members(
            {
                'A': {
                    'type': 'map',
                    'key': {'type': 'string'},
                    'value': {'type': 'string'},
                }
            }
        ).build_model()
        self.assertIsInstance(shape.members['A'], model.MapShape)
        map_shape = shape.members['A']
        self.assertEqual(map_shape.key.type_name, 'string')
        self.assertEqual(map_shape.value.type_name, 'string')

    def test_nested_structure(self):
        b = model.DenormalizedStructureBuilder()
        shape = b.with_members(
            {
                'A': {
                    'type': 'structure',
                    'members': {
                        'B': {
                            'type': 'structure',
                            'members': {
                                'C': {
                                    'type': 'string',
                                }
                            },
                        }
                    },
                }
            }
        ).build_model()
        self.assertEqual(
            shape.members['A'].members['B'].members['C'].type_name, 'string'
        )

    def test_enum_values_on_string_used(self):
        b = model.DenormalizedStructureBuilder()
        enum_values = ['foo', 'bar', 'baz']
        shape = b.with_members(
            {
                'A': {
                    'type': 'string',
                    'enum': enum_values,
                },
            }
        ).build_model()
        self.assertIsInstance(shape, model.StructureShape)
        string_shape = shape.members['A']
        self.assertIsInstance(string_shape, model.StringShape)
        self.assertEqual(string_shape.metadata['enum'], enum_values)
        self.assertEqual(string_shape.enum, enum_values)

    def test_documentation_on_shape_used(self):
        b = model.DenormalizedStructureBuilder()
        shape = b.with_members(
            {
                'A': {
                    'type': 'string',
                    'documentation': 'MyDocs',
                },
            }
        ).build_model()
        self.assertEqual(shape.members['A'].documentation, 'MyDocs')

    def test_min_max_used_in_metadata(self):
        b = model.DenormalizedStructureBuilder()
        shape = b.with_members(
            {
                'A': {
                    'type': 'string',
                    'documentation': 'MyDocs',
                    'min': 2,
                    'max': 3,
                },
            }
        ).build_model()
        metadata = shape.members['A'].metadata
        self.assertEqual(metadata.get('min'), 2)
        self.assertEqual(metadata.get('max'), 3)

    def test_use_shape_name_when_provided(self):
        b = model.DenormalizedStructureBuilder()
        shape = b.with_members(
            {
                'A': {
                    'type': 'string',
                    'shape_name': 'MyStringShape',
                },
            }
        ).build_model()
        self.assertEqual(shape.members['A'].name, 'MyStringShape')

    def test_unknown_shape_type(self):
        b = model.DenormalizedStructureBuilder()
        with self.assertRaises(model.InvalidShapeError):
            b.with_members(
                {
                    'A': {
                        'type': 'brand-new-shape-type',
                    },
                }
            ).build_model()

    def test_ordered_shape_builder(self):
        b = model.DenormalizedStructureBuilder()
        shape = b.with_members(
            OrderedDict(
                [
                    ('A', {'type': 'string'}),
                    (
                        'B',
                        {
                            'type': 'structure',
                            'members': OrderedDict(
                                [
                                    ('C', {'type': 'string'}),
                                    ('D', {'type': 'string'}),
                                ]
                            ),
                        },
                    ),
                ]
            )
        ).build_model()

        # Members should be in order
        self.assertEqual(['A', 'B'], list(shape.members.keys()))

        # Nested structure members should *also* stay ordered
        self.assertEqual(['C', 'D'], list(shape.members['B'].members.keys()))


if __name__ == '__main__':
    unittest.main()