-
Notifications
You must be signed in to change notification settings - Fork 0
/
query_test.go
149 lines (140 loc) · 4.58 KB
/
query_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
package tpuf_test
import (
"bytes"
"context"
"io"
"net/http"
"testing"
"github.com/bamo/tpuf-go"
"github.com/stretchr/testify/assert"
)
func TestQuery(t *testing.T) {
tests := []struct {
name string
namespace string
request *tpuf.QueryRequest
httpResponse *http.Response
httpError error
expectedError string
expectedMethod string
expectedURL string
expectedBody string
expectedResult []*tpuf.QueryResult
}{
{
name: "vector search",
namespace: "test-namespace",
request: &tpuf.QueryRequest{
Vector: []float32{0.1, 0.2, 0.3},
DistanceMetric: tpuf.DistanceMetricCosine,
TopK: 5,
IncludeVectors: true,
},
httpResponse: &http.Response{
StatusCode: http.StatusOK,
Body: io.NopCloser(bytes.NewBufferString(`[
{"id":"1","dist":0.1,"vector":[0.11,0.21,0.31]},
{"id":"2","dist":0.2,"vector":[0.12,0.22,0.32]}
]`)),
},
expectedMethod: http.MethodPost,
expectedURL: "https://api.turbopuffer.com/v1/vectors/test-namespace/query",
expectedBody: `{"vector":[0.1,0.2,0.3],"distance_metric":"cosine_distance","top_k":5,"include_vectors":true}`,
expectedResult: []*tpuf.QueryResult{
{ID: "1", Dist: 0.1, Vector: []float32{0.11, 0.21, 0.31}},
{ID: "2", Dist: 0.2, Vector: []float32{0.12, 0.22, 0.32}},
},
},
{
name: "BM25 text search",
namespace: "test-namespace",
request: &tpuf.QueryRequest{
RankBy: []interface{}{"description", "BM25", "fox jumping"},
TopK: 3,
},
httpResponse: &http.Response{
StatusCode: http.StatusOK,
Body: io.NopCloser(bytes.NewBufferString(`[
{"id":"1","dist":1.5},
{"id":"2","dist":1.2},
{"id":"3","dist":0.8}
]`)),
},
expectedMethod: http.MethodPost,
expectedURL: "https://api.turbopuffer.com/v1/vectors/test-namespace/query",
expectedBody: `{"rank_by":["description","BM25","fox jumping"],"top_k":3}`,
expectedResult: []*tpuf.QueryResult{
{ID: "1", Dist: 1.5},
{ID: "2", Dist: 1.2},
{ID: "3", Dist: 0.8},
},
},
{
name: "filter-only search",
namespace: "test-namespace",
request: &tpuf.QueryRequest{
Filters: &tpuf.AndFilter{
Filters: []tpuf.Filter{
&tpuf.BaseFilter{Attribute: "category", Operator: tpuf.OpEq, Value: "electronics"},
&tpuf.BaseFilter{Attribute: "price", Operator: tpuf.OpGte, Value: 100},
},
},
TopK: 2,
},
httpResponse: &http.Response{
StatusCode: http.StatusOK,
Body: io.NopCloser(bytes.NewBufferString(`[
{"id":"1","dist":0},
{"id":"2","dist":0}
]`)),
},
expectedMethod: http.MethodPost,
expectedURL: "https://api.turbopuffer.com/v1/vectors/test-namespace/query",
expectedBody: `{"filters":["And",[["category", "Eq", "electronics"],["price", "Gte", 100]]],"top_k":2}`,
expectedResult: []*tpuf.QueryResult{
{ID: "1", Dist: 0},
{ID: "2", Dist: 0},
},
},
{
name: "query error",
namespace: "test-namespace",
request: &tpuf.QueryRequest{TopK: 1},
httpResponse: &http.Response{
StatusCode: http.StatusBadRequest,
Body: io.NopCloser(bytes.NewBufferString(`{"error":"Invalid query","status":"error"}`)),
},
expectedError: "failed to query documents: error: Invalid query (HTTP 400)",
expectedMethod: http.MethodPost,
expectedURL: "https://api.turbopuffer.com/v1/vectors/test-namespace/query",
expectedBody: `{"top_k":1}`,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
client := &tpuf.Client{
ApiToken: "test-token",
HttpClient: &fakeHttpClient{
doFunc: func(req *http.Request) (*http.Response, error) {
assert.Equal(t, tt.expectedMethod, req.Method, "unexpected request method")
assert.Equal(t, tt.expectedURL, req.URL.String(), "unexpected request URL")
body, _ := io.ReadAll(req.Body)
assert.JSONEq(t, tt.expectedBody, string(body), "unexpected request body")
assert.Equal(t, "Bearer test-token", req.Header.Get("Authorization"), "unexpected Authorization header")
assert.Equal(t, "application/json", req.Header.Get("Content-Type"), "unexpected Content-Type header")
assert.Equal(t, "application/json", req.Header.Get("Accept"), "unexpected Accept header")
return tt.httpResponse, tt.httpError
},
},
}
results, err := client.Query(context.Background(), tt.namespace, tt.request)
if tt.expectedError == "" {
assert.NoError(t, err)
assert.Equal(t, tt.expectedResult, results, "unexpected query results")
} else {
assert.EqualError(t, err, tt.expectedError)
assert.Nil(t, results)
}
})
}
}