From 42c6ae9be8f81021eabf25bc2a15822710d42c4b Mon Sep 17 00:00:00 2001 From: Patrick Fairbank Date: Sat, 24 May 2014 18:52:10 -0700 Subject: [PATCH] Added match model. --- database.go | 10 ++- .../20140524180123_CreateMatches.sql | 24 +++++ match.go | 59 ++++++++++++ match_test.go | 89 +++++++++++++++++++ team.go | 3 +- team_test.go | 40 +++++++-- 6 files changed, 213 insertions(+), 12 deletions(-) create mode 100644 db/migrations/20140524180123_CreateMatches.sql create mode 100644 match.go create mode 100644 match_test.go diff --git a/database.go b/database.go index 9c26a00..3657b02 100644 --- a/database.go +++ b/database.go @@ -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") } diff --git a/db/migrations/20140524180123_CreateMatches.sql b/db/migrations/20140524180123_CreateMatches.sql new file mode 100644 index 0000000..1dbb14a --- /dev/null +++ b/db/migrations/20140524180123_CreateMatches.sql @@ -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; diff --git a/match.go b/match.go new file mode 100644 index 0000000..cbf927b --- /dev/null +++ b/match.go @@ -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() +} diff --git a/match_test.go b/match_test.go new file mode 100644 index 0000000..0465698 --- /dev/null +++ b/match_test.go @@ -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) + } +} diff --git a/team.go b/team.go index 8bcbcc1..2eeef87 100644 --- a/team.go +++ b/team.go @@ -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 } diff --git a/team_test.go b/team_test.go index a3ca91c..fbdec0d 100644 --- a/team_test.go +++ b/team_test.go @@ -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) }