Skip to content

Commit

Permalink
Added unit test for CallArgs()
Browse files Browse the repository at this point in the history
  • Loading branch information
vediatoni committed Jun 8, 2022
1 parent 9a06da1 commit d7d436a
Showing 1 changed file with 67 additions and 0 deletions.
67 changes: 67 additions & 0 deletions codegen/field_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

"github.com/99designs/gqlgen/codegen/config"
"github.com/stretchr/testify/require"
ast2 "github.com/vektah/gqlparser/v2/ast"
)

func TestFindField(t *testing.T) {
Expand Down Expand Up @@ -114,3 +115,69 @@ func TestEqualFieldName(t *testing.T) {
})
}
}

func TestField_CallArgs(t *testing.T) {
tt := []struct {
Name string
Field
Expected string
}{
{
Name: "Field with method that has context, and two args (string, interface)",
Field: Field{
MethodHasContext: true,
Args: []*FieldArgument{
{
ArgumentDefinition: &ast2.ArgumentDefinition{
Name: "test",
},
TypeReference: &config.TypeReference{
GO: &types.Interface{},
},
},
{
ArgumentDefinition: &ast2.ArgumentDefinition{
Name: "test2",
},
TypeReference: &config.TypeReference{
GO: types.Typ[types.Int],
},
},
},
},
Expected: `ctx, ` + `
func () interface{} {
if fc.Args["test"] == nil {
return nil
}
return fc.Args["test"].(interface{})
}(), fc.Args["test2"].(int)`,
},
{
Name: "Resolver field that isn't root object with single int argument",
Field: Field{
Object: &Object{
Root: false,
},
IsResolver: true,
Args: []*FieldArgument{
{
ArgumentDefinition: &ast2.ArgumentDefinition{
Name: "test",
},
TypeReference: &config.TypeReference{
GO: types.Typ[types.Int],
},
},
},
},
Expected: `rctx, obj, fc.Args["test"].(int)`,
},
}

for _, tc := range tt {
t.Run(tc.Name, func(t *testing.T) {
require.Equal(t, tc.CallArgs(), tc.Expected)
})
}
}

0 comments on commit d7d436a

Please sign in to comment.