-
Notifications
You must be signed in to change notification settings - Fork 0
/
schema_test.go
150 lines (129 loc) · 4.13 KB
/
schema_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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package main
import (
"errors"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"os"
"path/filepath"
"strings"
"testing"
)
func TestSchema_Close(t *testing.T) {
t.Run("Double close check", func(t *testing.T) {
schema := Schema{}
assert.Panics(t, func() { failOnError(schema.Close()) })
})
testDir := GetTestDir()
defer testDir.Close()
database, err := OpenDatabase(testDir.Path(), "schemas.db")
require.Nil(t, err)
defer database.Close()
t.Run("Nominal operation", func(t *testing.T) {
schema, err := database.GetSchema("nominal")
require.Nil(t, err)
require.NotNil(t, schema)
assert.NotNil(t, schema.store)
assert.FileExists(t, filepath.Join(schema.db.Path(), "nominal", "main.pix"))
err = schema.Close()
assert.Nil(t, err)
assert.Nil(t, schema.store)
err = os.RemoveAll(filepath.Join(schema.db.Path(), "nominal"))
assert.Nil(t, err)
})
t.Run("Error handling", func(t *testing.T) {
schema, err := database.GetSchema("nominal")
require.Nil(t, err)
require.NotNil(t, schema)
store := schema.store
err = store.Close()
assert.Nil(t, err)
err = schema.Close()
assert.Error(t, err)
assert.Equal(t, store, schema.store)
})
}
func TestSchema_PutAndCount(t *testing.T) {
testDir := GetTestDir()
defer testDir.Close()
db, err := OpenDatabase(testDir.Path(), "put.db")
require.Nil(t, err)
defer db.Close()
schema, err := db.GetSchema("testing")
require.Nil(t, err)
defer func() { failOnError(schema.Close()) }()
assert.Zero(t, schema.Count())
assert.Nil(t, schema.Put([]byte("hello"), []byte("world")))
assert.EqualValues(t, 1, schema.Count())
assert.Nil(t, schema.Put([]byte("world"), []byte("hello")))
assert.EqualValues(t, 2, schema.Count())
assert.Nil(t, schema.Put([]byte("hello"), []byte("hello")))
assert.EqualValues(t, 2, schema.Count())
}
func TestSchema_LoadData(t *testing.T) {
testDir := GetTestDir()
defer testDir.Close()
db, err := OpenDatabase(testDir.Path(), "load.db")
require.Nil(t, err)
defer db.Close()
schema, err := db.GetSchema("schema")
require.Nil(t, err)
// With nothing in the database, nothing should happen.
log := captureLog(t, func(t *testing.T) {
loaded := 0
loader := NewDataLoader(func([]byte) error { loaded++; return nil }, func() error { return nil })
require.NotNil(t, loader)
err = schema.LoadData("nothing", loader)
assert.Nil(t, err)
assert.Zero(t, loaded)
})
assert.Len(t, log, 1)
assert.True(t, strings.HasSuffix(log[0], "Loaded 0 nothing."))
// It should also have closed the schema.
assert.Panics(t, func() { failOnError(schema.Close()) })
runTest := func(setupFn func(*Schema)) ([]string, []string, uint32, error) {
schema, err = db.GetSchema("schema")
require.Nil(t, err)
setupFn(schema)
err = nil
marshaled := make([]string, 0, 4)
loaded := 0
loader := NewDataLoader(func(data []byte) error {
str := string(data)
marshaled = append(marshaled, str)
if str == "error" {
return errors.New("got error")
}
return nil
}, func() error { loaded++; return nil })
log := captureLog(t, func(t *testing.T) {
err = schema.LoadData("stuff", loader)
assert.Panics(t, func() { failOnError(schema.Close()) })
})
schema, _ = db.GetSchema("schema")
defer func() { failOnError(schema.Close()) }()
count := schema.Count()
return log, marshaled, count, err
}
log, marshalled, count, err := runTest(func(schema *Schema) {
assert.Nil(t, schema.Put([]byte("hello"), []byte("world")))
assert.Nil(t, schema.Put([]byte("world"), []byte("hello")))
assert.Nil(t, schema.Put([]byte("final"), []byte("biscuit")))
})
assert.Nil(t, err)
assert.Equal(t, []string{"world", "hello", "biscuit"}, marshalled)
if assert.Len(t, log, 1) {
assert.True(t, strings.HasSuffix(log[0], "Loaded 3 stuff."))
}
assert.EqualValues(t, 3, count)
log, marshalled, count, err = runTest(func(schema *Schema) {
assert.Nil(t, schema.Put([]byte("final"), []byte("error")))
})
assert.Error(t, err)
assert.Equal(t, []string{"world", "hello", "error"}, marshalled)
assert.Empty(t, log)
assert.EqualValues(t, count, 2)
}
func TestSchema_Name(t *testing.T) {
s := Schema{name: "foible"}
assert.Equal(t, "foible", s.Name())
}