-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp_test.go
53 lines (45 loc) · 1.2 KB
/
app_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package main
import (
"reflect"
"testing"
)
func TestInsertAndQueryData(t *testing.T) {
err := initDatabase(":memory:")
if err != nil {
t.Fatal("error initializing database: ", err)
}
insertErr := insertTestData()
if insertErr != nil {
t.Fatal(insertErr)
}
expectedAlbums := []AlbumDbRow{
{4, Album{"Blue Train", "John Coltrane", 56.99}},
{1, Album{"Giant Steps", "John Coltrane", 63.99}},
}
gotAlbums, err := albumsByArtist("John Coltrane")
if err != nil {
t.Fatalf("error querying data: %v", err)
}
if len(gotAlbums) != len(expectedAlbums) {
t.Fatalf("expected to get: %d albums as result, got: %d", len(expectedAlbums), len(gotAlbums))
}
gotAlbumMap := make(map[int]AlbumDbRow)
for _, a := range gotAlbums {
gotAlbumMap[a.ID] = a
}
for _, a := range expectedAlbums {
if _, ok := gotAlbumMap[a.ID]; !ok {
t.Error("expected to get album with ID:", a.ID)
}
}
expectedAlbum := AlbumDbRow{
3, Album{"Sarah Vaughan", "Sarah Vaughan", 34.98},
}
gotAlbum, err := albumByID(expectedAlbum.ID)
if err != nil {
t.Fatal("expected non-nil error, got:", err)
}
if !reflect.DeepEqual(expectedAlbum, gotAlbum) {
t.Fatalf("expected: %#v, got: %#v", expectedAlbum, gotAlbum)
}
}