forked from jlelse/GoBlog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
database_test.go
63 lines (55 loc) · 1.15 KB
/
database_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
54
55
56
57
58
59
60
61
62
63
package main
import (
"testing"
)
func Test_database(t *testing.T) {
t.Run("Basic Database Test", func(t *testing.T) {
app := &goBlog{
cfg: &config{},
}
db, err := app.openDatabase(":memory:", false)
if err != nil {
t.Fatalf("Error: %v", err)
}
_, err = db.Exec("create table test(test text);")
if err != nil {
t.Fatalf("Error: %v", err)
}
_, err = db.Exec("insert into test (test) values ('Test')")
if err != nil {
t.Fatalf("Error: %v", err)
}
row, err := db.QueryRow("select count(test) from test")
if err != nil {
t.Fatalf("Error: %v", err)
}
var test1 int
err = row.Scan(&test1)
if err != nil {
t.Fatalf("Error: %v", err)
}
if test1 != 1 {
t.Error("Wrong result")
}
rows, err := db.Query("select count(test), test from test")
if err != nil {
t.Fatalf("Error: %v", err)
}
var test2 int
var testStr string
if !rows.Next() {
t.Error("No result row")
}
err = rows.Scan(&test2, &testStr)
if err != nil {
t.Fatalf("Error: %v", err)
}
if test2 != 1 || testStr != "Test" {
t.Error("Wrong result")
}
err = db.close()
if err != nil {
t.Fatalf("Error: %v", err)
}
})
}