-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdiskv_test.go
238 lines (212 loc) · 5.13 KB
/
diskv_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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
package diskvd
import (
"errors"
"os"
"path/filepath"
"reflect"
"testing"
"github.com/peterbourgon/diskv/v3"
shelvetest "github.com/lucmq/go-shelve/driver/test"
"github.com/lucmq/go-shelve/shelve"
)
var (
dbPath = filepath.Join(os.TempDir(), "diskv-test")
testError = errors.New("test error")
recordExtension = "json"
)
type CustomIndexLen struct {
diskv.Index
len int
}
func (i *CustomIndexLen) Len() int { return i.len }
func OpenTestDB() (shelve.DB, error) {
// Clean-up the database directory
err := os.RemoveAll(dbPath)
if err != nil && !os.IsNotExist(err) {
return nil, err
}
return NewDefault(dbPath, "")
}
func ReopenTestDB() (shelve.DB, error) {
return NewDefault(dbPath, "")
}
func OpenTestDBWithExtension() (shelve.DB, error) {
// Clean-up the database directory
err := os.RemoveAll(dbPath)
if err != nil && !os.IsNotExist(err) {
return nil, err
}
return NewDefault(dbPath, recordExtension)
}
func ReopenTestDBWithExtension() (shelve.DB, error) {
return NewDefault(dbPath, recordExtension)
}
func TestDB(t *testing.T) {
shelvetest.NewDBTests(OpenTestDB, ReopenTestDB).TestAll(t)
}
func TestDB_WithRecordExtension(t *testing.T) {
tests := shelvetest.NewDBTests(
OpenTestDBWithExtension,
ReopenTestDBWithExtension,
)
tests.TestAll(t)
}
func TestNewDefault(t *testing.T) {
t.Run("Error", func(t *testing.T) {
db, err := NewDefault("", "")
if err == nil {
t.Fatalf("expected error")
}
if db != nil {
defer db.Close()
}
})
}
func TestLen(t *testing.T) {
t.Run("Custom IndexLen", func(t *testing.T) {
seed := map[string]string{
"key-1": "value-1", "key-2": "value-2",
"key-3": "value-3", "key-4": "value-4",
}
db := shelvetest.StartDatabase(t, OpenTestDB, seed)
// Replace the Index
index := CustomIndexLen{len: 4}
db.(*Store).db.Index = &index
if n := db.Len(); n != int64(index.len) {
t.Errorf("expected len to be %d, but got %d", index.len, n)
}
})
t.Run("No Index", func(t *testing.T) {
seed := map[string]string{
"key-1": "value-1", "key-2": "value-2",
"key-3": "value-3", "key-4": "value-4",
}
db := shelvetest.StartDatabase(t, OpenTestDB, seed)
// Replace the Index
db.(*Store).db.Index = nil
if n := db.Len(); n != int64(len(seed)) {
t.Errorf("expected len to be %d, but got %d", len(seed), n)
}
})
}
func TestItems(t *testing.T) {
t.Run("Get error", func(t *testing.T) {
// Arrange
seed := map[string]string{
"key-1": "value-1", "key-2": "value-2",
"key-3": "value-3", "key-4": "value-4",
}
db := shelvetest.StartDatabase(t, OpenTestDB, seed)
// Replace the get function
db.(*Store).get = func(s *Store, key []byte) ([]byte, error) {
return nil, testError
}
// Act
err := db.Items(
[]byte("key-1"),
shelve.Asc,
func(k, v []byte) (bool, error) { return true, nil },
)
// Assert
if !errors.Is(err, testError) {
t.Errorf("expected error %v, but got %v", testError, err)
}
})
}
func TestSplitFilepath(t *testing.T) {
tests := []struct {
name string
input string
expected []string
}{
{
name: "empty input",
input: "",
expected: []string{},
},
{
name: "single-level path",
input: "foo",
expected: []string{"foo"},
},
{
name: "multi-level path",
input: "foo/bar/baz",
expected: []string{"foo", "bar", "baz"},
},
{
name: "path with leading slash",
input: "/foo/bar/baz",
expected: []string{"foo", "bar", "baz"},
},
{
name: "path with trailing slash",
input: "foo/bar/baz/",
expected: []string{"foo", "bar", "baz"},
},
{
name: "path with both leading and trailing slashes",
input: "/foo/bar/baz/",
expected: []string{"foo", "bar", "baz"},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
actual := SplitFilepath(tt.input)
if !reflect.DeepEqual(actual, tt.expected) {
t.Errorf("expected %v, got %v", tt.expected, actual)
}
})
}
}
func TestFilepathToPathKey(t *testing.T) {
tests := []struct {
name string
input string
expected *diskv.PathKey
}{
{
name: "empty input",
input: "",
expected: &diskv.PathKey{},
},
{
name: "root path",
input: "/",
expected: &diskv.PathKey{},
},
{
name: "single-level path",
input: "foo",
expected: &diskv.PathKey{FileName: "foo"},
},
{
name: "multi-level path",
input: "foo/bar/baz",
expected: &diskv.PathKey{Path: []string{"foo", "bar"}, FileName: "baz"},
},
{
name: "path with leading slash",
input: "/foo/bar/baz",
expected: &diskv.PathKey{Path: []string{"foo", "bar"}, FileName: "baz"},
},
{
name: "path with trailing slash",
input: "foo/bar/baz/",
expected: &diskv.PathKey{Path: []string{"foo", "bar"}, FileName: "baz"},
},
{
name: "path with both leading and trailing slashes",
input: "/foo/bar/baz/",
expected: &diskv.PathKey{Path: []string{"foo", "bar"}, FileName: "baz"},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
actual := FilepathToPathKey(tt.input)
if !reflect.DeepEqual(actual, tt.expected) {
t.Errorf("expected %v, got %v", tt.expected, actual)
}
})
}
}