-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #23 from boatilus/support-count
Add support for returning row count
- Loading branch information
Showing
10 changed files
with
206 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,4 +18,9 @@ | |
.idea | ||
|
||
#VSCode Folder Ignore | ||
.vscode | ||
.vscode | ||
|
||
# Supabase | ||
supabase | ||
|
||
.env |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package postgrest | ||
|
||
import ( | ||
"os" | ||
"regexp" | ||
"testing" | ||
) | ||
|
||
const urlEnv = "POSTGREST_URL" | ||
const apiKeyEnv = "API_KEY" | ||
|
||
// If false, mock responses with httpmock. If true, use POSTGREST_URL (and | ||
// optionally, API_KEY for Supabase), to run tests against an actual Postgres | ||
// instance. | ||
var mockResponses bool = false | ||
|
||
var mockPath *regexp.Regexp | ||
|
||
// A mock table/result set. | ||
var users = []map[string]interface{}{ | ||
{ | ||
"id": float64(1), // numeric types are returned as float64s | ||
"name": "sean", | ||
"email": "sean@test.com", | ||
}, | ||
{ | ||
"id": float64(2), | ||
"name": "patti", | ||
"email": "patti@test.com", | ||
}, | ||
} | ||
|
||
func createClient(t *testing.T) *Client { | ||
// If a POSTGREST_URL environment variable is specified, we'll use that | ||
// to test against real endpoints. | ||
url := os.Getenv(urlEnv) | ||
if url == "" { | ||
url = "http://mock.xyz" | ||
mockResponses = true | ||
|
||
var err error | ||
mockPath, err = regexp.Compile(regexp.QuoteMeta(url) + "?.*") | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
} | ||
|
||
headers := make(map[string]string) | ||
if apiKeyEnv != "" { | ||
// If the API_KEY env is specified, we'll use it to auth with Supabase. | ||
headers["apikey"] = os.Getenv(apiKeyEnv) | ||
} | ||
|
||
return NewClient(url, "", headers) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,8 @@ | ||
module github.com/supabase/postgrest-go | ||
|
||
go 1.16 | ||
|
||
require ( | ||
github.com/jarcoal/httpmock v1.1.0 | ||
github.com/stretchr/testify v1.7.0 | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= | ||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= | ||
github.com/jarcoal/httpmock v1.1.0 h1:F47ChZj1Y2zFsCXxNkBPwNNKnAyOATcdQibk0qEdVCE= | ||
github.com/jarcoal/httpmock v1.1.0/go.mod h1:ATjnClrvW/3tijVmpL/va5Z3aAyGvqU3gCT8nX0Txik= | ||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= | ||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= | ||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= | ||
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY= | ||
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= | ||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= | ||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= | ||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo= | ||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
package postgrest | ||
|
||
import ( | ||
"encoding/json" | ||
"net/http" | ||
"testing" | ||
|
||
"github.com/jarcoal/httpmock" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestNewClient(t *testing.T) { | ||
assert.NotNil(t, NewClient("", "", nil)) | ||
} | ||
|
||
func TestSelect(t *testing.T) { | ||
assert := assert.New(t) | ||
c := createClient(t) | ||
|
||
t.Run("ValidResult", func(t *testing.T) { | ||
got := []map[string]interface{}{} | ||
|
||
if mockResponses { | ||
httpmock.Activate() | ||
defer httpmock.DeactivateAndReset() | ||
|
||
responder, _ := httpmock.NewJsonResponder(200, users) | ||
httpmock.RegisterRegexpResponder("GET", mockPath, responder) | ||
} | ||
|
||
bs, count, err := c.From("users").Select("id, name, email", "", false).Execute() | ||
assert.NoError(err) | ||
|
||
err = json.Unmarshal(bs, &got) | ||
assert.NoError(err) | ||
assert.EqualValues(users, got) | ||
assert.Equal(countType(0), count) | ||
}) | ||
|
||
t.Run("WithCount", func(t *testing.T) { | ||
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, users) | ||
|
||
resp.Header.Add("Content-Range", "0-1/2") | ||
return resp, nil | ||
}) | ||
} | ||
|
||
bs, count, err := c.From("users").Select("id, name, email", "exact", false).Execute() | ||
assert.NoError(err) | ||
|
||
err = json.Unmarshal(bs, &got) | ||
assert.NoError(err) | ||
assert.EqualValues(users, got) | ||
assert.Equal(countType(2), count) | ||
}) | ||
} | ||
|
||
func TestFilter(t *testing.T) { | ||
assert := assert.New(t) | ||
c := createClient(t) | ||
|
||
t.Run("Eq", func(t *testing.T) { | ||
want := "[{\"email\":\"patti@test.com\"}]" | ||
|
||
if mockResponses { | ||
httpmock.Activate() | ||
defer httpmock.DeactivateAndReset() | ||
|
||
httpmock.RegisterRegexpResponder("GET", mockPath, httpmock.NewStringResponder(200, want)) | ||
} | ||
|
||
got, _, err := c.From("users").Select("email", "", false).Eq("email", "patti@test.com").ExecuteString() | ||
assert.NoError(err) | ||
assert.Equal(want, got) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters