forked from healeycodes/crane-search
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsearch_test.go
80 lines (68 loc) · 1.82 KB
/
search_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
// Simple Full-Text Search engine
// Copied/Modified from https://github.com/akrylysov/simplefts
// LICENSE: https://github.com/akrylysov/simplefts/blob/master/LICENSE
package search
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestIndex(t *testing.T) {
idx := make(Index)
assert.Nil(t, idx.Search("foo"))
assert.Nil(t, idx.Search("donut"))
idx.Add([]document{{ID: 1, Text: "A donut on a glass plate. Only the donuts."}})
assert.Nil(t, idx.Search("a"))
assert.Equal(t, idx.Search("donut"), []int{1})
assert.Equal(t, idx.Search("DoNuts"), []int{1})
assert.Equal(t, idx.Search("glass"), []int{1})
idx.Add([]document{{ID: 2, Text: "donut is a donut"}})
assert.Nil(t, idx.Search("a"))
assert.Equal(t, idx.Search("donut"), []int{1, 2})
assert.Equal(t, idx.Search("DoNuts"), []int{1, 2})
assert.Equal(t, idx.Search("glass"), []int{1})
}
func TestTokenizer(t *testing.T) {
testCases := []struct {
text string
tokens []string
}{
{
text: "",
tokens: []string{},
},
{
text: "a",
tokens: []string{"a"},
},
{
text: "small wild,cat!",
tokens: []string{"small", "wild", "cat"},
},
}
for _, tc := range testCases {
t.Run(tc.text, func(st *testing.T) {
assert.EqualValues(st, tc.tokens, Tokenize(tc.text))
})
}
}
func TestLowercaseFilter(t *testing.T) {
var (
in = []string{"Cat", "DOG", "fish"}
out = []string{"cat", "dog", "fish"}
)
assert.Equal(t, out, LowercaseFilter(in))
}
func TestStopwordFilter(t *testing.T) {
var (
in = []string{"i", "am", "the", "cat"}
out = []string{"am", "cat"}
)
assert.Equal(t, out, StopwordFilter(in))
}
func TestStemmerFilter(t *testing.T) {
var (
in = []string{"cat", "cats", "fish", "fishing", "fished", "airline"}
out = []string{"cat", "cat", "fish", "fish", "fish", "airlin"}
)
assert.Equal(t, out, StemmerFilter(in))
}