-
Notifications
You must be signed in to change notification settings - Fork 5
/
complexity_test.go
80 lines (72 loc) · 1.32 KB
/
complexity_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package gbox
import (
"testing"
"github.com/jensneuse/graphql-go-tools/pkg/graphql"
"github.com/stretchr/testify/require"
)
func TestComplexity(t *testing.T) {
testCases := map[string]struct {
complexity *Complexity
expectedErrorCount int
}{
"disabled_all": {
complexity: &Complexity{},
},
"invalid": {
complexity: &Complexity{
NodeCountLimit: 1,
MaxDepth: 1,
MaxComplexity: 1,
},
expectedErrorCount: 3,
},
"invalid_node_count_limit": {
complexity: &Complexity{
NodeCountLimit: 1,
},
expectedErrorCount: 1,
},
"invalid_max_depth": {
complexity: &Complexity{
MaxDepth: 1,
},
expectedErrorCount: 1,
},
"invalid_max_complexity": {
complexity: &Complexity{
MaxComplexity: 1,
},
expectedErrorCount: 1,
},
}
s, _ := graphql.NewSchemaFromString(`
type Query {
books: [Book!]!
}
type Book {
id: ID!
title: String!
buyers: [User!]!
}
type User {
id: ID!
name: String!
}
`)
gqlRequest := &graphql.Request{
Query: `query GetBooks {
books {
buyers {
id
name
}
}
}`,
}
s.Normalize()
gqlRequest.Normalize(s)
for name, testCase := range testCases {
err := testCase.complexity.validateRequest(s, gqlRequest)
require.Equalf(t, testCase.expectedErrorCount, err.Count(), "case %s: unexpected error count", name)
}
}