Преглед изворни кода

Fix finding playlist from db

Toni Fadjukoff пре 6 година
родитељ
комит
f0a37ac398
3 измењених фајлова са 122 додато и 3 уклоњено
  1. 3 0
      bot.go
  2. 11 3
      db.go
  3. 108 0
      db_test.go

+ 3 - 0
bot.go Прегледај датотеку

@@ -324,6 +324,9 @@ func (app *App) AutomateSection(title string) error {
324 324
 				} else {
325 325
 					playlistId, tracks := findPlaylist(changedWikiText)
326 326
 					currentTracks, err := app.db.FindPlaylistBySection(section.title)
327
+					if err != nil {
328
+						log.Println("Failed to find tracks from DB", err)
329
+					}
327 330
 					log.Println("Checking if playlist needs updating", currentTracks, tracks)
328 331
 					if len(tracks) > 0 && (err != nil || reflect.DeepEqual(currentTracks, tracks)) {
329 332
 

+ 11 - 3
db.go Прегледај датотеку

@@ -6,6 +6,7 @@ import (
6 6
 	"errors"
7 7
 	_ "github.com/lib/pq"
8 8
 	"log"
9
+	"os"
9 10
 )
10 11
 
11 12
 type DB struct {
@@ -13,7 +14,14 @@ type DB struct {
13 14
 }
14 15
 
15 16
 func InitDatabase() *DB {
16
-	connStr := "user=levyraati dbname=levyraati sslmode=disable"
17
+	if os.Getenv("PGUSER") == "" {
18
+		os.Setenv("PGUSER", "levyraati")
19
+	}
20
+	if os.Getenv("PGDATABASE") == "" {
21
+		os.Setenv("PGDATABASE", "levyraati")
22
+	}
23
+
24
+	connStr := "sslmode=disable"
17 25
 	var err error
18 26
 	database, err := sql.Open("postgres", connStr)
19 27
 	if err != nil {
@@ -191,12 +199,12 @@ func (db *DB) FindPlaylistBySection(sectionName string) ([]string, error) {
191 199
 	WHERE r.section = $1`
192 200
 	row := db.database.QueryRow(query, sectionName)
193 201
 	var tracksJson []byte
194
-	var tracks []string
202
+	tracks := make([]string, 0)
195 203
 	err := row.Scan(&tracksJson)
196 204
 	if err != nil {
197 205
 		return nil, err
198 206
 	}
199
-	err = json.Unmarshal(tracksJson, tracks)
207
+	err = json.Unmarshal(tracksJson, &tracks)
200 208
 	if err != nil {
201 209
 		return nil, err
202 210
 	}

+ 108 - 0
db_test.go Прегледај датотеку

@@ -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
+}