Skip to content

Commit

Permalink
code health: add test_helpers.ConnectWithValidation
Browse files Browse the repository at this point in the history
The code that creates and verifies a connection to a Tarantool instance
spreads across tests. The patch extracts the code into a separate test
helper function.
  • Loading branch information
oleg-jukovec committed Jun 30, 2022
1 parent 3b98329 commit d04f8be
Show file tree
Hide file tree
Showing 9 changed files with 96 additions and 237 deletions.
5 changes: 3 additions & 2 deletions call_16_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@ import (
"testing"

. "github.com/tarantool/go-tarantool"
"github.com/tarantool/go-tarantool/test_helpers"
)

func TestConnection_Call(t *testing.T) {
var resp *Response
var err error

conn := connect(t, server, opts)
conn := test_helpers.ConnectWithValidation(t, server, opts)
defer conn.Close()

// Call16
Expand All @@ -30,7 +31,7 @@ func TestCallRequest(t *testing.T) {
var resp *Response
var err error

conn := connect(t, server, opts)
conn := test_helpers.ConnectWithValidation(t, server, opts)
defer conn.Close()

req := NewCallRequest("simple_incr").Args([]interface{}{1})
Expand Down
5 changes: 3 additions & 2 deletions call_17_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@ import (
"testing"

. "github.com/tarantool/go-tarantool"
"github.com/tarantool/go-tarantool/test_helpers"
)

func TestConnection_Call(t *testing.T) {
var resp *Response
var err error

conn := connect(t, server, opts)
conn := test_helpers.ConnectWithValidation(t, server, opts)
defer conn.Close()

// Call17
Expand All @@ -30,7 +31,7 @@ func TestCallRequest(t *testing.T) {
var resp *Response
var err error

conn := connect(t, server, opts)
conn := test_helpers.ConnectWithValidation(t, server, opts)
defer conn.Close()

req := NewCallRequest("simple_incr").Args([]interface{}{1})
Expand Down
25 changes: 5 additions & 20 deletions connection_pool/connection_pool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -739,10 +739,7 @@ func TestInsert(t *testing.T) {

// Connect to servers[2] to check if tuple
// was inserted on RW instance
conn, err := tarantool.Connect(servers[2], connOpts)
require.Nilf(t, err, "failed to connect %s", servers[2])
require.NotNilf(t, conn, "conn is nil after Connect")

conn := test_helpers.ConnectWithValidation(t, servers[2], connOpts)
defer conn.Close()

resp, err = conn.Select(spaceNo, indexNo, 0, 1, tarantool.IterEq, []interface{}{"rw_insert_key"})
Expand Down Expand Up @@ -812,10 +809,7 @@ func TestDelete(t *testing.T) {

// Connect to servers[2] to check if tuple
// was inserted on RW instance
conn, err := tarantool.Connect(servers[2], connOpts)
require.Nilf(t, err, "failed to connect %s", servers[2])
require.NotNilf(t, conn, "conn is nil after Connect")

conn := test_helpers.ConnectWithValidation(t, servers[2], connOpts)
defer conn.Close()

resp, err := conn.Insert(spaceNo, []interface{}{"delete_key", "delete_value"})
Expand Down Expand Up @@ -873,10 +867,7 @@ func TestUpsert(t *testing.T) {

// Connect to servers[2] to check if tuple
// was inserted on RW instance
conn, err := tarantool.Connect(servers[2], connOpts)
require.Nilf(t, err, "failed to connect %s", servers[2])
require.NotNilf(t, conn, "conn is nil after Connect")

conn := test_helpers.ConnectWithValidation(t, servers[2], connOpts)
defer conn.Close()

// Mode is `RW` by default, we have only one RW instance (servers[2])
Expand Down Expand Up @@ -941,10 +932,7 @@ func TestUpdate(t *testing.T) {

// Connect to servers[2] to check if tuple
// was inserted on RW instance
conn, err := tarantool.Connect(servers[2], connOpts)
require.Nilf(t, err, "failed to connect %s", servers[2])
require.NotNilf(t, conn, "conn is nil after Connect")

conn := test_helpers.ConnectWithValidation(t, servers[2], connOpts)
defer conn.Close()

resp, err := conn.Insert(spaceNo, []interface{}{"update_key", "update_value"})
Expand Down Expand Up @@ -1026,10 +1014,7 @@ func TestReplace(t *testing.T) {

// Connect to servers[2] to check if tuple
// was inserted on RW instance
conn, err := tarantool.Connect(servers[2], connOpts)
require.Nilf(t, err, "failed to connect %s", servers[2])
require.NotNilf(t, conn, "conn is nil after Connect")

conn := test_helpers.ConnectWithValidation(t, servers[2], connOpts)
defer conn.Close()

resp, err := conn.Insert(spaceNo, []interface{}{"replace_key", "replace_value"})
Expand Down
27 changes: 7 additions & 20 deletions datetime/datetime_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,6 @@ var spaceTuple1 = "testDatetime_1"
var spaceTuple2 = "testDatetime_2"
var index = "primary"

func connectWithValidation(t *testing.T) *Connection {
t.Helper()

conn, err := Connect(server, opts)
if err != nil {
t.Fatalf("Failed to connect: %s", err.Error())
}
if conn == nil {
t.Fatalf("conn is nil after Connect")
}
return conn
}

func skipIfDatetimeUnsupported(t *testing.T) {
t.Helper()

Expand Down Expand Up @@ -168,7 +155,7 @@ var datetimeSample = []struct {
func TestDatetimeInsertSelectDelete(t *testing.T) {
skipIfDatetimeUnsupported(t)

conn := connectWithValidation(t)
conn := test_helpers.ConnectWithValidation(t, server, opts)
defer conn.Close()

for _, testcase := range datetimeSample {
Expand All @@ -188,7 +175,7 @@ func TestDatetimeInsertSelectDelete(t *testing.T) {
func TestDatetimeMax(t *testing.T) {
skipIfDatetimeUnsupported(t)

conn := connectWithValidation(t)
conn := test_helpers.ConnectWithValidation(t, server, opts)
defer conn.Close()

tupleInsertSelectDelete(t, conn, maxTime)
Expand All @@ -197,7 +184,7 @@ func TestDatetimeMax(t *testing.T) {
func TestDatetimeMin(t *testing.T) {
skipIfDatetimeUnsupported(t)

conn := connectWithValidation(t)
conn := test_helpers.ConnectWithValidation(t, server, opts)
defer conn.Close()

tupleInsertSelectDelete(t, conn, minTime)
Expand All @@ -206,7 +193,7 @@ func TestDatetimeMin(t *testing.T) {
func TestDatetimeReplace(t *testing.T) {
skipIfDatetimeUnsupported(t)

conn := connectWithValidation(t)
conn := test_helpers.ConnectWithValidation(t, server, opts)
defer conn.Close()

tm, err := time.Parse(time.RFC3339, "2007-01-02T15:04:05Z")
Expand Down Expand Up @@ -356,7 +343,7 @@ func (c *Tuple2) DecodeMsgpack(d *msgpack.Decoder) error {
func TestCustomEncodeDecodeTuple1(t *testing.T) {
skipIfDatetimeUnsupported(t)

conn := connectWithValidation(t)
conn := test_helpers.ConnectWithValidation(t, server, opts)
defer conn.Close()

dt1, _ := time.Parse(time.RFC3339, "2010-05-24T17:51:56.000000009Z")
Expand Down Expand Up @@ -416,7 +403,7 @@ func TestCustomEncodeDecodeTuple1(t *testing.T) {
func TestCustomDecodeFunction(t *testing.T) {
skipIfDatetimeUnsupported(t)

conn := connectWithValidation(t)
conn := test_helpers.ConnectWithValidation(t, server, opts)
defer conn.Close()

// Call function 'call_datetime_testdata' returning a table of custom tuples.
Expand Down Expand Up @@ -459,7 +446,7 @@ func TestCustomDecodeFunction(t *testing.T) {
func TestCustomEncodeDecodeTuple5(t *testing.T) {
skipIfDatetimeUnsupported(t)

conn := connectWithValidation(t)
conn := test_helpers.ConnectWithValidation(t, server, opts)
defer conn.Close()

tm := time.Unix(500, 1000)
Expand Down
19 changes: 3 additions & 16 deletions decimal/decimal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -337,19 +337,6 @@ func BenchmarkMPDecodeDecimal(b *testing.B) {
}
}

func connectWithValidation(t *testing.T) *Connection {
t.Helper()

conn, err := Connect(server, opts)
if err != nil {
t.Fatalf("Failed to connect: %s", err.Error())
}
if conn == nil {
t.Fatalf("conn is nil after Connect")
}
return conn
}

func tupleValueIsDecimal(t *testing.T, tuples []interface{}, number decimal.Decimal) {
if len(tuples) != 1 {
t.Fatalf("Response Data len (%d) != 1", len(tuples))
Expand Down Expand Up @@ -499,7 +486,7 @@ func BenchmarkDecodeStringFromBCD(b *testing.B) {
func TestSelect(t *testing.T) {
skipIfDecimalUnsupported(t)

conn := connectWithValidation(t)
conn := test_helpers.ConnectWithValidation(t, server, opts)
defer conn.Close()

number, err := decimal.NewFromString("-12.34")
Expand Down Expand Up @@ -559,7 +546,7 @@ func assertInsert(t *testing.T, conn *Connection, numString string) {
func TestInsert(t *testing.T) {
skipIfDecimalUnsupported(t)

conn := connectWithValidation(t)
conn := test_helpers.ConnectWithValidation(t, server, opts)
defer conn.Close()

samples := append(correctnessSamples, benchmarkSamples...)
Expand All @@ -573,7 +560,7 @@ func TestInsert(t *testing.T) {
func TestReplace(t *testing.T) {
skipIfDecimalUnsupported(t)

conn := connectWithValidation(t)
conn := test_helpers.ConnectWithValidation(t, server, opts)
defer conn.Close()

number, err := decimal.NewFromString("-12.34")
Expand Down
Loading

0 comments on commit d04f8be

Please sign in to comment.