|
@@ -0,0 +1,97 @@
|
|
|
+package main
|
|
|
+
|
|
|
+import (
|
|
|
+ "os"
|
|
|
+ "testing"
|
|
|
+ "time"
|
|
|
+)
|
|
|
+
|
|
|
+// Test the db.ExpireScores
|
|
|
+func TestExpireScores(t *testing.T) {
|
|
|
+ const testdb = ".space-test.db"
|
|
|
+
|
|
|
+ db := DBData{}
|
|
|
+ os.Remove(testdb)
|
|
|
+ db.Open(testdb)
|
|
|
+ defer db.Close()
|
|
|
+ defer os.Remove(testdb)
|
|
|
+
|
|
|
+ db.User = "Testing"
|
|
|
+ // stuff score data into database
|
|
|
+ // SaveScore(when int64, date int64, hand int, won int, score int)
|
|
|
+ type TData struct {
|
|
|
+ when int64
|
|
|
+ date int64
|
|
|
+ hand int
|
|
|
+ won int
|
|
|
+ score int
|
|
|
+ }
|
|
|
+
|
|
|
+ var data []TData = []TData{
|
|
|
+ {when: 1645929178, date: 1645858800, hand: 1, won: 1, score: 1115},
|
|
|
+ {1645929274, 1645858800, 2, 0, 955},
|
|
|
+ {1645929373, 1645858800, 3, 0, 700},
|
|
|
+ {1645931772, 1643698800, 1, 0, 660},
|
|
|
+ {1645931841, 1643698800, 2, 0, 645},
|
|
|
+ {1645931942, 1643698800, 3, 0, 695},
|
|
|
+ {1645988653, 1645945200, 1, 0, 645},
|
|
|
+ {1645988739, 1645945200, 2, 0, 750},
|
|
|
+ {1645988820, 1645945200, 3, 0, 530},
|
|
|
+ {1645993583, 1643785200, 1, 1, 1045},
|
|
|
+ {1646004744, 1643785200, 2, 0, 700},
|
|
|
+ {1646004825, 1643785200, 3, 0, 600},
|
|
|
+ }
|
|
|
+ for idx := range data {
|
|
|
+ db.SaveScore(data[idx].when, data[idx].date, data[idx].hand,
|
|
|
+ data[idx].won, data[idx].score)
|
|
|
+ }
|
|
|
+
|
|
|
+ // Data loaded .. call Expire!
|
|
|
+ var next_month time.Time = time.Unix(1645929274, 0)
|
|
|
+ next_month = next_month.AddDate(0, 1, 0)
|
|
|
+ FirstOfMonthDate(&next_month)
|
|
|
+
|
|
|
+ var next_unix int64 = next_month.Unix()
|
|
|
+ db.ExpireScores(next_unix)
|
|
|
+
|
|
|
+ // 1. Verify the scores table is empty.
|
|
|
+ scores := db.GetScores(5)
|
|
|
+
|
|
|
+ if len(scores) != 0 {
|
|
|
+ t.Errorf("Scores not empty after ExpireScores got %d expected 0.\n", len(scores))
|
|
|
+ }
|
|
|
+ // 2. Verify the months table is correct.
|
|
|
+ monthly := db.GetMonthlyScores(5)
|
|
|
+
|
|
|
+ /*
|
|
|
+ type MonthlyData struct {
|
|
|
+ Date int64
|
|
|
+ User string
|
|
|
+ Days int
|
|
|
+ Hands_Won int
|
|
|
+ Score int
|
|
|
+ }
|
|
|
+ */
|
|
|
+ if len(monthly) != 1 {
|
|
|
+ t.Errorf("Monthly Scores: got %d, expected 1.\n", len(monthly))
|
|
|
+ } else {
|
|
|
+ var md MonthlyData = MonthlyData{1643698800, "Testing", 4, 2, 9040}
|
|
|
+
|
|
|
+ if monthly[0].Date != md.Date {
|
|
|
+ t.Errorf("Date %d, expected %d.\n", monthly[0].Date, md.Date)
|
|
|
+ }
|
|
|
+ if monthly[0].User != md.User {
|
|
|
+ t.Errorf("User %s, expected %s.\n", monthly[0].User, md.User)
|
|
|
+ }
|
|
|
+ if monthly[0].Days != md.Days {
|
|
|
+ t.Errorf("Days %d, expected %d.\n", monthly[0].Days, md.Days)
|
|
|
+ }
|
|
|
+ if monthly[0].Hands_Won != md.Hands_Won {
|
|
|
+ t.Errorf("Hands %d, expected %d.\n", monthly[0].Hands_Won, md.Hands_Won)
|
|
|
+ }
|
|
|
+ if monthly[0].Score != md.Score {
|
|
|
+ t.Errorf("Score %d, expected %d.\n", monthly[0].Score, md.Score)
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+}
|