Skip to content

Commit

Permalink
Merge pull request #96 from vektah/refactor-tests
Browse files Browse the repository at this point in the history
Refactor tests
  • Loading branch information
vektah authored Apr 28, 2018
2 parents 0c7bdfc + 7b1c819 commit 79c69d1
Show file tree
Hide file tree
Showing 18 changed files with 255 additions and 967 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
/docs/public
/example/chat/node_modules
/example/chat/package-lock.json
/codegen/tests/gen
/codegen/testdata/gen
2 changes: 2 additions & 0 deletions codegen/codegen.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ func (cfg *Config) normalize() error {
if cfg.ModelPackageName == "" {
cfg.ModelPackageName = filepath.Base(cfg.modelDir)
}
cfg.ModelPackageName = sanitizePackageName(cfg.ModelPackageName)
cfg.modelPackagePath = fullPackageName(cfg.modelDir, cfg.ModelPackageName)

if cfg.ExecFilename == "" {
Expand All @@ -103,6 +104,7 @@ func (cfg *Config) normalize() error {
if cfg.ExecPackageName == "" {
cfg.ExecPackageName = filepath.Base(cfg.execDir)
}
cfg.ExecPackageName = sanitizePackageName(cfg.ExecPackageName)
cfg.execPackagePath = fullPackageName(cfg.execDir, cfg.ExecPackageName)

builtins := map[string]string{
Expand Down
8 changes: 6 additions & 2 deletions codegen/import_build.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ func buildImports(types NamedTypes, destDir string) Imports {

var invalidPackageNameChar = regexp.MustCompile(`[^\w]`)

func sanitizePackageName(pkg string) string {
return invalidPackageNameChar.ReplaceAllLiteralString(filepath.Base(pkg), "_")
}

func (s Imports) addPkg(types NamedTypes, destDir string, pkg string) (Imports, *Import) {
if pkg == "" {
return s, nil
Expand All @@ -43,11 +47,11 @@ func (s Imports) addPkg(types NamedTypes, destDir string, pkg string) (Imports,

localName := ""
if !strings.HasSuffix(destDir, pkg) {
localName = invalidPackageNameChar.ReplaceAllLiteralString(filepath.Base(pkg), "_")
localName = sanitizePackageName(filepath.Base(pkg))
i := 1
imp := s.findByName(localName)
for imp != nil && imp.Package != pkg {
localName = invalidPackageNameChar.ReplaceAllLiteralString(filepath.Base(pkg), "_") + strconv.Itoa(i)
localName = sanitizePackageName(filepath.Base(pkg)) + strconv.Itoa(i)
imp = s.findByName(localName)
i++
if i > 10 {
Expand Down
38 changes: 38 additions & 0 deletions codegen/import_test.go
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)
}
92 changes: 92 additions & 0 deletions codegen/input_test.go
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)
}
59 changes: 59 additions & 0 deletions codegen/interface_test.go
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
}
31 changes: 31 additions & 0 deletions codegen/testdata/element.go
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
}
6 changes: 1 addition & 5 deletions test/models.go → codegen/testdata/interfaces.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package test
package testdata

import "math"

Expand All @@ -25,7 +25,3 @@ type Rectangle struct {
func (r *Rectangle) Area() float64 {
return r.Length * r.Width
}

type RecursiveInputSlice struct {
Self *[]*RecursiveInputSlice
}
File renamed without changes.
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
Expand Down
5 changes: 5 additions & 0 deletions codegen/testdata/recursive.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package testdata

type RecursiveInputSlice struct {
Self *[]*RecursiveInputSlice
}
42 changes: 0 additions & 42 deletions codegen/tests/input_union_test.go

This file was deleted.

2 changes: 1 addition & 1 deletion handler/stub.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func (e *executableSchemaStub) Mutation(ctx context.Context, op *query.Operation

func (e *executableSchemaStub) Subscription(ctx context.Context, op *query.Operation) func() *graphql.Response {
return func() *graphql.Response {
time.Sleep(20 * time.Millisecond)
time.Sleep(50 * time.Millisecond)
select {
case <-ctx.Done():
return nil
Expand Down
Loading

0 comments on commit 79c69d1

Please sign in to comment.