1
1
package contact
2
2
3
3
import (
4
+ "github.com/stretchr/testify/assert"
4
5
"net/http"
5
6
"testing"
6
7
"time"
@@ -15,9 +16,8 @@ func TestMain(m *testing.M) {
15
16
func TestCreateWithEmptyMSISDN (t * testing.T ) {
16
17
client := mbtest .Client (t )
17
18
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 )
21
21
}
22
22
23
23
func TestCreate (t * testing.T ) {
@@ -31,97 +31,53 @@ func TestCreate(t *testing.T) {
31
31
Custom1 : "First" ,
32
32
Custom2 : "Second" ,
33
33
})
34
- if err != nil {
35
- t .Fatalf ("unexpected error creating Contact: %s" , err )
36
- }
34
+ assert .NoError (t , err )
37
35
38
36
mbtest .AssertEndpointCalled (t , http .MethodPost , "/contacts" )
39
37
mbtest .AssertTestdata (t , "contactRequestObjectCreate.json" , mbtest .Request .Body )
40
38
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 )
52
40
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 )
68
47
}
69
48
70
49
func TestDelete (t * testing.T ) {
71
50
mbtest .WillReturn ([]byte ("" ), http .StatusNoContent )
72
51
client := mbtest .Client (t )
73
52
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 )
77
55
78
56
mbtest .AssertEndpointCalled (t , http .MethodDelete , "/contacts/contact-id" )
79
57
}
80
58
81
59
func TestDeleteWithEmptyID (t * testing.T ) {
82
60
client := mbtest .Client (t )
83
61
84
- if err := Delete (client , "" ); err == nil {
85
- t .Fatalf ("expected error, got nil" )
86
- }
62
+ err := Delete (client , "" )
63
+ assert .Error (t , err )
87
64
}
88
65
89
66
func TestList (t * testing.T ) {
90
67
mbtest .WillReturnTestdata (t , "contactListObject.json" , http .StatusOK )
91
68
client := mbtest .Client (t )
92
69
93
70
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 )
113
72
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 )
117
78
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 )
125
81
126
82
mbtest .AssertEndpointCalled (t , http .MethodGet , "/contacts" )
127
83
}
@@ -140,13 +96,10 @@ func TestListPagination(t *testing.T) {
140
96
141
97
for _ , tc := range tt {
142
98
_ , err := List (client , tc .options )
143
- if err != nil {
144
- t .Fatalf ("unexpected error listing contacts: %s" , err )
145
- }
99
+ assert .NoError (t , err )
146
100
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 )
150
103
}
151
104
}
152
105
@@ -155,85 +108,40 @@ func TestRead(t *testing.T) {
155
108
client := mbtest .Client (t )
156
109
157
110
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 )
161
112
162
113
mbtest .AssertEndpointCalled (t , http .MethodGet , "/contacts/contact-id" )
163
114
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 )
199
124
200
125
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 ))
204
127
205
128
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 ))
209
130
}
210
131
211
132
func TestReadWithCustomDetails (t * testing.T ) {
212
133
mbtest .WillReturnTestdata (t , "contactObjectWithCustomDetails.json" , http .StatusOK )
213
134
client := mbtest .Client (t )
214
135
215
136
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 )
219
138
220
139
mbtest .AssertEndpointCalled (t , http .MethodGet , "/contacts/contact-id" )
221
140
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 )
237
145
}
238
146
239
147
func TestUpdate (t * testing.T ) {
@@ -249,9 +157,8 @@ func TestUpdate(t *testing.T) {
249
157
}
250
158
251
159
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 )
255
162
256
163
mbtest .AssertEndpointCalled (t , http .MethodPatch , "/contacts/contact-id" )
257
164
mbtest .AssertTestdata (t , tc .expectedTestdata , mbtest .Request .Body )
0 commit comments