-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
testing.go
54 lines (48 loc) · 1.39 KB
/
testing.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
package squirrel
import (
"os"
"path/filepath"
"testing"
qt "github.com/frankban/quicktest"
)
func TestingNewCache(c *qt.C, opts NewCacheOpts) *Cache {
if opts.SetJournalMode == "wal" && (opts.Memory || opts.Path == "") {
c.Skip("can't use WAL with anonymous or memory database")
}
if opts.Memory && opts.SetLockingMode != "exclusive" {
c.Skip("in-memory databases are always exclusive")
}
if opts.Path == "" && opts.SetLockingMode == "normal" {
c.Skip("anonymous databases are always exclusive")
}
cache, err := NewCache(opts)
c.Assert(err, qt.IsNil)
c.Cleanup(func() {
err := cache.Close()
c.Check(err, qt.IsNil)
})
return cache
}
func TestingTempCachePath(c testing.TB) string {
if cleanupDatabases {
// Put the database in the test temp dir, so it gets removed automatically.
return filepath.Join(c.TempDir(), "squirrel.db")
}
// Create a temporary file in the OS temp dir, so we can inspect it after the tests.
f, err := os.CreateTemp("", "squirrel.db")
if err != nil {
c.Fatalf("creating temp cache path: %v", err)
}
path := f.Name()
c.Logf("cache path: %v", path)
f.Close()
return path
}
// Whether to remove databases after tests run, or leave them behind and log where they are for
// inspection.
const cleanupDatabases = true
func TestingDefaultCacheOpts(tb testing.TB) (ret NewCacheOpts) {
ret.Path = TestingTempCachePath(tb)
//ret.SetJournalMode = "wal"
return
}