Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commite326aaf

Browse files
Redefine exc within tests
1 parent6236f11 commite326aaf

File tree

1 file changed

+26
-5
lines changed

1 file changed

+26
-5
lines changed

‎tests/unit/aws/handlers/test_service.py‎

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
fromcollections.abcimportIterable
2+
13
importpytest
2-
frommoto.ec2.exceptionsimportInvalidKeyPairNameError
3-
frommoto.ses.exceptionsimportMessageRejectedError
4+
frommoto.core.exceptionsimportRESTError,ServiceException
5+
frommoto.ec2.exceptionsimportEC2_ERROR_RESPONSE
46

57
fromlocalstack.aws.apiimportCommonServiceException,RequestContext
68
fromlocalstack.aws.chainimportHandlerChain
@@ -202,6 +204,10 @@ def capture_original_exception_handler(
202204
asserterr_context.service_exception.status_code==500
203205

204206
deftest_moto_service_exception_is_translated(self,service_response_handler_chain):
207+
# Redefine exception here but use the right base exc. This is to improve tolerance against Moto refactors.
208+
classMessageRejectedError(ServiceException):
209+
code="MessageRejected"
210+
205211
# Ensure ServiceExceptions are translated
206212
context=create_aws_request_context(
207213
"ses",
@@ -225,6 +231,20 @@ def test_moto_service_exception_is_translated(self, service_response_handler_cha
225231
assertcontext.service_exception.status_code==400
226232

227233
deftest_moto_rest_error_is_translated(self,service_response_handler_chain):
234+
# Redefine exception here but use the right base exc. This is to improve tolerance against Moto refactors.
235+
classInvalidKeyPairNameError(RESTError):
236+
code=400
237+
request_id_tag_name="RequestID"
238+
extended_templates= {"custom_response":EC2_ERROR_RESPONSE}
239+
env=RESTError.extended_environment(extended_templates)
240+
241+
def__init__(self,key:Iterable[str]):
242+
super().__init__(
243+
"InvalidKeyPair.NotFound",
244+
f"The keypair '{key}' does not exist.",
245+
template="custom_response",
246+
)
247+
228248
# Ensure RESTErrors are translated
229249
context=create_aws_request_context(
230250
"ec2",
@@ -238,12 +258,13 @@ def test_moto_rest_error_is_translated(self, service_response_handler_chain):
238258
"MinCount":1,
239259
},
240260
)
241-
msg="The keypair 'some-key-pair' does not exist."
242-
moto_exception=InvalidKeyPairNameError(msg)
261+
moto_exception=InvalidKeyPairNameError({"some-key-pair"})
243262

244263
ServiceExceptionSerializer().create_exception_response(moto_exception,context)
245264

246-
assertmsgincontext.service_exception.message
265+
assert (
266+
"The keypair '{'some-key-pair'}' does not exist."incontext.service_exception.message
267+
)
247268
assertcontext.service_exception.code=="InvalidKeyPair.NotFound"
248269
assertnotcontext.service_exception.sender_fault
249270
assertcontext.service_exception.status_code==400

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp