Move tests to snapshots

This commit is contained in:
Ben Kraft
2020-07-15 17:35:47 -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 ( import (
"fmt" "fmt"
"go/format" "go/format"
"io/ioutil"
"path/filepath"
"sort" "sort"
"strings" "strings"
"testing" "testing"
@@ -11,146 +13,53 @@ import (
"github.com/vektah/gqlparser/ast" "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) { func gofmt(src string) (string, error) {
src = strings.TrimSpace(src) src = strings.TrimSpace(src)
formatted, err := format.Source([]byte(src)) formatted, err := format.Source([]byte(src))
if err != nil { if err != nil {
return src, err return src, fmt.Errorf("go parse error: %w", err)
} }
return string(formatted), nil 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) { func TestTypeForOperation(t *testing.T) {
tests := []struct { // This test uses the schema, queries, and expected-output in ./testdata.
name string // The schema is in schema.graphql. The queries are in TestName.graphql;
operation string // the test asserts that such queries, when run through the type-generator,
expectedGoType string // produce the types in TestName.go (the name of the overall response type
}{{ // will be Response).
"SimpleQuery", //
`{ user { id } }`, // Change update on the next line to true to update all the expected output
`type Response struct{ // files to match current output.
User *struct { // TODO(benkraft): Make this a flag or something.
Id string ` + "`json:\"id\"`" + ` update := false
} ` + "`json:\"user\"`" + `
}`, files, err := ioutil.ReadDir(dataDir)
}, { if err != nil {
"QueryWithAlias", t.Fatal(err)
`{ User: user { ID: id } }`, }
`type Response struct{
User *struct { schemaText := readFile(t, "schema.graphql")
ID string
} for _, file := range files {
}`, graphqlFilename := file.Name()
// Here on out, we use aliases, just because aliases are a lot less if graphqlFilename == "schema.graphql" || !strings.HasSuffix(graphqlFilename, ".graphql") {
// annoying to write in Go strings than Go struct tags. continue
}, {
"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
}
} }
goFilename := graphqlFilename + ".go"
type role string t.Run(graphqlFilename, func(t *testing.T) {
const ( expectedGoType, err := gofmt(readFile(t, goFilename))
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)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@@ -161,7 +70,8 @@ func TestTypeForOperation(t *testing.T) {
t.Fatal(graphqlError) t.Fatal(graphqlError)
} }
queryDoc, graphqlListError := gqlparser.LoadQuery(schema, test.operation) queryDoc, graphqlListError := gqlparser.LoadQuery(
schema, readFile(t, graphqlFilename))
if graphqlListError != nil { if graphqlListError != nil {
t.Fatal(graphqlListError) t.Fatal(graphqlListError)
} }
@@ -171,7 +81,7 @@ func TestTypeForOperation(t *testing.T) {
} }
g := newGenerator(&Config{Package: "test_package"}, schema) g := newGenerator(&Config{Package: "test_package"}, schema)
_, err = g.getTypeForOperation(queryDoc.Operations[0]) err = g.addOperation(queryDoc.Operations[0])
if err != nil { if err != nil {
t.Error(err) t.Error(err)
} }
@@ -184,11 +94,25 @@ func TestTypeForOperation(t *testing.T) {
if goType != expectedGoType { if goType != expectedGoType {
t.Errorf("got:\n%v\nwant:\n%v\n", 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) { func TestTypeForInputType(t *testing.T) {
tests := []struct { tests := []struct {
name string name string
@@ -224,6 +148,8 @@ func TestTypeForInputType(t *testing.T) {
}, },
}} }}
schemaText := readFile(t, "schema.graphql")
for _, test := range tests { for _, test := range tests {
test := test test := test
t.Run(test.name, func(t *testing.T) { t.Run(test.name, func(t *testing.T) {