Debian Codegraph @

tests/validation/harness.py

raw
1 from typing import Any, Dict, List, Optional, Type, Union
2
3 from graphql.error import GraphQLError
4 from graphql.language import parse
5 from graphql.type import GraphQLSchema
6 from graphql.utilities import build_schema
7 from graphql.validation import ASTValidationRule
8 from graphql.validation.validate import validate, validate_sdl
9
10 __all__ = [
11 "test_schema",
12 "assert_validation_errors",
13 "assert_sdl_validation_errors",
14 ]
15
16 test_schema = build_schema("""
17 interface Mammal {
18 mother: Mammal
19 father: Mammal
20 }
21
22 interface Pet {
23 name(surname: Boolean): String
24 }
25
26 interface Canine implements Mammal {
27 name(surname: Boolean): String
28 mother: Canine
29 father: Canine
30 }
31
32 enum DogCommand {
33 SIT
34 HEEL
35 DOWN
36 }
37
38 type Dog implements Pet & Mammal & Canine {
39 name(surname: Boolean): String
40 nickname: String
41 barkVolume: Int
42 barks: Boolean
43 doesKnowCommand(dogCommand: DogCommand): Boolean
44 isHouseTrained(atOtherHomes: Boolean = true): Boolean
45 isAtLocation(x: Int, y: Int): Boolean
46 mother: Dog
47 father: Dog
48 }
49
50 type Cat implements Pet {
51 name(surname: Boolean): String
52 nickname: String
53 meows: Boolean
54 meowsVolume: Int
55 furColor: FurColor
56 }
57
58 union CatOrDog = Cat | Dog
59
60 type Human {
61 name(surname: Boolean): String
62 pets: [Pet]
63 relatives: [Human]
64 }
65
66 enum FurColor {
67 BROWN
68 BLACK
69 TAN
70 SPOTTED
71 NO_FUR
72 UNKNOWN
73 }
74
75 input ComplexInput {
76 requiredField: Boolean!
77 nonNullField: Boolean! = false
78 intField: Int
79 stringField: String
80 booleanField: Boolean
81 stringListField: [String]
82 }
83
84 input OneOfInput @oneOf {
85 stringField: String
86 intField: Int
87 }
88
89 type ComplicatedArgs {
90 # TODO List
91 # TODO Coercion
92 # TODO NotNulls
93 intArgField(intArg: Int): String
94 nonNullIntArgField(nonNullIntArg: Int!): String
95 stringArgField(stringArg: String): String
96 booleanArgField(booleanArg: Boolean): String
97 enumArgField(enumArg: FurColor): String
98 floatArgField(floatArg: Float): String
99 idArgField(idArg: ID): String
100 stringListArgField(stringListArg: [String]): String
101 stringListNonNullArgField(stringListNonNullArg: [String!]): String
102 complexArgField(complexArg: ComplexInput): String
103 oneOfArgField(oneOfArg: OneOfInput): String
104 multipleReqs(req1: Int!, req2: Int!): String
105 nonNullFieldWithDefault(arg: Int! = 0): String
106 multipleOpts(opt1: Int = 0, opt2: Int = 0): String
107 multipleOptAndReq(req1: Int!, req2: Int!, opt1: Int = 0, opt2: Int = 0): String
108 }
109
110 type QueryRoot {
111 human(id: ID): Human
112 dog: Dog
113 cat: Cat
114 pet: Pet
115 catOrDog: CatOrDog
116 complicatedArgs: ComplicatedArgs
117 }
118
119 schema {
120 query: QueryRoot
121 }
122
123 directive @onField on FIELD
124 """)
125
126
127 def assert_validation_errors(
128 rule: Type[ASTValidationRule],
129 query_str: str,
130 errors: List[Union[GraphQLError, Dict[str, Any]]],
131 schema: GraphQLSchema = test_schema,
132 ) -> List[GraphQLError]:
133 doc = parse(query_str)
134 returned_errors = validate(schema, doc, [rule])
135 assert returned_errors == errors
136 return returned_errors
137
138
139 def assert_sdl_validation_errors(
140 rule: Type[ASTValidationRule],
141 sdl_str: str,
142 errors: List[Union[GraphQLError, Dict[str, Any]]],
143 schema: Optional[GraphQLSchema] = None,
144 ) -> List[GraphQLError]:
145 doc = parse(sdl_str)
146 returned_errors = validate_sdl(doc, schema, [rule])
147 assert returned_errors == errors
148 return returned_errors