Move tests to snapshots

This commit is contained in:
Ben Kraft
2020-07-15 11:46:32 -07:00
parent 3b80f09c79
commit 279228f9a4
18 changed files with 204 additions and 131 deletions
+1
View File
@@ -0,0 +1 @@
{ User: user { ID: id } }
+5
View File
@@ -0,0 +1,5 @@
type Response struct {
User *struct {
ID string
}
}
+6
View File
@@ -0,0 +1,6 @@
{
user {
ID: id
AlsoID: id
}
}
+6
View File
@@ -0,0 +1,6 @@
type Response struct {
User *struct {
ID string
AlsoID string
} `json:"user"`
}
+5
View File
@@ -0,0 +1,5 @@
{
user {
roles
}
}
+12
View File
@@ -0,0 +1,12 @@
type Response struct {
User *struct {
Roles []role `json:"roles"`
} `json:"user"`
}
type role string
const (
studentRole role = "STUDENT"
teacherRole role = "TEACHER"
)
+5
View File
@@ -0,0 +1,5 @@
query ($query: UserQueryInput) {
user(query: $query) {
id
}
}
+19
View File
@@ -0,0 +1,19 @@
type Response struct {
User *struct {
Id string `json:"id"`
} `json:"user"`
}
type role string
const (
studentRole role = "STUDENT"
teacherRole role = "TEACHER"
)
type userQueryInput struct {
Email *string `json:"email"`
Name *string `json:"name"`
Id *string `json:"id"`
Role *role `json:"role"`
}
+8
View File
@@ -0,0 +1,8 @@
{
user {
emails
emailsOrNull
emailsWithNulls
emailsWithNullsOrNull
}
}
+8
View File
@@ -0,0 +1,8 @@
type Response struct {
User *struct {
Emails []string `json:"emails"`
EmailsOrNull []string `json:"emailsOrNull"`
EmailsWithNulls []*string `json:"emailsWithNulls"`
EmailsWithNullsOrNull []*string `json:"emailsWithNullsOrNull"`
} `json:"user"`
}
+8
View File
@@ -0,0 +1,8 @@
{
user {
authMethods {
provider
email
}
}
}
+8
View File
@@ -0,0 +1,8 @@
type Response struct {
User *struct {
AuthMethods []struct {
Provider *string `json:"provider"`
Email *string `json:"email"`
} `json:"authMethods"`
} `json:"user"`
}
+1
View File
@@ -0,0 +1 @@
{ user { id } }
+5
View File
@@ -0,0 +1,5 @@
type Response struct {
User *struct {
Id string `json:"id"`
} `json:"user"`
}
+4
View File
@@ -0,0 +1,4 @@
{
Me: user { roles }
OtherUser: user { roles }
}
+15
View File
@@ -0,0 +1,15 @@
type Response struct {
Me *struct {
Roles []role `json:"roles"`
}
OtherUser *struct {
Roles []role `json:"roles"`
}
}
type role string
const (
studentRole role = "STUDENT"
teacherRole role = "TEACHER"
)
+31
View File
@@ -0,0 +1,31 @@
enum Role {
STUDENT
TEACHER
}
input UserQueryInput {
email: String
name: String
id: ID
role: Role
}
type AuthMethod {
provider: String
email: String
}
type User {
id: ID!
roles: [Role!]
name: String
emails: [String!]!
emailsOrNull: [String!]
emailsWithNulls: [String]!
emailsWithNullsOrNull: [String]
authMethods: [AuthMethod!]!
}
type Query {
user(query: UserQueryInput): User
}
+57 -131
View File
@@ -3,6 +3,8 @@ package generate
import (
"fmt"
"go/format"
"io/ioutil"
"path/filepath"
"sort"
"strings"
"testing"
@@ -11,146 +13,53 @@ import (
"github.com/vektah/gqlparser/ast"
)
const dataDir = "testdata"
func readFile(t *testing.T, filename string) string {
data, err := ioutil.ReadFile(filepath.Join(dataDir, filename))
if err != nil {
t.Fatal(err)
}
return string(data)
}
func gofmt(src string) (string, error) {
src = strings.TrimSpace(src)
formatted, err := format.Source([]byte(src))
if err != nil {
return src, err
return src, fmt.Errorf("go parse error: %w", err)
}
return string(formatted), nil
}
var schemaText = `
enum Role {
STUDENT
TEACHER
}
input UserQueryInput {
email: String
name: String
id: ID
role: Role
}
type AuthMethod {
provider: String
email: String
}
type User {
id: ID!
roles: [Role!]
name: String
emails: [String!]!
emailsOrNull: [String!]
emailsWithNulls: [String]!
emailsWithNullsOrNull: [String]
authMethods: [AuthMethod!]!
}
type Query {
user: User
}
`
func TestTypeForOperation(t *testing.T) {
tests := []struct {
name string
operation string
expectedGoType string
}{{
"SimpleQuery",
`{ user { id } }`,
`type Response struct{
User *struct {
Id string ` + "`json:\"id\"`" + `
} ` + "`json:\"user\"`" + `
}`,
}, {
"QueryWithAlias",
`{ User: user { ID: id } }`,
`type Response struct{
User *struct {
ID string
}
}`,
// Here on out, we use aliases, just because aliases are a lot less
// annoying to write in Go strings than Go struct tags.
}, {
"QueryWithDoubleAlias",
`{
User: user {
ID: id
AlsoID: id
}
}`,
`type Response struct{
User *struct {
ID string
AlsoID string
}
}`,
}, {
"QueryWithSlices",
`{
User: user {
Emails: emails
EmailsOrNull: emailsOrNull
EmailsWithNulls: emailsWithNulls
EmailsWithNullsOrNull: emailsWithNullsOrNull
}
}`,
`type Response struct{
User *struct {
Emails []string
EmailsOrNull []string
EmailsWithNulls []*string
EmailsWithNullsOrNull []*string
}
}`,
}, {
"QueryWithStructs",
`{
User: user {
AuthMethods: authMethods {
Provider: provider
Email: email
}
}
}`,
`type Response struct{
User *struct {
AuthMethods []struct {
Provider *string
Email *string
}
}
}`,
}, {
"QueryWithEnums",
`{
User: user {
Roles: roles
}
}`,
`type Response struct{
User *struct {
Roles []role
}
// This test uses the schema, queries, and expected-output in ./testdata.
// The schema is in schema.graphql. The queries are in TestName.graphql;
// the test asserts that such queries, when run through the type-generator,
// produce the types in TestName.go (the name of the overall response type
// will be Response).
//
// Change update on the next line to true to update all the expected output
// files to match current output.
// TODO(benkraft): Make this a flag or something.
update := false
files, err := ioutil.ReadDir(dataDir)
if err != nil {
t.Fatal(err)
}
schemaText := readFile(t, "schema.graphql")
for _, file := range files {
graphqlFilename := file.Name()
if graphqlFilename == "schema.graphql" || !strings.HasSuffix(graphqlFilename, ".graphql") {
continue
}
goFilename := graphqlFilename + ".go"
type role string
const (
studentRole role = "STUDENT"
teacherRole role = "TEACHER"
)`,
}}
for _, test := range tests {
test := test
t.Run(test.name, func(t *testing.T) {
expectedGoType, err := gofmt(test.expectedGoType)
t.Run(graphqlFilename, func(t *testing.T) {
expectedGoType, err := gofmt(readFile(t, goFilename))
if err != nil {
t.Fatal(err)
}
@@ -161,7 +70,8 @@ func TestTypeForOperation(t *testing.T) {
t.Fatal(graphqlError)
}
queryDoc, graphqlListError := gqlparser.LoadQuery(schema, test.operation)
queryDoc, graphqlListError := gqlparser.LoadQuery(
schema, readFile(t, graphqlFilename))
if graphqlListError != nil {
t.Fatal(graphqlListError)
}
@@ -171,7 +81,7 @@ func TestTypeForOperation(t *testing.T) {
}
g := newGenerator(&Config{Package: "test_package"}, schema)
_, err = g.getTypeForOperation(queryDoc.Operations[0])
err = g.addOperation(queryDoc.Operations[0])
if err != nil {
t.Error(err)
}
@@ -184,11 +94,25 @@ func TestTypeForOperation(t *testing.T) {
if goType != expectedGoType {
t.Errorf("got:\n%v\nwant:\n%v\n", goType, expectedGoType)
if update {
t.Log("Updating testdata dir to match")
err = ioutil.WriteFile(
filepath.Join(dataDir, goFilename), []byte(goType), 0644)
if err != nil {
t.Errorf("Unable to update testdata dir: %v", err)
}
}
}
})
}
if update {
// This is an error to ensure we don't commit update := true
t.Error("Updated testdata dir")
}
}
// TODO(benkraft): Figure out how to do this with testdata-files
func TestTypeForInputType(t *testing.T) {
tests := []struct {
name string
@@ -224,6 +148,8 @@ func TestTypeForInputType(t *testing.T) {
},
}}
schemaText := readFile(t, "schema.graphql")
for _, test := range tests {
test := test
t.Run(test.name, func(t *testing.T) {