This repository has been archived by the owner on Apr 17, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
indexer_test.go
359 lines (290 loc) · 6.79 KB
/
indexer_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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
package wally
import (
"fmt"
"io/ioutil"
"os"
"strings"
"testing"
rdb "github.com/dancannon/gorethink"
"github.com/stretchr/testify/assert"
)
const (
TestConfig = `
database:
host: localhost:28015
name: testing
tables:
document_table: documents
index_table: indexes
`
)
var (
session *rdb.Session
)
func init() {
var err error
Conf, err = LoadConfig([]byte(TestConfig))
if err != nil {
fmt.Errorf(err.Error())
}
session, err = rdb.Connect(rdb.ConnectOpts{
Address: os.Getenv("RETHINKDB_URL"),
Database: "test",
})
if err != nil {
fmt.Errorf(err.Error())
}
// Reset database
rdb.DBCreate(Conf.Database.Name).Exec(session)
rdb.DB(Conf.Database.Name).TableCreate(Conf.Tables.DocumentTable).Exec(session)
rdb.DB(Conf.Database.Name).TableCreate(Conf.Tables.IndexTable).Exec(session)
rdb.DB(Conf.Database.Name).Table(Conf.Tables.IndexTable).IndexCreate("word").Exec(session)
}
func tearDbDown(session *rdb.Session) {
rdb.DB(Conf.Database.Name).Table(Conf.Tables.DocumentTable).Delete(rdb.DeleteOpts{
Durability: "soft",
ReturnChanges: false,
}).Exec(session)
rdb.DB(Conf.Database.Name).Table(Conf.Tables.IndexTable).Delete(rdb.DeleteOpts{
Durability: "soft",
ReturnChanges: false,
}).Exec(session)
}
func TestIndexer_Stopper(t *testing.T) {
tests := []struct {
Input string
Output string
}{
{
"computer",
"computer",
},
{
"the",
"",
},
{
"technology",
"technology",
},
{
"wasn't",
"",
},
}
for _, test := range tests {
assert.Equal(t, test.Output, Stopper(test.Input))
}
}
func TestIndexer_SplitTextIntoWords(t *testing.T) {
tests := []struct {
Input interface{}
Output []string
}{
{
"I am a block of text",
[]string{"I", "am", "a", "block", "of", "text"},
},
{
" superfluous whitespace ",
[]string{"superfluous", "whitespace"},
},
{
" ",
[]string{},
},
{
[]byte("fancy a byte?"),
[]string{"fancy", "a", "byte?"},
},
{
32,
[]string{"32"},
},
{
true,
[]string{},
},
}
for _, test := range tests {
assert.Equal(t, test.Output, SplitTextIntoWords(test.Input))
}
}
func TestIndexer_Indexer(t *testing.T) {
docID := "12345-67890-ABCDE"
tests := []struct {
Input interface{}
Output []Index
}{
{
"I am a block of text and I am going to be parsed",
[]Index{
Index{Word: "block", DocumentID: docID, Count: 0},
Index{Word: "text", DocumentID: docID, Count: 0},
Index{Word: "going", DocumentID: docID, Count: 0},
Index{Word: "parsed", DocumentID: docID, Count: 0},
},
},
{
[]byte("I am another block of text but now I am in bytes"),
[]Index{
Index{Word: "another", DocumentID: docID, Count: 0},
Index{Word: "block", DocumentID: docID, Count: 0},
Index{Word: "text", DocumentID: docID, Count: 0},
Index{Word: "now", DocumentID: docID, Count: 0},
Index{Word: "bytes", DocumentID: docID, Count: 0},
},
},
}
for x, test := range tests {
i := Indexer(test.Input, docID)
assert.NotEqual(t, "", i[x].ID)
}
}
func TestIndexer_IndexString(t *testing.T) {
indexID := "world"
index := Index{ID: indexID}
assert.Equal(t, "Index#world", index.String())
}
func TestIndexer_IndexPut(t *testing.T) {
defer tearDbDown(session)
index := Index{Word: "hello", Count: 5, DocumentID: "12345-67890-ABCDE"}
err := index.Put(session)
assert.Nil(t, err)
res, err := rdb.DB(Conf.Database.Name).Table(Conf.Tables.IndexTable).Get(index.ID).Run(session)
assert.Nil(t, err)
var i Index
err = res.One(&i)
assert.Nil(t, err)
assert.Equal(t, i.ID, "12345-67890-ABCDE::hello")
assert.Equal(t, i.Word, "hello")
assert.Equal(t, i.Count, int64(5))
assert.Equal(t, i.DocumentID, "12345-67890-ABCDE")
}
func TestIndexer_IndexPutInvalid(t *testing.T) {
defer tearDbDown(session)
i := Index{ID: "1"}
i2 := Index{ID: "1"}
err := i.Put(session)
assert.NoError(t, err)
err = i2.Put(session)
assert.Error(t, err)
}
func TestIndexer_IndexBatchPut(t *testing.T) {
indexes := []Index{
{
ID: "1",
Word: "dupe",
},
{
ID: "1",
Word: "dupe",
},
}
err := IndexBatchPut(session, indexes)
assert.Error(t, err)
}
func TestIndexer_DocumentString(t *testing.T) {
docID := "12345-67890-ABCDE"
doc := Document{ID: docID}
assert.Equal(t, "Document#12345-67890-ABCDE", doc.String())
}
func TestIndexer_DocumentPut(t *testing.T) {
defer tearDbDown(session)
doc := Document{
ID: "www.google.com",
Content: "Lorem ipsum dolor sit amet.",
}
err := doc.Put(session)
assert.Nil(t, err)
res, err := rdb.DB(Conf.Database.Name).Table(Conf.Tables.DocumentTable).Run(session)
assert.Nil(t, err)
var d Document
err = res.One(&d)
assert.Nil(t, err)
assert.Equal(t, d.ID, "www.google.com")
assert.Equal(t, d.Content, "Lorem ipsum dolor sit amet.")
}
func TestIndexer_DocumentPutDupeDocs(t *testing.T) {
defer tearDbDown(session)
doc1 := Document{ID: "1"}
doc2 := Document{ID: "1"}
err := doc1.Put(session)
assert.NoError(t, err)
err = doc2.Put(session)
assert.Error(t, err)
}
func TestIndexer_RemoveDuplicates(t *testing.T) {
indexes := []Index{
Index{Word: "hello"},
Index{Word: "world"},
Index{Word: "hello"},
Index{Word: "hello"},
Index{Word: "world"},
Index{Word: "cruel"},
}
indexes = RemoveDuplicates(indexes)
assert.Equal(t, len(indexes), 3)
}
func BenchmarkSplitTextIntoWords(b *testing.B) {
file, err := ioutil.ReadFile("test_data/test.txt") // 30654 words
if err != nil {
b.Error("Could not load test data")
}
for n := 0; n < b.N; n++ {
SplitTextIntoWords(file)
}
}
func BenchmarkSplitTextIntoWords_two(b *testing.B) {
file, err := ioutil.ReadFile("test_data/test_2.txt") // 1891 words
if err != nil {
b.Error("Could not load test data")
}
for n := 0; n < b.N; n++ {
SplitTextIntoWords(file)
}
}
func BenchmarkStopper(b *testing.B) {
file, err := ioutil.ReadFile("test_data/test.txt") // 30654 words
if err != nil {
b.Error("Could not load test data")
}
data := strings.Fields(string(file))
for n := 0; n < b.N; n++ {
for _, word := range data {
Stopper(word)
}
}
}
func BenchmarkStopper_two(b *testing.B) {
file, err := ioutil.ReadFile("test_data/test_2.txt") // 1891 words
if err != nil {
b.Error("Could not load test data")
}
data := strings.Fields(string(file))
for n := 0; n < b.N; n++ {
for _, word := range data {
Stopper(word)
}
}
}
func BenchmarkIndexer(b *testing.B) {
docID := "12345-67890-ABCDE"
file, err := ioutil.ReadFile("test_data/test.txt") // 30654 words
if err != nil {
b.Error("Could not load test data")
}
for n := 0; n < b.N; n++ {
Indexer(file, docID)
}
}
func BenchmarkIndexer_two(b *testing.B) {
docID := "12345-67890-ABCDE"
file, err := ioutil.ReadFile("test_data/test_2.txt") // 30654 words
if err != nil {
b.Error("Could not load test data")
}
for n := 0; n < b.N; n++ {
Indexer(file, docID)
}
}