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

api: add ability to mock connections in tests #363

Merged
merged 4 commits into from
Jan 25, 2024

Conversation

DerekBum
Copy link

@DerekBum DerekBum commented Dec 7, 2023

Added an ability to mock connections via new MockRequest, MockResponse and MockDoer structs. Huge rework for the responses was done in the progress, here is list of changes for every commit (3 commits total):

First commit creates a new Response interface. But still, only one Response implementation exists:
ConnResponse. Custom responses (including mocks) are expected to implement this interface.

Create a Response interface and its implementation ConnResponse.

For the Future method Get now returns response data. To get the actual response new method GetResponse is added.

IsPush() method is added to the response iterator. It returns the information if the current response is a Push. Right now it does not have a lot of usage, but it will be crucial once we create a separate response for pushes

Second commit creates different implementations of the Response interface. Special types of responses are used with special requests.

Thus Response interface was simplified: some special methods were moved to the corresponding implementations. This means that if a user wants to access this methods, the response should be casted to its actual type.

SelectResponse, ExecuteResponse, PrepareResponse, PushResponse are part of a public API. Pos(), MetaData(), SQLInfo() methods created for them to get specific info.

Future constructors now accept Request as their argument.
Future methods AppendPush and SetResponse accept response Header and data as their arguments.

IsPush() method is used to return the information if the current response is a PushResponse. PushCode constant is removed. To get information, if the current response is a push response, IsPush() method could be used instead.

After this patch, operations Ping, Select, Insert, Replace, Delete, Update, Upsert, Call, Call16, Call17, Eval, Execute of a Connector return response data instead of an actual responses.
After this patch, operations Ping, Select, Insert, Replace, Delete, Update, Upsert, Call, Call16, Call17, Eval, Execute of a Pooler return response data instead of an actual responses.

Third commit creates a mock implementations MockRequest, MockResponse and MockDoer.
The last one allows to mock not the full Connection, but its part -- a structure, that implements new Doer interface (a Do function).

Also added missing comments, all the changes are recorded in the CHANGELOG and README files. Added new tests and examples.

So this entity is easier to implement and it is enough to mock tests that require working Connection. All new mock structs and an example for MockDoer usage are added to the test_helpers package.

Added new structs MockDoer, MockRequest to test_helpers.
Renamed StrangerResponse to MockResponse.

Added new connection log constant: LogAppendPushFailed. It is logged when connection fails to append a push response.

Closes #237

I didn't forget about (remove if it is not applicable):

@DerekBum DerekBum marked this pull request as draft December 7, 2023 15:45
@DerekBum DerekBum force-pushed the DerekBum/gh-237-ability-to-mock-connections branch 3 times, most recently from 0ee8687 to 4b9b201 Compare December 7, 2023 16:01
response.go Outdated Show resolved Hide resolved
response.go Outdated Show resolved Hide resolved
response.go Outdated Show resolved Hide resolved
response.go Outdated Show resolved Hide resolved
dial.go Outdated Show resolved Hide resolved
@DerekBum DerekBum force-pushed the DerekBum/gh-237-ability-to-mock-connections branch from 1be6374 to de97a4f Compare December 11, 2023 20:47
response.go Outdated Show resolved Hide resolved
connection.go Outdated Show resolved Hide resolved
@DerekBum DerekBum force-pushed the DerekBum/gh-237-ability-to-mock-connections branch 2 times, most recently from c9fa63b to a4c84d3 Compare December 18, 2023 08:51
connection.go Outdated Show resolved Hide resolved
connection.go Outdated Show resolved Hide resolved
connection.go Outdated Show resolved Hide resolved
connection.go Outdated Show resolved Hide resolved
connection.go Outdated Show resolved Hide resolved
response.go Outdated Show resolved Hide resolved
request.go Outdated Show resolved Hide resolved
response.go Outdated Show resolved Hide resolved
future.go Outdated Show resolved Hide resolved
response.go Outdated Show resolved Hide resolved
response.go Outdated Show resolved Hide resolved
@DerekBum DerekBum force-pushed the DerekBum/gh-237-ability-to-mock-connections branch 2 times, most recently from d78087f to dde51ea Compare December 26, 2023 18:35
connection.go Outdated Show resolved Hide resolved
dial.go Outdated Show resolved Hide resolved
prepared.go Outdated Show resolved Hide resolved
prepared.go Outdated Show resolved Hide resolved
request.go Show resolved Hide resolved
response.go Outdated Show resolved Hide resolved
response_it.go Show resolved Hide resolved
watch.go Outdated Show resolved Hide resolved
test_helpers/main.go Outdated Show resolved Hide resolved
test_helpers/connection_mock.go Outdated Show resolved Hide resolved
prepared.go Outdated Show resolved Hide resolved
@DerekBum DerekBum force-pushed the DerekBum/gh-237-ability-to-mock-connections branch 3 times, most recently from b6cab56 to 0b7b2ca Compare December 28, 2023 23:00
Copy link
Collaborator

@oleg-jukovec oleg-jukovec left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the patchset. I didn't look at the code and tests very carefully, we'll leave that for the next review iteration.

It looks fine in general. So we need to do:

  1. Add CHANGELOG.md entry.
  2. Update README.md main example.
  3. Update README.md migration section.
  4. Add comments to all public API methods/types (include test_helpers).
  5. Add tests for new types/methods. I suppose it would be at least a response types + CreateResponse method in all requests.

test_helpers/request_mock.go Outdated Show resolved Hide resolved
test_helpers/response_mock.go Outdated Show resolved Hide resolved
test_helpers/doer_mock.go Outdated Show resolved Hide resolved
test_helpers/utils.go Outdated Show resolved Hide resolved
test_helpers/utils.go Outdated Show resolved Hide resolved
test_helpers/doer_mock.go Outdated Show resolved Hide resolved
test_helpers/doer_mock.go Outdated Show resolved Hide resolved
tarantool_test.go Outdated Show resolved Hide resolved
@DerekBum DerekBum force-pushed the DerekBum/gh-237-ability-to-mock-connections branch 3 times, most recently from d68febb to 5cf1cff Compare December 30, 2023 00:38
@DerekBum
Copy link
Author

Updated README and CHANGELOG, added more comments and more tests (I will squash commits at the last step).

CHANGELOG.md Outdated Show resolved Hide resolved
CHANGELOG.md Outdated Show resolved Hide resolved
README.md Outdated Show resolved Hide resolved
README.md Outdated Show resolved Hide resolved
README.md Outdated Show resolved Hide resolved
response.go Outdated Show resolved Hide resolved
response.go Outdated Show resolved Hide resolved
response.go Outdated Show resolved Hide resolved
response.go Outdated Show resolved Hide resolved
response.go Outdated Show resolved Hide resolved
@DerekBum DerekBum force-pushed the DerekBum/gh-237-ability-to-mock-connections branch from 1bda50d to 64c9b58 Compare January 16, 2024 15:32
Copy link
Member

@DifferentialOrange DifferentialOrange left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had partially reviewed this PR. I tried to use by commit approach (since there is a lot of changes), but it seems like some things are not where they should be. I understand that it's really a bother, but it would be nice to see a proper changes separation by commits to improve reviewing experience.

box_error_test.go Show resolved Hide resolved
box_error_test.go Show resolved Hide resolved
box_error_test.go Show resolved Hide resolved
crud/tarantool_test.go Show resolved Hide resolved
dial.go Outdated Show resolved Hide resolved
example_test.go Outdated
Comment on lines 1162 to 1164
// ExampleConnection_Do_failure demonstrates how to send a request and process
// failure.
func ExampleConnection_Do_failure() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's about this one?

future_test.go Show resolved Hide resolved
test_helpers/main.go Outdated Show resolved Hide resolved
connector.go Show resolved Hide resolved
dial.go Outdated Show resolved Hide resolved
@DerekBum DerekBum force-pushed the DerekBum/gh-237-ability-to-mock-connections branch 2 times, most recently from 0c29003 to dbbf383 Compare January 21, 2024 02:05
@DerekBum
Copy link
Author

Updated the commits so the changes got separated properly.

Copy link
Member

@DifferentialOrange DifferentialOrange left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the updates! I had left a couple of comments, should be LGTM after resolving them.

response.go Outdated Show resolved Hide resolved
CHANGELOG.md Outdated Show resolved Hide resolved
README.md Outdated Show resolved Hide resolved
const.go Outdated Show resolved Hide resolved
crud/select.go Outdated Show resolved Hide resolved
example_test.go Outdated
Comment on lines 1029 to 1033
if resp.Header().Error != tarantool.ErrorNo {
fmt.Printf("response error code: %s\n", resp.Header().Error)
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"else print success" sounds even better, since now it's always the success

test_helpers/example_test.go Show resolved Hide resolved
response.go Outdated Show resolved Hide resolved
@DerekBum DerekBum force-pushed the DerekBum/gh-237-ability-to-mock-connections branch from dbbf383 to 9d46839 Compare January 22, 2024 12:37
@DerekBum
Copy link
Author

Updated the commits so the changes in CHANGELOG.md, README.md and doc comments got separated properly.

CHANGELOG.md Outdated
@@ -28,6 +28,19 @@ Versioning](http://semver.org/spec/v2.0.0.html) except to the first release.
in requests instead of their IDs.
- `GetSchema` function to get the actual schema (#7)
- Support connection via an existing socket fd (#321)
<<<<<<< HEAD
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The reabase artifact.

README.md Outdated
@@ -201,12 +202,24 @@ The subpackage has been deleted. You could use `pool` instead.
unique string ID, which allows them to be distinguished.
* `pool.GetPoolInfo` has been renamed to `pool.GetInfo`. Return type has been changed
to `map[string]ConnectionInfo`.
* Operations `Ping`, `Select`, `Insert`, `Replace`, `Delete`, `Update`, `Upsert`,
<<<<<<< HEAD
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And here

README.md Outdated Show resolved Hide resolved
@DerekBum DerekBum force-pushed the DerekBum/gh-237-ability-to-mock-connections branch from 9d46839 to 274676b Compare January 22, 2024 15:17
CHANGELOG.md Outdated Show resolved Hide resolved
CHANGELOG.md Outdated Show resolved Hide resolved
README.md Outdated Show resolved Hide resolved
README.md Outdated Show resolved Hide resolved
README.md Outdated Show resolved Hide resolved
README.md Outdated Show resolved Hide resolved
README.md Show resolved Hide resolved
CHANGELOG.md Outdated Show resolved Hide resolved
README.md Show resolved Hide resolved
@DerekBum DerekBum force-pushed the DerekBum/gh-237-ability-to-mock-connections branch from 274676b to cc1d250 Compare January 23, 2024 15:18
@DerekBum
Copy link
Author

Updated CHANGELOG and README.

README.md Outdated Show resolved Hide resolved
@DerekBum DerekBum force-pushed the DerekBum/gh-237-ability-to-mock-connections branch 2 times, most recently from e3ec602 to d143165 Compare January 24, 2024 13:14
This commit creates a new `Response` interface.
But still, only one `Response` implementation exists:
`ConnResponse`. Custom responses (including mocks)
are expected to implement this interface.

Create a `Response` interface and its implementation
`ConnResponse`.

For the `Future` method `Get` now returns response data.
To get the actual response new method `GetResponse` is added.

`IsPush()` method is added to the response iterator. It returns
the information if the current response is a `Push`.
Right now it does not have a lot of usage, but it will be
crucial once we create a separate response for pushes.

Part of #237
Different implementations of the `Response` interface created.
Special types of responses are used with special requests.

Thus `Response` interface was simplified: some special methods
were moved to the corresponding implementations.
This means that if a user wants to access this methods, the
response should be casted to its actual type.

`SelectResponse`, `ExecuteResponse`, `PrepareResponse`,
`PushResponse` are part of a public API. `Pos()`, `MetaData()`,
`SQLInfo()` methods created for them to get specific info.

`Future` constructors now accept `Request` as their argument.
`Future` methods `AppendPush` and `SetResponse` accept
response `Header` and data as their arguments.

`IsPush()` method is used to return the information if the current
response is a `PushResponse`. `PushCode` constant is removed. To get
information, if the current response is a push response, `IsPush()`
method could be used instead.

After this patch, operations `Ping`, `Select`, `Insert`, `Replace`,
`Delete`, `Update`, `Upsert`, `Call`, `Call16`, `Call17`, `Eval`, `Execute`
of a `Connector` return response data instead of an actual responses.
After this patch, operations `Ping`, `Select`, `Insert`, `Replace`,
`Delete`, `Update`, `Upsert`, `Call`, `Call16`, `Call17`, `Eval`, `Execute`
of a `Pooler` return response data instead of an actual responses.

Part of #237
@DerekBum DerekBum force-pushed the DerekBum/gh-237-ability-to-mock-connections branch 2 times, most recently from a4c3608 to 33a77dd Compare January 24, 2024 23:04
Create a mock implementations `MockRequest`, `MockResponse` and
`MockDoer`.
The last one allows to mock not the full `Connection`, but its
part -- a structure, that implements new `Doer` interface
(a `Do` function).

Also added missing comments, all the changes are recorded in
the `CHANGELOG` and `README` files. Added new tests and
examples.

So this entity is easier to implement and it is enough to
mock tests that require working `Connection`.
All new mock structs and an example for `MockDoer` usage
are added to the `test_helpers` package.

Added new structs `MockDoer`, `MockRequest` to `test_helpers`.
Renamed `StrangerResponse` to `MockResponse`.

Added new connection log constant: `LogAppendPushFailed`.
It is logged when connection fails to append a push response.

Closes #237
@DerekBum DerekBum force-pushed the DerekBum/gh-237-ability-to-mock-connections branch from 33a77dd to 63e64fa Compare January 25, 2024 08:45
1.10.14 build is flacky on macOS-12 runner.
Bump to 1.10.15 hopefully helps with this issue.
@DerekBum DerekBum force-pushed the DerekBum/gh-237-ability-to-mock-connections branch from 63e64fa to 9a1b8e4 Compare January 25, 2024 08:46
@oleg-jukovec oleg-jukovec merged commit a2ad272 into master Jan 25, 2024
22 checks passed
@oleg-jukovec oleg-jukovec deleted the DerekBum/gh-237-ability-to-mock-connections branch January 25, 2024 10:39
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.

v2: ability to mock Connector.Do()/Future/Response in tests
3 participants