tarantool / go-tarantool

Tarantool 1.10+ client for Go language
https://pkg.go.dev/github.com/tarantool/go-tarantool/v2
BSD 2-Clause "Simplified" License
180 stars 57 forks source link

api: add ability to mock connections in tests #363

Closed DerekBum closed 7 months ago

DerekBum commented 9 months ago

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 commented 8 months ago

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

DerekBum commented 8 months ago

Updated everything, returned deleted tests. The failing ci test is because of shutdown_test.go:130: Could not check the Tarantool version: fork/exec /home/runner/work/go-tarantool/go-tarantool/bin/tarantool: bad file descriptor

DerekBum commented 7 months ago

Updated the commits so the changes got separated properly.

DerekBum commented 7 months ago

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

DerekBum commented 7 months ago

Updated CHANGELOG and README.