Skip to content

Commit 4bca38b

Browse files
author
Lucas Michot
authored
Use testify for testing. (#99)
* Use testify for testing. * Do not test for 1.12.x and 1.13.x.
1 parent 80f709f commit 4bca38b

27 files changed

+527
-1478
lines changed

.github/workflows/tests.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ jobs:
1111
strategy:
1212
fail-fast: true
1313
matrix:
14-
go-version: [ 1.12.x, 1.13.x, 1.14.x, 1.15.x, 1.16.x ]
14+
go-version: [ 1.14.x, 1.15.x, 1.16.x ]
1515

1616
name: Go ${{ matrix.go-version }}
1717

balance/balance_test.go

Lines changed: 11 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,9 @@ import (
66

77
messagebird "github.com/messagebird/go-rest-api/v6"
88
"github.com/messagebird/go-rest-api/v6/internal/mbtest"
9+
"github.com/stretchr/testify/assert"
910
)
1011

11-
const Epsilon float32 = 0.001
12-
13-
func cmpFloat32(a, b float32) bool {
14-
return (a-b) < Epsilon && (b-a) < Epsilon
15-
}
16-
1712
func TestMain(m *testing.M) {
1813
mbtest.EnableServer(m)
1914
}
@@ -23,21 +18,14 @@ func TestRead(t *testing.T) {
2318
client := mbtest.Client(t)
2419

2520
balance, err := Read(client)
26-
if err != nil {
27-
t.Fatalf("Didn't expect error while fetching the balance: %s", err)
28-
}
2921

30-
if balance.Payment != "prepaid" {
31-
t.Errorf("Unexpected balance payment: %s", balance.Payment)
32-
}
22+
assert.NoError(t, err)
3323

34-
if balance.Type != "credits" {
35-
t.Errorf("Unexpected balance type: %s", balance.Type)
36-
}
24+
assert.Equal(t, "prepaid", balance.Payment)
3725

38-
if !cmpFloat32(balance.Amount, 9.2) {
39-
t.Errorf("Unexpected balance amount: %.2f", balance.Amount)
40-
}
26+
assert.Equal(t, "credits", balance.Type)
27+
28+
assert.EqualValuesf(t, 9.2, balance.Amount, "Unexpected balance amount: %.2f", balance.Amount)
4129
}
4230

4331
func TestReadError(t *testing.T) {
@@ -47,19 +35,12 @@ func TestReadError(t *testing.T) {
4735
_, err := Read(client)
4836

4937
errorResponse, ok := err.(messagebird.ErrorResponse)
50-
if !ok {
51-
t.Fatalf("Expected ErrorResponse to be returned, instead I got %s", err)
52-
}
5338

54-
if len(errorResponse.Errors) != 1 {
55-
t.Fatalf("Unexpected number of errors: %d, expected: 1", len(errorResponse.Errors))
56-
}
39+
assert.True(t, ok)
40+
41+
assert.Len(t, errorResponse.Errors, 1)
5742

58-
if errorResponse.Errors[0].Code != 2 {
59-
t.Errorf("Unexpected error code: %d, expected: 2", errorResponse.Errors[0].Code)
60-
}
43+
assert.Equal(t, 2, errorResponse.Errors[0].Code)
6144

62-
if errorResponse.Errors[0].Parameter != "access_key" {
63-
t.Errorf("Unexpected error parameter: %s, expected: access_key", errorResponse.Errors[0].Parameter)
64-
}
45+
assert.Equal(t, "access_key", errorResponse.Errors[0].Parameter)
6546
}

contact/contact_test.go

Lines changed: 45 additions & 138 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package contact
22

33
import (
4+
"github.com/stretchr/testify/assert"
45
"net/http"
56
"testing"
67
"time"
@@ -15,9 +16,8 @@ func TestMain(m *testing.M) {
1516
func TestCreateWithEmptyMSISDN(t *testing.T) {
1617
client := mbtest.Client(t)
1718

18-
if _, err := Create(client, &Request{}); err == nil {
19-
t.Fatalf("expected error, got nil")
20-
}
19+
_, err := Create(client, &Request{})
20+
assert.Error(t, err)
2121
}
2222

2323
func TestCreate(t *testing.T) {
@@ -31,97 +31,53 @@ func TestCreate(t *testing.T) {
3131
Custom1: "First",
3232
Custom2: "Second",
3333
})
34-
if err != nil {
35-
t.Fatalf("unexpected error creating Contact: %s", err)
36-
}
34+
assert.NoError(t, err)
3735

3836
mbtest.AssertEndpointCalled(t, http.MethodPost, "/contacts")
3937
mbtest.AssertTestdata(t, "contactRequestObjectCreate.json", mbtest.Request.Body)
4038

41-
if contact.MSISDN != 31612345678 {
42-
t.Fatalf("expected 31612345678, got %d", contact.MSISDN)
43-
}
44-
45-
if contact.FirstName != "Foo" {
46-
t.Fatalf("expected Foo, got %s", contact.FirstName)
47-
}
48-
49-
if contact.LastName != "Bar" {
50-
t.Fatalf("expected Bar, got %s", contact.LastName)
51-
}
39+
assert.Equal(t, int64(31612345678), contact.MSISDN)
5240

53-
if contact.CustomDetails.Custom1 != "First" {
54-
t.Fatalf("expected First, got %s", contact.CustomDetails.Custom1)
55-
}
56-
57-
if contact.CustomDetails.Custom2 != "Second" {
58-
t.Fatalf("expected Second, got %s", contact.CustomDetails.Custom2)
59-
}
60-
61-
if contact.CustomDetails.Custom3 != "Third" {
62-
t.Fatalf("expected Third, got %s", contact.CustomDetails.Custom3)
63-
}
64-
65-
if contact.CustomDetails.Custom4 != "Fourth" {
66-
t.Fatalf("expected Fourth, got %s", contact.CustomDetails.Custom4)
67-
}
41+
assert.Equal(t, "Foo", contact.FirstName)
42+
assert.Equal(t, "Bar", contact.LastName)
43+
assert.Equal(t, "First", contact.CustomDetails.Custom1)
44+
assert.Equal(t, "Second", contact.CustomDetails.Custom2)
45+
assert.Equal(t, "Third", contact.CustomDetails.Custom3)
46+
assert.Equal(t, "Fourth", contact.CustomDetails.Custom4)
6847
}
6948

7049
func TestDelete(t *testing.T) {
7150
mbtest.WillReturn([]byte(""), http.StatusNoContent)
7251
client := mbtest.Client(t)
7352

74-
if err := Delete(client, "contact-id"); err != nil {
75-
t.Fatalf("unexpected error deleting Contact: %s", err)
76-
}
53+
err := Delete(client, "contact-id")
54+
assert.NoError(t, err)
7755

7856
mbtest.AssertEndpointCalled(t, http.MethodDelete, "/contacts/contact-id")
7957
}
8058

8159
func TestDeleteWithEmptyID(t *testing.T) {
8260
client := mbtest.Client(t)
8361

84-
if err := Delete(client, ""); err == nil {
85-
t.Fatalf("expected error, got nil")
86-
}
62+
err := Delete(client, "")
63+
assert.Error(t, err)
8764
}
8865

8966
func TestList(t *testing.T) {
9067
mbtest.WillReturnTestdata(t, "contactListObject.json", http.StatusOK)
9168
client := mbtest.Client(t)
9269

9370
list, err := List(client, DefaultListOptions)
94-
if err != nil {
95-
t.Fatalf("unexpected error retrieving Contact list: %s", err)
96-
}
97-
98-
if list.Offset != 0 {
99-
t.Fatalf("expected 0, got %d", list.Offset)
100-
}
101-
102-
if list.Limit != 20 {
103-
t.Fatalf("expected 0, got %d", list.Limit)
104-
}
105-
106-
if list.Count != 2 {
107-
t.Fatalf("expected 2, got %d", list.Count)
108-
}
109-
110-
if list.TotalCount != 2 {
111-
t.Fatalf("expected 2, got %d", list.TotalCount)
112-
}
71+
assert.NoError(t, err)
11372

114-
if actualCount := len(list.Items); actualCount != 2 {
115-
t.Fatalf("expected 2, got %d", actualCount)
116-
}
73+
assert.Equal(t, 0, list.Offset)
74+
assert.Equal(t, 20, list.Limit)
75+
assert.Equal(t, 2, list.Count)
76+
assert.Equal(t, 2, list.TotalCount)
77+
assert.Len(t, list.Items, 2)
11778

118-
if list.Items[0].ID != "first-id" {
119-
t.Fatalf("expected first-id, got %s", list.Items[0].ID)
120-
}
121-
122-
if list.Items[1].ID != "second-id" {
123-
t.Fatalf("expected second-id, got %s", list.Items[1].ID)
124-
}
79+
assert.Equal(t, "first-id", list.Items[0].ID)
80+
assert.Equal(t, "second-id", list.Items[1].ID)
12581

12682
mbtest.AssertEndpointCalled(t, http.MethodGet, "/contacts")
12783
}
@@ -140,13 +96,10 @@ func TestListPagination(t *testing.T) {
14096

14197
for _, tc := range tt {
14298
_, err := List(client, tc.options)
143-
if err != nil {
144-
t.Fatalf("unexpected error listing contacts: %s", err)
145-
}
99+
assert.NoError(t, err)
146100

147-
if query := mbtest.Request.URL.RawQuery; query != tc.expected {
148-
t.Fatalf("expected %s, got %s", tc.expected, query)
149-
}
101+
query := mbtest.Request.URL.RawQuery
102+
assert.Equal(t, tc.expected, query)
150103
}
151104
}
152105

@@ -155,85 +108,40 @@ func TestRead(t *testing.T) {
155108
client := mbtest.Client(t)
156109

157110
contact, err := Read(client, "contact-id")
158-
if err != nil {
159-
t.Fatalf("unexpected error reading Contact: %s", err)
160-
}
111+
assert.NoError(t, err)
161112

162113
mbtest.AssertEndpointCalled(t, http.MethodGet, "/contacts/contact-id")
163114

164-
if contact.ID != "contact-id" {
165-
t.Fatalf("expected contact-id, got %s", contact.ID)
166-
}
167-
168-
if contact.HRef != "https://rest.messagebird.com/contacts/contact-id" {
169-
t.Fatalf("expected https://rest.messagebird.com/contacts/contact-id, got %s", contact.HRef)
170-
}
171-
172-
if contact.MSISDN != 31612345678 {
173-
t.Fatalf("expected 31612345678, got %d", contact.MSISDN)
174-
}
175-
176-
if contact.FirstName != "Foo" {
177-
t.Fatalf("expected Foo, got %s", contact.FirstName)
178-
}
179-
180-
if contact.LastName != "Bar" {
181-
t.Fatalf("expected Bar, got %s", contact.LastName)
182-
}
183-
184-
if contact.Groups.TotalCount != 3 {
185-
t.Fatalf("expected 3, got %d", contact.Groups.TotalCount)
186-
}
187-
188-
if contact.Groups.HRef != "https://rest.messagebird.com/contacts/contact-id/groups" {
189-
t.Fatalf("expected https://rest.messagebird.com/contacts/contact-id/groups, got %s", contact.Groups.HRef)
190-
}
191-
192-
if contact.Messages.TotalCount != 5 {
193-
t.Fatalf("expected 5, got %d", contact.Messages.TotalCount)
194-
}
195-
196-
if contact.Messages.HRef != "https://rest.messagebird.com/contacts/contact-id/messages" {
197-
t.Fatalf("expected https://rest.messagebird.com/contacts/contact-id/messages, got %s", contact.Messages.HRef)
198-
}
115+
assert.Equal(t, "contact-id", contact.ID)
116+
assert.Equal(t, "https://rest.messagebird.com/contacts/contact-id", contact.HRef)
117+
assert.Equal(t, int64(31612345678), contact.MSISDN)
118+
assert.Equal(t, "Foo", contact.FirstName)
119+
assert.Equal(t, "Bar", contact.LastName)
120+
assert.Equal(t, 3, contact.Groups.TotalCount)
121+
assert.Equal(t, "https://rest.messagebird.com/contacts/contact-id/groups", contact.Groups.HRef)
122+
assert.Equal(t, 5, contact.Messages.TotalCount)
123+
assert.Equal(t, "https://rest.messagebird.com/contacts/contact-id/messages", contact.Messages.HRef)
199124

200125
expectedCreatedDatetime, _ := time.Parse(time.RFC3339, "2018-07-13T10:34:08+00:00")
201-
if !contact.CreatedDatetime.Equal(expectedCreatedDatetime) {
202-
t.Fatalf("expected %s, got %s", expectedCreatedDatetime, contact.CreatedDatetime)
203-
}
126+
assert.True(t, contact.CreatedDatetime.Equal(expectedCreatedDatetime))
204127

205128
expectedUpdatedDatetime, _ := time.Parse(time.RFC3339, "2018-07-13T10:44:08+00:00")
206-
if !contact.UpdatedDatetime.Equal(expectedUpdatedDatetime) {
207-
t.Fatalf("expected %s, got %s", expectedUpdatedDatetime, contact.UpdatedDatetime)
208-
}
129+
assert.True(t, contact.UpdatedDatetime.Equal(expectedUpdatedDatetime))
209130
}
210131

211132
func TestReadWithCustomDetails(t *testing.T) {
212133
mbtest.WillReturnTestdata(t, "contactObjectWithCustomDetails.json", http.StatusOK)
213134
client := mbtest.Client(t)
214135

215136
contact, err := Read(client, "contact-id")
216-
if err != nil {
217-
t.Fatalf("unexpected error reading Contact with custom details: %s", err)
218-
}
137+
assert.NoError(t, err)
219138

220139
mbtest.AssertEndpointCalled(t, http.MethodGet, "/contacts/contact-id")
221140

222-
if contact.CustomDetails.Custom1 != "First" {
223-
t.Fatalf("expected First, got %s", contact.CustomDetails.Custom1)
224-
}
225-
226-
if contact.CustomDetails.Custom2 != "Second" {
227-
t.Fatalf("expected Second, got %s", contact.CustomDetails.Custom2)
228-
}
229-
230-
if contact.CustomDetails.Custom3 != "Third" {
231-
t.Fatalf("expected Third, got %s", contact.CustomDetails.Custom3)
232-
}
233-
234-
if contact.CustomDetails.Custom4 != "Fourth" {
235-
t.Fatalf("expected Fourth, got %s", contact.CustomDetails.Custom4)
236-
}
141+
assert.Equal(t, "First", contact.CustomDetails.Custom1)
142+
assert.Equal(t, "Second", contact.CustomDetails.Custom2)
143+
assert.Equal(t, "Third", contact.CustomDetails.Custom3)
144+
assert.Equal(t, "Fourth", contact.CustomDetails.Custom4)
237145
}
238146

239147
func TestUpdate(t *testing.T) {
@@ -249,9 +157,8 @@ func TestUpdate(t *testing.T) {
249157
}
250158

251159
for _, tc := range tt {
252-
if _, err := Update(client, "contact-id", tc.contactRequest); err != nil {
253-
t.Fatalf("unexpected error updating Contact: %s\n", err)
254-
}
160+
_, err := Update(client, "contact-id", tc.contactRequest)
161+
assert.NoError(t, err)
255162

256163
mbtest.AssertEndpointCalled(t, http.MethodPatch, "/contacts/contact-id")
257164
mbtest.AssertTestdata(t, tc.expectedTestdata, mbtest.Request.Body)

0 commit comments

Comments
 (0)