Skip to content

Commit d04f8be

Browse files
committed
code health: add test_helpers.ConnectWithValidation
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.
1 parent 3b98329 commit d04f8be

File tree

9 files changed

+96
-237
lines changed

9 files changed

+96
-237
lines changed

call_16_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,14 @@ import (
77
"testing"
88

99
. "github.com/tarantool/go-tarantool"
10+
"github.com/tarantool/go-tarantool/test_helpers"
1011
)
1112

1213
func TestConnection_Call(t *testing.T) {
1314
var resp *Response
1415
var err error
1516

16-
conn := connect(t, server, opts)
17+
conn := test_helpers.ConnectWithValidation(t, server, opts)
1718
defer conn.Close()
1819

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

33-
conn := connect(t, server, opts)
34+
conn := test_helpers.ConnectWithValidation(t, server, opts)
3435
defer conn.Close()
3536

3637
req := NewCallRequest("simple_incr").Args([]interface{}{1})

call_17_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,14 @@ import (
77
"testing"
88

99
. "github.com/tarantool/go-tarantool"
10+
"github.com/tarantool/go-tarantool/test_helpers"
1011
)
1112

1213
func TestConnection_Call(t *testing.T) {
1314
var resp *Response
1415
var err error
1516

16-
conn := connect(t, server, opts)
17+
conn := test_helpers.ConnectWithValidation(t, server, opts)
1718
defer conn.Close()
1819

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

33-
conn := connect(t, server, opts)
34+
conn := test_helpers.ConnectWithValidation(t, server, opts)
3435
defer conn.Close()
3536

3637
req := NewCallRequest("simple_incr").Args([]interface{}{1})

connection_pool/connection_pool_test.go

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -739,10 +739,7 @@ func TestInsert(t *testing.T) {
739739

740740
// Connect to servers[2] to check if tuple
741741
// was inserted on RW instance
742-
conn, err := tarantool.Connect(servers[2], connOpts)
743-
require.Nilf(t, err, "failed to connect %s", servers[2])
744-
require.NotNilf(t, conn, "conn is nil after Connect")
745-
742+
conn := test_helpers.ConnectWithValidation(t, servers[2], connOpts)
746743
defer conn.Close()
747744

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

813810
// Connect to servers[2] to check if tuple
814811
// was inserted on RW instance
815-
conn, err := tarantool.Connect(servers[2], connOpts)
816-
require.Nilf(t, err, "failed to connect %s", servers[2])
817-
require.NotNilf(t, conn, "conn is nil after Connect")
818-
812+
conn := test_helpers.ConnectWithValidation(t, servers[2], connOpts)
819813
defer conn.Close()
820814

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

874868
// Connect to servers[2] to check if tuple
875869
// was inserted on RW instance
876-
conn, err := tarantool.Connect(servers[2], connOpts)
877-
require.Nilf(t, err, "failed to connect %s", servers[2])
878-
require.NotNilf(t, conn, "conn is nil after Connect")
879-
870+
conn := test_helpers.ConnectWithValidation(t, servers[2], connOpts)
880871
defer conn.Close()
881872

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

942933
// Connect to servers[2] to check if tuple
943934
// was inserted on RW instance
944-
conn, err := tarantool.Connect(servers[2], connOpts)
945-
require.Nilf(t, err, "failed to connect %s", servers[2])
946-
require.NotNilf(t, conn, "conn is nil after Connect")
947-
935+
conn := test_helpers.ConnectWithValidation(t, servers[2], connOpts)
948936
defer conn.Close()
949937

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

10271015
// Connect to servers[2] to check if tuple
10281016
// was inserted on RW instance
1029-
conn, err := tarantool.Connect(servers[2], connOpts)
1030-
require.Nilf(t, err, "failed to connect %s", servers[2])
1031-
require.NotNilf(t, conn, "conn is nil after Connect")
1032-
1017+
conn := test_helpers.ConnectWithValidation(t, servers[2], connOpts)
10331018
defer conn.Close()
10341019

10351020
resp, err := conn.Insert(spaceNo, []interface{}{"replace_key", "replace_value"})

datetime/datetime_test.go

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -33,19 +33,6 @@ var spaceTuple1 = "testDatetime_1"
3333
var spaceTuple2 = "testDatetime_2"
3434
var index = "primary"
3535

36-
func connectWithValidation(t *testing.T) *Connection {
37-
t.Helper()
38-
39-
conn, err := Connect(server, opts)
40-
if err != nil {
41-
t.Fatalf("Failed to connect: %s", err.Error())
42-
}
43-
if conn == nil {
44-
t.Fatalf("conn is nil after Connect")
45-
}
46-
return conn
47-
}
48-
4936
func skipIfDatetimeUnsupported(t *testing.T) {
5037
t.Helper()
5138

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

171-
conn := connectWithValidation(t)
158+
conn := test_helpers.ConnectWithValidation(t, server, opts)
172159
defer conn.Close()
173160

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

191-
conn := connectWithValidation(t)
178+
conn := test_helpers.ConnectWithValidation(t, server, opts)
192179
defer conn.Close()
193180

194181
tupleInsertSelectDelete(t, conn, maxTime)
@@ -197,7 +184,7 @@ func TestDatetimeMax(t *testing.T) {
197184
func TestDatetimeMin(t *testing.T) {
198185
skipIfDatetimeUnsupported(t)
199186

200-
conn := connectWithValidation(t)
187+
conn := test_helpers.ConnectWithValidation(t, server, opts)
201188
defer conn.Close()
202189

203190
tupleInsertSelectDelete(t, conn, minTime)
@@ -206,7 +193,7 @@ func TestDatetimeMin(t *testing.T) {
206193
func TestDatetimeReplace(t *testing.T) {
207194
skipIfDatetimeUnsupported(t)
208195

209-
conn := connectWithValidation(t)
196+
conn := test_helpers.ConnectWithValidation(t, server, opts)
210197
defer conn.Close()
211198

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

359-
conn := connectWithValidation(t)
346+
conn := test_helpers.ConnectWithValidation(t, server, opts)
360347
defer conn.Close()
361348

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

419-
conn := connectWithValidation(t)
406+
conn := test_helpers.ConnectWithValidation(t, server, opts)
420407
defer conn.Close()
421408

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

462-
conn := connectWithValidation(t)
449+
conn := test_helpers.ConnectWithValidation(t, server, opts)
463450
defer conn.Close()
464451

465452
tm := time.Unix(500, 1000)

decimal/decimal_test.go

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -337,19 +337,6 @@ func BenchmarkMPDecodeDecimal(b *testing.B) {
337337
}
338338
}
339339

340-
func connectWithValidation(t *testing.T) *Connection {
341-
t.Helper()
342-
343-
conn, err := Connect(server, opts)
344-
if err != nil {
345-
t.Fatalf("Failed to connect: %s", err.Error())
346-
}
347-
if conn == nil {
348-
t.Fatalf("conn is nil after Connect")
349-
}
350-
return conn
351-
}
352-
353340
func tupleValueIsDecimal(t *testing.T, tuples []interface{}, number decimal.Decimal) {
354341
if len(tuples) != 1 {
355342
t.Fatalf("Response Data len (%d) != 1", len(tuples))
@@ -499,7 +486,7 @@ func BenchmarkDecodeStringFromBCD(b *testing.B) {
499486
func TestSelect(t *testing.T) {
500487
skipIfDecimalUnsupported(t)
501488

502-
conn := connectWithValidation(t)
489+
conn := test_helpers.ConnectWithValidation(t, server, opts)
503490
defer conn.Close()
504491

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

562-
conn := connectWithValidation(t)
549+
conn := test_helpers.ConnectWithValidation(t, server, opts)
563550
defer conn.Close()
564551

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

576-
conn := connectWithValidation(t)
563+
conn := test_helpers.ConnectWithValidation(t, server, opts)
577564
defer conn.Close()
578565

579566
number, err := decimal.NewFromString("-12.34")

0 commit comments

Comments
 (0)