Skip to content

Commit 0b7b2ca

Browse files
committed
delete ConnectionMock for DoerMock
1 parent 42ea55c commit 0b7b2ca

12 files changed

+203
-242
lines changed

connector.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,18 @@ package tarantool
22

33
import "time"
44

5+
type Doer interface {
6+
Do(req Request) (fut *Future)
7+
}
8+
59
type Connector interface {
10+
Doer
611
ConnectedNow() bool
712
Close() error
813
ConfiguredTimeout() time.Duration
914
NewPrepared(expr string) (*Prepared, error)
1015
NewStream() (*Stream, error)
1116
NewWatcher(key string, callback WatchCallback) (Watcher, error)
12-
Do(req Request) (fut *Future)
1317

1418
// Deprecated: the method will be removed in the next major version,
1519
// use a PingRequest object + Do() instead.

example_test.go

-41
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package tarantool_test
22

33
import (
4-
"bytes"
54
"context"
65
"fmt"
76
"net"
@@ -1379,43 +1378,3 @@ func ExampleFdDialer() {
13791378
// Output:
13801379
// <nil>
13811380
}
1382-
1383-
func ExampleMockConnection() {
1384-
conn := test_helpers.NewMockConnection()
1385-
1386-
req := test_helpers.NewMockRequest()
1387-
resp, err := test_helpers.NewMockResponse(tarantool.Header{
1388-
RequestId: 0,
1389-
Code: 0,
1390-
}, bytes.NewReader([]byte{'v', '2'}))
1391-
// nil
1392-
fmt.Println(err)
1393-
1394-
conn.SetResponse(resp)
1395-
1396-
fut := conn.Do(req)
1397-
futResp, futErr := fut.GetResponse()
1398-
// nil
1399-
fmt.Println(futErr)
1400-
1401-
futRespConv, ok := futResp.(*test_helpers.MockResponse)
1402-
if !ok {
1403-
fmt.Println("Failed to convert")
1404-
}
1405-
if futRespConv != resp {
1406-
fmt.Println("Responses are not equal")
1407-
}
1408-
1409-
conn.SetResponse(resp)
1410-
1411-
err = fmt.Errorf("some error")
1412-
conn.SetError(err)
1413-
1414-
fut = conn.Do(req)
1415-
futResp, futErr = fut.GetResponse()
1416-
if futErr != err {
1417-
fmt.Println("Errors are not equal")
1418-
}
1419-
// nil
1420-
fmt.Println(futResp)
1421-
}

prepared.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@ package tarantool
33
import (
44
"context"
55
"fmt"
6+
"io"
7+
68
"github.com/tarantool/go-iproto"
79
"github.com/vmihailenco/msgpack/v5"
8-
"io"
910
)
1011

1112
// PreparedID is a type for Prepared Statement ID

schema.go

+15-3
Original file line numberDiff line numberDiff line change
@@ -381,14 +381,20 @@ func (indexField *IndexField) DecodeMsgpack(d *msgpack.Decoder) error {
381381
}
382382

383383
// GetSchema returns the actual schema for the connection.
384-
func GetSchema(conn Connector) (Schema, error) {
384+
func GetSchema(doer Doer) (Schema, error) {
385385
schema := Schema{}
386386
schema.SpacesById = make(map[uint32]Space)
387387
schema.Spaces = make(map[string]Space)
388388

389389
// Reload spaces.
390390
var spaces []Space
391-
err := conn.SelectTyped(vspaceSpId, 0, 0, maxSchemas, IterAll, []interface{}{}, &spaces)
391+
req := NewSelectRequest(vspaceSpId).
392+
Index(0).
393+
Offset(0).
394+
Limit(maxSchemas).
395+
Iterator(IterAll).
396+
Key([]interface{}{})
397+
err := doer.Do(req).GetTyped(&spaces)
392398
if err != nil {
393399
return Schema{}, err
394400
}
@@ -399,7 +405,13 @@ func GetSchema(conn Connector) (Schema, error) {
399405

400406
// Reload indexes.
401407
var indexes []Index
402-
err = conn.SelectTyped(vindexSpId, 0, 0, maxSchemas, IterAll, []interface{}{}, &indexes)
408+
req = NewSelectRequest(vindexSpId).
409+
Index(0).
410+
Offset(0).
411+
Limit(maxSchemas).
412+
Iterator(IterAll).
413+
Key([]interface{}{})
414+
err = doer.Do(req).GetTyped(&indexes)
403415
if err != nil {
404416
return Schema{}, err
405417
}

tarantool_test.go

+94-26
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package tarantool_test
22

33
import (
4-
"bytes"
54
"context"
65
"encoding/binary"
76
"fmt"
@@ -2641,7 +2640,7 @@ func (req *waitCtxRequest) Async() bool {
26412640
}
26422641

26432642
func (req *waitCtxRequest) CreateResponse(header Header, body io.Reader) (Response, error) {
2644-
resp, err := test_helpers.NewMockResponse(header, body)
2643+
resp, err := test_helpers.CreateMockResponse(header, body)
26452644
return resp, err
26462645
}
26472646

@@ -3873,36 +3872,105 @@ func TestFdDialer(t *testing.T) {
38733872
require.Equal(t, int8(0), resp[0])
38743873
}
38753874

3876-
func TestMockConnection(t *testing.T) {
3877-
conn := test_helpers.NewMockConnection()
3878-
3879-
req := test_helpers.NewMockRequest()
3880-
resp, err := test_helpers.NewMockResponse(Header{
3881-
RequestId: 0,
3882-
Code: 0,
3883-
}, bytes.NewReader([]byte{'v', '2'}))
3884-
require.NoError(t, err)
3875+
func TestGetSchema_ok(t *testing.T) {
3876+
space1 := Space{
3877+
Id: 1,
3878+
Name: "name1",
3879+
Indexes: make(map[string]Index),
3880+
IndexesById: make(map[uint32]Index),
3881+
Fields: make(map[string]Field),
3882+
FieldsById: make(map[uint32]Field),
3883+
}
3884+
index := Index{
3885+
Id: 1,
3886+
SpaceId: 2,
3887+
Name: "index_name",
3888+
Type: "index_type",
3889+
Unique: true,
3890+
Fields: make([]IndexField, 0),
3891+
}
3892+
space2 := Space{
3893+
Id: 2,
3894+
Name: "name2",
3895+
Indexes: map[string]Index{
3896+
"index_name": index,
3897+
},
3898+
IndexesById: map[uint32]Index{
3899+
1: index,
3900+
},
3901+
Fields: make(map[string]Field),
3902+
FieldsById: make(map[uint32]Field),
3903+
}
3904+
3905+
mockDoer := test_helpers.NewMockDoer(t,
3906+
test_helpers.NewMockResponse(t, [][]interface{}{
3907+
{
3908+
uint32(1),
3909+
"skip",
3910+
"name1",
3911+
"",
3912+
0,
3913+
},
3914+
{
3915+
uint32(2),
3916+
"skip",
3917+
"name2",
3918+
"",
3919+
0,
3920+
},
3921+
}),
3922+
test_helpers.NewMockResponse(t, [][]interface{}{
3923+
{
3924+
uint32(2),
3925+
uint32(1),
3926+
"index_name",
3927+
"index_type",
3928+
uint8(1),
3929+
uint8(0),
3930+
},
3931+
}),
3932+
)
38853933

3886-
conn.SetResponse(resp)
3934+
expectedSchema := Schema{
3935+
SpacesById: map[uint32]Space{
3936+
1: space1,
3937+
2: space2,
3938+
},
3939+
Spaces: map[string]Space{
3940+
"name1": space1,
3941+
"name2": space2,
3942+
},
3943+
}
38873944

3888-
fut := conn.Do(req)
3889-
futResp, futErr := fut.GetResponse()
3890-
require.NoError(t, futErr)
3945+
schema, err := GetSchema(&mockDoer)
3946+
require.NoError(t, err)
3947+
require.Equal(t, expectedSchema, schema)
3948+
}
38913949

3892-
futRespConv, ok := futResp.(*test_helpers.MockResponse)
3893-
require.True(t, ok)
3894-
require.Equal(t, resp, futRespConv)
3950+
func TestGetSchema_spaces_select_error(t *testing.T) {
3951+
mockDoer := test_helpers.NewMockDoer(t, fmt.Errorf("some error"))
38953952

3896-
conn.SetResponse(resp)
3953+
schema, err := GetSchema(&mockDoer)
3954+
require.Error(t, err)
3955+
require.Equal(t, Schema{}, schema)
3956+
}
38973957

3898-
err = fmt.Errorf("some error")
3899-
conn.SetError(err)
3958+
func TestGetSchema_index_select_error(t *testing.T) {
3959+
mockDoer := test_helpers.NewMockDoer(t,
3960+
test_helpers.NewMockResponse(t, [][]interface{}{
3961+
{
3962+
uint32(1),
3963+
"skip",
3964+
"name1",
3965+
"",
3966+
0,
3967+
},
3968+
}),
3969+
fmt.Errorf("some error"))
39003970

3901-
fut = conn.Do(req)
3902-
futResp, futErr = fut.GetResponse()
3903-
require.Error(t, futErr)
3904-
require.Equal(t, err, futErr)
3905-
require.Nil(t, futResp)
3971+
schema, err := GetSchema(&mockDoer)
3972+
require.Error(t, err)
3973+
require.Equal(t, Schema{}, schema)
39063974
}
39073975

39083976
// runTestMain is a body of TestMain function

test_helpers/connection_mock.go

-120
This file was deleted.

0 commit comments

Comments
 (0)