-
Notifications
You must be signed in to change notification settings - Fork 28
/
filterbuilder_test.go
265 lines (213 loc) · 6.41 KB
/
filterbuilder_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
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
package postgrest
import (
"context"
"encoding/json"
"net/http"
"sort"
"testing"
"time"
"github.com/jarcoal/httpmock"
"github.com/stretchr/testify/assert"
)
func TestFilterBuilder_ExecuteTo(t *testing.T) {
assert := assert.New(t)
c := createClient(t)
t.Run("ValidResult", func(t *testing.T) {
want := []TestResult{
{
ID: float64(1),
Name: "sean",
Email: "[email protected]",
},
}
var got []TestResult
if mockResponses {
httpmock.Activate()
defer httpmock.DeactivateAndReset()
responder, _ := httpmock.NewJsonResponder(200, []map[string]interface{}{
users[0],
})
httpmock.RegisterRegexpResponder("GET", mockPath, responder)
}
count, err := c.From("users").Select("id, name, email", "", false).Eq("name", "sean").ExecuteTo(&got)
assert.NoError(err)
assert.EqualValues(want, got)
assert.Equal(countType(0), count)
})
t.Run("WithCount", func(t *testing.T) {
want := []TestResult{
{
ID: float64(1),
Name: "sean",
Email: "[email protected]",
},
}
var got []TestResult
if mockResponses {
httpmock.Activate()
defer httpmock.DeactivateAndReset()
httpmock.RegisterRegexpResponder("GET", mockPath, func(req *http.Request) (*http.Response, error) {
resp, _ := httpmock.NewJsonResponse(200, []map[string]interface{}{
users[0],
})
resp.Header.Add("Content-Range", "0-1/1")
return resp, nil
})
}
count, err := c.From("users").Select("id, name, email", "exact", false).Eq("name", "sean").ExecuteTo(&got)
assert.NoError(err)
assert.EqualValues(want, got)
assert.Equal(countType(1), count)
})
}
func ExampleFilterBuilder_ExecuteTo() {
// Given a database with a "users" table containing "id", "name" and "email"
// columns:
var res []struct {
ID int64 `json:"id"`
Name string `json:"name"`
Email string `json:"email"`
}
client := NewClient("http://localhost:3000", "", nil)
count, err := client.From("users").Select("*", "exact", false).ExecuteTo(&res)
if err == nil && count > 0 {
// The value for res will contain all columns for all users, and count will
// be the exact number of rows in the users table.
}
}
func TestFilterBuilder_Limit(t *testing.T) {
c := createClient(t)
assert := assert.New(t)
want := []map[string]interface{}{users[0]}
got := []map[string]interface{}{}
if mockResponses {
httpmock.Activate()
defer httpmock.DeactivateAndReset()
httpmock.RegisterRegexpResponder("GET", mockPath, func(req *http.Request) (*http.Response, error) {
resp, _ := httpmock.NewJsonResponse(200, want)
resp.Header.Add("Content-Range", "*/2")
return resp, nil
})
}
bs, count, err := c.From("users").Select("id, name, email", "exact", false).Limit(1, "").Execute()
assert.NoError(err)
err = json.Unmarshal(bs, &got)
assert.NoError(err)
assert.EqualValues(want, got)
// Matching supabase-js, the count returned is not the number of transformed
// rows, but the number of filtered rows.
assert.Equal(countType(len(users)), count, "expected count to be %v", len(users))
}
func TestFilterBuilder_ContextCanceled(t *testing.T) {
c := createClient(t)
assert := assert.New(t)
if mockResponses {
httpmock.Activate()
defer httpmock.DeactivateAndReset()
}
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Nanosecond)
defer cancel()
time.Sleep(1 * time.Nanosecond)
_, _, err := c.From("users").Select("id, name, email", "exact", false).Limit(1, "").ExecuteWithContext(ctx)
// This test should immediately fail on a canceled context.
assert.Error(err)
}
func TestFilterBuilder_Order(t *testing.T) {
c := createClient(t)
assert := assert.New(t)
want := make([]map[string]interface{}, len(users))
copy(want, users)
sort.Slice(want, func(i, j int) bool {
return j < i
})
got := []map[string]interface{}{}
if mockResponses {
httpmock.Activate()
defer httpmock.DeactivateAndReset()
httpmock.RegisterRegexpResponder("GET", mockPath, func(req *http.Request) (*http.Response, error) {
resp, _ := httpmock.NewJsonResponse(200, want)
resp.Header.Add("Content-Range", "*/2")
return resp, nil
})
}
bs, count, err := c.
From("users").
Select("id, name, email", "exact", false).
Order("name", &OrderOpts{Ascending: true}).
Execute()
assert.NoError(err)
err = json.Unmarshal(bs, &got)
assert.NoError(err)
assert.EqualValues(want, got)
assert.Equal(countType(len(users)), count)
}
func TestFilterBuilder_Range(t *testing.T) {
c := createClient(t)
assert := assert.New(t)
want := users
got := []map[string]interface{}{}
if mockResponses {
httpmock.Activate()
defer httpmock.DeactivateAndReset()
httpmock.RegisterRegexpResponder("GET", mockPath, func(req *http.Request) (*http.Response, error) {
resp, _ := httpmock.NewJsonResponse(200, want)
resp.Header.Add("Content-Range", "*/2")
return resp, nil
})
}
bs, count, err := c.
From("users").
Select("id, name, email", "exact", false).
Range(0, 1, "").
Execute()
assert.NoError(err)
err = json.Unmarshal(bs, &got)
assert.NoError(err)
assert.EqualValues(want, got)
assert.Equal(countType(len(users)), count)
}
func TestFilterBuilder_Single(t *testing.T) {
c := createClient(t)
assert := assert.New(t)
want := users[0]
got := make(map[string]interface{})
t.Run("ValidResult", func(t *testing.T) {
if mockResponses {
httpmock.Activate()
defer httpmock.DeactivateAndReset()
httpmock.RegisterRegexpResponder("GET", mockPath, func(req *http.Request) (*http.Response, error) {
resp, _ := httpmock.NewJsonResponse(200, want)
resp.Header.Add("Content-Range", "*/2")
return resp, nil
})
}
bs, count, err := c.
From("users").
Select("id, name, email", "exact", false).
Limit(1, "").
Single().
Execute()
assert.NoError(err)
err = json.Unmarshal(bs, &got)
assert.NoError(err)
assert.EqualValues(want, got)
assert.Equal(countType(len(users)), count)
})
// An error will be returned from PostgREST if the total count of the result
// set > 1, so Single can pretty easily err.
t.Run("Error", func(t *testing.T) {
if mockResponses {
httpmock.Activate()
defer httpmock.DeactivateAndReset()
httpmock.RegisterRegexpResponder("GET", mockPath, func(req *http.Request) (*http.Response, error) {
resp, _ := httpmock.NewJsonResponse(500, ExecuteError{
Message: "error message",
})
resp.Header.Add("Content-Range", "*/2")
return resp, nil
})
}
_, _, err := c.From("users").Select("*", "", false).Single().Execute()
assert.Error(err)
})
}