Skip to content

Commit

Permalink
Speed up tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Adam Scarr committed Aug 10, 2018
1 parent 3575c28 commit 36e8407
Show file tree
Hide file tree
Showing 22 changed files with 2,649 additions and 239 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@
/integration/node_modules
/integration/schema-fetched.graphql
/example/chat/package-lock.json
/codegen/tests/gen
/codegen/gen

.idea/
10 changes: 5 additions & 5 deletions codegen/codegen_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ func TestGenerateServer(t *testing.T) {
id: Int
}
`
serverFilename := "tests/gen/" + name + "/server/server.go"
serverFilename := "gen/" + name + "/server/server.go"
cfg := Config{
SchemaStr: schema,
Exec: PackageConfig{Filename: "tests/gen/" + name + "/exec.go"},
Model: PackageConfig{Filename: "tests/gen/" + name + "/model.go"},
Resolver: PackageConfig{Filename: "tests/gen/" + name + "/resolver.go", Type: "Resolver"},
Exec: PackageConfig{Filename: "gen/" + name + "/exec.go"},
Model: PackageConfig{Filename: "gen/" + name + "/model.go"},
Resolver: PackageConfig{Filename: "gen/" + name + "/resolver.go", Type: "Resolver"},
}

_ = syscall.Unlink(cfg.Resolver.Filename)
Expand All @@ -36,7 +36,7 @@ func TestGenerateServer(t *testing.T) {
require.NoError(t, err)

conf := loader.Config{}
conf.CreateFromFilenames("tests/gen/"+name, serverFilename)
conf.CreateFromFilenames("gen/"+name, serverFilename)

_, err = conf.Load()
require.NoError(t, err)
Expand Down
8 changes: 4 additions & 4 deletions codegen/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ func TestLoadConfig(t *testing.T) {
})

t.Run("malformed config", func(t *testing.T) {
_, err := LoadConfig("tests/cfg/malformedconfig.yml")
_, err := LoadConfig("testdata/cfg/malformedconfig.yml")
require.EqualError(t, err, "unable to parse config: yaml: unmarshal errors:\n line 1: cannot unmarshal !!str `asdf` into codegen.Config")
})

t.Run("unknown keys", func(t *testing.T) {
_, err := LoadConfig("tests/cfg/unknownkeys.yml")
_, err := LoadConfig("testdata/cfg/unknownkeys.yml")
require.EqualError(t, err, "unable to parse config: yaml: unmarshal errors:\n line 2: field unknown not found in type codegen.Config")
})
}
Expand All @@ -33,7 +33,7 @@ func TestLoadDefaultConfig(t *testing.T) {
var cfg *Config

t.Run("will find closest match", func(t *testing.T) {
err = os.Chdir(filepath.Join(testDir, "tests", "cfg", "subdir"))
err = os.Chdir(filepath.Join(testDir, "testdata", "cfg", "subdir"))
require.NoError(t, err)

cfg, err = LoadConfigFromDefaultLocations()
Expand All @@ -42,7 +42,7 @@ func TestLoadDefaultConfig(t *testing.T) {
})

t.Run("will find config in parent dirs", func(t *testing.T) {
err = os.Chdir(filepath.Join(testDir, "tests", "cfg", "otherdir"))
err = os.Chdir(filepath.Join(testDir, "testdata", "cfg", "otherdir"))
require.NoError(t, err)

cfg, err = LoadConfigFromDefaultLocations()
Expand Down
31 changes: 0 additions & 31 deletions codegen/import_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,37 +6,6 @@ import (
"github.com/stretchr/testify/require"
)

func TestInvalidPackagenames(t *testing.T) {
err := generate("invalid-packagename", `
type Query {
invalidIdentifier: InvalidIdentifier
}
type InvalidIdentifier {
id: Int!
}
`, TypeMap{
"InvalidIdentifier": {Model: "github.com/99designs/gqlgen/codegen/tests/invalid-packagename.InvalidIdentifier"},
})

require.NoError(t, err)
}

func TestImportCollisions(t *testing.T) {
err := generate("complexinput", `
type Query {
collision: It
}
type It {
id: ID!
}
`, TypeMap{
"It": {Model: "github.com/99designs/gqlgen/codegen/tests/introspection.It"},
})

require.NoError(t, err)
}

func TestDeterministicDecollisioning(t *testing.T) {
a := Imports{
imports: []*Import{
Expand Down
152 changes: 22 additions & 130 deletions codegen/input_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"testing"

"github.com/stretchr/testify/require"
"golang.org/x/tools/go/loader"
)

func TestTypeUnionAsInput(t *testing.T) {
Expand Down Expand Up @@ -32,134 +33,25 @@ func TestTypeInInput(t *testing.T) {
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
}
`, TypeMap{
"Changes": {Model: "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!]
}
`, TypeMap{
"RecursiveInputSlice": {Model: "github.com/99designs/gqlgen/codegen/tests.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!
}
`, TypeMap{
"Changes": {Model: "map[string]interface{}"},
})

require.NoError(t, err)
}

func TestKeywordInputFields(t *testing.T) {
err := generate("input_keywords_fields", `
input Object {
break: String!
default: String!
func: String!
interface: String!
select: String!
case: String!
defer: String!
go: String!
map: String!
struct: String!
chan: String!
else: String!
goto: String!
package: String!
switch: String!
const: String!
fallthrough: String!
if: String!
range: String!
type: String!
continue: String!
for: String!
import: String!
return: String!
var: String!
}
type Query {
test(input: Object): Boolean!
}
`)

require.NoError(t, err)
}

func TestInputKeywordArgs(t *testing.T) {
err := generate("input_keyword_args", `
type Query {
test(
break: String!,
default: String!,
func: String!,
interface: String!,
select: String!,
case: String!,
defer: String!,
go: String!,
map: String!,
struct: String!,
chan: String!,
else: String!,
goto: String!,
package: String!,
switch: String!,
const: String!,
fallthrough: String!,
if: String!,
range: String!,
type: String!,
continue: String!,
for: String!,
import: String!,
return: String!,
var: String!,
): Boolean!
}
`)

require.NoError(t, err)
func generate(name string, schema string, typemap ...TypeMap) error {
cfg := Config{
SchemaStr: schema,
Exec: PackageConfig{Filename: "gen/" + name + "/exec.go"},
Model: PackageConfig{Filename: "gen/" + name + "/model.go"},
}

if len(typemap) > 0 {
cfg.Models = typemap[0]
}
err := Generate(cfg)
if err == nil {
conf := loader.Config{}
conf.Import("github.com/99designs/gqlgen/codegen/testdata/gen/" + name)

_, err = conf.Load()
if err != nil {
panic(err)
}
}
return err
}
65 changes: 0 additions & 65 deletions codegen/interface_test.go

This file was deleted.

File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion codegen/tests/element.go → codegen/testserver/element.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package tests
package testserver

import (
"context"
Expand Down
Loading

0 comments on commit 36e8407

Please sign in to comment.