-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatabase_test.go
232 lines (201 loc) · 4.82 KB
/
database_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
package nve
import (
"os"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func withNewDB(runtest func(db *DB)) {
withNewDBPath(func(db *DB, dbPath string) {
runtest(db)
})
}
func generateTempDBPath() string {
tempFile, err := os.CreateTemp(os.TempDir(), "*.db")
if err != nil {
panic(err)
}
file := tempFile.Name()
os.Remove(file)
return file
}
func withNewDBPath(runtest func(db *DB, dbPath string)) {
dbPath := generateTempDBPath()
db := MustOpen(dbPath)
defer func() {
if db != nil {
db.Close()
}
os.Remove(dbPath)
}()
runtest(db, dbPath)
}
func TestDatabaseInitialization(t *testing.T) {
testCases := []struct {
name string
setup func(db *DB, dbPath string) *DB
assert func(*testing.T, *DB, string)
}{
{
name: "Creates a new database",
assert: func(t *testing.T, d *DB, dbPath string) {
_, err := os.Stat(dbPath)
assert.NoError(t, err, "DB does not exist")
},
},
{
name: "Re-opens existing database",
setup: func(db *DB, dbPath string) *DB {
db.Close()
return MustOpen(dbPath)
},
assert: func(t *testing.T, db *DB, dbPath string) {
if err := db.Ping(); err != nil {
assert.NoError(t, err, "DB did not open")
}
},
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
withNewDBPath(func(db *DB, dbPath string) {
if tc.setup != nil {
db = tc.setup(db, dbPath)
}
tc.assert(t, db, dbPath)
})
})
}
}
func TestDocumentInsertion(t *testing.T) {
var (
fileRef *FileRef
data []byte
)
testCases := []struct {
name string
setup func()
assert func(*testing.T, *DB)
}{
{
name: "is successful",
assert: func(t *testing.T, db *DB) { checkCount(t, db, 1) },
},
{
name: "is indexed",
assert: func(t *testing.T, db *DB) { checkIsUnmodified(t, db, fileRef) },
},
{
name: "requires filename",
setup: func() { fileRef.Filename = "" },
assert: func(t *testing.T, db *DB) { checkCount(t, db, 0) },
},
{
name: "requires md5",
setup: func() { fileRef.MD5 = "" },
assert: func(t *testing.T, db *DB) { checkCount(t, db, 0) },
},
{
name: "requires last modified date",
setup: func() { fileRef.ModifiedAt = time.Time{} },
assert: func(t *testing.T, db *DB) { checkCount(t, db, 0) },
},
{
name: "allows empty data",
setup: func() { data = nil },
assert: func(t *testing.T, db *DB) { checkCount(t, db, 1) },
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
fileRef = &FileRef{
Filename: "/tmp/some_file.txt",
MD5: "b9fe6c5ee4966accc23e32adea6f537d",
ModifiedAt: time.Now(),
}
data = []byte("some data")
withNewDB(func(db *DB) {
if tc.setup != nil {
tc.setup()
}
db.Upsert(fileRef, data)
tc.assert(t, db)
})
})
}
}
func TestDocumentUpdate(t *testing.T) {
var (
data = []byte("some data")
fileRef = FileRef{
Filename: "/tmp/some_file.txt",
MD5: "b9fe6c5ee4966accc23e32adea6f537d",
ModifiedAt: time.Now(),
}
)
testCases := []struct {
name string
update *FileRef
expectChanged bool
}{
{
name: "updates when MD5 changes",
update: &FileRef{
Filename: fileRef.Filename,
MD5: "NEW MD5____",
ModifiedAt: fileRef.ModifiedAt,
},
expectChanged: true,
},
{
name: "updates when modified timestamp changes",
update: &FileRef{
Filename: fileRef.Filename,
MD5: fileRef.MD5,
ModifiedAt: time.Now().Add(5 * time.Second),
},
expectChanged: true,
},
{
name: "does not update with zero changes",
update: &fileRef,
expectChanged: false,
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
withNewDB(func(db *DB) {
// Insert initial record
err := db.Insert(&fileRef, data)
if err != nil {
assert.FailNow(t, "Query failed", err)
}
newRef := tc.update
if err := db.Upsert(newRef, []byte("fresher data")); err != nil {
assert.FailNow(t, "upsert failed", err)
}
var currentData []string
if err := db.Select(¤tData, "SELECT text from content_index"); err != nil {
assert.FailNow(t, "index did not return results", err)
}
if tc.expectChanged {
assert.Equal(t, "fresher data", currentData[0], "document data was not updated")
} else {
assert.Equal(t, "some data", currentData[0], "document data was updated")
}
})
})
}
}
func checkCount(t *testing.T, db *DB, expected int) {
var count int
db.QueryRow("SELECT count(*) from documents").Scan(&count)
if count != expected {
t.Errorf("document count '%v' does not match expected '%v'\n", count, expected)
}
}
func checkIsUnmodified(t *testing.T, db *DB, fileRef *FileRef) {
if !db.IsUnmodified(fileRef) {
t.Errorf("expected file '%s' to appear in index", fileRef.Filename)
}
}