-
Notifications
You must be signed in to change notification settings - Fork 114
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add integration tests against a gqlgen server (#50)
## Summary: We have lots of tests covering codegen, but not a lot that actually run the code. For things where all we do is generate types, that's (mostly) fine (especially now that we actually build the code), but as we generate more nontrivial non-type code we need to actually run it. So I wrote some integration tests that spin up a little gqlgen server, and make calls to it; we can add more over time especially as the JSON marshalling logic gets complex (to support fragments). They're more work to write than the snapshot tests, but of course they can test a lot more. In addition to gqlgen, I pulled in testify assert/require, because I really wanted to be able to use assert.Equal and such for these. I didn't bother converting existing tests, although I assume they will become useful elsewhere in time. Both gqlgen and testify are of course only used in tests. Fixes #21 and #24. Issue: #21 Issue: #24 ## Test plan: make check Author: benjaminjkraft Reviewers: aberkan, dnerdy, benjaminjkraft, csilvers, MiguelCastillo Required Reviewers: Approved by: aberkan, dnerdy Checks: ✅ Test (1.17), ✅ Test (1.16), ✅ Test (1.15), ✅ Test (1.14), ✅ Test (1.13), ✅ Lint, ✅ Test (1.17), ✅ Test (1.16), ✅ Test (1.15), ✅ Test (1.14), ✅ Test (1.13), ✅ Lint Pull request URL: #50
- Loading branch information
1 parent
7003923
commit 3746ecd
Showing
15 changed files
with
2,773 additions
and
52 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,11 @@ | ||
# These are the defaults, and are just included to be explicit. | ||
package: example | ||
schema: schema.graphql | ||
queries: | ||
operations: | ||
- genqlient.graphql | ||
generated: generated.go | ||
|
||
# We map github's DateTime type to Go's time.Time (which conveniently already | ||
# defines MarshalJSON and UnmarshalJSAON). | ||
# defines MarshalJSON and UnmarshalJSON). | ||
scalars: | ||
DateTime: time.Time |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,4 @@ | ||
schema: schema.graphql | ||
operations: | ||
- "*_test.go" | ||
generated: generated.go |
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,66 @@ | ||
// Package integration contains genqlient's integration tests, which run | ||
// against a real server (defined in internal/integration/server/server.go). | ||
// | ||
// These are especially important for cases where we generate nontrivial logic, | ||
// such as JSON-unmarshaling. | ||
package integration | ||
|
||
import ( | ||
"context" | ||
"net/http" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/Khan/genqlient/graphql" | ||
"github.com/Khan/genqlient/internal/integration/server" | ||
) | ||
|
||
func TestSimpleQuery(t *testing.T) { | ||
_ = `# @genqlient | ||
query simpleQuery { me { id name luckyNumber } }` | ||
|
||
ctx := context.Background() | ||
server := server.RunServer() | ||
defer server.Close() | ||
client := graphql.NewClient(server.URL, http.DefaultClient) | ||
|
||
resp, err := simpleQuery(ctx, client) | ||
require.NoError(t, err) | ||
|
||
assert.Equal(t, "1", resp.Me.Id) | ||
assert.Equal(t, "Yours Truly", resp.Me.Name) | ||
assert.Equal(t, 17, resp.Me.LuckyNumber) | ||
} | ||
|
||
func TestVariables(t *testing.T) { | ||
_ = `# @genqlient | ||
query queryWithVariables($id: ID!) { user(id: $id) { id name luckyNumber } }` | ||
|
||
ctx := context.Background() | ||
server := server.RunServer() | ||
defer server.Close() | ||
client := graphql.NewClient(server.URL, http.DefaultClient) | ||
|
||
resp, err := queryWithVariables(ctx, client, "2") | ||
require.NoError(t, err) | ||
|
||
assert.Equal(t, "2", resp.User.Id) | ||
assert.Equal(t, "Raven", resp.User.Name) | ||
assert.Equal(t, -1, resp.User.LuckyNumber) | ||
|
||
resp, err = queryWithVariables(ctx, client, "374892379482379") | ||
require.NoError(t, err) | ||
|
||
assert.Zero(t, resp.User) | ||
} | ||
|
||
func TestGeneratedCode(t *testing.T) { | ||
// TODO(benkraft): Check that gqlgen is up to date too. In practice that's | ||
// less likely to be a problem, since it should only change if you update | ||
// the schema, likely too add something new, in which case you'll notice. | ||
RunGenerateTest(t, "internal/integration/genqlient.yaml") | ||
} | ||
|
||
//go:generate go run github.com/Khan/genqlient genqlient.yaml |
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,10 @@ | ||
type Query { | ||
me: User | ||
user(id: ID!): User | ||
} | ||
|
||
type User { | ||
id: ID! | ||
name: String! | ||
luckyNumber: Int | ||
} |
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,10 @@ | ||
schema: | ||
- ../schema.graphql | ||
|
||
exec: | ||
filename: gqlgen_exec.go | ||
package: server | ||
|
||
model: | ||
filename: gqlgen_models.go | ||
package: server |
Oops, something went wrong.