db_test.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. package main
  2. import (
  3. "fmt"
  4. "os"
  5. "testing"
  6. "time"
  7. )
  8. const DEBUG_DB bool = false
  9. func TestLock(t *testing.T) {
  10. const testdb = ".space-settings.db"
  11. db := DBData{}
  12. // Start with empty database
  13. os.Remove(testdb)
  14. db.Open(testdb)
  15. defer db.Close()
  16. if !DEBUG_DB {
  17. defer os.Remove(testdb)
  18. }
  19. db.User = LOCK_USERNAME
  20. when := time.Now().Add(-time.Hour).Unix()
  21. var oldLock string = fmt.Sprintf("%d,%d", os.Getpid(), when)
  22. db.SetSetting(LOCK_SETTING, oldLock)
  23. b := db.Lock(1)
  24. if !b {
  25. t.Error("Failed to get lock (time on lock expired).")
  26. }
  27. }
  28. func TestSettings(t *testing.T) {
  29. const testdb = ".space-settings.db"
  30. db := DBData{}
  31. // Start with empty database
  32. os.Remove(testdb)
  33. db.Open(testdb)
  34. defer db.Close()
  35. if !DEBUG_DB {
  36. defer os.Remove(testdb)
  37. }
  38. users := []string{"test", "bob", "fish"}
  39. settings := map[string]string{
  40. "LastPlayed": "20200101",
  41. "Deck": "green",
  42. "Rabbit": "carrot",
  43. }
  44. for _, user := range users {
  45. db.User = user
  46. for key, value := range settings {
  47. db.SetSetting(key, value)
  48. }
  49. }
  50. for _, user := range users {
  51. db.User = user
  52. for key, value := range settings {
  53. found := db.GetSetting(key, "?")
  54. if found != value {
  55. t.Errorf("Settings %s/%s: got %s, expected %s\n",
  56. user, key, found, value)
  57. }
  58. }
  59. found := db.GetSetting("missing", "ok")
  60. if found != "ok" {
  61. t.Errorf("Default %s: expected ok, got %s\n", user, found)
  62. }
  63. }
  64. }
  65. // Test the db.ExpireScores
  66. func TestExpireScores(t *testing.T) {
  67. const testdb = ".space-test.db"
  68. db := DBData{}
  69. // Start with empty database
  70. os.Remove(testdb)
  71. db.Open(testdb)
  72. defer db.Close()
  73. if !DEBUG_DB {
  74. defer os.Remove(testdb)
  75. }
  76. db.Unlock()
  77. db.User = "Testing"
  78. // stuff score data into database
  79. // SaveScore(when int64, date int64, hand int, won int, best int, score int)
  80. type TData struct {
  81. when YMD
  82. date YMD
  83. hand int
  84. won int
  85. run int
  86. score int
  87. }
  88. type TScore struct {
  89. Score int
  90. Run int
  91. Won int
  92. }
  93. var scoredata map[YMD]TScore = make(map[YMD]TScore)
  94. var data []TData = []TData{
  95. {20230101, 20230101, 1, 0, 8, 750},
  96. {20230101, 20230101, 2, 0, 8, 780},
  97. // {20230101, 20230101, 3, 1, 5, 980},
  98. {20230101, 20230102, 1, 1, 9, 945},
  99. {20230101, 20230102, 2, 1, 6, 985},
  100. {20230101, 20230102, 3, 0, 10, 700},
  101. {20230101, 20230115, 1, 0, 7, 600},
  102. {20230101, 20230115, 2, 0, 7, 730},
  103. {20230101, 20230115, 3, 1, 5, 1000}}
  104. var score, best, won int
  105. for idx := range data {
  106. db.SaveScore(data[idx].when, data[idx].date, data[idx].hand,
  107. data[idx].won, data[idx].run, data[idx].score)
  108. // Calculate the score, best, and won
  109. score += data[idx].score
  110. if data[idx].run > best {
  111. best = data[idx].run
  112. }
  113. won += data[idx].won
  114. w := data[idx].date
  115. _, has := scoredata[w]
  116. if !has {
  117. scoredata[w] = TScore{}
  118. }
  119. sd, has := scoredata[w]
  120. sd.Score += data[idx].score
  121. sd.Won += data[idx].won
  122. if sd.Run < data[idx].run {
  123. sd.Run = data[idx].run
  124. }
  125. scoredata[w] = sd
  126. }
  127. played := db.WhenPlayed()
  128. playdata := map[YMD]int{20230101: 2,
  129. 20230102: 3, 20230115: 3}
  130. for k, v := range played {
  131. if v != playdata[k] {
  132. t.Errorf("WhenPlayed %d: expected %d, got %d\n",
  133. k, playdata[k], v)
  134. }
  135. }
  136. scores := db.GetScores(5)
  137. // Verify this has the expected scores (pre-expire)
  138. // t.Errorf("ScoreData: %#v\n", scoredata)
  139. // t.Errorf("GetScores: %#v\n", scores)
  140. for _, s := range scores {
  141. sd, _ := scoredata[s.Date]
  142. if sd.Score != s.Score {
  143. t.Errorf("GetScores %d: Got score %d, expected %d\n",
  144. s.Date, s.Score, sd.Score)
  145. }
  146. if sd.Won != s.Won {
  147. t.Errorf("GetScores %d: Got Won %d, expected %d\n",
  148. s.Date, s.Won, sd.Won)
  149. }
  150. if sd.Run != s.Run {
  151. t.Errorf("GetScores %d: Got Run %d, expected %d\n",
  152. s.Date, s.Run, sd.Run)
  153. }
  154. }
  155. // Data loaded .. call Expire!
  156. var next_month YMD = 20230202 //time.Time = time.Unix(1645929274, 0)
  157. next_month.SetDay(1)
  158. db.ExpireScores(next_month)
  159. // 1. Verify the scores table is empty.
  160. scores = db.GetScores(5)
  161. if len(scores) != 0 {
  162. t.Errorf("Scores not empty after ExpireScores got %d expected 0.\n", len(scores))
  163. }
  164. // 2. Verify the months table is correct.
  165. monthly := db.GetMonthlyScores(5)
  166. /*
  167. type MonthlyData struct {
  168. Date int64
  169. User string
  170. Days int
  171. Hands_Won int
  172. Best int
  173. Score int
  174. }
  175. */
  176. if len(monthly) != 1 {
  177. t.Errorf("Monthly Scores: got %d, expected 1.\n", len(monthly))
  178. } else {
  179. var md MonthlyData = MonthlyData{202301, "Testing", 3, won, best, score}
  180. if monthly[0].Date != md.Date {
  181. t.Errorf("Date %d, expected %d.\n", monthly[0].Date, md.Date)
  182. }
  183. if monthly[0].User != md.User {
  184. t.Errorf("User %s, expected %s.\n", monthly[0].User, md.User)
  185. }
  186. if monthly[0].Days != md.Days {
  187. t.Errorf("Days %d, expected %d.\n", monthly[0].Days, md.Days)
  188. }
  189. if monthly[0].Hands_Won != md.Hands_Won {
  190. t.Errorf("Hands %d, expected %d.\n", monthly[0].Hands_Won, md.Hands_Won)
  191. }
  192. if monthly[0].Best_Run != md.Best_Run {
  193. t.Errorf("Run %d, expected %d.\n", monthly[0].Best_Run, md.Best_Run)
  194. }
  195. if monthly[0].Score != md.Score {
  196. t.Errorf("Score %d, expected %d.\n", monthly[0].Score, md.Score)
  197. }
  198. }
  199. }