db_test.go 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. package main
  2. import (
  3. "io/ioutil"
  4. "os"
  5. "testing"
  6. )
  7. var db *DB
  8. func initTestCase(t *testing.T) {
  9. os.Setenv("PGDATABASE", "levyraati_test")
  10. os.Setenv("PGUSER", "levyraati_test")
  11. os.Setenv("PGPASSWORD", "levyraati_test")
  12. db = InitDatabase()
  13. execSQLFile(t, "sql/clear-schema.sql")
  14. execSQLFile(t, "sql/database.sql")
  15. execSQLFile(t, "sql/database-test.sql")
  16. }
  17. func execSQLFile(t *testing.T, filename string) {
  18. data, err := ioutil.ReadFile(filename)
  19. if err != nil {
  20. t.Fatal("Unable to read setup file", filename)
  21. }
  22. _, err = db.database.Exec(string(data))
  23. if err != nil {
  24. t.Fatal("Unable to setup database", err)
  25. }
  26. }
  27. func TestDbConnWorks(t *testing.T) {
  28. initTestCase(t)
  29. if db == nil {
  30. t.Error("Database not initialized")
  31. }
  32. }
  33. func TestFindAllEntries(t *testing.T) {
  34. initTestCase(t)
  35. songs, err := db.FindAllEntries("Lamperi")
  36. if err != nil {
  37. t.Error("Error while searching for entries")
  38. }
  39. if len(songs) != 52 {
  40. t.Error("Should have found 52 entries, got", len(songs))
  41. }
  42. }
  43. func TestFindAllPanels(t *testing.T) {
  44. initTestCase(t)
  45. panels, err := db.FindAllPanels()
  46. if err != nil {
  47. t.Error("Error while searching for panels", err)
  48. }
  49. if len(panels) != 1 {
  50. t.Error("Should have found 1 panel, got", len(panels))
  51. }
  52. }
  53. func TestFindPlaylistBySection(t *testing.T) {
  54. initTestCase(t)
  55. playlistEntries, err := db.FindPlaylistBySection("Viikko 07")
  56. if err == nil || err.Error() != "sql: no rows in result set" {
  57. t.Error("err should be 'sql: no rows in result set'")
  58. }
  59. if len(playlistEntries) != 0 {
  60. t.Error("Should have found 0 playlistEntries, got", len(playlistEntries))
  61. }
  62. }
  63. func TestUpdatePlaylistBySection(t *testing.T) {
  64. initTestCase(t)
  65. updated, err := db.UpdatePlaylistBySection("Viikko 07", []string{"ID1", "ID2", "ID3", "ID4"})
  66. if err != nil {
  67. t.Error("Error while updating for panels", err)
  68. }
  69. if !updated {
  70. t.Error("Could not update section")
  71. }
  72. playlistEntries, err := db.FindPlaylistBySection("Viikko 07")
  73. if err != nil {
  74. t.Error("Error while searching for playlist", err)
  75. }
  76. if len(playlistEntries) != 4 {
  77. t.Error("Should have found 4 playlistEntries, got", len(playlistEntries))
  78. }
  79. if playlistEntries[0] != "ID1" {
  80. t.Error("Should have been ID1, got ", playlistEntries[0])
  81. }
  82. if playlistEntries[1] != "ID2" {
  83. t.Error("Should have been ID2, got ", playlistEntries[1])
  84. }
  85. if playlistEntries[2] != "ID3" {
  86. t.Error("Should have been ID3, got ", playlistEntries[2])
  87. }
  88. if playlistEntries[3] != "ID4" {
  89. t.Error("Should have been ID4, got ", playlistEntries[3])
  90. }
  91. }
  92. func TestUpdatePlaylistBySectionUnknownSection(t *testing.T) {
  93. initTestCase(t)
  94. updated, err := db.UpdatePlaylistBySection("Viikko joskus", []string{"ID1", "ID2", "ID3", "ID4"})
  95. if err != nil {
  96. t.Error("Error while updating for panels", err)
  97. }
  98. if updated {
  99. t.Error("Should not update unknown section")
  100. }
  101. _, err = db.FindPlaylistBySection("Viikko joskus")
  102. if err == nil {
  103. t.Error("Should be error with unknown section", err)
  104. }
  105. }