forked from nicklaw5/helix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbits_test.go
108 lines (90 loc) · 2.91 KB
/
bits_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
package helix
import (
"net/http"
"testing"
"time"
)
func TestGetBitsLeaderboard(t *testing.T) {
t.Parallel()
testCases := []struct {
statusCode int
options *Options
count int
period string
startedAt time.Time
respBody string
}{
{
http.StatusBadRequest,
&Options{ClientID: "my-client-id"},
1,
"all",
time.Time{},
`{"error":"Bad Request","status":400,"message":"The parameter \"count\" was malformed: the value must be less than or equal to 100"}`,
},
{
http.StatusOK,
&Options{ClientID: "my-client-id"},
2,
"week",
time.Time{},
`{"data":[{"user_id":"158010205","rank":1,"score":12543},{"user_id":"7168163","rank":2,"score":6900}],"date_range":{"started_at":"2018-02-05T08:00:00Z","ended_at":"2018-02-12T08:00:00Z"},"total":2}`,
},
{
http.StatusOK,
&Options{ClientID: "my-client-id"},
2,
"week",
time.Now().Add(-744 * time.Hour),
`{"data":[{"user_id":"158010205","rank":1,"score":12543},{"user_id":"7168163","rank":2,"score":6900}],"date_range":{"started_at":"2018-02-05T08:00:00Z","ended_at":"2018-02-12T08:00:00Z"},"total":2}`,
},
}
for _, testCase := range testCases {
c := newMockClient(testCase.options, newMockHandler(testCase.statusCode, testCase.respBody, nil))
params := &BitsLeaderboardParams{
Count: testCase.count,
Period: testCase.period,
}
if !testCase.startedAt.IsZero() {
params.StartedAt = testCase.startedAt
}
resp, err := c.GetBitsLeaderboard(params)
if err != nil {
t.Error(err)
}
if resp.StatusCode != testCase.statusCode {
t.Errorf("expected status code to be \"%d\", got \"%d\"", testCase.statusCode, resp.StatusCode)
}
// Test error cases
if testCase.statusCode != http.StatusOK {
if resp.Error != "Bad Request" {
t.Errorf("expected error to be \"%s\", got \"%s\"", "Bad Request", resp.Error)
}
if resp.ErrorStatus != testCase.statusCode {
t.Errorf("expected error status to be \"%d\", got \"%d\"", testCase.statusCode, resp.ErrorStatus)
}
errMsg := "The parameter \"count\" was malformed: the value must be less than or equal to 100"
if resp.ErrorMessage != errMsg {
t.Errorf("expected error message to be \"%s\", got \"%s\"", errMsg, resp.ErrorMessage)
}
continue
}
// Test success cases
if len(resp.Data.UserBitTotals) != testCase.count {
t.Errorf("expected number of results to be \"%d\", got \"%d\"", testCase.count, len(resp.Data.UserBitTotals))
}
userData := resp.Data.UserBitTotals[0]
if userData.UserID != "158010205" || userData.Rank != 1 || userData.Score != 12543 {
t.Error("expected bits user data does not match expected values")
}
if resp.Data.DateRange.EndedAt.IsZero() {
t.Error("expected DateRange.EndedAt to not be zero")
}
if resp.Data.DateRange.StartedAt.IsZero() {
t.Error("expected DateRange.Started to not be zero")
}
if resp.Data.Total < 1 {
t.Error("expected Total to be more than zero")
}
}
}