googleapis / google-cloud-go

Google Cloud Client Libraries for Go.
https://cloud.google.com/go/docs/reference
Apache License 2.0
3.75k stars 1.29k forks source link

all: make the operation type easier to use in tests #6141

Open ConorNevin opened 2 years ago

ConorNevin commented 2 years ago

Is your feature request related to a problem? Please describe.

Writing tests for API calls that return a *compute.Operation is very frustrating ss the only field on the type is an unexported interface preventing any initialisation of the Operation type in test code. This means we end up hacking around calls to the library when testing which is far from desirable.

Describe the solution you'd like A clear and concise description of what you want to happen. What should the API be? How would you use it?

Either expose the Operation Handle and its implementations or expose a method that allows the creation of an operation.

Describe alternatives you've considered

Most of our tests use the context to bypass the operation wait - but I'd be happier not having that kind of logic in the test code

Additional context N/A

codyoss commented 2 years ago

cc @noahdietz perhaps something to consider. This generally applies to all operations, not just compute.

@ConorNevin Thanks for the report. I can not sure if we will expose testing hooks for this but we will take it under consideration. I have two suggestions for you in the meantime though.

  1. Consider abstracting away the use of our clients behind a private interface. You then could hide the types our api exposes and use types native to your application. This would allow you to fully mock away all API interactions.

  2. Another thing that could be done for testing is using a fake HTTP server. The compute client here communicates or HTTP/JSON. So you could do something like this doc describes: https://github.com/googleapis/google-api-go-client/blob/main/testing.md#testing-http-services-using-fakes . Hope that helps in the meantime.