-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaen_test.go
68 lines (59 loc) · 1.5 KB
/
aen_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
package aen_test
import (
"os"
"testing"
"github.com/3c7/aen"
"go.etcd.io/bbolt"
)
// Provides a temporary file path
func ProvideTemporaryFilepath(deleted bool) (path string, err error) {
tmpfile, err := os.CreateTemp("", "database")
if err != nil {
return "", err
}
tmpfile.Close()
if deleted {
os.Remove(tmpfile.Name())
}
return tmpfile.Name(), nil
}
func TestOpenDatabaseFailure(t *testing.T) {
tmpfile, err := ProvideTemporaryFilepath(true)
if err != nil {
t.Errorf("Error creating temporary file path: %v", err)
}
_, err = aen.OpenDatabase(tmpfile, false)
if err == nil {
t.Errorf("OpenDatabase should return an error as the file %s was deleted before.", tmpfile)
}
t.Logf("OpenDatabase returned expected error: %v", err)
}
func TestOpenDatabaseEnsured(t *testing.T) {
tmpfile, err := ProvideTemporaryFilepath(true)
if err != nil {
t.Errorf("Error creating temporary file path: %v", err)
}
db, err := aen.OpenDatabase(tmpfile, true)
if err != nil {
t.Errorf("Error opening database: %v", err)
}
db.Close()
os.Remove(tmpfile)
}
func TestOpenDatabase(t *testing.T) {
tmpfile, err := ProvideTemporaryFilepath(true)
if err != nil {
t.Errorf("Could not create temporary file: %v", err)
}
db, err := bbolt.Open(tmpfile, 0600, nil)
if err != nil {
t.Errorf("Could not create database instance: %v", err)
}
db.Close()
db2, err := aen.OpenDatabase(tmpfile, false)
if err != nil {
t.Fatalf("Could not open database file created by bolt.")
}
db2.Close()
os.Remove(tmpfile)
}