-
Notifications
You must be signed in to change notification settings - Fork 0
/
index_test.go
156 lines (132 loc) · 3.96 KB
/
index_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
package main
import (
"testing"
"github.com/google/go-cmp/cmp"
)
func Test_new_has_capacity_matching_document_size(t *testing.T) {
index := New(20)
if index.Capacity() != 20 {
t.Errorf("index.Capacity()=%v, want 20", index.Capacity())
}
}
func Test_new_has_zero_word_count_after_initialisation(t *testing.T) {
index := New(20)
if index.WordCount() != 0 {
t.Errorf("index.WordCount()=%v, want 0", index.WordCount())
}
}
func Test_update_with_single_document_updates_word_count_correctly(t *testing.T) {
index := New(20)
doc := fooMD()
index.Update(doc)
if index.WordCount() != 2 {
t.Errorf("index.WordCount()=%v, want 2", index.WordCount())
}
}
func Test_search_errors_if_term_not_found_in_index(t *testing.T) {
index := New(20)
_, err := index.Search("world")
if err != ErrWordNotIndexed {
t.Errorf("index.Search(`world`) error=%v, want ErrWordNotIndexed", err)
}
}
func Test_searching_multiple_documents_returns_document_matching_term(t *testing.T) {
index := New(20)
doc := fooMD()
index.Update(doc)
doc = barMD()
index.Update(doc)
docs, _ := index.Search("hello")
expected := DocList{{Document: "foo.md", Count: 1}}
if !cmp.Equal(docs, expected) {
t.Errorf("index.Search(`hello`) mismatch (-want +got)\n%s", cmp.Diff(expected, docs))
}
}
func Test_fuzzy_search_multiple_documents_should_return_matching_documents_by_term_distance(t *testing.T) {
index := New(20)
doc := fooMD()
index.Update(doc)
doc = barMD()
index.Update(doc)
docs, _ := index.Search("hell")
expected := DocList{{Document: "foo.md", Count: 1, Distance: 1}, {Document: "bar.md", Count: 1, Distance: 7}}
if !cmp.Equal(docs, expected) {
t.Errorf("index.Search(`hello`) mismatch (-want +got)\n%v", cmp.Diff(expected, docs))
}
}
func Test_searching_multiple_documents_should_return_documents_matching_common_term(t *testing.T) {
index := New(2)
doc := fooMD()
index.Update(doc)
doc = barMD()
index.Update(doc)
docs, _ := index.Search("world")
expected := DocList{{Document: "foo.md", Count: 1}, {Document: "bar.md", Count: 1}}
if !cmp.Equal(docs, expected) {
t.Errorf("index.Search(`hello`) mismatch (-want +got)\n%v", cmp.Diff(expected, docs))
}
}
func Test_index_deduplicate_documents_on_update(t *testing.T) {
index := New(20)
doc := fooMD()
index.Update(doc)
index.Update(doc)
docs, _ := index.Search("hello")
expected := DocList{{Document: "foo.md", Count: 1}}
if !cmp.Equal(docs, expected) {
t.Errorf("index.Search(`hello`) mismatch (-want +got)\n%s", cmp.Diff(expected, docs))
}
}
func Test_index_search_term_not_found_after_update_removes_it(t *testing.T) {
index := New(20)
doc := bazMD("hello", "world")
index.Update(doc)
doc = bazMD("hello")
index.Update(doc)
docs, _ := index.Search("world")
expected := DocList{{Document: "baz.md", Count: 1, Distance: 8}}
if !cmp.Equal(docs, expected) {
t.Errorf("index.Search(`world`) mismatch (-want +got)\n%v", cmp.Diff(expected, docs))
}
}
func Test_update_removing_word_should_update_search_terms(t *testing.T) {
index := New(20)
doc := bazMD("ciao", "world")
index.Update(doc)
doc = barMD()
index.Update(doc)
doc = bazMD("world")
index.Update(doc)
expected := DocList{{Document: "bar.md", Count: 1}}
docs, _ := index.Search("ciao")
if !cmp.Equal(docs, expected) {
t.Errorf("index.Search(`ciao`) mismatch (-want +got)\n%s", cmp.Diff(expected, docs))
}
expected = DocList{{Document: "baz.md", Count: 1}, {Document: "bar.md", Count: 1}}
docs, _ = index.Search("world")
if !cmp.Equal(docs, expected) {
t.Errorf("index.Search(`world`) mismatch (-want +got)\n%s", cmp.Diff(expected, docs))
}
}
func fooMD() *Document {
return &Document{
Name: "foo.md",
WordCount: map[string]int{"hello": 1, "world": 1},
}
}
func bazMD(words ...string) *Document {
var m = make(map[string]int)
for _, word := range words {
m[word] = 1
}
return &Document{
Name: "baz.md",
WordCount: m,
}
}
func barMD() *Document {
return &Document{
Name: "bar.md",
WordCount: map[string]int{"ciao": 1, "world": 1},
}
}