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

Simplify mocking of iterator and waiter objects #769

Merged
merged 6 commits into from
Jan 16, 2024

Conversation

mgyucht
Copy link
Contributor

@mgyucht mgyucht commented Jan 16, 2024

Changes

It is currently challenging to mock APIs that return iterator structs like *listing.PaginatedIterator or *listing.DedupedIterator. To simplify mocking of iterator responses, we can change the return type for these methods to return the listing.Iterator interface. Then, users who need to write tests can use listing.SliceIterator to mock the response, for example:

w.GetMockWarehousesAPI().EXPECT().List(mock.Anything, mock.Anything).Return(&listing.SliceIterator[sql.EndpointInfo]{
	{
		Id:   "1",
		Name: "foo",
	},
	{
		Id:   "2",
		Name: "bar",
	},
})

Similarly, methods returning Wait* objects are difficult to mock. The Get() method calls the internal poll field, but that cannot be set by users who want to mock the response. Exposing this method allows users to define mock responses, for example:

w.GetMockWarehousesAPI().EXPECT().Create(mock.Anything, createRequest).Return(&sql.WaitGetWarehouseRunning[sql.CreateWarehouseResponse]{
	Poll: poll.Simple(getResponse),
}, nil)

Callers can then call w.Warehouses.Create(...).Get().

Lastly, waiters for which the initial method doesn't have a response have a type parameter of any which cannot be mocked easily. Instead, I changed this to struct{} to indicate that the response is empty.

Tests

  • Unit tests for new helper functionality.

@mgyucht mgyucht added this pull request to the merge queue Jan 16, 2024
Merged via the queue into main with commit a0de560 Jan 16, 2024
4 checks passed
@mgyucht mgyucht deleted the expose-poll-for-testing branch January 16, 2024 10:38
github-merge-queue bot pushed a commit that referenced this pull request Jan 16, 2024
## Changes
This was meant to be included in #769, but I forgot to push the change.

## Tests
<!-- 
How is this tested? Please see the checklist below and also describe any
other relevant tests
-->

- [ ] `make test` passing
- [ ] `make fmt` applied
- [ ] relevant integration tests applied
mgyucht added a commit that referenced this pull request Jan 16, 2024
This patch release contains two small changes:
* Retry on Status Code 503 ([#733](#733)), improving the stability of the SDK in light of transient API unavailability.
* Simplify mocking of iterator and waiter objects ([#769](#769), [#770](#770)). See the [Testing section of the README.md](https://github.com/databricks/databricks-sdk-go#testing) for usage information and examples.
mgyucht added a commit that referenced this pull request Jan 16, 2024
This patch release contains two small changes:
* Retry on Status Code 503 ([#733](#733)), improving the stability of the SDK in light of transient API unavailability.
* Simplify mocking of iterator and waiter objects ([#769](#769), [#770](#770)). See the [Testing section of the README.md](https://github.com/databricks/databricks-sdk-go#testing) for usage information and examples.
@mgyucht mgyucht mentioned this pull request Jan 16, 2024
github-merge-queue bot pushed a commit that referenced this pull request Jan 16, 2024
This patch release contains two small changes:
* Retry on Status Code 503
([#733](#733)),
improving the stability of the SDK in light of transient API
unavailability.
* Simplify mocking of iterator and waiter objects
([#769](#769),
[#770](#770)). See
the [Testing section of the
README.md](https://github.com/databricks/databricks-sdk-go#testing) for
usage information and examples.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants