Skip to content

Commit

Permalink
Shored up test coverage.
Browse files Browse the repository at this point in the history
  • Loading branch information
patfair committed Sep 6, 2014
1 parent 6b25b55 commit 316bd7f
Show file tree
Hide file tree
Showing 12 changed files with 172 additions and 26 deletions.
24 changes: 24 additions & 0 deletions arena_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
package main

import (
"bytes"
"github.com/stretchr/testify/assert"
"log"
"testing"
"time"
)
Expand Down Expand Up @@ -483,3 +485,25 @@ func TestSubstituteTeam(t *testing.T) {
mainArena.LoadMatch(&match)
assert.Nil(t, mainArena.SubstituteTeam(107, "R1"))
}

func TestSetupNetwork(t *testing.T) {
clearDb()
defer clearDb()
var err error
db, err = OpenDatabase(testDbPath)
assert.Nil(t, err)
defer db.Close()
eventSettings, _ = db.GetEventSettings()
mainArena.Setup()

// Verify the setup ran by checking the log for the expected failure messages.
eventSettings.NetworkSecurityEnabled = true
aironetTelnetPort = 10023
catalystTelnetPort = 10023
mainArena.LoadMatch(&Match{Type: "test"})
var writer bytes.Buffer
log.SetOutput(&writer)
time.Sleep(time.Millisecond * 10) // Allow some time for the asynchronous configuration to happen.
assert.Contains(t, writer.String(), "Failed to configure team Ethernet")
assert.Contains(t, writer.String(), "Failed to configure team WiFi")
}
25 changes: 25 additions & 0 deletions displays_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,29 @@ func TestPitDisplay(t *testing.T) {
assert.Contains(t, recorder.Body.String(), "Pit Display - Untitled Event - Cheesy Arena")
}

func TestPitDisplayWebsocket(t *testing.T) {
clearDb()
defer clearDb()
var err error
db, err = OpenDatabase(testDbPath)
assert.Nil(t, err)
defer db.Close()
eventSettings, _ = db.GetEventSettings()
mainArena.Setup()

server, wsUrl := startTestServer()
defer server.Close()
conn, _, err := websocket.DefaultDialer.Dial(wsUrl+"/displays/pit/websocket", nil)
assert.Nil(t, err)
defer conn.Close()
ws := &Websocket{conn}

// Check forced reloading as that is the only purpose the pit websocket serves.
recorder := getHttpResponse("/setup/field/reload_displays")
assert.Equal(t, 302, recorder.Code)
readWebsocketType(t, ws, "reload")
}

func TestAnnouncerDisplay(t *testing.T) {
clearDb()
defer clearDb()
Expand All @@ -121,6 +144,7 @@ func TestAnnouncerDisplayWebsocket(t *testing.T) {
db, err = OpenDatabase(testDbPath)
assert.Nil(t, err)
defer db.Close()
eventSettings, _ = db.GetEventSettings()
mainArena.Setup()

server, wsUrl := startTestServer()
Expand Down Expand Up @@ -463,6 +487,7 @@ func TestAllianceStationDisplayWebsocket(t *testing.T) {
// Change to a different screen.
mainArena.allianceStationDisplayScreen = "logo"
mainArena.allianceStationDisplayNotifier.Notify(nil)
readWebsocketType(t, ws, "matchTime")
readWebsocketType(t, ws, "setAllianceStationDisplay")

// Inform the server what display ID this is.
Expand Down
21 changes: 20 additions & 1 deletion driver_station_connection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package main
import (
"fmt"
"github.com/stretchr/testify/assert"
"io/ioutil"
"net"
"testing"
"time"
Expand Down Expand Up @@ -154,7 +155,13 @@ func TestDecodeStatusPacket(t *testing.T) {
}

func TestListenForDsPackets(t *testing.T) {
db, _ = OpenDatabase(testDbPath)
clearDb()
defer clearDb()
var err error
db, err = OpenDatabase(testDbPath)
assert.Nil(t, err)
defer db.Close()
eventSettings, _ = db.GetEventSettings()

listener, err := DsPacketListener()
if assert.Nil(t, err) {
Expand Down Expand Up @@ -212,6 +219,18 @@ func TestListenForDsPackets(t *testing.T) {
assert.True(t, time.Since(mainArena.AllianceStations["B1"].DsConn.LastPacketTime).Seconds() < 0.1)
assert.True(t, time.Since(mainArena.AllianceStations["B1"].DsConn.LastRobotLinkedTime).Seconds() < 0.1)

// Should log the packet to file if received during a match.
dsConn = mainArena.AllianceStations["B1"].DsConn
dsConn.signalMatchStart(&Match{Type: "Qualification", DisplayName: "1"})
mainArena.matchStartTime = time.Now().Add(-1 * time.Second)
mainArena.MatchState = AUTO_PERIOD
_, err = conn.Write(packet[:])
assert.Nil(t, err)
time.Sleep(time.Millisecond * 10)
logContents, err := ioutil.ReadFile(dsConn.log.logFile.Name())
assert.Nil(t, err)
assert.Contains(t, string(logContents), "254,B1,true,false,true,false,19.750000,02121300,39072,0,256")

// Should ignore a packet coming from an expected team in the wrong position.
statusBefore := mainArena.AllianceStations["R3"].DsConn.DriverStationStatus
packet[10] = 'R'
Expand Down
4 changes: 2 additions & 2 deletions match_play.go
Original file line number Diff line number Diff line change
Expand Up @@ -459,12 +459,12 @@ func CommitMatchScore(match *Match, matchResult *MatchResult) error {
go func() {
err = PublishMatches()
if err != nil {
log.Println(err)
log.Printf("Failed to publish matches: %s", err.Error())
}
if match.Type == "qualification" {
err = PublishRankings()
if err != nil {
log.Println(err)
log.Printf("Failed to publish rankings: %s", err.Error())
}
}
}()
Expand Down
20 changes: 19 additions & 1 deletion match_play_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@
package main

import (
"bytes"
"fmt"
"github.com/gorilla/websocket"
"github.com/mitchellh/mapstructure"
"github.com/stretchr/testify/assert"
"log"
"testing"
"time"
)
Expand Down Expand Up @@ -174,6 +176,17 @@ func TestCommitMatch(t *testing.T) {
assert.Equal(t, 3, matchResult.PlayNumber)
match, _ = db.GetMatchById(1)
assert.Equal(t, "T", match.Winner)

// Verify TBA publishing by checking the log for the expected failure messages.
tbaBaseUrl = "fakeurl"
eventSettings.TbaPublishingEnabled = true
var writer bytes.Buffer
log.SetOutput(&writer)
err = CommitMatchScore(match, matchResult)
assert.Nil(t, err)
time.Sleep(time.Millisecond * 10) // Allow some time for the asynchronous publishing to happen.
assert.Contains(t, writer.String(), "Failed to publish matches")
assert.Contains(t, writer.String(), "Failed to publish rankings")
}

func TestCommitEliminationTie(t *testing.T) {
Expand Down Expand Up @@ -325,17 +338,22 @@ func TestMatchPlayWebsocketCommands(t *testing.T) {
mainArena.blueRealtimeScore.CurrentScore.AutoHighHot = 37
ws.Write("commitResults", nil)
readWebsocketType(t, ws, "reload")
readWebsocketType(t, ws, "setAllianceStationDisplay")
assert.Equal(t, 29, mainArena.savedMatchResult.RedScore.AutoHighHot)
assert.Equal(t, 37, mainArena.savedMatchResult.BlueScore.AutoHighHot)
assert.Equal(t, PRE_MATCH, mainArena.MatchState)
ws.Write("discardResults", nil)
readWebsocketType(t, ws, "reload")
readWebsocketType(t, ws, "setAllianceStationDisplay")
assert.Equal(t, PRE_MATCH, mainArena.MatchState)

// Test changing the audience display.
// Test changing the displays.
ws.Write("setAudienceDisplay", "logo")
readWebsocketType(t, ws, "setAudienceDisplay")
assert.Equal(t, "logo", mainArena.audienceDisplayScreen)
ws.Write("setAllianceStationDisplay", "logo")
readWebsocketType(t, ws, "setAllianceStationDisplay")
assert.Equal(t, "logo", mainArena.allianceStationDisplayScreen)
}

func TestMatchPlayWebsocketNotifications(t *testing.T) {
Expand Down
18 changes: 9 additions & 9 deletions setup_alliance_selection.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,27 +180,27 @@ func AllianceSelectionFinalizeHandler(w http.ResponseWriter, r *http.Request) {
return
}

// Back up the database.
err = db.Backup("post_alliance_selection")
if err != nil {
handleWebErr(w, err)
return
}

if eventSettings.TbaPublishingEnabled {
// Publish alliances and schedule to The Blue Alliance.
err = PublishAlliances()
if err != nil {
handleWebErr(w, err)
renderAllianceSelection(w, r, fmt.Sprintf("Failed to publish alliances: %s", err.Error()))
return
}
err = PublishMatches()
if err != nil {
handleWebErr(w, err)
renderAllianceSelection(w, r, fmt.Sprintf("Failed to publish matches: %s", err.Error()))
return
}
}

// Back up the database.
err = db.Backup("post_alliance_selection")
if err != nil {
handleWebErr(w, err)
return
}

http.Redirect(w, r, "/setup/alliance_selection", 302)
}

Expand Down
10 changes: 7 additions & 3 deletions setup_alliance_selection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,9 +141,13 @@ func TestSetupAllianceSelectionErrors(t *testing.T) {
recorder = postHttpResponse("/setup/alliance_selection/finalize", "startTime=asdf")
assert.Equal(t, 200, recorder.Code)
assert.Contains(t, recorder.Body.String(), "valid start time")
recorder = postHttpResponse("/setup/alliance_selection/finalize",
"startTime=2014-01-01 01:00:00 PM")
assert.Equal(t, 302, recorder.Code)

// 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")
Expand Down
36 changes: 36 additions & 0 deletions setup_field_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Copyright 2014 Team 254. All Rights Reserved.
// Author: pat@patfairbank.com (Patrick Fairbank)

package main

import (
"github.com/stretchr/testify/assert"
"testing"
)

func TestSetupField(t *testing.T) {
clearDb()
defer clearDb()
var err error
db, err = OpenDatabase(testDbPath)
assert.Nil(t, err)
defer db.Close()
eventSettings, _ = db.GetEventSettings()
mainArena.Setup()

mainArena.allianceStationDisplays["12345"] = ""
recorder := getHttpResponse("/setup/field")
assert.Equal(t, 200, recorder.Code)
assert.Contains(t, recorder.Body.String(), "12345")
assert.NotContains(t, recorder.Body.String(), "selected")

recorder = postHttpResponse("/setup/field", "displayId=12345&allianceStation=B1")
assert.Equal(t, 302, recorder.Code)
recorder = getHttpResponse("/setup/field")
assert.Contains(t, recorder.Body.String(), "12345")
assert.Contains(t, recorder.Body.String(), "selected")

recorder = postHttpResponse("/setup/field/lights", "mode=strobe")
assert.Equal(t, 302, recorder.Code)
assert.Equal(t, "strobe", mainArena.lights.currentMode)
}
16 changes: 8 additions & 8 deletions setup_schedule.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,22 +107,22 @@ func ScheduleSavePostHandler(w http.ResponseWriter, r *http.Request) {
}
}

// Back up the database.
err = db.Backup("post_scheduling")
if err != nil {
handleWebErr(w, err)
return
}

if eventSettings.TbaPublishingEnabled && cachedMatchType != "practice" {
// Publish schedule to The Blue Alliance.
err = PublishMatches()
if err != nil {
handleWebErr(w, err)
http.Error(w, "Failed to publish matches: "+err.Error(), 500)
return
}
}

// Back up the database.
err = db.Backup("post_scheduling")
if err != nil {
handleWebErr(w, err)
return
}

http.Redirect(w, r, "/setup/schedule", 302)
}

Expand Down
6 changes: 5 additions & 1 deletion setup_schedule_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,13 @@ func TestSetupSchedule(t *testing.T) {
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.

// Save schedule.
// Save schedule and check that it is published to TBA.
tbaBaseUrl = "fakeurl"
eventSettings.TbaPublishingEnabled = true
recorder = postHttpResponse("/setup/schedule/save", "")
matches, err := db.GetMatchesByType("qualification")
assert.Equal(t, 500, recorder.Code)
assert.Contains(t, recorder.Body.String(), "Failed to publish matches")
assert.Nil(t, err)
assert.Equal(t, 64, len(matches))
assert.Equal(t, 1388595600, matches[0].Time.Unix())
Expand Down
2 changes: 1 addition & 1 deletion setup_teams.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ func TeamDeletePostHandler(w http.ResponseWriter, r *http.Request) {
func TeamsPublishHandler(w http.ResponseWriter, r *http.Request) {
err := PublishTeams()
if err != nil {
handleWebErr(w, err)
http.Error(w, "Failed to publish teams: "+err.Error(), 500)
return
}
http.Redirect(w, r, "/setup/teams", 302)
Expand Down
16 changes: 16 additions & 0 deletions setup_teams_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,3 +165,19 @@ func TestSetupTeamsWpaKeys(t *testing.T) {
assert.Equal(t, 500, recorder.Code)
assert.Contains(t, recorder.Body.String(), "WPA key must be between 8 and 63 characters")
}

func TestSetupTeamsPublish(t *testing.T) {
clearDb()
defer clearDb()
var err error
db, err = OpenDatabase(testDbPath)
assert.Nil(t, err)
defer db.Close()
eventSettings, _ = db.GetEventSettings()
tbaBaseUrl = "fakeurl"
eventSettings.TbaPublishingEnabled = true

recorder := postHttpResponse("/setup/teams/publish", "")
assert.Equal(t, 500, recorder.Code)
assert.Contains(t, recorder.Body.String(), "Failed to publish teams")
}

0 comments on commit 316bd7f

Please sign in to comment.