2014-05-24 18:52:10 -07:00
|
|
|
// Copyright 2014 Team 254. All Rights Reserved.
|
|
|
|
|
// Author: pat@patfairbank.com (Patrick Fairbank)
|
|
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
|
|
import (
|
2014-05-25 23:03:50 -07:00
|
|
|
"github.com/stretchr/testify/assert"
|
2014-05-24 18:52:10 -07:00
|
|
|
"testing"
|
|
|
|
|
"time"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func TestGetNonexistentMatch(t *testing.T) {
|
|
|
|
|
clearDb()
|
|
|
|
|
defer clearDb()
|
|
|
|
|
db, err := OpenDatabase(testDbPath)
|
2014-05-25 23:03:50 -07:00
|
|
|
assert.Nil(t, err)
|
2014-05-24 18:52:10 -07:00
|
|
|
defer db.Close()
|
|
|
|
|
|
|
|
|
|
match, err := db.GetMatchById(1114)
|
2014-05-25 23:03:50 -07:00
|
|
|
assert.Nil(t, err)
|
|
|
|
|
assert.Nil(t, match)
|
2014-05-24 18:52:10 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestMatchCrud(t *testing.T) {
|
|
|
|
|
clearDb()
|
|
|
|
|
defer clearDb()
|
|
|
|
|
db, err := OpenDatabase(testDbPath)
|
2014-05-25 23:03:50 -07:00
|
|
|
assert.Nil(t, err)
|
2014-05-24 18:52:10 -07:00
|
|
|
defer db.Close()
|
|
|
|
|
|
2014-05-26 17:07:55 -07:00
|
|
|
match := Match{0, "qualification", "254", time.Now().UTC(), 1, false, 2, false, 3, false, 4, false, 5,
|
2014-05-24 18:52:10 -07:00
|
|
|
false, 6, false, "", time.Now().UTC()}
|
|
|
|
|
db.CreateMatch(&match)
|
2014-05-26 17:07:55 -07:00
|
|
|
match2, err := db.GetMatchById(1)
|
2014-05-25 23:03:50 -07:00
|
|
|
assert.Nil(t, err)
|
|
|
|
|
assert.Equal(t, match, *match2)
|
2014-05-24 18:52:10 -07:00
|
|
|
|
|
|
|
|
match.Status = "started"
|
|
|
|
|
db.SaveMatch(&match)
|
2014-05-26 17:07:55 -07:00
|
|
|
match2, err = db.GetMatchById(1)
|
2014-05-25 23:03:50 -07:00
|
|
|
assert.Nil(t, err)
|
|
|
|
|
assert.Equal(t, match.Status, match2.Status)
|
2014-05-24 18:52:10 -07:00
|
|
|
|
|
|
|
|
db.DeleteMatch(&match)
|
2014-05-26 17:07:55 -07:00
|
|
|
match2, err = db.GetMatchById(1)
|
2014-05-25 23:03:50 -07:00
|
|
|
assert.Nil(t, err)
|
|
|
|
|
assert.Nil(t, match2)
|
2014-05-24 18:52:10 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestTruncateMatches(t *testing.T) {
|
|
|
|
|
clearDb()
|
|
|
|
|
defer clearDb()
|
|
|
|
|
db, err := OpenDatabase(testDbPath)
|
2014-05-25 23:03:50 -07:00
|
|
|
assert.Nil(t, err)
|
2014-05-24 18:52:10 -07:00
|
|
|
defer db.Close()
|
|
|
|
|
|
2014-05-26 17:07:55 -07:00
|
|
|
match := Match{0, "qualification", "254", time.Now().UTC(), 1, false, 2, false, 3, false, 4, false, 5,
|
2014-05-24 18:52:10 -07:00
|
|
|
false, 6, false, "", time.Now().UTC()}
|
|
|
|
|
db.CreateMatch(&match)
|
|
|
|
|
db.TruncateMatches()
|
2014-05-26 17:07:55 -07:00
|
|
|
match2, err := db.GetMatchById(1)
|
2014-05-25 23:03:50 -07:00
|
|
|
assert.Nil(t, err)
|
|
|
|
|
assert.Nil(t, match2)
|
2014-05-24 18:52:10 -07:00
|
|
|
}
|
2014-05-26 16:39:20 -07:00
|
|
|
|
|
|
|
|
func TestGetMatchesByType(t *testing.T) {
|
|
|
|
|
clearDb()
|
|
|
|
|
defer clearDb()
|
|
|
|
|
db, err := OpenDatabase(testDbPath)
|
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
defer db.Close()
|
|
|
|
|
|
2014-05-26 17:07:55 -07:00
|
|
|
match := Match{0, "qualification", "1", time.Now().UTC(), 1, false, 2, false, 3, false, 4, false, 5,
|
2014-05-26 16:39:20 -07:00
|
|
|
false, 6, false, "", time.Now().UTC()}
|
|
|
|
|
db.CreateMatch(&match)
|
2014-05-26 17:07:55 -07:00
|
|
|
match2 := Match{0, "practice", "1", time.Now().UTC(), 1, false, 2, false, 3, false, 4, false, 5,
|
2014-05-26 16:39:20 -07:00
|
|
|
false, 6, false, "", time.Now().UTC()}
|
|
|
|
|
db.CreateMatch(&match2)
|
2014-05-26 17:07:55 -07:00
|
|
|
match3 := Match{0, "practice", "2", time.Now().UTC(), 1, false, 2, false, 3, false, 4, false, 5,
|
2014-05-26 16:39:20 -07:00
|
|
|
false, 6, false, "", time.Now().UTC()}
|
|
|
|
|
db.CreateMatch(&match3)
|
|
|
|
|
|
|
|
|
|
matches, err := db.GetMatchesByType("test")
|
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
assert.Empty(t, matches)
|
|
|
|
|
matches, err = db.GetMatchesByType("practice")
|
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
assert.Equal(t, 2, len(matches))
|
|
|
|
|
matches, err = db.GetMatchesByType("qualification")
|
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
assert.Equal(t, 1, len(matches))
|
|
|
|
|
}
|