Skip to content

Commit

Permalink
delete ConnectionMock for DoerMock
Browse files Browse the repository at this point in the history
  • Loading branch information
DerekBum committed Dec 28, 2023
1 parent 42ea55c commit 0b7b2ca
Show file tree
Hide file tree
Showing 12 changed files with 203 additions and 242 deletions.
6 changes: 5 additions & 1 deletion connector.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,18 @@ package tarantool

import "time"

type Doer interface {
Do(req Request) (fut *Future)
}

type Connector interface {
Doer
ConnectedNow() bool
Close() error
ConfiguredTimeout() time.Duration
NewPrepared(expr string) (*Prepared, error)
NewStream() (*Stream, error)
NewWatcher(key string, callback WatchCallback) (Watcher, error)
Do(req Request) (fut *Future)

// Deprecated: the method will be removed in the next major version,
// use a PingRequest object + Do() instead.
Expand Down
41 changes: 0 additions & 41 deletions example_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package tarantool_test

import (
"bytes"
"context"
"fmt"
"net"
Expand Down Expand Up @@ -1379,43 +1378,3 @@ func ExampleFdDialer() {
// Output:
// <nil>
}

func ExampleMockConnection() {
conn := test_helpers.NewMockConnection()

req := test_helpers.NewMockRequest()
resp, err := test_helpers.NewMockResponse(tarantool.Header{
RequestId: 0,
Code: 0,
}, bytes.NewReader([]byte{'v', '2'}))
// nil
fmt.Println(err)

conn.SetResponse(resp)

fut := conn.Do(req)
futResp, futErr := fut.GetResponse()
// nil
fmt.Println(futErr)

futRespConv, ok := futResp.(*test_helpers.MockResponse)
if !ok {
fmt.Println("Failed to convert")
}
if futRespConv != resp {
fmt.Println("Responses are not equal")
}

conn.SetResponse(resp)

err = fmt.Errorf("some error")
conn.SetError(err)

fut = conn.Do(req)
futResp, futErr = fut.GetResponse()
if futErr != err {
fmt.Println("Errors are not equal")
}
// nil
fmt.Println(futResp)
}
3 changes: 2 additions & 1 deletion prepared.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ package tarantool
import (
"context"
"fmt"
"io"

"github.com/tarantool/go-iproto"
"github.com/vmihailenco/msgpack/v5"
"io"
)

// PreparedID is a type for Prepared Statement ID
Expand Down
18 changes: 15 additions & 3 deletions schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -381,14 +381,20 @@ func (indexField *IndexField) DecodeMsgpack(d *msgpack.Decoder) error {
}

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

// Reload spaces.
var spaces []Space
err := conn.SelectTyped(vspaceSpId, 0, 0, maxSchemas, IterAll, []interface{}{}, &spaces)
req := NewSelectRequest(vspaceSpId).
Index(0).
Offset(0).
Limit(maxSchemas).
Iterator(IterAll).
Key([]interface{}{})
err := doer.Do(req).GetTyped(&spaces)
if err != nil {
return Schema{}, err
}
Expand All @@ -399,7 +405,13 @@ func GetSchema(conn Connector) (Schema, error) {

// Reload indexes.
var indexes []Index
err = conn.SelectTyped(vindexSpId, 0, 0, maxSchemas, IterAll, []interface{}{}, &indexes)
req = NewSelectRequest(vindexSpId).
Index(0).
Offset(0).
Limit(maxSchemas).
Iterator(IterAll).
Key([]interface{}{})
err = doer.Do(req).GetTyped(&indexes)
if err != nil {
return Schema{}, err
}
Expand Down
120 changes: 94 additions & 26 deletions tarantool_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package tarantool_test

import (
"bytes"
"context"
"encoding/binary"
"fmt"
Expand Down Expand Up @@ -2641,7 +2640,7 @@ func (req *waitCtxRequest) Async() bool {
}

func (req *waitCtxRequest) CreateResponse(header Header, body io.Reader) (Response, error) {
resp, err := test_helpers.NewMockResponse(header, body)
resp, err := test_helpers.CreateMockResponse(header, body)
return resp, err
}

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

func TestMockConnection(t *testing.T) {
conn := test_helpers.NewMockConnection()

req := test_helpers.NewMockRequest()
resp, err := test_helpers.NewMockResponse(Header{
RequestId: 0,
Code: 0,
}, bytes.NewReader([]byte{'v', '2'}))
require.NoError(t, err)
func TestGetSchema_ok(t *testing.T) {
space1 := Space{
Id: 1,
Name: "name1",
Indexes: make(map[string]Index),
IndexesById: make(map[uint32]Index),
Fields: make(map[string]Field),
FieldsById: make(map[uint32]Field),
}
index := Index{
Id: 1,
SpaceId: 2,
Name: "index_name",
Type: "index_type",
Unique: true,
Fields: make([]IndexField, 0),
}
space2 := Space{
Id: 2,
Name: "name2",
Indexes: map[string]Index{
"index_name": index,
},
IndexesById: map[uint32]Index{
1: index,
},
Fields: make(map[string]Field),
FieldsById: make(map[uint32]Field),
}

mockDoer := test_helpers.NewMockDoer(t,
test_helpers.NewMockResponse(t, [][]interface{}{
{
uint32(1),
"skip",
"name1",
"",
0,
},
{
uint32(2),
"skip",
"name2",
"",
0,
},
}),
test_helpers.NewMockResponse(t, [][]interface{}{
{
uint32(2),
uint32(1),
"index_name",
"index_type",
uint8(1),
uint8(0),
},
}),
)

conn.SetResponse(resp)
expectedSchema := Schema{
SpacesById: map[uint32]Space{
1: space1,
2: space2,
},
Spaces: map[string]Space{
"name1": space1,
"name2": space2,
},
}

fut := conn.Do(req)
futResp, futErr := fut.GetResponse()
require.NoError(t, futErr)
schema, err := GetSchema(&mockDoer)
require.NoError(t, err)
require.Equal(t, expectedSchema, schema)
}

futRespConv, ok := futResp.(*test_helpers.MockResponse)
require.True(t, ok)
require.Equal(t, resp, futRespConv)
func TestGetSchema_spaces_select_error(t *testing.T) {
mockDoer := test_helpers.NewMockDoer(t, fmt.Errorf("some error"))

conn.SetResponse(resp)
schema, err := GetSchema(&mockDoer)
require.Error(t, err)
require.Equal(t, Schema{}, schema)
}

err = fmt.Errorf("some error")
conn.SetError(err)
func TestGetSchema_index_select_error(t *testing.T) {
mockDoer := test_helpers.NewMockDoer(t,
test_helpers.NewMockResponse(t, [][]interface{}{
{
uint32(1),
"skip",
"name1",
"",
0,
},
}),
fmt.Errorf("some error"))

fut = conn.Do(req)
futResp, futErr = fut.GetResponse()
require.Error(t, futErr)
require.Equal(t, err, futErr)
require.Nil(t, futResp)
schema, err := GetSchema(&mockDoer)
require.Error(t, err)
require.Equal(t, Schema{}, schema)
}

// runTestMain is a body of TestMain function
Expand Down
120 changes: 0 additions & 120 deletions test_helpers/connection_mock.go

This file was deleted.

Loading

0 comments on commit 0b7b2ca

Please sign in to comment.