ó U!¶\c@s§ddlZddlmZddlmZddlmZddlmZm Z m Z m Z ddl m Z defd„ƒYZeƒZd efd „ƒYZdS( iÿÿÿÿN(tdeque(tpformat(tvalidate_parameters(tParamValidationErrortStubResponseErrortStubAssertionErrortUnStubbedResponseError(t AWSResponset_ANYcBs)eZdZd„Zd„Zd„ZRS(sZ A helper object that compares equal to everything. Copied from unittest.mock cCstS(N(tTrue(tselftother((s1/tmp/pip-install-usGedi/botocore/botocore/stub.pyt__eq__scCstS(N(tFalse(R R ((s1/tmp/pip-install-usGedi/botocore/botocore/stub.pyt__ne__ scCsdS(Ns((R ((s1/tmp/pip-install-usGedi/botocore/botocore/stub.pyt__repr__#s(t__name__t __module__t__doc__R RR(((s1/tmp/pip-install-usGedi/botocore/botocore/stub.pyRs  tStubbercBs¡eZdZd„Zd„Zd„Zd„Zd„Zdd„Z d„Z ddd dddd „Z d „Z d „Z d „Zd„Zd„Zd„ZRS(sü This class will allow you to stub out requests so you don't have to hit an endpoint to write tests. Responses are returned first in, first out. If operations are called out of order, or are called with no remaining queued responses, an error will be raised. **Example:** :: import datetime import botocore.session from botocore.stub import Stubber s3 = botocore.session.get_session().create_client('s3') stubber = Stubber(s3) response = { 'IsTruncated': False, 'Name': 'test-bucket', 'MaxKeys': 1000, 'Prefix': '', 'Contents': [{ 'Key': 'test.txt', 'ETag': '"abc123"', 'StorageClass': 'STANDARD', 'LastModified': datetime.datetime(2016, 1, 20, 22, 9), 'Owner': {'ID': 'abc123', 'DisplayName': 'myname'}, 'Size': 14814 }], 'EncodingType': 'url', 'ResponseMetadata': { 'RequestId': 'abc123', 'HTTPStatusCode': 200, 'HostId': 'abc123' }, 'Marker': '' } expected_params = {'Bucket': 'test-bucket'} stubber.add_response('list_objects', response, expected_params) stubber.activate() service_response = s3.list_objects(Bucket='test-bucket') assert service_response == response This class can also be called as a context manager, which will handle activation / deactivation for you. **Example:** :: import datetime import botocore.session from botocore.stub import Stubber s3 = botocore.session.get_session().create_client('s3') response = { "Owner": { "ID": "foo", "DisplayName": "bar" }, "Buckets": [{ "CreationDate": datetime.datetime(2016, 1, 20, 22, 9), "Name": "baz" }] } with Stubber(s3) as stubber: stubber.add_response('list_buckets', response, {}) service_response = s3.list_buckets() assert service_response == response If you have an input parameter that is a randomly generated value, or you otherwise don't care about its value, you can use ``stub.ANY`` to ignore it in validation. **Example:** :: import datetime import botocore.session from botocore.stub import Stubber, ANY s3 = botocore.session.get_session().create_client('s3') stubber = Stubber(s3) response = { 'IsTruncated': False, 'Name': 'test-bucket', 'MaxKeys': 1000, 'Prefix': '', 'Contents': [{ 'Key': 'test.txt', 'ETag': '"abc123"', 'StorageClass': 'STANDARD', 'LastModified': datetime.datetime(2016, 1, 20, 22, 9), 'Owner': {'ID': 'abc123', 'DisplayName': 'myname'}, 'Size': 14814 }], 'EncodingType': 'url', 'ResponseMetadata': { 'RequestId': 'abc123', 'HTTPStatusCode': 200, 'HostId': 'abc123' }, 'Marker': '' } expected_params = {'Bucket': ANY} stubber.add_response('list_objects', response, expected_params) with stubber: service_response = s3.list_objects(Bucket='test-bucket') assert service_response == response cCs+||_d|_d|_tƒ|_dS(sA :param client: The client to add your stubs to. t boto_stubbertboto_stubber_expected_paramsN(tclientt _event_idt_expected_params_event_idRt_queue(R R((s1/tmp/pip-install-usGedi/botocore/botocore/stub.pyt__init__¢s   cCs|jƒ|S(N(tactivate(R ((s1/tmp/pip-install-usGedi/botocore/botocore/stub.pyt __enter__«s cCs|jƒdS(N(t deactivate(R texception_typetexception_valuet traceback((s1/tmp/pip-install-usGedi/botocore/botocore/stub.pyt__exit__¯scCsN|jjjjd|jd|jƒ|jjjjd|jd|jƒdS(s5 Activates the stubber on the client sbefore-parameter-build.*.*t unique_idsbefore-call.*.*N( Rtmetateventstregister_firstt_assert_expected_paramsRtregistert_get_response_handlerR(R ((s1/tmp/pip-install-usGedi/botocore/botocore/stub.pyR²s   cCsN|jjjjd|jd|jƒ|jjjjd|jd|jƒdS(s7 Deactivates the stubber on the client sbefore-parameter-build.*.*R"sbefore-call.*.*N(RR#R$t unregisterR&RR(R(R ((s1/tmp/pip-install-usGedi/botocore/botocore/stub.pyR¿s   cCs|j|||ƒdS(sx Adds a service response to the response queue. This will be validated against the service model to ensure correctness. It should be noted, however, that while missing attributes are often considered correct, your code may not function properly if you leave them out. Therefore you should always fill in every value you see in a typical response for your particular request. :param method: The name of the client method to stub. :type method: str :param service_response: A dict response stub. Provided parameters will be validated against the service model. :type service_response: dict :param expected_params: A dictionary of the expected parameters to be called for the provided service response. The parameters match the names of keyword arguments passed to that client call. If any of the parameters differ a ``StubResponseError`` is thrown. You can use stub.ANY to indicate a particular parameter to ignore in validation. stub.ANY is only valid for top level params. N(t _add_response(R tmethodtservice_responsetexpected_params((s1/tmp/pip-install-usGedi/botocore/botocore/stub.pyt add_responseÌscCs©t|j|ƒs7td|jjjj|fƒ‚ntddidƒ}|jjjj |ƒ}|j ||ƒi|d6||fd6|d6}|j j |ƒdS(Ns"Client %s does not have method: %siÈtoperation_nametresponseR-( thasattrRt ValueErrorR#t service_modelt service_nameRtNonetmethod_to_api_mappingtgett_validate_responseRtappend(R R+R,R-t http_responseR/R0((s1/tmp/pip-install-usGedi/botocore/botocore/stub.pyR*ås  tic CsËtd |id ƒ}ii|d6d6i|d6|d6d6} |d k r^| dj|ƒn|d k r~| dj|ƒn|jjjj|ƒ} i| d6|| fd6|d6} |jj| ƒd S( s Adds a ``ClientError`` to the response queue. :param method: The name of the service method to return the error on. :type method: str :param service_error_code: The service error code to return, e.g. ``NoSuchBucket`` :type service_error_code: str :param service_message: The service message to return, e.g. 'The specified bucket does not exist.' :type service_message: str :param http_status_code: The HTTP status code to return, e.g. 404, etc :type http_status_code: int :param service_error_meta: Additional keys to be added to the service Error :type service_error_meta: dict :param expected_params: A dictionary of the expected parameters to be called for the provided service response. The parameters match the names of keyword arguments passed to that client call. If any of the parameters differ a ``StubResponseError`` is thrown. You can use stub.ANY to indicate a particular parameter to ignore in validation. :param response_meta: Additional keys to be added to the response's ResponseMetadata :type response_meta: dict tHTTPStatusCodetResponseMetadatatMessagetCodetErrorR/R0R-N( RR5tupdateRR#R6R7RR9( R R+tservice_error_codetservice_messagethttp_status_codetservice_error_metaR-t response_metaR:tparsed_responseR/R0((s1/tmp/pip-install-usGedi/botocore/botocore/stub.pytadd_client_errorùs %    cCs2t|jƒ}|dkr.td|ƒ‚ndS(s< Asserts that all expected calls were made. is %d responses remaining in queue.N(tlenRtAssertionError(R t remaining((s1/tmp/pip-install-usGedi/botocore/botocore/stub.pytassert_no_pending_responses;s cCsg|js$td|jddƒ‚n|jdd}||jkrctd|jdd|ƒ‚ndS(NR/treasons‹Unexpected API Call: A call was made but no additional calls expected. Either the API Call was not stubbed or it was called multiple times.is*Operation mismatch: found response for %s.(RRtnameR(R tmodeltparamsRN((s1/tmp/pip-install-usGedi/botocore/botocore/stub.pyt_assert_expected_call_orderDs    cKs!|j||ƒ|jjƒdS(NR0(RQRtpopleft(R RORPtcontexttkwargs((s1/tmp/pip-install-usGedi/botocore/botocore/stub.pyR(Tsc Ks |j|ƒrdS|j||ƒ|jdd}|dkrDdSxk|jƒD]]\}}||ks}||||krQtd|jddt|ƒt|ƒfƒ‚qQqQWt|j ƒƒt|j ƒƒkrtd|jddt|ƒt|ƒfƒ‚ndS(NiR-R/RMs)Expected parameters: %s, but received: %s( t_should_not_stubRQRR5titemsRRNRtsortedtkeys(R RORPRSRTR-tparamtvalue((s1/tmp/pip-install-usGedi/botocore/botocore/stub.pyR&Ys"   #$ cCs|r|jdƒrtSdS(Ntis_presign_request(R7R (R RS((s1/tmp/pip-install-usGedi/botocore/botocore/stub.pyRUpscCsŠ|jjj}|j|ƒ}|j}|}d|krRtj|ƒ}|d=n|dk rnt||ƒn|r†tddƒ‚ndS(NR=treports6Service response should only contain ResponseMetadata.( RR#R3toperation_modelt output_shapetcopyR5RR(R R/R,R3R]R^R0((s1/tmp/pip-install-usGedi/botocore/botocore/stub.pyR8ws    N(RRRRRR!RRR5R.R*RHRLRQR(R&RUR8(((s1/tmp/pip-install-usGedi/botocore/botocore/stub.pyR)s$x     ?    (R_t collectionsRtpprintRtbotocore.validateRtbotocore.exceptionsRRRRtbotocore.awsrequestRtobjectRtANYR(((s1/tmp/pip-install-usGedi/botocore/botocore/stub.pyt s "