Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

boto3 stubber should allow SQS resource to be mocked #2978

Closed
2 tasks
neilferreira opened this issue Jun 29, 2023 · 2 comments
Closed
2 tasks

boto3 stubber should allow SQS resource to be mocked #2978

neilferreira opened this issue Jun 29, 2023 · 2 comments
Assignees
Labels
feature-request This issue requests a feature. response-requested Waiting on additional info and feedback. stubber

Comments

@neilferreira
Copy link

Describe the feature

The boto3 Stubber can be used to add responses to boto3 client calls, which can then be used to mock API calls within pytest.

Using the boto3 Stubber to mock boto3.resource instead of boto3.client is not a well-documented or perhaps supported feature.

This code:

sqs = boto3.resource("sqs")
sqs_stubber = Stubber(sqs.meta.client)
sqs_stubber.add_response(
    method="get_queue_by_name",
    service_response={},
    expected_params={},
)
sqs_stubber.activate()

Raises:

self = <botocore.stub.Stubber object at 0x12ffce4f0>, method = 'get_queue_by_name', service_response = {}, expected_params = {}

    def _add_response(self, method, service_response, expected_params):
        if not hasattr(self.client, method):
>           raise ValueError(
                "Client %s does not have method: %s"
                % (self.client.meta.service_model.service_name, method)
            )
E           ValueError: Client sqs does not have method: get_queue_by_name

If there is any way to mock boto3.resource("sqs").get_queue_by_name() that would be great.

Use Case

I would like to use pytest to verify the inputs and outputs of some boto3 SDK calls within my codebase.

Proposed Solution

Some people have mentioned a workaround on #1125 - but a proper and documented solution would be great.

Other Information

No response

Acknowledgements

  • I may be able to implement this feature request
  • This feature might incur a breaking change

SDK version used

1.29.163

Environment details (OS name and version, etc.)

Any

@neilferreira neilferreira added feature-request This issue requests a feature. needs-triage This issue or PR still needs to be triaged. labels Jun 29, 2023
@tim-finnigan tim-finnigan added investigating This issue is being investigated and/or work is in progress to resolve the issue. and removed needs-triage This issue or PR still needs to be triaged. labels Jul 3, 2023
@tim-finnigan
Copy link
Contributor

Hi @neilferreira thanks for the feature request. As mentioned in #1125 the Stubber will only work for client methods (but you can access the client methods from a resource as documented here and demonstrated in your code snippet.)

There is no get_queue_by_name client method, could you use the get_queue_url method instead?

(I should also note that there is a freeze on feature requests involving the resources interface as noted here.)

@tim-finnigan tim-finnigan self-assigned this Jul 3, 2023
@tim-finnigan tim-finnigan added stubber response-requested Waiting on additional info and feedback. and removed investigating This issue is being investigated and/or work is in progress to resolve the issue. labels Jul 3, 2023
@neilferreira
Copy link
Author

@tim-finnigan Thankyou for the explanation. Refactoring my code to use get_queue_url is a perfectly fine solution given the freeze on the resource interface.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature-request This issue requests a feature. response-requested Waiting on additional info and feedback. stubber
Projects
None yet
Development

No branches or pull requests

2 participants