-
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 #24 from boatilus/update-execute-to
Update ExecuteTo functions to return count New tests added Comment typos fixed and added more Readme typos fixed
- Loading branch information
Showing
9 changed files
with
282 additions
and
23 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
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
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 |
---|---|---|
|
@@ -16,6 +16,12 @@ var mockResponses bool = false | |
|
||
var mockPath *regexp.Regexp | ||
|
||
type TestResult struct { | ||
ID float64 `json:"id"` | ||
Name string `name:"sean"` | ||
Email string `email:"[email protected]"` | ||
} | ||
|
||
// A mock table/result set. | ||
var users = []map[string]interface{}{ | ||
{ | ||
|
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,89 @@ | ||
package postgrest | ||
|
||
import ( | ||
"net/http" | ||
"testing" | ||
|
||
"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. | ||
} | ||
} |
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
Oops, something went wrong.