-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi_test.go
86 lines (74 loc) · 1.7 KB
/
api_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
package dota2api
import (
"flag"
"testing"
"os"
"encoding/json"
"io/ioutil"
"github.com/pkg/errors"
"github.com/stretchr/testify/suite"
)
var writeFixtures bool
func init() {
flag.BoolVar(&writeFixtures, "write-fixtures", false, "write the fixtures instead of testing against them")
flag.Parse()
}
type Suite struct {
suite.Suite
c *Client
}
func (s *Suite) SetupSuite() {
key, ok := os.LookupEnv("DOTA2_API_KEY")
if !ok {
s.FailNow("DOTA2_API_KEY environment variable not set")
}
s.c = NewClient(key)
}
func (s *Suite) fixtureTest(v interface{}, path string) error {
b, err := json.MarshalIndent(v, "", " ")
if err != nil {
return errors.Wrap(err, "error creating fixture")
}
if writeFixtures {
if err := os.MkdirAll("fixtures", os.ModeDir); err != nil {
return errors.Wrap(err, "error creating dir")
}
if err := ioutil.WriteFile(path, b, 0666); err != nil {
return errors.Wrap(err, "error writing fixture file")
}
} else {
f, err := ioutil.ReadFile(path)
if err != nil {
return errors.Wrap(err, "error reading fixture file")
}
s.Equal(f, b)
}
return nil
}
func (s *Suite) TestGetMatchHistoryBySequenceNum() {
res, err := s.c.GetMatchHistoryBySequenceNum(2081736343, 10)
if !s.NoError(err) {
s.T().FailNow()
}
if !s.NotNil(res) {
s.T().FailNow()
}
if !s.NoError(s.fixtureTest(res, "fixtures/GetMatchHistoryBySequenceNum.json")) {
s.T().FailNow()
}
}
func (s *Suite) TestGetMatchHistory() {
res, err := s.c.GetMatchHistory()
if !s.NoError(err) {
s.T().FailNow()
}
if !s.NotNil(res) {
s.T().FailNow()
}
if !s.NoError(s.fixtureTest(res, "fixtures/GetMatchHistory.json")) {
s.T().FailNow()
}
}
func TestSuite(t *testing.T) {
suite.Run(t, new(Suite))
}