From 977244f8ba74a04efc5fa9e63520108f27cbd61b Mon Sep 17 00:00:00 2001 From: Patrick Fairbank Date: Wed, 27 Aug 2014 13:42:10 -0700 Subject: [PATCH] Added unit tests for cards. --- displays_test.go | 19 +++++++++++++ match_play_test.go | 49 +++++++++++++++++++++++++++++++- setup_alliance_selection_test.go | 4 +++ 3 files changed, 71 insertions(+), 1 deletion(-) diff --git a/displays_test.go b/displays_test.go index 9bd3337..f89883f 100644 --- a/displays_test.go +++ b/displays_test.go @@ -383,6 +383,25 @@ func TestRefereeDisplayWebsocket(t *testing.T) { readWebsocketType(t, ws, "reload") assert.Equal(t, 1, len(mainArena.redRealtimeScore.Fouls)) + // Test card setting. + cardData := struct { + Alliance string + TeamId int + Card string + }{"red", 256, "yellow"} + ws.Write("card", cardData) + cardData.Alliance = "blue" + cardData.TeamId = 1680 + cardData.Card = "red" + ws.Write("card", cardData) + time.Sleep(time.Millisecond * 10) // Allow some time for the command to be processed. + if assert.Equal(t, 1, len(mainArena.redRealtimeScore.Cards)) { + assert.Equal(t, "yellow", mainArena.redRealtimeScore.Cards["256"]) + } + if assert.Equal(t, 1, len(mainArena.blueRealtimeScore.Cards)) { + assert.Equal(t, "red", mainArena.blueRealtimeScore.Cards["1680"]) + } + // Test match committing. mainArena.MatchState = POST_MATCH ws.Write("commitMatch", foulData) diff --git a/match_play_test.go b/match_play_test.go index e19b515..612e838 100644 --- a/match_play_test.go +++ b/match_play_test.go @@ -189,7 +189,7 @@ func TestCommitEliminationTie(t *testing.T) { match := &Match{Id: 0, Type: "qualification", Red1: 1, Red2: 2, Red3: 3, Blue1: 4, Blue2: 5, Blue3: 6} db.CreateMatch(match) matchResult := &MatchResult{MatchId: match.Id, RedScore: Score{AutoHighHot: 1}, RedFouls: []Foul{Foul{}}} - CommitMatchScore(match, matchResult) + err = CommitMatchScore(match, matchResult) assert.Nil(t, err) match, _ = db.GetMatchById(1) assert.Equal(t, "T", match.Winner) @@ -200,6 +200,53 @@ func TestCommitEliminationTie(t *testing.T) { assert.Equal(t, "B", match.Winner) } +func TestCommitCards(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() + + // Check that a yellow card sticks with a team. + team := &Team{Id: 5} + db.CreateTeam(team) + match := &Match{Id: 0, Type: "qualification", Red1: 1, Red2: 2, Red3: 3, Blue1: 4, Blue2: 5, Blue3: 6} + db.CreateMatch(match) + matchResult := &MatchResult{MatchId: match.Id, BlueCards: map[string]string{"5": "yellow"}} + err = CommitMatchScore(match, matchResult) + assert.Nil(t, err) + team, _ = db.GetTeamById(5) + assert.True(t, team.YellowCard) + + // Check that editing a match result removes a yellow card from a team. + matchResult = &MatchResult{MatchId: match.Id} + err = CommitMatchScore(match, matchResult) + assert.Nil(t, err) + team, _ = db.GetTeamById(5) + assert.False(t, team.YellowCard) + + // Check that a red card causes a yellow card to stick with a team. + matchResult = &MatchResult{MatchId: match.Id, BlueCards: map[string]string{"5": "red"}} + err = CommitMatchScore(match, matchResult) + assert.Nil(t, err) + team, _ = db.GetTeamById(5) + assert.True(t, team.YellowCard) + + // Check that a red card in eliminations zeroes out the score. + createTestAlliances(db, 2) + match.Type = "elimination" + db.SaveMatch(match) + *matchResult = buildTestMatchResult(match.Id, 10) + matchResult.RedCards = map[string]string{"1": "red"} + err = CommitMatchScore(match, matchResult) + assert.Nil(t, err) + assert.Equal(t, 0, matchResult.RedScoreSummary().Score) + assert.Equal(t, 593, matchResult.BlueScoreSummary().Score) +} + func TestMatchPlayWebsocketCommands(t *testing.T) { clearDb() defer clearDb() diff --git a/setup_alliance_selection_test.go b/setup_alliance_selection_test.go index 99dd179..c89e4ed 100644 --- a/setup_alliance_selection_test.go +++ b/setup_alliance_selection_test.go @@ -20,6 +20,7 @@ func TestSetupAllianceSelection(t *testing.T) { 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}) } @@ -79,6 +80,7 @@ func TestSetupAllianceSelection(t *testing.T) { 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() @@ -91,6 +93,8 @@ func TestSetupAllianceSelection(t *testing.T) { 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) {