2014-06-08 21:47:31 -07:00
|
|
|
// Copyright 2014 Team 254. All Rights Reserved.
|
|
|
|
|
// Author: pat@patfairbank.com (Patrick Fairbank)
|
|
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
|
|
import (
|
2017-08-23 22:41:56 -07:00
|
|
|
"github.com/Team254/cheesy-arena/model"
|
2014-06-08 21:47:31 -07:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
|
"testing"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func TestSetupSchedule(t *testing.T) {
|
2017-08-23 22:41:56 -07:00
|
|
|
setupTest(t)
|
2014-06-08 21:47:31 -07:00
|
|
|
|
|
|
|
|
for i := 0; i < 38; i++ {
|
2017-08-23 22:41:56 -07:00
|
|
|
db.CreateTeam(&model.Team{Id: i + 101})
|
2014-06-08 21:47:31 -07:00
|
|
|
}
|
2017-08-23 22:41:56 -07:00
|
|
|
db.CreateMatch(&model.Match{Type: "practice", DisplayName: "1"})
|
2014-06-08 21:47:31 -07:00
|
|
|
|
|
|
|
|
// Check the default setting values.
|
|
|
|
|
recorder := getHttpResponse("/setup/schedule")
|
|
|
|
|
assert.Equal(t, 200, recorder.Code)
|
|
|
|
|
assert.Contains(t, recorder.Body.String(), "360") // The default match spacing.
|
|
|
|
|
|
|
|
|
|
// Submit a schedule for generation.
|
|
|
|
|
postData := "numScheduleBlocks=3&startTime0=2014-01-01 09:00:00 AM&numMatches0=7&matchSpacingSec0=480&" +
|
|
|
|
|
"startTime1=2014-01-02 09:56:00 AM&numMatches1=17&matchSpacingSec1=420&startTime2=2014-01-03 01:00:00 PM&" +
|
|
|
|
|
"numMatches2=40&matchSpacingSec2=360&matchType=qualification"
|
|
|
|
|
recorder = postHttpResponse("/setup/schedule/generate", postData)
|
|
|
|
|
assert.Equal(t, 302, recorder.Code)
|
|
|
|
|
recorder = getHttpResponse("/setup/schedule")
|
|
|
|
|
assert.Contains(t, recorder.Body.String(), "2014-01-01 09:48:00") // Last match of first block.
|
|
|
|
|
assert.Contains(t, recorder.Body.String(), "2014-01-02 11:48:00") // Last match of second block.
|
|
|
|
|
assert.Contains(t, recorder.Body.String(), "2014-01-03 16:54:00") // Last match of third block.
|
|
|
|
|
|
2014-09-05 22:17:12 -07:00
|
|
|
// Save schedule and check that it is published to TBA.
|
2017-08-27 16:27:02 -07:00
|
|
|
tbaClient.BaseUrl = "fakeUrl"
|
2014-09-05 22:17:12 -07:00
|
|
|
eventSettings.TbaPublishingEnabled = true
|
2014-06-08 21:47:31 -07:00
|
|
|
recorder = postHttpResponse("/setup/schedule/save", "")
|
|
|
|
|
matches, err := db.GetMatchesByType("qualification")
|
2014-09-05 22:17:12 -07:00
|
|
|
assert.Equal(t, 500, recorder.Code)
|
2015-09-11 23:43:19 -07:00
|
|
|
assert.Contains(t, recorder.Body.String(), "Failed to delete published matches")
|
2014-06-08 21:47:31 -07:00
|
|
|
assert.Nil(t, err)
|
|
|
|
|
assert.Equal(t, 64, len(matches))
|
2015-05-31 19:31:10 -07:00
|
|
|
assert.Equal(t, int64(1388595600), matches[0].Time.Unix())
|
|
|
|
|
assert.Equal(t, int64(1388685360), matches[7].Time.Unix())
|
|
|
|
|
assert.Equal(t, int64(1388782800), matches[24].Time.Unix())
|
2014-06-08 21:47:31 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestSetupScheduleErrors(t *testing.T) {
|
2017-08-23 22:41:56 -07:00
|
|
|
setupTest(t)
|
2014-06-08 21:47:31 -07:00
|
|
|
|
|
|
|
|
// No teams.
|
|
|
|
|
postData := "numScheduleBlocks=1&startTime0=2014-01-01 09:00:00 AM&numMatches0=7&matchSpacingSec0=480&" +
|
|
|
|
|
"matchType=practice"
|
|
|
|
|
recorder := postHttpResponse("/setup/schedule/generate", postData)
|
|
|
|
|
assert.Equal(t, 200, recorder.Code)
|
|
|
|
|
assert.Contains(t, recorder.Body.String(), "No team list is configured.")
|
|
|
|
|
|
|
|
|
|
// Insufficient number of teams.
|
|
|
|
|
for i := 0; i < 17; i++ {
|
2017-08-23 22:41:56 -07:00
|
|
|
db.CreateTeam(&model.Team{Id: i + 101})
|
2014-06-08 21:47:31 -07:00
|
|
|
}
|
|
|
|
|
postData = "numScheduleBlocks=1&startTime0=2014-01-01 09:00:00 AM&numMatches0=7&matchSpacingSec0=480&" +
|
|
|
|
|
"matchType=practice"
|
|
|
|
|
recorder = postHttpResponse("/setup/schedule/generate", postData)
|
|
|
|
|
assert.Equal(t, 200, recorder.Code)
|
|
|
|
|
assert.Contains(t, recorder.Body.String(), "There must be at least 18 teams to generate a schedule.")
|
|
|
|
|
|
|
|
|
|
// More matches per team than schedules exist for.
|
2017-08-23 22:41:56 -07:00
|
|
|
db.CreateTeam(&model.Team{Id: 118})
|
2014-06-08 21:47:31 -07:00
|
|
|
postData = "numScheduleBlocks=1&startTime0=2014-01-01 09:00:00 AM&numMatches0=700&matchSpacingSec0=480&" +
|
|
|
|
|
"matchType=practice"
|
|
|
|
|
recorder = postHttpResponse("/setup/schedule/generate", postData)
|
|
|
|
|
assert.Equal(t, 200, recorder.Code)
|
|
|
|
|
assert.Contains(t, recorder.Body.String(), "No schedule template exists for 18 teams and 233 matches")
|
|
|
|
|
|
|
|
|
|
// Incomplete scheduling data received.
|
|
|
|
|
postData = "numScheduleBlocks=1&startTime0=2014-01-01 09:00:00 AM&numMatches0=&matchSpacingSec0=480&" +
|
|
|
|
|
"matchType=practice"
|
|
|
|
|
recorder = postHttpResponse("/setup/schedule/generate", postData)
|
|
|
|
|
assert.Equal(t, 200, recorder.Code)
|
|
|
|
|
assert.Contains(t, recorder.Body.String(), "Incomplete or invalid schedule block parameters specified.")
|
|
|
|
|
|
|
|
|
|
// Previous schedule already exists.
|
|
|
|
|
for i := 18; i < 38; i++ {
|
2017-08-23 22:41:56 -07:00
|
|
|
db.CreateTeam(&model.Team{Id: i + 101})
|
2014-06-08 21:47:31 -07:00
|
|
|
}
|
2017-08-23 22:41:56 -07:00
|
|
|
db.CreateMatch(&model.Match{Type: "practice", DisplayName: "1"})
|
|
|
|
|
db.CreateMatch(&model.Match{Type: "practice", DisplayName: "2"})
|
2014-06-08 21:47:31 -07:00
|
|
|
postData = "numScheduleBlocks=1&startTime0=2014-01-01 09:00:00 AM&numMatches0=64&matchSpacingSec0=480&" +
|
|
|
|
|
"matchType=practice"
|
|
|
|
|
recorder = postHttpResponse("/setup/schedule/generate", postData)
|
|
|
|
|
assert.Equal(t, 302, recorder.Code)
|
|
|
|
|
recorder = postHttpResponse("/setup/schedule/save", postData)
|
|
|
|
|
assert.Equal(t, 200, recorder.Code)
|
|
|
|
|
assert.Contains(t, recorder.Body.String(), "schedule of 2 practice matches already exists")
|
|
|
|
|
}
|