Skip to content

Commit 7c8a020

Browse files
authored
Merge pull request #4038 from surajdm123/add-tests-4
Added test cases for QueryGeneratorOptions
2 parents 38aab2d + 49825bf commit 7c8a020

1 file changed

Lines changed: 80 additions & 0 deletions

File tree

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package graphql.util.querygenerator
2+
3+
import graphql.schema.GraphQLFieldDefinition
4+
import graphql.schema.GraphQLFieldsContainer
5+
import spock.lang.Specification
6+
7+
import java.util.function.Predicate
8+
9+
class QueryGeneratorOptionsTest extends Specification {
10+
11+
def "default builder sets maxFieldCount to 10_000 and always-true predicates"() {
12+
when:
13+
def options = QueryGeneratorOptions.newBuilder().build()
14+
15+
then:
16+
options.maxFieldCount == 10_000
17+
options.filterFieldContainerPredicate.test(Mock(GraphQLFieldsContainer))
18+
options.filterFieldDefinitionPredicate.test(Mock(GraphQLFieldDefinition))
19+
}
20+
21+
def "builder sets maxFieldCount to custom value within range"() {
22+
when:
23+
def options = QueryGeneratorOptions.newBuilder()
24+
.maxFieldCount(500)
25+
.build()
26+
27+
then:
28+
options.maxFieldCount == 500
29+
}
30+
31+
def "builder throws exception if maxFieldCount is negative"() {
32+
when:
33+
QueryGeneratorOptions.newBuilder().maxFieldCount(-1)
34+
35+
then:
36+
def e = thrown(IllegalArgumentException)
37+
e.message == "Max field count cannot be negative"
38+
}
39+
40+
def "builder throws exception if maxFieldCount exceeds MAX_FIELD_COUNT_LIMIT"() {
41+
when:
42+
QueryGeneratorOptions.newBuilder().maxFieldCount(10_001)
43+
44+
then:
45+
def e = thrown(IllegalArgumentException)
46+
e.message == "Max field count cannot exceed 10000"
47+
}
48+
49+
def "builder uses custom field container predicate"() {
50+
given:
51+
def customPredicate = Mock(Predicate)
52+
customPredicate.test(_) >> false
53+
54+
when:
55+
def options = QueryGeneratorOptions.newBuilder()
56+
.filterFieldContainerPredicate(customPredicate)
57+
.build()
58+
59+
then:
60+
!options.filterFieldContainerPredicate.test(Mock(GraphQLFieldsContainer))
61+
}
62+
63+
def "builder uses custom field definition predicate"() {
64+
given:
65+
def customPredicate = { GraphQLFieldDefinition defn -> defn.name == "includeMe" } as Predicate
66+
67+
and:
68+
def included = Mock(GraphQLFieldDefinition) { getName() >> "includeMe" }
69+
def excluded = Mock(GraphQLFieldDefinition) { getName() >> "skipMe" }
70+
71+
when:
72+
def options = QueryGeneratorOptions.newBuilder()
73+
.filterFieldDefinitionPredicate(customPredicate)
74+
.build()
75+
76+
then:
77+
options.filterFieldDefinitionPredicate.test(included)
78+
!options.filterFieldDefinitionPredicate.test(excluded)
79+
}
80+
}

0 commit comments

Comments
 (0)