Skip to content

Commit 663a641

Browse files
committed
tests: migrate crud package to testify
The crud tests used a mix of standard testing package (t.Error, t.Fatal) and testify. This inconsistency made the codebase harder to maintain and provided less helpful failure messages. Migrated all assertions to use testify/assert and testify/require for better error messages and consistency with the rest of the codebase.
1 parent 1f2b310 commit 663a641

3 files changed

Lines changed: 75 additions & 180 deletions

File tree

crud/operations_test.go

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import (
44
"bytes"
55
"testing"
66

7+
"github.com/stretchr/testify/assert"
8+
"github.com/stretchr/testify/require"
79
"github.com/vmihailenco/msgpack/v5"
810

911
"github.com/tarantool/go-tarantool/v3/crud"
@@ -104,20 +106,12 @@ func TestOperation_EncodeMsgpack(t *testing.T) {
104106
t.Run(test.name, func(t *testing.T) {
105107
var refBuf bytes.Buffer
106108
encRef := msgpack.NewEncoder(&refBuf)
107-
if err := encRef.Encode(test.ref); err != nil {
108-
t.Errorf("error while encoding: %v", err.Error())
109-
}
109+
require.NoError(t, encRef.Encode(test.ref), "error while encoding reference")
110110

111111
var buf bytes.Buffer
112112
enc := msgpack.NewEncoder(&buf)
113-
114-
if err := enc.Encode(test.op); err != nil {
115-
t.Errorf("error while encoding: %v", err.Error())
116-
}
117-
if !bytes.Equal(refBuf.Bytes(), buf.Bytes()) {
118-
t.Errorf("encode response is wrong:\n expected %v\n got: %v",
119-
refBuf, buf.Bytes())
120-
}
113+
require.NoError(t, enc.Encode(test.op), "error while encoding operation")
114+
assert.Equal(t, refBuf.Bytes(), buf.Bytes(), "encode response is wrong")
121115
})
122116
}
123117
}

crud/request_test.go

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import (
66
"fmt"
77
"testing"
88

9+
"github.com/stretchr/testify/assert"
10+
"github.com/stretchr/testify/require"
911
"github.com/tarantool/go-iproto"
1012
"github.com/tarantool/go-option"
1113
"github.com/vmihailenco/msgpack/v5"
@@ -78,18 +80,12 @@ func assertBodyEqual(t testing.TB, reference tarantool.Request, req tarantool.Re
7880
t.Helper()
7981

8082
reqBody, err := extractRequestBody(req)
81-
if err != nil {
82-
t.Fatalf("An unexpected Response.Body() error: %q", err.Error())
83-
}
83+
require.NoError(t, err, "An unexpected Response.Body() error")
8484

8585
refBody, err := extractRequestBody(reference)
86-
if err != nil {
87-
t.Fatalf("An unexpected Response.Body() error: %q", err.Error())
88-
}
86+
require.NoError(t, err, "An unexpected Response.Body() error")
8987

90-
if !bytes.Equal(reqBody, refBody) {
91-
t.Errorf("Encoded request %v != reference %v", reqBody, refBody)
92-
}
88+
assert.Equal(t, refBody, reqBody, "Encoded request body mismatch")
9389
}
9490

9591
func BenchmarkLenRequest(b *testing.B) {
@@ -164,9 +160,7 @@ func TestRequestsCodes(t *testing.T) {
164160
}
165161

166162
for _, test := range tests {
167-
if rtype := test.req.Type(); rtype != test.rtype {
168-
t.Errorf("An invalid request type 0x%x, expected 0x%x", rtype, test.rtype)
169-
}
163+
assert.Equal(t, test.rtype, test.req.Type(), "An invalid request type")
170164
}
171165
}
172166

@@ -202,9 +196,7 @@ func TestRequestsAsync(t *testing.T) {
202196
}
203197

204198
for _, test := range tests {
205-
if async := test.req.Async(); async != test.async {
206-
t.Errorf("An invalid async %t, expected %t", async, test.async)
207-
}
199+
assert.Equal(t, test.async, test.req.Async(), "An invalid async value")
208200
}
209201
}
210202

@@ -240,9 +232,7 @@ func TestRequestsCtx_default(t *testing.T) {
240232
}
241233

242234
for _, test := range tests {
243-
if ctx := test.req.Ctx(); ctx != test.expected {
244-
t.Errorf("An invalid ctx %t, expected %t", ctx, test.expected)
245-
}
235+
assert.Equal(t, test.expected, test.req.Ctx(), "An invalid ctx value")
246236
}
247237
}
248238

@@ -279,9 +269,7 @@ func TestRequestsCtx_setter(t *testing.T) {
279269
}
280270

281271
for _, test := range tests {
282-
if ctx := test.req.Ctx(); ctx != test.expected {
283-
t.Errorf("An invalid ctx %t, expected %t", ctx, test.expected)
284-
}
272+
assert.Equal(t, test.expected, test.req.Ctx(), "An invalid ctx value")
285273
}
286274
}
287275

0 commit comments

Comments
 (0)