forked from Team254/cheesy-arena
-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup_alliance_selection_test.go
264 lines (245 loc) · 9.49 KB
/
setup_alliance_selection_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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
// Copyright 2014 Team 254. All Rights Reserved.
// Author: [email protected] (Patrick Fairbank)
package main
import (
"github.com/stretchr/testify/assert"
"testing"
)
func TestSetupAllianceSelection(t *testing.T) {
clearDb()
defer clearDb()
var err error
db, err = OpenDatabase(testDbPath)
assert.Nil(t, err)
defer db.Close()
cachedAlliances = [][]*AllianceTeam{}
cachedRankedTeams = []*RankedTeam{}
eventSettings, _ = db.GetEventSettings()
eventSettings.NumElimAlliances = 15
eventSettings.SelectionRound3Order = "L"
mainArena.Setup()
for i := 1; i <= 10; i++ {
db.CreateRanking(&Ranking{TeamId: 100 + i, Rank: i})
}
// Check that there are no alliance placeholders to start.
recorder := getHttpResponse("/setup/alliance_selection")
assert.Equal(t, 200, recorder.Code)
assert.NotContains(t, recorder.Body.String(), "Captain")
assert.NotContains(t, recorder.Body.String(), ">110<")
// Start the alliance selection.
recorder = postHttpResponse("/setup/alliance_selection/start", "")
assert.Equal(t, 302, recorder.Code)
if assert.Equal(t, 15, len(cachedAlliances)) {
assert.Equal(t, 4, len(cachedAlliances[0]))
}
recorder = getHttpResponse("/setup/alliance_selection")
assert.Contains(t, recorder.Body.String(), "Captain")
assert.Contains(t, recorder.Body.String(), ">110<")
// Reset the alliance selection.
recorder = postHttpResponse("/setup/alliance_selection/reset", "")
assert.Equal(t, 302, recorder.Code)
assert.NotContains(t, recorder.Body.String(), "Captain")
assert.NotContains(t, recorder.Body.String(), ">110<")
eventSettings.NumElimAlliances = 3
eventSettings.SelectionRound3Order = ""
recorder = postHttpResponse("/setup/alliance_selection/start", "")
assert.Equal(t, 302, recorder.Code)
if assert.Equal(t, 3, len(cachedAlliances)) {
assert.Equal(t, 3, len(cachedAlliances[0]))
}
// Update one team at a time.
recorder = postHttpResponse("/setup/alliance_selection", "selection0_0=110")
assert.Equal(t, 302, recorder.Code)
assert.Equal(t, 110, cachedAlliances[0][0].TeamId)
recorder = getHttpResponse("/setup/alliance_selection")
assert.Contains(t, recorder.Body.String(), "\"110\"")
assert.NotContains(t, recorder.Body.String(), ">110<")
// Update multiple teams at a time.
recorder = postHttpResponse("/setup/alliance_selection", "selection0_0=101&selection0_1=102&selection1_0=103")
assert.Equal(t, 302, recorder.Code)
assert.Equal(t, 101, cachedAlliances[0][0].TeamId)
assert.Equal(t, 102, cachedAlliances[0][1].TeamId)
assert.Equal(t, 103, cachedAlliances[1][0].TeamId)
recorder = getHttpResponse("/setup/alliance_selection")
assert.Contains(t, recorder.Body.String(), ">110<")
// Update remainder of teams.
recorder = postHttpResponse("/setup/alliance_selection", "selection0_0=101&selection0_1=102&"+
"selection0_2=103&selection1_0=104&selection1_1=105&selection1_2=106&selection2_0=107&selection2_1=108&"+
"selection2_2=109")
assert.Equal(t, 302, recorder.Code)
recorder = getHttpResponse("/setup/alliance_selection")
assert.Contains(t, recorder.Body.String(), ">110<")
// Finalize alliance selection.
db.CreateTeam(&Team{Id: 254, YellowCard: true})
recorder = postHttpResponse("/setup/alliance_selection/finalize", "startTime=2014-01-01 01:00:00 PM")
assert.Equal(t, 302, recorder.Code)
alliances, err := db.GetAllAlliances()
assert.Nil(t, err)
if assert.Equal(t, 3, len(alliances)) {
assert.Equal(t, 101, alliances[0][0].TeamId)
assert.Equal(t, 105, alliances[1][1].TeamId)
assert.Equal(t, 109, alliances[2][2].TeamId)
}
matches, err := db.GetMatchesByType("elimination")
assert.Nil(t, err)
assert.Equal(t, 6, len(matches))
team, _ := db.GetTeamById(254)
assert.False(t, team.YellowCard)
}
func TestSetupAllianceSelectionErrors(t *testing.T) {
clearDb()
defer clearDb()
var err error
db, err = OpenDatabase(testDbPath)
assert.Nil(t, err)
defer db.Close()
cachedAlliances = [][]*AllianceTeam{}
cachedRankedTeams = []*RankedTeam{}
eventSettings, _ = db.GetEventSettings()
eventSettings.NumElimAlliances = 2
for i := 1; i <= 6; i++ {
db.CreateRanking(&Ranking{TeamId: 100 + i, Rank: i})
}
// Start an alliance selection that is already underway.
recorder := postHttpResponse("/setup/alliance_selection/start", "")
assert.Equal(t, 302, recorder.Code)
recorder = postHttpResponse("/setup/alliance_selection/start", "")
assert.Equal(t, 200, recorder.Code)
assert.Contains(t, recorder.Body.String(), "already in progress")
// Select invalid teams.
recorder = postHttpResponse("/setup/alliance_selection", "selection0_0=asdf")
assert.Equal(t, 200, recorder.Code)
assert.Contains(t, recorder.Body.String(), "Invalid team number")
recorder = postHttpResponse("/setup/alliance_selection", "selection0_0=100")
assert.Equal(t, 200, recorder.Code)
assert.Contains(t, recorder.Body.String(), "not present at this event")
recorder = postHttpResponse("/setup/alliance_selection", "selection0_0=101&selection1_1=101")
assert.Equal(t, 200, recorder.Code)
assert.Contains(t, recorder.Body.String(), "already part of an alliance")
// Finalize early and without required parameters.
recorder = postHttpResponse("/setup/alliance_selection/finalize",
"startTime=2014-01-01 01:00:00 PM&matchSpacingSec=360")
assert.Equal(t, 200, recorder.Code)
assert.Contains(t, recorder.Body.String(), "until all spots have been filled")
recorder = postHttpResponse("/setup/alliance_selection", "selection0_0=101&selection0_1=102&"+
"selection0_2=103&selection1_0=104&selection1_1=105&selection1_2=106")
assert.Equal(t, 302, recorder.Code)
recorder = postHttpResponse("/setup/alliance_selection/finalize", "startTime=asdf")
assert.Equal(t, 200, recorder.Code)
assert.Contains(t, recorder.Body.String(), "valid start time")
// Finalize for real and check that TBA publishing is triggered.
tbaBaseUrl = "fakeurl"
eventSettings.TbaPublishingEnabled = true
recorder = postHttpResponse("/setup/alliance_selection/finalize", "startTime=2014-01-01 01:00:00 PM")
assert.Equal(t, 200, recorder.Code)
assert.Contains(t, recorder.Body.String(), "Failed to publish alliances")
// Do other things after finalization.
recorder = postHttpResponse("/setup/alliance_selection/finalize", "startTime=2014-01-01 01:00:00 PM")
assert.Equal(t, 200, recorder.Code)
assert.Contains(t, recorder.Body.String(), "already been finalized")
recorder = postHttpResponse("/setup/alliance_selection/reset", "")
assert.Equal(t, 200, recorder.Code)
assert.Contains(t, recorder.Body.String(), "already been finalized")
recorder = postHttpResponse("/setup/alliance_selection", "selection0_0=asdf")
assert.Equal(t, 200, recorder.Code)
assert.Contains(t, recorder.Body.String(), "already been finalized")
cachedAlliances = [][]*AllianceTeam{}
cachedRankedTeams = []*RankedTeam{}
recorder = postHttpResponse("/setup/alliance_selection/start", "")
assert.Equal(t, 200, recorder.Code)
assert.Contains(t, recorder.Body.String(), "already been finalized")
}
func TestSetupAllianceSelectionAutofocus(t *testing.T) {
clearDb()
defer clearDb()
var err error
db, err = OpenDatabase(testDbPath)
assert.Nil(t, err)
defer db.Close()
cachedAlliances = [][]*AllianceTeam{}
cachedRankedTeams = []*RankedTeam{}
eventSettings, _ = db.GetEventSettings()
eventSettings.NumElimAlliances = 2
// Straight draft.
eventSettings.SelectionRound2Order = "F"
eventSettings.SelectionRound3Order = "F"
recorder := postHttpResponse("/setup/alliance_selection/start", "")
assert.Equal(t, 302, recorder.Code)
i, j := determineNextCell()
assert.Equal(t, 0, i)
assert.Equal(t, 0, j)
cachedAlliances[0][0].TeamId = 1
i, j = determineNextCell()
assert.Equal(t, 0, i)
assert.Equal(t, 1, j)
cachedAlliances[0][1].TeamId = 2
i, j = determineNextCell()
assert.Equal(t, 1, i)
assert.Equal(t, 0, j)
cachedAlliances[1][0].TeamId = 3
i, j = determineNextCell()
assert.Equal(t, 1, i)
assert.Equal(t, 1, j)
cachedAlliances[1][1].TeamId = 4
i, j = determineNextCell()
assert.Equal(t, 0, i)
assert.Equal(t, 2, j)
cachedAlliances[0][2].TeamId = 5
i, j = determineNextCell()
assert.Equal(t, 1, i)
assert.Equal(t, 2, j)
cachedAlliances[1][2].TeamId = 6
i, j = determineNextCell()
assert.Equal(t, 0, i)
assert.Equal(t, 3, j)
cachedAlliances[0][3].TeamId = 7
i, j = determineNextCell()
assert.Equal(t, 1, i)
assert.Equal(t, 3, j)
cachedAlliances[1][3].TeamId = 8
i, j = determineNextCell()
assert.Equal(t, -1, i)
assert.Equal(t, -1, j)
// Double-serpentine draft.
eventSettings.SelectionRound2Order = "L"
eventSettings.SelectionRound3Order = "L"
recorder = postHttpResponse("/setup/alliance_selection/reset", "")
assert.Equal(t, 302, recorder.Code)
recorder = postHttpResponse("/setup/alliance_selection/start", "")
assert.Equal(t, 302, recorder.Code)
i, j = determineNextCell()
assert.Equal(t, 0, i)
assert.Equal(t, 0, j)
cachedAlliances[0][0].TeamId = 1
i, j = determineNextCell()
assert.Equal(t, 0, i)
assert.Equal(t, 1, j)
cachedAlliances[0][1].TeamId = 2
i, j = determineNextCell()
assert.Equal(t, 1, i)
assert.Equal(t, 0, j)
cachedAlliances[1][0].TeamId = 3
i, j = determineNextCell()
assert.Equal(t, 1, i)
assert.Equal(t, 1, j)
cachedAlliances[1][1].TeamId = 4
i, j = determineNextCell()
assert.Equal(t, 1, i)
assert.Equal(t, 2, j)
cachedAlliances[1][2].TeamId = 5
i, j = determineNextCell()
assert.Equal(t, 0, i)
assert.Equal(t, 2, j)
cachedAlliances[0][2].TeamId = 6
i, j = determineNextCell()
assert.Equal(t, 1, i)
assert.Equal(t, 3, j)
cachedAlliances[1][3].TeamId = 7
i, j = determineNextCell()
assert.Equal(t, 0, i)
assert.Equal(t, 3, j)
cachedAlliances[0][3].TeamId = 8
i, j = determineNextCell()
assert.Equal(t, -1, i)
assert.Equal(t, -1, j)
}