This repository was archived by the owner on Jun 11, 2026. It is now read-only.
forked from singularperturbation/sphinx
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsphinx_test.go
More file actions
308 lines (263 loc) · 7.38 KB
/
Copy pathsphinx_test.go
File metadata and controls
308 lines (263 loc) · 7.38 KB
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
package sphinx
import (
"bufio"
"bytes"
"encoding/hex"
"io/ioutil"
"os"
"strconv"
"strings"
"testing"
)
func deserializeRequestBody(b *bytes.Buffer) <-chan string {
outputChannel := make(chan string)
go func() {
var word = make([]byte, 4)
outputChannel <- strconv.Itoa(b.Len())
for byteLength, err := b.Read(word); err == nil; byteLength, err = b.Read(word) {
var hexRepresentation = make([]string, 0, byteLength)
for i := 0; i < byteLength; i++ {
hexRepresentation = append(hexRepresentation, hex.EncodeToString(word[i:i+1]))
}
output := strings.Join(hexRepresentation, ":")
// Full 4-byte representation with separators is 11 characters long
if len(output) != 11 {
output = output + ":"
}
outputChannel <- output
}
close(outputChannel)
}()
return outputChannel
}
// TestFixtureRequests compares each line of each fixture file to a request buffer
// that we generate. We need to get the file name, generate our own fixture data
// from the query, index, and comment in the file name, and compare line-by-line
// to the data from the fixture file.
func TestFixtureRequests(t *testing.T) {
const (
prefix = "fixture_data/generated/"
suffix = ".tst"
)
// Get fixture data from generated fixture directory and
var (
files []os.FileInfo
err error
)
if files, err = ioutil.ReadDir(prefix); err != nil {
t.Fatalf(
"Could not read fixture files: `%v`\n",
err,
)
}
for _, file := range files {
t.Logf("Testing fixture data for file %v\n", file.Name())
fileBaseName := strings.TrimSuffix(file.Name(), suffix)
fileParts := strings.Split(fileBaseName, "_")
// Normalize for missing comment and replace 'ALL' for index with '*'
if len(fileParts) == 2 {
fileParts = append(fileParts, "")
}
if fileParts[1] == "ALL" {
fileParts[1] = "*"
}
q := DefaultQuery()
q.Keywords = fileParts[0]
q.Index = fileParts[1]
q.Comment = fileParts[2]
buf, err := buildInternalQuery(q)
if err != nil {
t.Errorf(
"Could not build request buffer for input query `%v` - got error `%v`\n",
buf, err,
)
continue
}
// Compare each line of the file to each line of the generated hex data
fixtureFile, err := os.Open(prefix + file.Name())
if err != nil {
t.Errorf("Could not open file %v for reading: %v.\n",
prefix+file.Name(),
err,
)
}
fixtureLines := bufio.NewScanner(fixtureFile)
if !fixtureLines.Scan() {
t.Fatalf("Can't read first line (buffer size) from the fixture data.")
}
generatedRequestBody := deserializeRequestBody(buf)
header := <-generatedRequestBody
fixtureHeader := fixtureLines.Text()
if header != fixtureLines.Text() {
t.Errorf(
"Buffer length mismatch: fixture data gives %v bytes, generated is %v bytes\n",
fixtureHeader,
header,
)
}
t.Logf("Buffer length: %v\n", header)
line := 0
for hexLine := range generatedRequestBody {
line++
// We're leaking a goroutine but it doesn't matter - quitting test anyway
if !fixtureLines.Scan() {
t.Errorf(
"Error %v on line %v of our request buffer - no more lines available to "+
"read, but still have lines from the fixture file.",
fixtureLines.Err(),
line,
)
break
}
fixtureText := fixtureLines.Text()
t.Logf("%-11v\t%-11v\n", fixtureText, hexLine)
if fixtureText != hexLine {
t.Errorf(
"Mismatch on line %v: \n%v\ndoes not match\n%v\n", line, fixtureText, hexLine,
)
}
}
if fixtureLines.Scan() {
t.Errorf(
"Still have line %v (and others?) to read from fixture data, but no "+
"more from the generated request body.\n",
fixtureLines.Text(),
)
}
fixtureFile.Close()
}
}
func TestRequestsWithFilters(t *testing.T) {
// TODO: Need to have separate folder for testing queries with filters
// t.Fail()
}
func TestRequestsWithWeights(t *testing.T) {
// TODO: Need to test index and field weights
// t.Fail()
}
func TestHeadersEqual(t *testing.T) {
const (
prefix = "fixture_data/generated_header/"
suffix = ".tst"
)
// Get fixture data from generated fixture directory and
var (
files []os.FileInfo
err error
)
if files, err = ioutil.ReadDir(prefix); err != nil {
t.Fatalf(
"Could not read fixture files: `%v`\n",
err,
)
}
for _, file := range files {
t.Logf("Testing fixture data for file %v\n", file.Name())
fileBaseName := strings.TrimSuffix(file.Name(), suffix)
fileParts := strings.Split(fileBaseName, "_")
// Normalize for missing comment and replace 'ALL' for index with '*'
if len(fileParts) == 2 {
fileParts = append(fileParts, "")
}
if fileParts[1] == "ALL" {
fileParts[1] = "*"
}
q := DefaultQuery()
q.Keywords = fileParts[0]
q.Index = fileParts[1]
q.Comment = fileParts[2]
headerBuffer, _, err := buildRequest(q)
if err != nil {
t.Errorf(
"Could not build header buffer for input query `%v` - got error `%v`\n",
headerBuffer, err,
)
continue
}
// Compare each line of the file to each line of the generated hex data
fixtureFile, err := os.Open(prefix + file.Name())
if err != nil {
t.Errorf("Could not open file %v for reading: %v.\n",
prefix+file.Name(),
err,
)
}
fixtureLines := bufio.NewScanner(fixtureFile)
if !fixtureLines.Scan() {
t.Fatalf("Can't read first line (buffer size) from the fixture data.")
}
generatedRequestBody := deserializeRequestBody(headerBuffer)
size := <-generatedRequestBody
fixtureSize := fixtureLines.Text()
if size != fixtureSize {
t.Errorf(
"Buffer length mismatch: fixture data gives %v bytes, generated is %v bytes\n",
fixtureSize,
size,
)
}
t.Logf("Buffer length: %v\n", size)
line := 0
for hexLine := range generatedRequestBody {
line++
// We're leaking a goroutine but it doesn't matter - quitting test anyway
if !fixtureLines.Scan() {
t.Errorf(
"Error %v on line %v of our request buffer - no more lines available to "+
"read, but still have lines from the fixture file.",
fixtureLines.Err(),
line,
)
break
}
fixtureText := fixtureLines.Text()
t.Logf("%-11v\t%-11v\n", fixtureText, hexLine)
if fixtureText != hexLine {
t.Errorf(
"Mismatch on line %v: \n%v\ndoes not match\n%v\n", line, fixtureText, hexLine,
)
}
}
if fixtureLines.Scan() {
t.Errorf(
"Still have line %v (and others?) to read from fixture data, but no "+
"more from the generated request body.\n",
fixtureLines.Text(),
)
}
fixtureFile.Close()
}
}
// Tests that can establish client, connect to localhost server, and run basic query
// without error. Only run if doing long version of tests.
func TestBasicClient(t *testing.T) {
if testing.Short() {
t.Skip("Not running simple integration test - don't know if Sphinx configured.")
} else {
s := SphinxClient{
Config: *NewDefaultConfig(),
}
err := s.Init(nil)
if err != nil {
t.Errorf("Unexpected error establishing connection : %v\n", err)
}
q := DefaultQuery()
q.Keywords = "test"
q.Index = "*"
q.Comment = ""
response, err := s.Query(q)
if err != nil {
t.Fatalf("Unexpected error doing basic query: %v\n", err)
}
if int(response.TotalFound) != len(response.Matches) {
t.Errorf(
"Mismatch between reported total %v in response and length of matches %v\n",
response.TotalFound, len(response.Matches),
)
}
if response.Total == 0 {
t.Errorf("Expected non-zero total for response, got: %v\n", response.Total)
}
s.Close()
}
}