This repository was archived by the owner on Jan 15, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdelete_test.go
More file actions
193 lines (177 loc) · 5.86 KB
/
delete_test.go
File metadata and controls
193 lines (177 loc) · 5.86 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
package tpuf_test
import (
"bytes"
"context"
"io"
"net/http"
"testing"
"github.com/bamo/tpuf-go"
"github.com/stretchr/testify/assert"
)
func TestDelete(t *testing.T) {
tests := []struct {
name string
namespace string
ids []string
httpResponse *http.Response
httpError error
expectedError string
expectedMethod string
expectedURL string
expectedBody string
}{
{
name: "successful delete",
namespace: "test-namespace",
ids: []string{"1", "2", "3"},
httpResponse: &http.Response{
StatusCode: http.StatusOK,
Body: io.NopCloser(bytes.NewBufferString(`{"status":"OK"}`)),
},
expectedMethod: http.MethodPost,
expectedURL: "https://api.turbopuffer.com/v1/namespaces/test-namespace",
expectedBody: `{"upserts":[{"id":"1"},{"id":"2"},{"id":"3"}]}`,
},
{
name: "delete error",
namespace: "test-namespace",
ids: []string{"4", "5"},
httpResponse: &http.Response{
StatusCode: http.StatusBadRequest,
Body: io.NopCloser(bytes.NewBufferString(`{"error":"Invalid request","status":"error"}`)),
},
expectedError: "failed to upsert documents: error: Invalid request (HTTP 400)",
expectedMethod: http.MethodPost,
expectedURL: "https://api.turbopuffer.com/v1/namespaces/test-namespace",
expectedBody: `{"upserts":[{"id":"4"},{"id":"5"}]}`,
},
}
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
},
},
}
err := client.Delete(context.Background(), tt.namespace, tt.ids)
if tt.expectedError == "" {
assert.NoError(t, err)
} else {
assert.EqualError(t, err, tt.expectedError)
}
})
}
}
func TestDeleteByFilter(t *testing.T) {
tests := []struct {
name string
namespace string
request *tpuf.DeleteByFilterRequest
httpResponse *http.Response
httpError error
expectedError string
expectedMethod string
expectedURL string
expectedBody string
}{
{
name: "successful delete by filter",
namespace: "test-namespace",
request: &tpuf.DeleteByFilterRequest{
Filter: &tpuf.BaseFilter{
Attribute: "category",
Operator: tpuf.OpEq,
Value: "electronics",
},
},
httpResponse: &http.Response{
StatusCode: http.StatusOK,
Body: io.NopCloser(bytes.NewBufferString(`{"status":"OK"}`)),
},
expectedMethod: http.MethodPost,
expectedURL: "https://api.turbopuffer.com/v1/namespaces/test-namespace",
expectedBody: `{"delete_by_filter":["category","Eq","electronics"]}`,
},
{
name: "delete by complex filter",
namespace: "test-namespace",
request: &tpuf.DeleteByFilterRequest{
Filter: &tpuf.AndFilter{
Filters: []tpuf.Filter{
&tpuf.BaseFilter{
Attribute: "category",
Operator: tpuf.OpEq,
Value: "electronics",
},
&tpuf.BaseFilter{
Attribute: "price",
Operator: tpuf.OpGt,
Value: 100,
},
},
},
},
httpResponse: &http.Response{
StatusCode: http.StatusOK,
Body: io.NopCloser(bytes.NewBufferString(`{"status":"OK"}`)),
},
expectedMethod: http.MethodPost,
expectedURL: "https://api.turbopuffer.com/v1/namespaces/test-namespace",
expectedBody: `{"delete_by_filter":["And",[["category","Eq","electronics"],["price","Gt",100]]]}`,
},
{
name: "delete by filter error",
namespace: "test-namespace",
request: &tpuf.DeleteByFilterRequest{
Filter: &tpuf.BaseFilter{
Attribute: "invalid_field",
Operator: tpuf.OpEq,
Value: "value",
},
},
httpResponse: &http.Response{
StatusCode: http.StatusBadRequest,
Body: io.NopCloser(bytes.NewBufferString(`{"error":"Invalid filter","status":"error"}`)),
},
expectedError: "failed to delete by filter: error: Invalid filter (HTTP 400)",
expectedMethod: http.MethodPost,
expectedURL: "https://api.turbopuffer.com/v1/namespaces/test-namespace",
expectedBody: `{"delete_by_filter":["invalid_field","Eq","value"]}`,
},
}
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
},
},
}
err := client.DeleteByFilter(context.Background(), tt.namespace, tt.request)
if tt.expectedError == "" {
assert.NoError(t, err)
} else {
assert.EqualError(t, err, tt.expectedError)
}
})
}
}