-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient_test.go
More file actions
88 lines (81 loc) · 2.64 KB
/
Copy pathclient_test.go
File metadata and controls
88 lines (81 loc) · 2.64 KB
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
package oddsapi
import (
"context"
"errors"
"strings"
"testing"
)
func TestSearchEventsBuildsURLAndAuth(t *testing.T) {
seen := map[string]any{}
client := NewClient(
WithAPIKey("test_key"),
WithBaseURL("https://api.odds-api.net/v1"),
WithTransport(func(method string, url string, headers map[string]string, body any) (any, error) {
seen["method"] = method
seen["url"] = url
seen["headers"] = headers
seen["body"] = body
return Response{"items": []any{}, "count": float64(0)}, nil
}),
)
result, err := client.SearchEvents(context.Background(), QueryParams{"sport": "rugby-league", "league": "NRL"})
if err != nil {
t.Fatal(err)
}
if result["count"] != float64(0) {
t.Fatalf("count = %v, want 0", result["count"])
}
if seen["method"] != "GET" {
t.Fatalf("method = %v, want GET", seen["method"])
}
if !strings.Contains(seen["url"].(string), "/events?") {
t.Fatalf("url = %s, want /events query", seen["url"])
}
headers := seen["headers"].(map[string]string)
if headers["X-API-Key"] != "test_key" {
t.Fatalf("X-API-Key = %q, want test_key", headers["X-API-Key"])
}
}
func TestFindBestOdds(t *testing.T) {
client := NewClient(
WithBaseURL("https://api.odds-api.net/v1"),
WithTransport(func(method string, url string, headers map[string]string, body any) (any, error) {
return Response{
"event_id": "event-1",
"items": []any{
map[string]any{"id": "a", "event_id": "event-1", "bookmaker": "book-a", "selection_key": "home", "market_key": "moneyline", "type": "moneyline", "period": float64(0), "odds": 1.9, "is_available": true},
map[string]any{"id": "b", "event_id": "event-1", "bookmaker": "book-b", "selection_key": "home", "market_key": "moneyline", "type": "moneyline", "period": float64(0), "odds": 2.1, "is_available": true},
},
"resume": "0-0",
}, nil
}),
)
best, err := client.FindBestOdds(context.Background(), "event-1", nil)
if err != nil {
t.Fatal(err)
}
if len(best) != 1 {
t.Fatalf("len(best) = %d, want 1", len(best))
}
if best[0].Bookmaker != "book-b" {
t.Fatalf("bookmaker = %q, want book-b", best[0].Bookmaker)
}
if best[0].Odds != 2.1 {
t.Fatalf("odds = %v, want 2.1", best[0].Odds)
}
}
func TestOddsApiError(t *testing.T) {
client := NewClient(
WithTransport(func(method string, url string, headers map[string]string, body any) (any, error) {
return nil, &OddsApiError{Status: 401, Body: Response{"detail": "bad key"}}
}),
)
_, err := client.GetMe(context.Background())
var apiErr *OddsApiError
if !errors.As(err, &apiErr) {
t.Fatalf("err = %T, want OddsApiError", err)
}
if apiErr.Status != 401 {
t.Fatalf("status = %d, want 401", apiErr.Status)
}
}