-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathgoals_test.go
623 lines (510 loc) · 16.1 KB
/
goals_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
package tonicpow
import (
"fmt"
"net/http"
"testing"
"github.com/stretchr/testify/assert"
)
// newTestGoal will return a dummy example for tests
func newTestGoal() *Goal {
return &Goal{
CampaignID: testCampaignID,
Description: "This is an example goal",
ID: testGoalID,
MaxPerPromoter: 1,
Name: testGoalName,
PayoutRate: 0.01,
PayoutType: "flat",
Title: "Example Goal",
}
}
// TestClient_CreateGoal will test the method CreateGoal()
func TestClient_CreateGoal(t *testing.T) {
// t.Parallel() (Cannot run in parallel - issues with overriding the mock client)
t.Run("create a goal (success)", func(t *testing.T) {
client, err := newTestClient()
assert.NoError(t, err)
assert.NotNil(t, client)
goal := newTestGoal()
endpoint := fmt.Sprintf("%s/%s", EnvironmentDevelopment.apiURL, modelGoal)
err = mockResponseData(http.MethodPost, endpoint, http.StatusCreated, goal)
assert.NoError(t, err)
var response *StandardResponse
response, err = client.CreateGoal(goal)
assert.NoError(t, err)
assert.NotNil(t, goal)
assert.NotNil(t, response)
assert.Equal(t, testGoalID, goal.ID)
})
t.Run("missing campaign id", func(t *testing.T) {
client, err := newTestClient()
assert.NoError(t, err)
assert.NotNil(t, client)
goal := newTestGoal()
goal.CampaignID = 0
endpoint := fmt.Sprintf("%s/%s", EnvironmentDevelopment.apiURL, modelGoal)
err = mockResponseData(http.MethodPost, endpoint, http.StatusCreated, goal)
assert.NoError(t, err)
var response *StandardResponse
response, err = client.CreateGoal(goal)
assert.Error(t, err)
assert.Nil(t, response)
})
t.Run("missing goal name", func(t *testing.T) {
client, err := newTestClient()
assert.NoError(t, err)
assert.NotNil(t, client)
goal := newTestGoal()
goal.Name = ""
endpoint := fmt.Sprintf("%s/%s", EnvironmentDevelopment.apiURL, modelGoal)
err = mockResponseData(http.MethodPost, endpoint, http.StatusCreated, goal)
assert.NoError(t, err)
var response *StandardResponse
response, err = client.CreateGoal(goal)
assert.Error(t, err)
assert.Nil(t, response)
})
t.Run("error from api (status code)", func(t *testing.T) {
client, err := newTestClient()
assert.NoError(t, err)
assert.NotNil(t, client)
goal := newTestGoal()
endpoint := fmt.Sprintf("%s/%s", EnvironmentDevelopment.apiURL, modelGoal)
err = mockResponseData(http.MethodPost, endpoint, http.StatusBadRequest, goal)
assert.NoError(t, err)
var response *StandardResponse
response, err = client.CreateGoal(goal)
assert.Error(t, err)
assert.NotNil(t, response)
})
t.Run("error from api (api error)", func(t *testing.T) {
client, err := newTestClient()
assert.NoError(t, err)
assert.NotNil(t, client)
goal := newTestGoal()
endpoint := fmt.Sprintf("%s/%s", EnvironmentDevelopment.apiURL, modelGoal)
apiError := &Error{
Code: 400,
Data: "field_name",
IPAddress: "127.0.0.1",
Message: "some error message",
Method: http.MethodPut,
RequestGUID: "7f3d97a8fd67ff57861904df6118dcc8",
StatusCode: http.StatusBadRequest,
URL: endpoint,
}
err = mockResponseData(http.MethodPost, endpoint, http.StatusBadRequest, apiError)
assert.NoError(t, err)
var response *StandardResponse
response, err = client.CreateGoal(goal)
assert.Error(t, err)
assert.Equal(t, apiError.Message, err.Error())
assert.NotNil(t, response)
})
}
// ExampleClient_CreateGoal example using CreateGoal()
//
// See more examples in /examples/
func ExampleClient_CreateGoal() {
// Load the client (using test client for example only)
client, err := newTestClient()
if err != nil {
fmt.Printf("error loading client: %s", err.Error())
return
}
// Mock response (for example only)
responseGoal := newTestGoal()
_ = mockResponseData(
http.MethodPost,
fmt.Sprintf("%s/%s", EnvironmentDevelopment.apiURL, modelGoal),
http.StatusCreated,
responseGoal,
)
// Create goal (using mocking response)
if _, err = client.CreateGoal(responseGoal); err != nil {
fmt.Printf("error creating goal: " + err.Error())
return
}
fmt.Printf("created goal: %s", responseGoal.Name)
// Output:created goal: example_goal
}
// BenchmarkClient_CreateGoal benchmarks the method CreateGoal()
func BenchmarkClient_CreateGoal(b *testing.B) {
client, _ := newTestClient()
goal := newTestGoal()
_ = mockResponseData(
http.MethodPost,
fmt.Sprintf("%s/%s", EnvironmentDevelopment.apiURL, modelGoal),
http.StatusCreated,
goal,
)
for i := 0; i < b.N; i++ {
_, _ = client.CreateGoal(goal)
}
}
// TestClient_GetGoal will test the method GetGoal()
func TestClient_GetGoal(t *testing.T) {
// t.Parallel() (Cannot run in parallel - issues with overriding the mock client)
t.Run("get a goal (success)", func(t *testing.T) {
client, err := newTestClient()
assert.NoError(t, err)
assert.NotNil(t, client)
goal := newTestGoal()
endpoint := fmt.Sprintf(
"%s/%s/details/%d", EnvironmentDevelopment.apiURL,
modelGoal, goal.ID,
)
err = mockResponseData(http.MethodGet, endpoint, http.StatusOK, goal)
assert.NoError(t, err)
var newGoal *Goal
var response *StandardResponse
newGoal, response, err = client.GetGoal(goal.ID)
assert.NoError(t, err)
assert.NotNil(t, newGoal)
assert.NotNil(t, response)
assert.Equal(t, testGoalID, goal.ID)
})
t.Run("missing goal id", func(t *testing.T) {
client, err := newTestClient()
assert.NoError(t, err)
assert.NotNil(t, client)
goal := newTestGoal()
goal.ID = 0
endpoint := fmt.Sprintf(
"%s/%s/details/%d", EnvironmentDevelopment.apiURL,
modelGoal, goal.ID,
)
err = mockResponseData(http.MethodGet, endpoint, http.StatusOK, goal)
assert.NoError(t, err)
var newGoal *Goal
var response *StandardResponse
newGoal, response, err = client.GetGoal(goal.ID)
assert.Error(t, err)
assert.Nil(t, newGoal)
assert.Nil(t, response)
})
t.Run("error from api (status code)", func(t *testing.T) {
client, err := newTestClient()
assert.NoError(t, err)
assert.NotNil(t, client)
goal := newTestGoal()
endpoint := fmt.Sprintf(
"%s/%s/details/%d", EnvironmentDevelopment.apiURL,
modelGoal, goal.ID,
)
err = mockResponseData(http.MethodGet, endpoint, http.StatusBadRequest, goal)
assert.NoError(t, err)
var newGoal *Goal
var response *StandardResponse
newGoal, response, err = client.GetGoal(goal.ID)
assert.Error(t, err)
assert.Nil(t, newGoal)
assert.NotNil(t, response)
})
t.Run("error from api (api error)", func(t *testing.T) {
client, err := newTestClient()
assert.NoError(t, err)
assert.NotNil(t, client)
goal := newTestGoal()
endpoint := fmt.Sprintf(
"%s/%s/details/%d", EnvironmentDevelopment.apiURL,
modelGoal, goal.ID,
)
apiError := &Error{
Code: 400,
Data: "field_name",
IPAddress: "127.0.0.1",
Message: "some error message",
Method: http.MethodPut,
RequestGUID: "7f3d97a8fd67ff57861904df6118dcc8",
StatusCode: http.StatusBadRequest,
URL: endpoint,
}
err = mockResponseData(http.MethodGet, endpoint, http.StatusBadRequest, apiError)
assert.NoError(t, err)
var newGoal *Goal
var response *StandardResponse
newGoal, response, err = client.GetGoal(goal.ID)
assert.Error(t, err)
assert.Nil(t, newGoal)
assert.NotNil(t, response)
assert.Equal(t, apiError.Message, err.Error())
})
}
// ExampleClient_GetGoal example using GetGoal()
//
// See more examples in /examples/
func ExampleClient_GetGoal() {
// Load the client (using test client for example only)
client, err := newTestClient()
if err != nil {
fmt.Printf("error loading client: %s", err.Error())
return
}
// Mock response (for example only)
responseGoal := newTestGoal()
_ = mockResponseData(
http.MethodGet,
fmt.Sprintf(
"%s/%s/details/%d", EnvironmentDevelopment.apiURL,
modelGoal, responseGoal.ID,
),
http.StatusOK,
responseGoal,
)
// Get goal (using mocking response)
if responseGoal, _, err = client.GetGoal(responseGoal.ID); err != nil {
fmt.Printf("error getting goal: " + err.Error())
return
}
fmt.Printf("goal: %s", responseGoal.Name)
// Output:goal: example_goal
}
// BenchmarkClient_GetGoal benchmarks the method GetGoal()
func BenchmarkClient_GetGoal(b *testing.B) {
client, _ := newTestClient()
goal := newTestGoal()
_ = mockResponseData(
http.MethodGet,
fmt.Sprintf(
"%s/%s/details/%d", EnvironmentDevelopment.apiURL,
modelGoal, goal.ID,
),
http.StatusOK,
goal,
)
for i := 0; i < b.N; i++ {
_, _, _ = client.GetGoal(goal.ID)
}
}
// TestClient_UpdateCampaign will test the method UpdateCampaign()
func TestClient_UpdateGoal(t *testing.T) {
// t.Parallel() (Cannot run in parallel - issues with overriding the mock client)
t.Run("update a goal (success)", func(t *testing.T) {
client, err := newTestClient()
assert.NoError(t, err)
assert.NotNil(t, client)
goal := newTestGoal()
goal.Title = "Updated Title"
endpoint := fmt.Sprintf("%s/%s", EnvironmentDevelopment.apiURL, modelGoal)
err = mockResponseData(http.MethodPut, endpoint, http.StatusOK, goal)
assert.NoError(t, err)
var response *StandardResponse
response, err = client.UpdateGoal(goal)
assert.NoError(t, err)
assert.NotNil(t, goal)
assert.NotNil(t, response)
assert.Equal(t, "Updated Title", goal.Title)
})
t.Run("missing id", func(t *testing.T) {
client, err := newTestClient()
assert.NoError(t, err)
assert.NotNil(t, client)
goal := newTestGoal()
goal.ID = 0
endpoint := fmt.Sprintf("%s/%s", EnvironmentDevelopment.apiURL, modelGoal)
err = mockResponseData(http.MethodPut, endpoint, http.StatusOK, goal)
assert.NoError(t, err)
var response *StandardResponse
response, err = client.UpdateGoal(goal)
assert.Error(t, err)
assert.Nil(t, response)
})
t.Run("error from api (status code)", func(t *testing.T) {
client, err := newTestClient()
assert.NoError(t, err)
assert.NotNil(t, client)
goal := newTestGoal()
endpoint := fmt.Sprintf("%s/%s", EnvironmentDevelopment.apiURL, modelGoal)
err = mockResponseData(http.MethodPut, endpoint, http.StatusBadRequest, goal)
assert.NoError(t, err)
var response *StandardResponse
response, err = client.UpdateGoal(goal)
assert.Error(t, err)
assert.NotNil(t, response)
})
t.Run("error from api (api error)", func(t *testing.T) {
client, err := newTestClient()
assert.NoError(t, err)
assert.NotNil(t, client)
goal := newTestGoal()
endpoint := fmt.Sprintf("%s/%s", EnvironmentDevelopment.apiURL, modelGoal)
apiError := &Error{
Code: 400,
Data: "field_name",
IPAddress: "127.0.0.1",
Message: "some error message",
Method: http.MethodPut,
RequestGUID: "7f3d97a8fd67ff57861904df6118dcc8",
StatusCode: http.StatusBadRequest,
URL: endpoint,
}
err = mockResponseData(http.MethodPut, endpoint, http.StatusBadRequest, apiError)
assert.NoError(t, err)
var response *StandardResponse
response, err = client.UpdateGoal(goal)
assert.Error(t, err)
assert.Equal(t, apiError.Message, err.Error())
assert.NotNil(t, response)
})
}
// ExampleClient_UpdateGoal example using UpdateGoal()
//
// See more examples in /examples/
func ExampleClient_UpdateGoal() {
// Load the client (using test client for example only)
client, err := newTestClient()
if err != nil {
fmt.Printf("error loading client: %s", err.Error())
return
}
// Mock response (for example only)
responseGoal := newTestGoal()
responseGoal.Title = "Updated Title"
_ = mockResponseData(
http.MethodPut,
fmt.Sprintf("%s/%s", EnvironmentDevelopment.apiURL, modelGoal),
http.StatusOK,
responseGoal,
)
// Update goal (using mocking response)
_, err = client.UpdateGoal(responseGoal)
if err != nil {
fmt.Printf("error updating goal: " + err.Error())
return
}
fmt.Printf("goal: %s", responseGoal.Title)
// Output:goal: Updated Title
}
// BenchmarkClient_UpdateGoal benchmarks the method UpdateGoal()
func BenchmarkClient_UpdateGoal(b *testing.B) {
client, _ := newTestClient()
goal := newTestGoal()
_ = mockResponseData(
http.MethodPut,
fmt.Sprintf("%s/%s", EnvironmentDevelopment.apiURL, modelGoal),
http.StatusOK,
goal,
)
for i := 0; i < b.N; i++ {
_, _ = client.UpdateGoal(goal)
}
}
// TestClient_DeleteGoal will test the method DeleteGoal()
func TestClient_DeleteGoal(t *testing.T) {
// t.Parallel() (Cannot run in parallel - issues with overriding the mock client)
t.Run("delete a goal (success)", func(t *testing.T) {
client, err := newTestClient()
assert.NoError(t, err)
assert.NotNil(t, client)
goal := newTestGoal()
endpoint := fmt.Sprintf("%s/%s?%s=%d", EnvironmentDevelopment.apiURL, modelGoal, fieldID, goal.ID)
err = mockResponseData(http.MethodDelete, endpoint, http.StatusOK, nil)
assert.NoError(t, err)
var deleted bool
var response *StandardResponse
deleted, response, err = client.DeleteGoal(goal.ID)
assert.NoError(t, err)
assert.Equal(t, true, deleted)
assert.Equal(t, testGoalID, goal.ID)
assert.NotNil(t, response)
})
t.Run("missing goal id", func(t *testing.T) {
client, err := newTestClient()
assert.NoError(t, err)
assert.NotNil(t, client)
goal := newTestGoal()
goal.ID = 0
endpoint := fmt.Sprintf("%s/%s?%s=%d", EnvironmentDevelopment.apiURL, modelGoal, fieldID, goal.ID)
err = mockResponseData(http.MethodDelete, endpoint, http.StatusOK, nil)
assert.NoError(t, err)
var deleted bool
var response *StandardResponse
deleted, response, err = client.DeleteGoal(goal.ID)
assert.Error(t, err)
assert.Equal(t, false, deleted)
assert.Nil(t, response)
})
t.Run("error from api (status code)", func(t *testing.T) {
client, err := newTestClient()
assert.NoError(t, err)
assert.NotNil(t, client)
goal := newTestGoal()
endpoint := fmt.Sprintf("%s/%s?%s=%d", EnvironmentDevelopment.apiURL, modelGoal, fieldID, goal.ID)
err = mockResponseData(http.MethodDelete, endpoint, http.StatusBadRequest, nil)
assert.NoError(t, err)
var deleted bool
var response *StandardResponse
deleted, response, err = client.DeleteGoal(goal.ID)
assert.Error(t, err)
assert.NotNil(t, response)
assert.Equal(t, false, deleted)
})
t.Run("error from api (api error)", func(t *testing.T) {
client, err := newTestClient()
assert.NoError(t, err)
assert.NotNil(t, client)
goal := newTestGoal()
endpoint := fmt.Sprintf("%s/%s?%s=%d", EnvironmentDevelopment.apiURL, modelGoal, fieldID, goal.ID)
apiError := &Error{
Code: 400,
Data: "field_name",
IPAddress: "127.0.0.1",
Message: "some error message",
Method: http.MethodPut,
RequestGUID: "7f3d97a8fd67ff57861904df6118dcc8",
StatusCode: http.StatusBadRequest,
URL: endpoint,
}
err = mockResponseData(http.MethodDelete, endpoint, http.StatusBadRequest, apiError)
assert.NoError(t, err)
var deleted bool
var response *StandardResponse
deleted, response, err = client.DeleteGoal(goal.ID)
assert.Error(t, err)
assert.NotNil(t, response)
assert.Equal(t, false, deleted)
assert.Equal(t, apiError.Message, err.Error())
})
}
// ExampleClient_DeleteGoal example using DeleteGoal()
//
// See more examples in /examples/
func ExampleClient_DeleteGoal() {
// Load the client (using test client for example only)
client, err := newTestClient()
if err != nil {
fmt.Printf("error loading client: %s", err.Error())
return
}
// Mock response (for example only)
responseGoal := newTestGoal()
_ = mockResponseData(
http.MethodDelete,
fmt.Sprintf("%s/%s?%s=%d", EnvironmentDevelopment.apiURL, modelGoal, fieldID, responseGoal.ID),
http.StatusOK,
nil,
)
// Delete goal (using mocking response)
var deleted bool
if deleted, _, err = client.DeleteGoal(responseGoal.ID); err != nil {
fmt.Printf("error deleting goal: " + err.Error())
return
}
fmt.Printf("goal deleted: %t", deleted)
// Output:goal deleted: true
}
// BenchmarkClient_DeleteGoal benchmarks the method DeleteGoal()
func BenchmarkClient_DeleteGoal(b *testing.B) {
client, _ := newTestClient()
goal := newTestGoal()
_ = mockResponseData(
http.MethodDelete,
fmt.Sprintf("%s/%s?%s=%d", EnvironmentDevelopment.apiURL, modelGoal, fieldID, goal.ID),
http.StatusOK,
nil,
)
for i := 0; i < b.N; i++ {
_, _, _ = client.DeleteGoal(goal.ID)
}
}