db_test.go 2.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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")
  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 {
  57. t.Error("Error while searching for playlist")
  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. }
  80. func TestUpdatePlaylistBySectionUnknownSection(t *testing.T) {
  81. initTestCase(t)
  82. updated, err := db.UpdatePlaylistBySection("Viikko joskus", []string{"ID1", "ID2", "ID3", "ID4"})
  83. if err != nil {
  84. t.Error("Error while updating for panels", err)
  85. }
  86. if updated {
  87. t.Error("Should not update unknown section")
  88. }
  89. _, err = db.FindPlaylistBySection("Viikko joskus")
  90. if err == nil {
  91. t.Error("Should be error with unknown section", err)
  92. }
  93. }