Skip to content

Commit cec8092

Browse files
committed
Tests for NewDirectives() for better coverage
1 parent c1dcff7 commit cec8092

File tree

1 file changed

+116
-0
lines changed

1 file changed

+116
-0
lines changed

directives_test.go

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import (
55
"testing"
66

77
"github.com/graphql-go/graphql"
8+
"github.com/graphql-go/graphql/gqlerrors"
9+
"github.com/graphql-go/graphql/language/location"
810
"github.com/graphql-go/graphql/testutil"
911
)
1012

@@ -37,6 +39,120 @@ func executeDirectivesTestQuery(t *testing.T, doc string) *graphql.Result {
3739
return testutil.TestExecute(t, ep)
3840
}
3941

42+
func TestDirectives_DirectivesMustBeNamed(t *testing.T) {
43+
invalidDirective := graphql.NewDirective(graphql.DirectiveConfig{
44+
Locations: []string{
45+
graphql.DirectiveLocationField,
46+
},
47+
})
48+
_, err := graphql.NewSchema(graphql.SchemaConfig{
49+
Query: graphql.NewObject(graphql.ObjectConfig{
50+
Name: "TestType",
51+
Fields: graphql.Fields{
52+
"a": &graphql.Field{
53+
Type: graphql.String,
54+
},
55+
},
56+
}),
57+
Directives: []*graphql.Directive{invalidDirective},
58+
})
59+
expectedErr := gqlerrors.FormattedError{
60+
Message: "Directive must be named.",
61+
Locations: []location.SourceLocation{},
62+
}
63+
if !reflect.DeepEqual(expectedErr, err) {
64+
t.Fatalf("Expected error to be equal, got: %v", testutil.Diff(expectedErr, err))
65+
}
66+
}
67+
68+
func TestDirectives_DirectiveNameMustBeValid(t *testing.T) {
69+
invalidDirective := graphql.NewDirective(graphql.DirectiveConfig{
70+
Name: "123invalid name",
71+
Locations: []string{
72+
graphql.DirectiveLocationField,
73+
},
74+
})
75+
_, err := graphql.NewSchema(graphql.SchemaConfig{
76+
Query: graphql.NewObject(graphql.ObjectConfig{
77+
Name: "TestType",
78+
Fields: graphql.Fields{
79+
"a": &graphql.Field{
80+
Type: graphql.String,
81+
},
82+
},
83+
}),
84+
Directives: []*graphql.Directive{invalidDirective},
85+
})
86+
expectedErr := gqlerrors.FormattedError{
87+
Message: `Names must match /^[_a-zA-Z][_a-zA-Z0-9]*$/ but "123invalid name" does not.`,
88+
Locations: []location.SourceLocation{},
89+
}
90+
if !reflect.DeepEqual(expectedErr, err) {
91+
t.Fatalf("Expected error to be equal, got: %v", testutil.Diff(expectedErr, err))
92+
}
93+
}
94+
95+
func TestDirectives_DirectiveNameMustProvideLocations(t *testing.T) {
96+
invalidDirective := graphql.NewDirective(graphql.DirectiveConfig{
97+
Name: "skip",
98+
})
99+
_, err := graphql.NewSchema(graphql.SchemaConfig{
100+
Query: graphql.NewObject(graphql.ObjectConfig{
101+
Name: "TestType",
102+
Fields: graphql.Fields{
103+
"a": &graphql.Field{
104+
Type: graphql.String,
105+
},
106+
},
107+
}),
108+
Directives: []*graphql.Directive{invalidDirective},
109+
})
110+
expectedErr := gqlerrors.FormattedError{
111+
Message: `Must provide locations for directive.`,
112+
Locations: []location.SourceLocation{},
113+
}
114+
if !reflect.DeepEqual(expectedErr, err) {
115+
t.Fatalf("Expected error to be equal, got: %v", testutil.Diff(expectedErr, err))
116+
}
117+
}
118+
119+
func TestDirectives_DirectiveArgNamesMustBeValid(t *testing.T) {
120+
invalidDirective := graphql.NewDirective(graphql.DirectiveConfig{
121+
Name: "skip",
122+
Description: "Directs the executor to skip this field or fragment when the `if` " +
123+
"argument is true.",
124+
Args: graphql.FieldConfigArgument{
125+
"123if": &graphql.ArgumentConfig{
126+
Type: graphql.NewNonNull(graphql.Boolean),
127+
Description: "Skipped when true.",
128+
},
129+
},
130+
Locations: []string{
131+
graphql.DirectiveLocationField,
132+
graphql.DirectiveLocationFragmentSpread,
133+
graphql.DirectiveLocationInlineFragment,
134+
},
135+
})
136+
_, err := graphql.NewSchema(graphql.SchemaConfig{
137+
Query: graphql.NewObject(graphql.ObjectConfig{
138+
Name: "TestType",
139+
Fields: graphql.Fields{
140+
"a": &graphql.Field{
141+
Type: graphql.String,
142+
},
143+
},
144+
}),
145+
Directives: []*graphql.Directive{invalidDirective},
146+
})
147+
expectedErr := gqlerrors.FormattedError{
148+
Message: `Names must match /^[_a-zA-Z][_a-zA-Z0-9]*$/ but "123if" does not.`,
149+
Locations: []location.SourceLocation{},
150+
}
151+
if !reflect.DeepEqual(expectedErr, err) {
152+
t.Fatalf("Expected error to be equal, got: %v", testutil.Diff(expectedErr, err))
153+
}
154+
}
155+
40156
func TestDirectivesWorksWithoutDirectives(t *testing.T) {
41157
query := `{ a, b }`
42158
expected := &graphql.Result{

0 commit comments

Comments
 (0)