This repository has been archived by the owner on Jul 4, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoddsapi_test.go
147 lines (127 loc) · 3.28 KB
/
oddsapi_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
package oddsapi
import (
"errors"
"github.com/jarcoal/httpmock"
"os"
"testing"
)
func TestNewClient(t *testing.T) {
key := "this-is-the-key"
client := NewClient(key)
if client.ApiKey != key {
t.Error("Client API Key not set")
}
}
func TestGetSportsMock(t *testing.T) {
httpmock.Activate()
defer httpmock.DeactivateAndReset()
httpmock.RegisterResponder("GET", "https://api.the-odds-api.com/v4/sports/",
httpmock.NewStringResponder(200, `[
{
"key": "americanfootball_nfl",
"active": true,
"group": "American Football",
"description": "US Football",
"title": "NFL",
"has_outrights": false
}
]`))
key := os.Getenv("ODDS_API_KEY")
c := NewClient(key)
s, err := c.GetSports(GetSportsInput{
IncludeInactive: false,
})
if err != nil {
t.Error(err.Error())
}
if s[0].Key != "americanfootball_nfl" {
t.Error("Could not decode response")
}
}
func TestGetEventsMock(t *testing.T) {
httpmock.Activate()
defer httpmock.DeactivateAndReset()
httpmock.RegisterResponder("GET", "https://api.the-odds-api.com/v4/sports/americanfootball_nfl/odds",
httpmock.NewStringResponder(200, `[
{
"id": "e912304de2b2ce35b473ce2ecd3d1502",
"sport_key": "americanfootball_nfl",
"sport_title": "NFL",
"commence_time": "2020-01-02T23:10:00Z",
"home_team": "Houston Texans",
"away_team": "Kansas City Chiefs",
"bookmakers": [
{
"key": "draftkings",
"title": "DraftKings",
"last_update": "2020-01-01T14:45:13Z",
"markets": [
{
"key": "h2h",
"outcomes": [
{
"name": "Houston Texans",
"price": 2.23
},
{
"name": "Kansas City Chiefs",
"price": 1.45
}
]
}
]
}
]
}
]`))
key := os.Getenv("ODDS_API_KEY")
c := NewClient(key)
s, err := c.GetEvents(GetEventsInput{
Sports: []string{"americanfootball_nfl"},
Regions: []RegionType{RegionUS},
Markets: []MarketType{MarketMoneyline},
})
if err != nil {
t.Error(err.Error())
}
if s[0].Id != "e912304de2b2ce35b473ce2ecd3d1502" {
t.Error("Could not decode response")
}
}
func TestErrors(t *testing.T) {
if !errors.Is(checkErr(401), ErrUnauthenticated) {
t.Error("Status code 401 not handled")
}
if !errors.Is(checkErr(422), ErrQueryParams) {
t.Error("Status code 422 not handled")
}
if !errors.Is(checkErr(429), ErrThrottled) {
t.Error("Status code 429 not handled")
}
if !errors.Is(checkErr(500), ErrServer) {
t.Error("Status code 500 not handled")
}
if !errors.Is(checkErr(999), ErrUnknown) {
t.Error("Unknown status code not handled")
}
}
// TestGetEvents actually makes a call to the API.
// Normally this is skipped during testing but should occasionally be run to verify library function
func TestGetEvents(t *testing.T) {
// SKIP
t.SkipNow()
key := os.Getenv("ODDS_API_KEY")
c := NewClient(key)
s, err := c.GetEvents(GetEventsInput{
Sports: []string{"americanfootball_nfl"},
Regions: []RegionType{RegionUS},
Markets: []MarketType{MarketMoneyline},
OddsFormat: OddsFormatDecimal,
})
if err != nil {
t.Error(err.Error())
}
if s[0].SportKey != "americanfootball_nfl" {
t.Errorf("Sport key %s != americanfootball_nfl", s[0].SportKey)
}
}