-
Notifications
You must be signed in to change notification settings - Fork 77
/
Copy pathtba_test.go
174 lines (147 loc) · 5.39 KB
/
tba_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
// Copyright 2014 Team 254. All Rights Reserved.
// Author: [email protected] (Patrick Fairbank)
package partner
import (
"bytes"
"encoding/json"
"github.com/Team254/cheesy-arena/game"
"github.com/Team254/cheesy-arena/model"
"github.com/stretchr/testify/assert"
"io"
"net/http"
"net/http/httptest"
"strings"
"testing"
"time"
)
func TestPublishTeams(t *testing.T) {
database := setupTestDb(t)
database.CreateTeam(&model.Team{Id: 254})
database.CreateTeam(&model.Team{Id: 1114})
// Mock the TBA server.
tbaServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
assert.Contains(t, r.URL.String(), "event/my_event_code")
var reader bytes.Buffer
reader.ReadFrom(r.Body)
assert.Equal(t, "[\"frc254\",\"frc1114\"]", reader.String())
assert.Equal(t, "my_secret_id", r.Header["X-Tba-Auth-Id"][0])
assert.Equal(t, "f5c022fde6d1186ea0719fe28ab6cc63", r.Header["X-Tba-Auth-Sig"][0])
}))
defer tbaServer.Close()
client := NewTbaClient("my_event_code", "my_secret_id", "my_secret")
client.BaseUrl = tbaServer.URL
assert.Nil(t, client.PublishTeams(database))
}
func TestPublishMatches(t *testing.T) {
database := setupTestDb(t)
match1 := model.Match{
Type: model.Qualification,
ShortName: "Q2",
Time: time.Unix(600, 0),
Red1: 7,
Red2: 8,
Red3: 9,
Blue1: 10,
Blue2: 11,
Blue3: 12,
Status: game.RedWonMatch,
TbaMatchKey: model.TbaMatchKey{"qm", 0, 2},
}
match2 := model.Match{Type: model.Playoff, ShortName: "SF2-2", TbaMatchKey: model.TbaMatchKey{"omg", 5, 29}}
database.CreateMatch(&match1)
database.CreateMatch(&match2)
matchResult1 := model.BuildTestMatchResult(match1.Id, 1)
database.CreateMatchResult(matchResult1)
// Mock the TBA server.
tbaServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
body, _ := io.ReadAll(r.Body)
var matches []*TbaMatch
json.Unmarshal(body, &matches)
assert.Equal(t, 2, len(matches))
assert.Equal(t, "qm", matches[0].CompLevel)
assert.Equal(t, 0, matches[0].SetNumber)
assert.Equal(t, 2, matches[0].MatchNumber)
assert.Equal(t, "omg", matches[1].CompLevel)
assert.Equal(t, 5, matches[1].SetNumber)
assert.Equal(t, 29, matches[1].MatchNumber)
}))
defer tbaServer.Close()
client := NewTbaClient("my_event_code", "my_secret_id", "my_secret")
client.BaseUrl = tbaServer.URL
assert.Nil(t, client.PublishMatches(database))
}
func TestPublishRankings(t *testing.T) {
database := setupTestDb(t)
database.CreateRanking(game.TestRanking2())
database.CreateRanking(game.TestRanking1())
// Mock the TBA server.
tbaServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
body, _ := io.ReadAll(r.Body)
var response TbaRankings
json.Unmarshal(body, &response)
assert.Equal(t, 2, len(response.Rankings))
assert.Equal(t, "frc254", response.Rankings[0].TeamKey)
assert.Equal(t, "frc1114", response.Rankings[1].TeamKey)
}))
defer tbaServer.Close()
client := NewTbaClient("my_event_code", "my_secret_id", "my_secret")
client.BaseUrl = tbaServer.URL
assert.Nil(t, client.PublishRankings(database))
}
func TestPublishAlliances(t *testing.T) {
database := setupTestDb(t)
model.BuildTestAlliances(database)
// Mock the TBA server.
tbaServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
var reader bytes.Buffer
reader.ReadFrom(r.Body)
if strings.Contains(r.URL.String(), "alliance_selections") {
assert.Equal(
t,
"[[\"frc254\",\"frc469\",\"frc2848\",\"frc74\",\"frc3175\"],[\"frc1718\",\"frc2451\",\"frc1619\"]]",
reader.String(),
)
} else {
assert.Equal(t, "{\"playoff_type\":10}", reader.String())
}
}))
defer tbaServer.Close()
client := NewTbaClient("my_event_code", "my_secret_id", "my_secret")
client.BaseUrl = tbaServer.URL
assert.Nil(t, client.PublishAlliances(database))
}
func TestPublishingErrors(t *testing.T) {
database := setupTestDb(t)
model.BuildTestAlliances(database)
// Mock the TBA server.
tbaServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
http.Error(w, "oh noes", 500)
}))
defer tbaServer.Close()
client := NewTbaClient("my_event_code", "my_secret_id", "my_secret")
client.BaseUrl = tbaServer.URL
assert.NotNil(t, client.PublishTeams(database))
assert.NotNil(t, client.PublishMatches(database))
assert.NotNil(t, client.PublishRankings(database))
assert.NotNil(t, client.PublishAlliances(database))
}
func TestPublishAwards(t *testing.T) {
database := setupTestDb(t)
database.CreateAward(&model.Award{0, model.JudgedAward, "Saftey Award", 254, ""})
database.CreateAward(&model.Award{0, model.JudgedAward, "Spirt Award", 0, "Bob Dorough"})
// Mock the TBA server.
tbaServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
assert.Contains(t, r.URL.String(), "event/my_event_code")
var reader bytes.Buffer
reader.ReadFrom(r.Body)
assert.Equal(t, "[{\"name_str\":\"Saftey Award\",\"team_key\":\"frc254\",\"awardee\":\"\"},"+
"{\"name_str\":\"Spirt Award\",\"team_key\":\"frc0\",\"awardee\":\"Bob Dorough\"}]", reader.String())
}))
defer tbaServer.Close()
client := NewTbaClient("my_event_code", "my_secret_id", "my_secret")
client.BaseUrl = tbaServer.URL
assert.Nil(t, client.PublishAwards(database))
}
func setupTestDb(t *testing.T) *model.Database {
return model.SetupTestDb(t, "partner")
}