Added match model.

This commit is contained in:
Patrick Fairbank
2014-05-24 18:52:10 -07:00
parent 37999ba6f5
commit 42c6ae9be8
6 changed files with 213 additions and 12 deletions

View File

@@ -18,6 +18,7 @@ type Database struct {
path string
db *sql.DB
eventSettingsMap *modl.DbMap
matchMap *modl.DbMap
teamMap *modl.DbMap
}
@@ -54,9 +55,12 @@ func (database *Database) Close() {
func (database *Database) mapTables() {
dialect := new(modl.SqliteDialect)
database.teamMap = modl.NewDbMap(database.db, dialect)
database.teamMap.AddTableWithName(Team{}, "teams").SetKeys(false, "Id")
database.eventSettingsMap = modl.NewDbMap(database.db, dialect)
database.eventSettingsMap.AddTableWithName(EventSettings{}, "event_settings").SetKeys(false, "Id")
database.matchMap = modl.NewDbMap(database.db, dialect)
database.matchMap.AddTableWithName(Match{}, "matches").SetKeys(false, "Id")
database.teamMap = modl.NewDbMap(database.db, dialect)
database.teamMap.AddTableWithName(Team{}, "teams").SetKeys(false, "Id")
}

View File

@@ -0,0 +1,24 @@
-- +goose Up
CREATE TABLE matches (
id INTEGER PRIMARY KEY,
type VARCHAR(16),
displayname VARCHAR(16),
time DATETIME,
red1 int,
red1issurrogate bool,
red2 int,
red2issurrogate bool,
red3 int,
red3issurrogate bool,
blue1 int,
blue1issurrogate bool,
blue2 int,
blue2issurrogate bool,
blue3 int,
blue3issurrogate bool,
status VARCHAR(16),
startedat DATETIME
);
-- +goose Down
DROP TABLE event_settings;

59
match.go Normal file
View File

@@ -0,0 +1,59 @@
// Copyright 2014 Team 254. All Rights Reserved.
// Author: pat@patfairbank.com (Patrick Fairbank)
//
// Model and datastore CRUD methods for a match at an event.
package main
import (
"time"
)
type Match struct {
Id int
Type string
DisplayName string
Time time.Time
Red1 int
Red1IsSurrogate bool
Red2 int
Red2IsSurrogate bool
Red3 int
Red3IsSurrogate bool
Blue1 int
Blue1IsSurrogate bool
Blue2 int
Blue2IsSurrogate bool
Blue3 int
Blue3IsSurrogate bool
Status string
StartedAt time.Time
}
func (database *Database) CreateMatch(match *Match) error {
return database.matchMap.Insert(match)
}
func (database *Database) GetMatchById(id int) (*Match, error) {
match := new(Match)
err := database.matchMap.Get(match, id)
if err != nil && err.Error() == "sql: no rows in result set" {
match = nil
err = nil
}
return match, err
}
func (database *Database) SaveMatch(match *Match) error {
_, err := database.matchMap.Update(match)
return err
}
func (database *Database) DeleteMatch(match *Match) error {
_, err := database.matchMap.Delete(match)
return err
}
func (database *Database) TruncateMatches() error {
return database.matchMap.TruncateTables()
}

89
match_test.go Normal file
View File

@@ -0,0 +1,89 @@
// Copyright 2014 Team 254. All Rights Reserved.
// Author: pat@patfairbank.com (Patrick Fairbank)
package main
import (
"testing"
"time"
)
func TestGetNonexistentMatch(t *testing.T) {
clearDb()
defer clearDb()
db, err := OpenDatabase(testDbPath)
if err != nil {
t.Error("Error:", err)
}
defer db.Close()
match, err := db.GetMatchById(1114)
if err != nil {
t.Error("Error:", err)
}
if match != nil {
t.Errorf("Expected '%v' to be nil", match)
}
}
func TestMatchCrud(t *testing.T) {
clearDb()
defer clearDb()
db, err := OpenDatabase(testDbPath)
if err != nil {
t.Error("Error:", err)
}
defer db.Close()
match := Match{254, "qualification", "254", time.Now().UTC(), 1, false, 2, false, 3, false, 4, false, 5,
false, 6, false, "", time.Now().UTC()}
db.CreateMatch(&match)
match2, err := db.GetMatchById(254)
if err != nil {
t.Error("Error:", err)
}
if match != *match2 {
t.Errorf("Expected '%v', got '%v'", match, match2)
}
match.Status = "started"
db.SaveMatch(&match)
match2, err = db.GetMatchById(254)
if err != nil {
t.Error("Error:", err)
}
if match.Status != match2.Status {
t.Errorf("Expected '%v', got '%v'", match.Status, match2.Status)
}
db.DeleteMatch(&match)
match2, err = db.GetMatchById(254)
if err != nil {
t.Error("Error:", err)
}
if match2 != nil {
t.Errorf("Expected '%v' to be nil", match2)
}
}
func TestTruncateMatches(t *testing.T) {
clearDb()
defer clearDb()
db, err := OpenDatabase(testDbPath)
if err != nil {
t.Error("Error:", err)
}
defer db.Close()
match := Match{254, "qualification", "254", time.Now().UTC(), 1, false, 2, false, 3, false, 4, false, 5,
false, 6, false, "", time.Now().UTC()}
db.CreateMatch(&match)
db.TruncateMatches()
match2, err := db.GetMatchById(254)
if err != nil {
t.Error("Error:", err)
}
if match2 != nil {
t.Errorf("Expected '%v' to be nil", match2)
}
}

View File

@@ -23,8 +23,9 @@ func (database *Database) CreateTeam(team *Team) error {
func (database *Database) GetTeamById(id int) (*Team, error) {
team := new(Team)
err := database.teamMap.Get(team, id)
if err != nil {
if err != nil && err.Error() == "sql: no rows in result set" {
team = nil
err = nil
}
return team, err
}

View File

@@ -11,9 +11,15 @@ func TestGetNonexistentTeam(t *testing.T) {
clearDb()
defer clearDb()
db, _ := OpenDatabase(testDbPath)
db, err := OpenDatabase(testDbPath)
if err != nil {
t.Error("Error:", err)
}
defer db.Close()
team, _ := db.GetTeamById(1114)
team, err := db.GetTeamById(1114)
if err != nil {
t.Error("Error:", err)
}
if team != nil {
t.Errorf("Expected '%v' to be nil", team)
}
@@ -23,24 +29,36 @@ func TestTeamCrud(t *testing.T) {
clearDb()
defer clearDb()
db, _ := OpenDatabase(testDbPath)
db, err := OpenDatabase(testDbPath)
if err != nil {
t.Error("Error:", err)
}
defer db.Close()
team := Team{254, "NASA Ames Research Center", "The Cheesy Poofs", "San Jose", "CA", "USA", 1999, "Barrage"}
db.CreateTeam(&team)
team2, _ := db.GetTeamById(254)
team2, err := db.GetTeamById(254)
if err != nil {
t.Error("Error:", err)
}
if team != *team2 {
t.Errorf("Expected '%v', got '%v'", team, team2)
}
team.Name = "Updated name"
db.SaveTeam(&team)
team2, _ = db.GetTeamById(254)
team2, err = db.GetTeamById(254)
if err != nil {
t.Error("Error:", err)
}
if team.Name != team2.Name {
t.Errorf("Expected '%v', got '%v'", team.Name, team2.Name)
}
db.DeleteTeam(&team)
team2, _ = db.GetTeamById(254)
team2, err = db.GetTeamById(254)
if err != nil {
t.Error("Error:", err)
}
if team2 != nil {
t.Errorf("Expected '%v' to be nil", team2)
}
@@ -50,12 +68,18 @@ func TestTruncateTeams(t *testing.T) {
clearDb()
defer clearDb()
db, _ := OpenDatabase(testDbPath)
db, err := OpenDatabase(testDbPath)
if err != nil {
t.Error("Error:", err)
}
defer db.Close()
team := Team{254, "NASA Ames Research Center", "The Cheesy Poofs", "San Jose", "CA", "USA", 1999, "Barrage"}
db.CreateTeam(&team)
db.TruncateTeams()
team2, _ := db.GetTeamById(254)
team2, err := db.GetTeamById(254)
if err != nil {
t.Error("Error:", err)
}
if team2 != nil {
t.Errorf("Expected '%v' to be nil", team2)
}