forked from tsenart/nap
-
Notifications
You must be signed in to change notification settings - Fork 2
/
db_test.go
83 lines (66 loc) · 1.42 KB
/
db_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
83
package nap
import (
"database/sql"
"testing"
"testing/quick"
_ "github.com/mattn/go-sqlite3"
)
func TestOpen(t *testing.T) {
// https://www.sqlite.org/inmemorydb.html
db, err := Open("sqlite3", ":memory:;:memory:;:memory:")
if err != nil {
t.Fatal(err)
}
defer db.Close()
if err = db.Ping(); err != nil {
t.Error(err)
}
if want, got := 3, len(db.pdbs); want != got {
t.Errorf("Unexpected number of physical dbs. Got: %d, Want: %d", got, want)
}
}
func TestClose(t *testing.T) {
db, err := Open("sqlite3", ":memory:;:memory:;:memory:")
if err != nil {
t.Fatal(err)
}
if err = db.Close(); err != nil {
t.Fatal(err)
}
if err = db.Ping(); err.Error() != "sql: database is closed" {
t.Errorf("Physical dbs were not closed correctly. Got: %s", err)
}
}
func TestWrap(t *testing.T) {
db, err := sql.Open("sqlite3", ":memory:")
if err != nil {
t.Fatal(err)
}
defer db.Close()
wdb, err := Wrap(db)
if err != nil {
t.Fatal(err)
}
if err := wdb.Ping(); err != nil {
t.Error(err)
}
if want, got := 1, len(wdb.pdbs); want != got {
t.Errorf("Unexpected number of physical dbs. Got: %d, Want: %d", got, want)
}
}
func TestSlave(t *testing.T) {
db := &DB{}
last := -1
err := quick.Check(func(n int) bool {
index := db.slave(n)
if n <= 1 {
return index == 0
}
result := index > 0 && index < n && index != last
last = index
return result
}, nil)
if err != nil {
t.Error(err)
}
}