Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
thisisnithin committed May 30, 2024
1 parent 18012b4 commit 929e873
Show file tree
Hide file tree
Showing 9 changed files with 197 additions and 170 deletions.
67 changes: 67 additions & 0 deletions v2/go.sum

Large diffs are not rendered by default.

2 changes: 0 additions & 2 deletions v2/pkg/astvalidation/reference/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ import (
"gopkg.in/yaml.v2"
)

//go:generate ./gen.sh

func main() {
currDir, _ := os.Getwd()
println(currDir)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"context"
"encoding/json"
"github.com/wundergraph/graphql-go-tools/v2/pkg/engine/datasource/httpclient"
"testing"

"github.com/stretchr/testify/require"
Expand All @@ -29,7 +30,7 @@ func TestSource_Load(t *testing.T) {

buf := &bytes.Buffer{}
source := &Source{introspectionData: &data}
require.NoError(t, source.Load(context.Background(), []byte(input), buf))
require.NoError(t, source.Load(context.Background(), []byte(input), []httpclient.File{}, buf))

actualResponse := &bytes.Buffer{}
require.NoError(t, json.Indent(actualResponse, buf.Bytes(), "", " "))
Expand Down
25 changes: 13 additions & 12 deletions v2/pkg/engine/resolve/authorization_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"encoding/json"
"errors"
"github.com/stretchr/testify/require"
"github.com/wundergraph/graphql-go-tools/v2/pkg/engine/datasource/httpclient"
"io"
"sync/atomic"
"testing"
Expand Down Expand Up @@ -508,8 +509,8 @@ func TestAuthorization(t *testing.T) {
func generateTestFederationGraphQLResponse(t *testing.T, ctrl *gomock.Controller) *GraphQLResponse {
userService := NewMockDataSource(ctrl)
userService.EXPECT().
Load(gomock.Any(), gomock.Any(), gomock.AssignableToTypeOf(&bytes.Buffer{})).
DoAndReturn(func(ctx context.Context, input []byte, w *bytes.Buffer) (err error) {
Load(gomock.Any(), gomock.Any(), gomock.Any(), gomock.AssignableToTypeOf(&bytes.Buffer{})).
DoAndReturn(func(ctx context.Context, input []byte, files []httpclient.File, w *bytes.Buffer) (err error) {
actual := string(input)
expected := `{"method":"POST","url":"http://localhost:4001","body":{"query":"{me {id username}}"}}`
assert.Equal(t, expected, actual)
Expand All @@ -520,8 +521,8 @@ func generateTestFederationGraphQLResponse(t *testing.T, ctrl *gomock.Controller

reviewsService := NewMockDataSource(ctrl)
reviewsService.EXPECT().
Load(gomock.Any(), gomock.Any(), gomock.AssignableToTypeOf(&bytes.Buffer{})).
DoAndReturn(func(ctx context.Context, input []byte, w *bytes.Buffer) (err error) {
Load(gomock.Any(), gomock.Any(), gomock.Any(), gomock.AssignableToTypeOf(&bytes.Buffer{})).
DoAndReturn(func(ctx context.Context, input []byte, files []httpclient.File, w *bytes.Buffer) (err error) {
actual := string(input)
expected := `{"method":"POST","url":"http://localhost:4002","body":{"query":"query($representations: [_Any!]!){_entities(representations: $representations){... on User {reviews {body product {upc __typename}}}}}","variables":{"representations":[{"__typename":"User","id":"1234"}]}}}`
assert.Equal(t, expected, actual)
Expand All @@ -532,8 +533,8 @@ func generateTestFederationGraphQLResponse(t *testing.T, ctrl *gomock.Controller

productService := NewMockDataSource(ctrl)
productService.EXPECT().
Load(gomock.Any(), gomock.Any(), gomock.AssignableToTypeOf(&bytes.Buffer{})).
DoAndReturn(func(ctx context.Context, input []byte, w *bytes.Buffer) (err error) {
Load(gomock.Any(), gomock.Any(), gomock.Any(), gomock.AssignableToTypeOf(&bytes.Buffer{})).
DoAndReturn(func(ctx context.Context, input []byte, files []httpclient.File, w *bytes.Buffer) (err error) {
actual := string(input)
expected := `{"method":"POST","url":"http://localhost:4003","body":{"query":"query($representations: [_Any!]!){_entities(representations: $representations){... on Product {name}}}","variables":{"representations":[{"__typename":"Product","upc":"top-1"},{"__typename":"Product","upc":"top-2"}]}}}`
assert.Equal(t, expected, actual)
Expand Down Expand Up @@ -797,8 +798,8 @@ func generateTestFederationGraphQLResponse(t *testing.T, ctrl *gomock.Controller
func generateTestFederationGraphQLResponseWithoutAuthorizationRules(t *testing.T, ctrl *gomock.Controller) *GraphQLResponse {
userService := NewMockDataSource(ctrl)
userService.EXPECT().
Load(gomock.Any(), gomock.Any(), gomock.AssignableToTypeOf(&bytes.Buffer{})).
DoAndReturn(func(ctx context.Context, input []byte, w *bytes.Buffer) (err error) {
Load(gomock.Any(), gomock.Any(), gomock.Any(), gomock.AssignableToTypeOf(&bytes.Buffer{})).
DoAndReturn(func(ctx context.Context, input []byte, files []httpclient.File, w *bytes.Buffer) (err error) {
actual := string(input)
expected := `{"method":"POST","url":"http://localhost:4001","body":{"query":"{me {id username}}"}}`
assert.Equal(t, expected, actual)
Expand All @@ -809,8 +810,8 @@ func generateTestFederationGraphQLResponseWithoutAuthorizationRules(t *testing.T

reviewsService := NewMockDataSource(ctrl)
reviewsService.EXPECT().
Load(gomock.Any(), gomock.Any(), gomock.AssignableToTypeOf(&bytes.Buffer{})).
DoAndReturn(func(ctx context.Context, input []byte, w *bytes.Buffer) (err error) {
Load(gomock.Any(), gomock.Any(), gomock.Any(), gomock.AssignableToTypeOf(&bytes.Buffer{})).
DoAndReturn(func(ctx context.Context, input []byte, files []httpclient.File, w *bytes.Buffer) (err error) {
actual := string(input)
expected := `{"method":"POST","url":"http://localhost:4002","body":{"query":"query($representations: [_Any!]!){_entities(representations: $representations){... on User {reviews {body product {upc __typename}}}}}","variables":{"representations":[{"__typename":"User","id":"1234"}]}}}`
assert.Equal(t, expected, actual)
Expand All @@ -821,8 +822,8 @@ func generateTestFederationGraphQLResponseWithoutAuthorizationRules(t *testing.T

productService := NewMockDataSource(ctrl)
productService.EXPECT().
Load(gomock.Any(), gomock.Any(), gomock.AssignableToTypeOf(&bytes.Buffer{})).
DoAndReturn(func(ctx context.Context, input []byte, w *bytes.Buffer) (err error) {
Load(gomock.Any(), gomock.Any(), gomock.Any(), gomock.AssignableToTypeOf(&bytes.Buffer{})).
DoAndReturn(func(ctx context.Context, input []byte, files []httpclient.File, w *bytes.Buffer) (err error) {
actual := string(input)
expected := `{"method":"POST","url":"http://localhost:4003","body":{"query":"query($representations: [_Any!]!){_entities(representations: $representations){... on Product {name}}}","variables":{"representations":[{"__typename":"Product","upc":"top-1"},{"__typename":"Product","upc":"top-2"}]}}}`
assert.Equal(t, expected, actual)
Expand Down
21 changes: 11 additions & 10 deletions v2/pkg/engine/resolve/loader_hooks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"context"
"github.com/golang/mock/gomock"
"github.com/stretchr/testify/assert"
"github.com/wundergraph/graphql-go-tools/v2/pkg/engine/datasource/httpclient"
"io"
"sync"
"sync/atomic"
Expand Down Expand Up @@ -47,8 +48,8 @@ func TestLoaderHooks_FetchPipeline(t *testing.T) {
t.Run("simple fetch with simple subgraph error", testFnWithPostEvaluation(func(t *testing.T, ctrl *gomock.Controller) (node *GraphQLResponse, ctx *Context, expectedOutput string, postEvaluation func(t *testing.T)) {
mockDataSource := NewMockDataSource(ctrl)
mockDataSource.EXPECT().
Load(gomock.Any(), gomock.Any(), gomock.AssignableToTypeOf(&bytes.Buffer{})).
DoAndReturn(func(ctx context.Context, input []byte, w io.Writer) (err error) {
Load(gomock.Any(), gomock.Any(), gomock.Any(), gomock.AssignableToTypeOf(&bytes.Buffer{})).
DoAndReturn(func(ctx context.Context, input []byte, files []httpclient.File, w io.Writer) (err error) {
pair := NewBufPair()
pair.WriteErr([]byte("errorMessage"), nil, nil, nil)
return writeGraphqlResponse(pair, w, false)
Expand Down Expand Up @@ -117,8 +118,8 @@ func TestLoaderHooks_FetchPipeline(t *testing.T) {

mockDataSource := NewMockDataSource(ctrl)
mockDataSource.EXPECT().
Load(gomock.Any(), gomock.Any(), gomock.AssignableToTypeOf(&bytes.Buffer{})).
DoAndReturn(func(ctx context.Context, input []byte, w io.Writer) (err error) {
Load(gomock.Any(), gomock.Any(), gomock.Any(), gomock.AssignableToTypeOf(&bytes.Buffer{})).
DoAndReturn(func(ctx context.Context, input []byte, files []httpclient.File, w io.Writer) (err error) {
pair := NewBufPair()
pair.WriteErr([]byte("errorMessage"), nil, nil, nil)
return writeGraphqlResponse(pair, w, false)
Expand Down Expand Up @@ -181,8 +182,8 @@ func TestLoaderHooks_FetchPipeline(t *testing.T) {
t.Run("parallel fetch with simple subgraph error", testFnWithPostEvaluation(func(t *testing.T, ctrl *gomock.Controller) (node *GraphQLResponse, ctx *Context, expectedOutput string, postEvaluation func(t *testing.T)) {
mockDataSource := NewMockDataSource(ctrl)
mockDataSource.EXPECT().
Load(gomock.Any(), gomock.Any(), gomock.AssignableToTypeOf(&bytes.Buffer{})).
DoAndReturn(func(ctx context.Context, input []byte, w io.Writer) (err error) {
Load(gomock.Any(), gomock.Any(), gomock.Any(), gomock.AssignableToTypeOf(&bytes.Buffer{})).
DoAndReturn(func(ctx context.Context, input []byte, files []httpclient.File, w io.Writer) (err error) {
pair := NewBufPair()
pair.WriteErr([]byte("errorMessage"), nil, nil, nil)
return writeGraphqlResponse(pair, w, false)
Expand Down Expand Up @@ -244,8 +245,8 @@ func TestLoaderHooks_FetchPipeline(t *testing.T) {
t.Run("parallel list item fetch with simple subgraph error", testFnWithPostEvaluation(func(t *testing.T, ctrl *gomock.Controller) (node *GraphQLResponse, ctx *Context, expectedOutput string, postEvaluation func(t *testing.T)) {
mockDataSource := NewMockDataSource(ctrl)
mockDataSource.EXPECT().
Load(gomock.Any(), gomock.Any(), gomock.AssignableToTypeOf(&bytes.Buffer{})).
DoAndReturn(func(ctx context.Context, input []byte, w io.Writer) (err error) {
Load(gomock.Any(), gomock.Any(), gomock.Any(), gomock.AssignableToTypeOf(&bytes.Buffer{})).
DoAndReturn(func(ctx context.Context, input []byte, files []httpclient.File, w io.Writer) (err error) {
pair := NewBufPair()
pair.WriteErr([]byte("errorMessage"), nil, nil, nil)
return writeGraphqlResponse(pair, w, false)
Expand Down Expand Up @@ -305,8 +306,8 @@ func TestLoaderHooks_FetchPipeline(t *testing.T) {
t.Run("fetch with subgraph error and custom extension code", testFnWithPostEvaluation(func(t *testing.T, ctrl *gomock.Controller) (node *GraphQLResponse, ctx *Context, expectedOutput string, postEvaluation func(t *testing.T)) {
mockDataSource := NewMockDataSource(ctrl)
mockDataSource.EXPECT().
Load(gomock.Any(), gomock.Any(), gomock.AssignableToTypeOf(&bytes.Buffer{})).
DoAndReturn(func(ctx context.Context, input []byte, w io.Writer) (err error) {
Load(gomock.Any(), gomock.Any(), gomock.Any(), gomock.AssignableToTypeOf(&bytes.Buffer{})).
DoAndReturn(func(ctx context.Context, input []byte, files []httpclient.File, w io.Writer) (err error) {
pair := NewBufPair()
pair.WriteErr([]byte("errorMessage"), nil, nil, []byte("{\"code\":\"GRAPHQL_VALIDATION_FAILED\"}"))
pair.WriteErr([]byte("errorMessage2"), nil, nil, []byte("{\"code\":\"BAD_USER_INPUT\"}"))
Expand Down
2 changes: 1 addition & 1 deletion v2/pkg/engine/resolve/resolve.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//go:generate mockgen --build_flags=--mod=mod -self_package=github.com/wundergraph/graphql-go-tools/v2/pkg/engine/resolve -destination=resolve_mock_test.go -package=resolve . DataSource,BeforeFetchHook,AfterFetchHook
//go:generate mockgen -self_package=github.com/wundergraph/graphql-go-tools/v2/pkg/engine/resolve -destination=resolve_mock_test.go -package=resolve . DataSource

package resolve

Expand Down
Loading

0 comments on commit 929e873

Please sign in to comment.