|
| 1 | +@testable import GraphQL |
| 2 | +import XCTest |
| 3 | + |
| 4 | +class FieldsOnCorrectTypeTests : ValidationTestCase { |
| 5 | + |
| 6 | + override func setUp() { |
| 7 | + rule = FieldsOnCorrectType |
| 8 | + } |
| 9 | + |
| 10 | + func testValidWithObjectFieldSelection() throws { |
| 11 | + try assertValid( |
| 12 | + "fragment objectFieldSelection on Dog { __typename name }" |
| 13 | + ) |
| 14 | + } |
| 15 | + |
| 16 | + func testValidWithAliasedObjectFieldSelection() throws { |
| 17 | + try assertValid( |
| 18 | + "fragment aliasedObjectFieldSelection on Dog { tn : __typename otherName : name }" |
| 19 | + ) |
| 20 | + } |
| 21 | + |
| 22 | + func testValidWithInterfaceFieldSelection() throws { |
| 23 | + try assertValid( |
| 24 | + "fragment interfaceFieldSelection on Pet { __typename name }" |
| 25 | + ) |
| 26 | + } |
| 27 | + |
| 28 | + func testValidWithAliasedInterfaceFieldSelection() throws { |
| 29 | + try assertValid( |
| 30 | + "fragment aliasedInterfaceFieldSelection on Pet { otherName : name }" |
| 31 | + ) |
| 32 | + } |
| 33 | + |
| 34 | + func testValidWithLyingAliasSelection() throws { |
| 35 | + try assertValid( |
| 36 | + "fragment lyingAliasSelection on Dog { name : nickname }" |
| 37 | + ) |
| 38 | + } |
| 39 | + |
| 40 | + func testValidWithInlineFragment() throws { |
| 41 | + try assertValid( |
| 42 | + "fragment inlineFragment on Pet { ... on Dog { name } ... { name } }" |
| 43 | + ) |
| 44 | + } |
| 45 | + |
| 46 | + func testValidWhenMetaFieldSelectionOnUnion() throws { |
| 47 | + try assertValid( |
| 48 | + "fragment metaFieldSelectionOnUnion on CatOrDog { __typename }" |
| 49 | + ) |
| 50 | + } |
| 51 | + |
| 52 | + func testValidWithIgnoresFieldsOnUnknownType() throws { |
| 53 | + try assertValid( |
| 54 | + "fragment ignoresFieldsOnUnknownType on UnknownType { unknownField }" |
| 55 | + ) |
| 56 | + } |
| 57 | + |
| 58 | + func testInvalidWhenTypeKnownAgain() throws { |
| 59 | + let errors = try assertInvalid( |
| 60 | + errorCount: 1, |
| 61 | + query: "fragment typeKnownAgain on Pet { unknown_pet_field { ... on Cat { unknown_cat_field } } }" |
| 62 | + ) |
| 63 | + try assertValidationError( |
| 64 | + error: errors.first, line: 1, column: 34, |
| 65 | + message: "Cannot query field \"unknown_pet_field\" on type \"Pet\"." |
| 66 | + ) |
| 67 | + } |
| 68 | + |
| 69 | + func testInvalidWhenFieldNotDefined() throws { |
| 70 | + let errors = try assertInvalid( |
| 71 | + errorCount: 1, |
| 72 | + query: "fragment fieldNotDefined on Dog { meowVolume }" |
| 73 | + ) |
| 74 | + try assertValidationError( |
| 75 | + error: errors.first, line: 1, column: 35, |
| 76 | + message: "Cannot query field \"meowVolume\" on type \"Dog\". Did you mean \"barkVolume\"?" |
| 77 | + ) |
| 78 | + } |
| 79 | + |
| 80 | + func testInvalidWhenDeepFieldNotDefined() throws { |
| 81 | + let errors = try assertInvalid( |
| 82 | + errorCount: 1, |
| 83 | + query: "fragment deepFieldNotDefined on Dog { unknown_field { deeper_unknown_field }}" |
| 84 | + ) |
| 85 | + try assertValidationError( |
| 86 | + error: errors.first, line: 1, column: 39, |
| 87 | + message: "Cannot query field \"unknown_field\" on type \"Dog\"." |
| 88 | + ) |
| 89 | + } |
| 90 | + |
| 91 | + func testInvalidWhenSubFieldNotDefined() throws { |
| 92 | + let errors = try assertInvalid( |
| 93 | + errorCount: 1, |
| 94 | + query: "fragment subFieldNotDefined on Human { pets { unknown_field } }" |
| 95 | + ) |
| 96 | + try assertValidationError( |
| 97 | + error: errors.first, line: 1, column: 47, |
| 98 | + message: "Cannot query field \"unknown_field\" on type \"Pet\"." |
| 99 | + ) |
| 100 | + } |
| 101 | + |
| 102 | + func testInvalidWhenFieldNotDefinedOnInlineFragment() throws { |
| 103 | + let errors = try assertInvalid( |
| 104 | + errorCount: 1, |
| 105 | + query: "fragment fieldNotDefinedOnInlineFragment on Pet { ... on Dog { meowVolume } }" |
| 106 | + ) |
| 107 | + try assertValidationError( |
| 108 | + error: errors.first, line: 1, column: 64, |
| 109 | + message: "Cannot query field \"meowVolume\" on type \"Dog\". Did you mean \"barkVolume\"?" |
| 110 | + ) |
| 111 | + } |
| 112 | + |
| 113 | + func testInvalidWhenAliasedFieldTargetNotDefined() throws { |
| 114 | + let errors = try assertInvalid( |
| 115 | + errorCount: 1, |
| 116 | + query: "fragment aliasedFieldTargetNotDefined on Dog { volume : mooVolume }" |
| 117 | + ) |
| 118 | + try assertValidationError( |
| 119 | + error: errors.first, line: 1, column: 48, |
| 120 | + message: "Cannot query field \"mooVolume\" on type \"Dog\". Did you mean \"barkVolume\"?" |
| 121 | + ) |
| 122 | + } |
| 123 | + |
| 124 | + func testInvalidWhenAliasedLyingFieldTargetNotDefined() throws { |
| 125 | + let errors = try assertInvalid( |
| 126 | + errorCount: 1, |
| 127 | + query: "fragment aliasedLyingFieldTargetNotDefined on Dog { barkVolume : kawVolume }" |
| 128 | + ) |
| 129 | + try assertValidationError( |
| 130 | + error: errors.first, line: 1, column: 53, |
| 131 | + message: "Cannot query field \"kawVolume\" on type \"Dog\". Did you mean \"barkVolume\"?" |
| 132 | + ) |
| 133 | + } |
| 134 | + |
| 135 | + func testInvalidWhenNotDefinedOnInterface() throws { |
| 136 | + let errors = try assertInvalid( |
| 137 | + errorCount: 1, |
| 138 | + query: "fragment notDefinedOnInterface on Pet { tailLength }" |
| 139 | + ) |
| 140 | + try assertValidationError( |
| 141 | + error: errors.first, line: 1, column: 41, |
| 142 | + message: "Cannot query field \"tailLength\" on type \"Pet\"." |
| 143 | + ) |
| 144 | + } |
| 145 | + |
| 146 | + func testInvalidWhenDefinedOnImplementorsButNotInterface() throws { |
| 147 | + let errors = try assertInvalid( |
| 148 | + errorCount: 1, |
| 149 | + query: "fragment definedOnImplementorsButNotInterface on Pet { nickname }" |
| 150 | + ) |
| 151 | + try assertValidationError( |
| 152 | + error: errors.first, line: 1, column: 56, |
| 153 | + message: "Cannot query field \"nickname\" on type \"Pet\". Did you mean \"name\"?" |
| 154 | + ) |
| 155 | + } |
| 156 | + |
| 157 | + /* |
| 158 | + func testInvalidWhenDirectFieldSelectionOnUnion() throws { |
| 159 | + let errors = try assertInvalid( |
| 160 | + errorCount: 1, |
| 161 | + query: "fragment directFieldSelectionOnUnion on CatOrDog { directField }" |
| 162 | + ) |
| 163 | + try assertValidationError( |
| 164 | + error: errors.first, line: 1, column: 0, |
| 165 | + message: "" |
| 166 | + ) |
| 167 | + } |
| 168 | + |
| 169 | + func testInvalidWhenDefinedOnImplementorsQueriedOnUnion() throws { |
| 170 | + let errors = try assertInvalid( |
| 171 | + errorCount: 1, |
| 172 | + query: "fragment definedOnImplementorsQueriedOnUnion on CatOrDog { name }" |
| 173 | + ) |
| 174 | + try assertValidationError( |
| 175 | + error: errors.first, line: 1, column: 0, |
| 176 | + message: "" |
| 177 | + ) |
| 178 | + } |
| 179 | + */ |
| 180 | + |
| 181 | +} |
| 182 | + |
| 183 | +extension FieldsOnCorrectTypeTests { |
| 184 | + static var allTests: [(String, (FieldsOnCorrectTypeTests) -> () throws -> Void)] { |
| 185 | + return [ |
| 186 | + ("testValidWithObjectFieldSelection", testValidWithObjectFieldSelection), |
| 187 | + ("testValidWithAliasedObjectFieldSelection", testValidWithAliasedObjectFieldSelection), |
| 188 | + ("testValidWithInterfaceFieldSelection", testValidWithInterfaceFieldSelection), |
| 189 | + ("testValidWithAliasedInterfaceFieldSelection", testValidWithAliasedInterfaceFieldSelection), |
| 190 | + ("testValidWithLyingAliasSelection", testValidWithLyingAliasSelection), |
| 191 | + ("testValidWithInlineFragment", testValidWithInlineFragment), |
| 192 | + ("testValidWhenMetaFieldSelectionOnUnion", testValidWhenMetaFieldSelectionOnUnion), |
| 193 | + ("testValidWithIgnoresFieldsOnUnknownType", testValidWithIgnoresFieldsOnUnknownType), |
| 194 | + ("testInvalidWhenTypeKnownAgain", testInvalidWhenTypeKnownAgain), |
| 195 | + ("testInvalidWhenFieldNotDefined", testInvalidWhenFieldNotDefined), |
| 196 | + ("testInvalidWhenDeepFieldNotDefined", testInvalidWhenDeepFieldNotDefined), |
| 197 | + ("testInvalidWhenSubFieldNotDefined", testInvalidWhenSubFieldNotDefined), |
| 198 | + ("testInvalidWhenFieldNotDefinedOnInlineFragment", testInvalidWhenFieldNotDefinedOnInlineFragment), |
| 199 | + ("testInvalidWhenAliasedFieldTargetNotDefined", testInvalidWhenAliasedFieldTargetNotDefined), |
| 200 | + ("testInvalidWhenAliasedLyingFieldTargetNotDefined", testInvalidWhenAliasedLyingFieldTargetNotDefined), |
| 201 | + ("testInvalidWhenNotDefinedOnInterface", testInvalidWhenNotDefinedOnInterface), |
| 202 | + ("testInvalidWhenDefinedOnImplementorsButNotInterface", testInvalidWhenDefinedOnImplementorsButNotInterface), |
| 203 | + /* |
| 204 | + ("testInvalidWhenDirectFieldSelectionOnUnion", testInvalidWhenDirectFieldSelectionOnUnion), |
| 205 | + ("testInvalidWhenDefinedOnImplementorsQueriedOnUnion", testInvalidWhenDefinedOnImplementorsQueriedOnUnion), |
| 206 | + */ |
| 207 | + ] |
| 208 | + } |
| 209 | +} |
0 commit comments