-
Notifications
You must be signed in to change notification settings - Fork 26
/
database_test.go
82 lines (70 loc) · 1.62 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package main
import (
"os"
"strings"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func Test_database(t *testing.T) {
t.Run("Basic Database Test", func(t *testing.T) {
app := &goBlog{
cfg: &config{},
}
db, err := app.openDatabase("file::memory:?cache=shared", false, 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)
}
})
}
func Test_database_dump(t *testing.T) {
app := &goBlog{
cfg: createDefaultTestConfig(t),
}
app.cfg.Db.DumpFile = strings.ReplaceAll(app.cfg.Db.File, ".db", ".sql")
_ = app.initConfig(false)
// Check if dump exists
dumpBytes, err := os.ReadFile(app.cfg.Db.DumpFile)
require.NoError(t, err)
assert.Contains(t, string(dumpBytes), "CREATE TABLE")
}