forked from tsenart/nap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
db_test.go
61 lines (48 loc) · 1.07 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
package nap
import (
"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:", nil, "service")
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:", nil, "service")
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 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)
}
}