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