2019-12-01 18:58:09 -07:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2019-12-01 20:53:13 -07:00
|
|
|
"bytes"
|
2019-12-01 18:58:09 -07:00
|
|
|
"os"
|
|
|
|
"testing"
|
2020-08-14 20:26:04 -06:00
|
|
|
|
|
|
|
"github.com/spf13/afero"
|
2019-12-01 18:58:09 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestState(t *testing.T) {
|
|
|
|
fs := new(afero.MemMapFs)
|
|
|
|
|
|
|
|
mustExist := func(path string) {
|
|
|
|
_, err := fs.Stat(path)
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
t.Errorf("File %s does not exist", path)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
s := NewState(fs)
|
2020-02-29 18:36:59 -07:00
|
|
|
s.Update()
|
2019-12-01 18:58:09 -07:00
|
|
|
|
2019-12-01 20:53:13 -07:00
|
|
|
pl := s.PointsLog()
|
|
|
|
if len(pl) != 0 {
|
|
|
|
t.Errorf("Empty points log is not empty")
|
|
|
|
}
|
2019-12-01 20:47:46 -07:00
|
|
|
|
2019-12-01 18:58:09 -07:00
|
|
|
mustExist("initialized")
|
|
|
|
mustExist("enabled")
|
|
|
|
mustExist("hours")
|
2019-12-01 20:53:13 -07:00
|
|
|
|
2019-12-01 20:47:46 -07:00
|
|
|
teamidsBuf, err := afero.ReadFile(fs, "teamids.txt")
|
|
|
|
if err != nil {
|
2019-12-01 20:53:13 -07:00
|
|
|
t.Errorf("Reading teamids.txt: %v", err)
|
2019-12-01 20:47:46 -07:00
|
|
|
}
|
2019-12-01 20:53:13 -07:00
|
|
|
|
2019-12-01 20:47:46 -07:00
|
|
|
teamids := bytes.Split(teamidsBuf, []byte("\n"))
|
|
|
|
if (len(teamids) != 101) || (len(teamids[100]) > 0) {
|
2019-12-01 20:53:13 -07:00
|
|
|
t.Errorf("There weren't 100 teamids, there were %d", len(teamids))
|
2019-12-01 20:47:46 -07:00
|
|
|
}
|
2019-12-05 21:50:43 -07:00
|
|
|
teamId := string(teamids[0])
|
2019-12-01 20:53:13 -07:00
|
|
|
|
2019-12-01 20:47:46 -07:00
|
|
|
if err := s.SetTeamName("bad team ID", "bad team name"); err == nil {
|
2019-12-01 20:53:13 -07:00
|
|
|
t.Errorf("Setting bad team ID didn't raise an error")
|
2019-12-01 20:47:46 -07:00
|
|
|
}
|
2019-12-01 20:53:13 -07:00
|
|
|
|
2019-12-05 21:50:43 -07:00
|
|
|
if err := s.SetTeamName(teamId, "My Team"); err != nil {
|
2019-12-01 20:53:13 -07:00
|
|
|
t.Errorf("Setting team name: %v", err)
|
2019-12-01 20:47:46 -07:00
|
|
|
}
|
2019-12-07 21:17:13 -07:00
|
|
|
|
2019-12-05 21:50:43 -07:00
|
|
|
category := "poot"
|
|
|
|
points := 3928
|
|
|
|
s.AwardPoints(teamId, category, points)
|
2020-02-29 18:36:59 -07:00
|
|
|
s.Update()
|
2019-12-07 21:17:13 -07:00
|
|
|
|
2019-12-05 21:50:43 -07:00
|
|
|
pl = s.PointsLog()
|
|
|
|
if len(pl) != 1 {
|
|
|
|
t.Errorf("After awarding points, points log has length %d", len(pl))
|
2020-08-14 20:26:04 -06:00
|
|
|
} else if (pl[0].TeamID != teamId) || (pl[0].Category != category) || (pl[0].Points != points) {
|
2019-12-05 21:50:43 -07:00
|
|
|
t.Errorf("Incorrect logged award %v", pl)
|
|
|
|
}
|
2019-12-07 21:17:13 -07:00
|
|
|
|
2019-12-05 21:54:32 -07:00
|
|
|
fs.Remove("initialized")
|
2020-02-29 18:36:59 -07:00
|
|
|
s.Update()
|
2019-12-07 21:17:13 -07:00
|
|
|
|
2019-12-05 21:54:32 -07:00
|
|
|
pl = s.PointsLog()
|
|
|
|
if len(pl) != 0 {
|
|
|
|
t.Errorf("After reinitialization, points log has length %d", len(pl))
|
|
|
|
}
|
2019-12-01 18:58:09 -07:00
|
|
|
}
|