-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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 #96 from vektah/refactor-tests
Refactor tests
- Loading branch information
Showing
18 changed files
with
255 additions
and
967 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 |
---|---|---|
|
@@ -3,4 +3,4 @@ | |
/docs/public | ||
/example/chat/node_modules | ||
/example/chat/package-lock.json | ||
/codegen/tests/gen | ||
/codegen/testdata/gen |
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 |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package codegen | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestInvalidPackagenames(t *testing.T) { | ||
err := generate("invalid-packagename", ` | ||
type Query { | ||
invalidIdentifier: InvalidIdentifier | ||
} | ||
type InvalidIdentifier { | ||
id: Int! | ||
} | ||
`, map[string]string{ | ||
"InvalidIdentifier": "github.com/vektah/gqlgen/codegen/testdata/invalid-packagename.InvalidIdentifier", | ||
}) | ||
|
||
require.NoError(t, err) | ||
} | ||
|
||
func TestImportCollisions(t *testing.T) { | ||
err := generate("complexinput", ` | ||
type Query { | ||
collision: It | ||
} | ||
type It { | ||
id: ID! | ||
} | ||
`, map[string]string{ | ||
"It": "github.com/vektah/gqlgen/codegen/testdata/introspection.It", | ||
}) | ||
|
||
require.NoError(t, err) | ||
} |
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,92 @@ | ||
package codegen | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestTypeUnionAsInput(t *testing.T) { | ||
err := generate("inputunion", ` | ||
type Query { | ||
addBookmark(b: Bookmarkable!): Boolean! | ||
} | ||
type Item {} | ||
union Bookmarkable = Item | ||
`) | ||
|
||
require.EqualError(t, err, "model plan failed: Bookmarkable! cannot be used as argument of Query.addBookmark. only input and scalar types are allowed") | ||
} | ||
|
||
func TestTypeInInput(t *testing.T) { | ||
err := generate("typeinput", ` | ||
type Query { | ||
addBookmark(b: BookmarkableInput!): Boolean! | ||
} | ||
type Item {} | ||
input BookmarkableInput { | ||
item: Item | ||
} | ||
`) | ||
|
||
require.EqualError(t, err, "model plan failed: Item cannot be used as a field of BookmarkableInput. only input and scalar types are allowed") | ||
} | ||
|
||
func TestRawMapInputs(t *testing.T) { | ||
err := generate("rawmap", ` | ||
type Query { | ||
mapInput(input: Changes): Boolean | ||
} | ||
input Changes { | ||
a: Int | ||
b: Int | ||
} | ||
`, map[string]string{ | ||
"Changes": "map[string]interface{}", | ||
}) | ||
|
||
require.NoError(t, err) | ||
} | ||
|
||
func TestRecursiveInputType(t *testing.T) { | ||
err := generate("recursiveinput", ` | ||
type Query { | ||
recursive(input: RecursiveInputSlice): Boolean | ||
} | ||
input RecursiveInputSlice { | ||
self: [RecursiveInputSlice!] | ||
} | ||
`, map[string]string{ | ||
"RecursiveInputSlice": "github.com/vektah/gqlgen/codegen/testdata.RecursiveInputSlice", | ||
}) | ||
|
||
require.NoError(t, err) | ||
} | ||
|
||
func TestComplexInputTypes(t *testing.T) { | ||
err := generate("complexinput", ` | ||
type Query { | ||
nestedInputs(input: [[OuterInput]] = [[{inner: {id: 1}}]]): Boolean | ||
nestedOutputs: [[OuterObject]] | ||
} | ||
input InnerInput { | ||
id:Int! | ||
} | ||
input OuterInput { | ||
inner: InnerInput! | ||
} | ||
type OuterObject { | ||
inner: InnerObject! | ||
} | ||
type InnerObject { | ||
id: Int! | ||
} | ||
`, map[string]string{ | ||
"Changes": "map[string]interface{}", | ||
}) | ||
|
||
require.NoError(t, err) | ||
} |
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,59 @@ | ||
package codegen | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
"golang.org/x/tools/go/loader" | ||
) | ||
|
||
func TestShapes(t *testing.T) { | ||
err := generate("shapes", ` | ||
type Query { | ||
shapes: [Shape] | ||
} | ||
interface Shape { | ||
area: Float | ||
} | ||
type Circle implements Shape { | ||
radius: Float | ||
area: Float | ||
} | ||
type Rectangle implements Shape { | ||
length: Float | ||
width: Float | ||
area: Float | ||
} | ||
union ShapeUnion = Circle | Rectangle | ||
`, map[string]string{ | ||
"Shape": "github.com/vektah/gqlgen/codegen/testdata.Shape", | ||
"ShapeUnion": "github.com/vektah/gqlgen/codegen/testdata.ShapeUnion", | ||
"Circle": "github.com/vektah/gqlgen/codegen/testdata.Circle", | ||
"Rectangle": "github.com/vektah/gqlgen/codegen/testdata.Rectangle", | ||
}) | ||
|
||
require.NoError(t, err) | ||
|
||
} | ||
|
||
func generate(name string, schema string, typemap ...map[string]string) error { | ||
cfg := Config{ | ||
SchemaStr: schema, | ||
ExecFilename: "testdata/gen/" + name + "/exec.go", | ||
ModelFilename: "testdata/gen/" + name + "/model.go", | ||
} | ||
if len(typemap) > 0 { | ||
cfg.Typemap = typemap[0] | ||
} | ||
err := Generate(cfg) | ||
if err == nil { | ||
conf := loader.Config{} | ||
conf.Import("github.com/vektah/gqlgen/codegen/testdata/gen/" + name) | ||
|
||
_, err = conf.Load() | ||
if err != nil { | ||
panic(err) | ||
} | ||
} | ||
return err | ||
} |
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,31 @@ | ||
package testdata | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"time" | ||
) | ||
|
||
type Element struct { | ||
ID int | ||
} | ||
|
||
type ElementResolver struct{} | ||
|
||
func (r *ElementResolver) Query_path(ctx context.Context) ([]Element, error) { | ||
return []Element{{1}, {2}, {3}, {4}}, nil | ||
} | ||
|
||
func (r *ElementResolver) Element_child(ctx context.Context, obj *Element) (Element, error) { | ||
return Element{obj.ID * 10}, nil | ||
} | ||
|
||
func (r *ElementResolver) Element_error(ctx context.Context, obj *Element, message *string) (bool, error) { | ||
// A silly hack to make the result order stable | ||
time.Sleep(time.Duration(obj.ID) * 10 * time.Millisecond) | ||
|
||
if message != nil { | ||
return true, errors.New(*message) | ||
} | ||
return false, nil | ||
} |
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
File renamed without changes.
2 changes: 1 addition & 1 deletion
2
.../invalid-identifier/invalid-identifier.go → ...invalid-packagename/invalid-identifier.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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package invalid_identifier | ||
package invalid_packagename | ||
|
||
type InvalidIdentifier struct { | ||
ID 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,5 @@ | ||
package testdata | ||
|
||
type RecursiveInputSlice struct { | ||
Self *[]*RecursiveInputSlice | ||
} |
This file was deleted.
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
Oops, something went wrong.