|
1 | 1 | package postgrest |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "encoding/json" |
4 | 5 | "net/http" |
| 6 | + "sort" |
5 | 7 | "testing" |
6 | 8 |
|
7 | 9 | "github.com/jarcoal/httpmock" |
@@ -87,3 +89,156 @@ func ExampleFilterBuilder_ExecuteTo() { |
87 | 89 | // be the exact number of rows in the users table. |
88 | 90 | } |
89 | 91 | } |
| 92 | + |
| 93 | +func TestFilterBuilder_Limit(t *testing.T) { |
| 94 | + c := createClient(t) |
| 95 | + assert := assert.New(t) |
| 96 | + |
| 97 | + want := []map[string]interface{}{users[0]} |
| 98 | + got := []map[string]interface{}{} |
| 99 | + |
| 100 | + if mockResponses { |
| 101 | + httpmock.Activate() |
| 102 | + defer httpmock.DeactivateAndReset() |
| 103 | + |
| 104 | + httpmock.RegisterRegexpResponder("GET", mockPath, func(req *http.Request) (*http.Response, error) { |
| 105 | + resp, _ := httpmock.NewJsonResponse(200, want) |
| 106 | + resp.Header.Add("Content-Range", "*/2") |
| 107 | + return resp, nil |
| 108 | + }) |
| 109 | + } |
| 110 | + |
| 111 | + bs, count, err := c.From("users").Select("id, name, email", "exact", false).Limit(1, "").Execute() |
| 112 | + assert.NoError(err) |
| 113 | + |
| 114 | + err = json.Unmarshal(bs, &got) |
| 115 | + assert.NoError(err) |
| 116 | + assert.EqualValues(want, got) |
| 117 | + |
| 118 | + // Matching supabase-js, the count returned is not the number of transformed |
| 119 | + // rows, but the number of filtered rows. |
| 120 | + assert.Equal(countType(len(users)), count, "expected count to be %v", len(users)) |
| 121 | +} |
| 122 | + |
| 123 | +func TestFilterBuilder_Order(t *testing.T) { |
| 124 | + c := createClient(t) |
| 125 | + assert := assert.New(t) |
| 126 | + |
| 127 | + want := make([]map[string]interface{}, len(users)) |
| 128 | + copy(want, users) |
| 129 | + |
| 130 | + sort.Slice(want, func(i, j int) bool { |
| 131 | + return j < i |
| 132 | + }) |
| 133 | + |
| 134 | + got := []map[string]interface{}{} |
| 135 | + |
| 136 | + if mockResponses { |
| 137 | + httpmock.Activate() |
| 138 | + defer httpmock.DeactivateAndReset() |
| 139 | + |
| 140 | + httpmock.RegisterRegexpResponder("GET", mockPath, func(req *http.Request) (*http.Response, error) { |
| 141 | + resp, _ := httpmock.NewJsonResponse(200, want) |
| 142 | + resp.Header.Add("Content-Range", "*/2") |
| 143 | + return resp, nil |
| 144 | + }) |
| 145 | + } |
| 146 | + |
| 147 | + bs, count, err := c. |
| 148 | + From("users"). |
| 149 | + Select("id, name, email", "exact", false). |
| 150 | + Order("name", &OrderOpts{Ascending: true}). |
| 151 | + Execute() |
| 152 | + assert.NoError(err) |
| 153 | + |
| 154 | + err = json.Unmarshal(bs, &got) |
| 155 | + assert.NoError(err) |
| 156 | + assert.EqualValues(want, got) |
| 157 | + assert.Equal(countType(len(users)), count) |
| 158 | +} |
| 159 | + |
| 160 | +func TestFilterBuilder_Range(t *testing.T) { |
| 161 | + c := createClient(t) |
| 162 | + assert := assert.New(t) |
| 163 | + |
| 164 | + want := users |
| 165 | + got := []map[string]interface{}{} |
| 166 | + |
| 167 | + if mockResponses { |
| 168 | + httpmock.Activate() |
| 169 | + defer httpmock.DeactivateAndReset() |
| 170 | + |
| 171 | + httpmock.RegisterRegexpResponder("GET", mockPath, func(req *http.Request) (*http.Response, error) { |
| 172 | + resp, _ := httpmock.NewJsonResponse(200, want) |
| 173 | + resp.Header.Add("Content-Range", "*/2") |
| 174 | + return resp, nil |
| 175 | + }) |
| 176 | + } |
| 177 | + |
| 178 | + bs, count, err := c. |
| 179 | + From("users"). |
| 180 | + Select("id, name, email", "exact", false). |
| 181 | + Range(0, 1, ""). |
| 182 | + Execute() |
| 183 | + assert.NoError(err) |
| 184 | + |
| 185 | + err = json.Unmarshal(bs, &got) |
| 186 | + assert.NoError(err) |
| 187 | + assert.EqualValues(want, got) |
| 188 | + assert.Equal(countType(len(users)), count) |
| 189 | +} |
| 190 | + |
| 191 | +func TestFilterBuilder_Single(t *testing.T) { |
| 192 | + c := createClient(t) |
| 193 | + assert := assert.New(t) |
| 194 | + |
| 195 | + want := users[0] |
| 196 | + got := make(map[string]interface{}) |
| 197 | + |
| 198 | + t.Run("ValidResult", func(t *testing.T) { |
| 199 | + if mockResponses { |
| 200 | + httpmock.Activate() |
| 201 | + defer httpmock.DeactivateAndReset() |
| 202 | + |
| 203 | + httpmock.RegisterRegexpResponder("GET", mockPath, func(req *http.Request) (*http.Response, error) { |
| 204 | + resp, _ := httpmock.NewJsonResponse(200, want) |
| 205 | + resp.Header.Add("Content-Range", "*/2") |
| 206 | + return resp, nil |
| 207 | + }) |
| 208 | + } |
| 209 | + |
| 210 | + bs, count, err := c. |
| 211 | + From("users"). |
| 212 | + Select("id, name, email", "exact", false). |
| 213 | + Limit(1, ""). |
| 214 | + Single(). |
| 215 | + Execute() |
| 216 | + assert.NoError(err) |
| 217 | + |
| 218 | + err = json.Unmarshal(bs, &got) |
| 219 | + assert.NoError(err) |
| 220 | + assert.EqualValues(want, got) |
| 221 | + assert.Equal(countType(len(users)), count) |
| 222 | + }) |
| 223 | + |
| 224 | + // An error will be returned from PostgREST if the total count of the result |
| 225 | + // set > 1, so Single can pretty easily err. |
| 226 | + t.Run("Error", func(t *testing.T) { |
| 227 | + if mockResponses { |
| 228 | + httpmock.Activate() |
| 229 | + defer httpmock.DeactivateAndReset() |
| 230 | + |
| 231 | + httpmock.RegisterRegexpResponder("GET", mockPath, func(req *http.Request) (*http.Response, error) { |
| 232 | + resp, _ := httpmock.NewJsonResponse(500, ExecuteError{ |
| 233 | + Message: "error message", |
| 234 | + }) |
| 235 | + |
| 236 | + resp.Header.Add("Content-Range", "*/2") |
| 237 | + return resp, nil |
| 238 | + }) |
| 239 | + } |
| 240 | + |
| 241 | + _, _, err := c.From("users").Select("*", "", false).Single().Execute() |
| 242 | + assert.Error(err) |
| 243 | + }) |
| 244 | +} |
0 commit comments