|
| 1 | +@testable import GraphQL |
| 2 | +import XCTest |
| 3 | + |
| 4 | +class UniqueDirectivesPerLocationRuleTests: ValidationTestCase { |
| 5 | + override func setUp() { |
| 6 | + rule = UniqueDirectivesPerLocationRule |
| 7 | + } |
| 8 | + |
| 9 | + func testNoDirectives() throws { |
| 10 | + try assertValid( |
| 11 | + """ |
| 12 | + fragment Test on Type { |
| 13 | + field |
| 14 | + } |
| 15 | + """, |
| 16 | + schema: schema |
| 17 | + ) |
| 18 | + } |
| 19 | + |
| 20 | + func testUniqueDirectivesInDifferentLocations() throws { |
| 21 | + try assertValid( |
| 22 | + """ |
| 23 | + fragment Test on Type @directiveA { |
| 24 | + field @directiveB |
| 25 | + } |
| 26 | + """, |
| 27 | + schema: schema |
| 28 | + ) |
| 29 | + } |
| 30 | + |
| 31 | + func testUniqueDirectivesInSameLocation() throws { |
| 32 | + try assertValid( |
| 33 | + """ |
| 34 | + fragment Test on Type @directiveA @directiveB { |
| 35 | + field @directiveA @directiveB |
| 36 | + } |
| 37 | + """, |
| 38 | + schema: schema |
| 39 | + ) |
| 40 | + } |
| 41 | + |
| 42 | + func testSameDirectivesInDifferentLocations() throws { |
| 43 | + try assertValid( |
| 44 | + """ |
| 45 | + fragment Test on Type @directiveA { |
| 46 | + field @directiveA |
| 47 | + } |
| 48 | + """, |
| 49 | + schema: schema |
| 50 | + ) |
| 51 | + } |
| 52 | + |
| 53 | + func testSameDirectivesInSimilarLocations() throws { |
| 54 | + try assertValid( |
| 55 | + """ |
| 56 | + fragment Test on Type { |
| 57 | + field @directive |
| 58 | + field @directive |
| 59 | + } |
| 60 | + """, |
| 61 | + schema: schema |
| 62 | + ) |
| 63 | + } |
| 64 | + |
| 65 | + func testRepeatableDirectivesInSameLocation() throws { |
| 66 | + try assertValid( |
| 67 | + """ |
| 68 | + fragment Test on Type @repeatable @repeatable { |
| 69 | + field @repeatable @repeatable |
| 70 | + } |
| 71 | + """, |
| 72 | + schema: schema |
| 73 | + ) |
| 74 | + } |
| 75 | + |
| 76 | + func testUnknownDirectivesMustBeIgnored() throws { |
| 77 | + try assertValid( |
| 78 | + """ |
| 79 | + type Test @unknown @unknown { |
| 80 | + field: String! @unknown @unknown |
| 81 | + } |
| 82 | +
|
| 83 | + extend type Test @unknown { |
| 84 | + anotherField: String! |
| 85 | + } |
| 86 | + """, |
| 87 | + schema: schema |
| 88 | + ) |
| 89 | + } |
| 90 | + |
| 91 | + func testDuplicateDirectivesInOneLocation() throws { |
| 92 | + let errors = try assertInvalid( |
| 93 | + errorCount: 1, |
| 94 | + query: |
| 95 | + """ |
| 96 | + fragment Test on Type { |
| 97 | + field @directive @directive |
| 98 | + } |
| 99 | + """, |
| 100 | + schema: schema |
| 101 | + ) |
| 102 | + try assertValidationError( |
| 103 | + error: errors[0], |
| 104 | + locations: [ |
| 105 | + (line: 2, column: 9), |
| 106 | + (line: 2, column: 20), |
| 107 | + ], |
| 108 | + message: #"The directive "@directive" can only be used once at this location."# |
| 109 | + ) |
| 110 | + } |
| 111 | + |
| 112 | + func testManyDuplicateDirectivesInOneLocation() throws { |
| 113 | + let errors = try assertInvalid( |
| 114 | + errorCount: 2, |
| 115 | + query: |
| 116 | + """ |
| 117 | + fragment Test on Type { |
| 118 | + field @directive @directive @directive |
| 119 | + } |
| 120 | + """, |
| 121 | + schema: schema |
| 122 | + ) |
| 123 | + try assertValidationError( |
| 124 | + error: errors[0], |
| 125 | + locations: [ |
| 126 | + (line: 2, column: 9), |
| 127 | + (line: 2, column: 20), |
| 128 | + ], |
| 129 | + message: #"The directive "@directive" can only be used once at this location."# |
| 130 | + ) |
| 131 | + try assertValidationError( |
| 132 | + error: errors[1], |
| 133 | + locations: [ |
| 134 | + (line: 2, column: 9), |
| 135 | + (line: 2, column: 31), |
| 136 | + ], |
| 137 | + message: #"The directive "@directive" can only be used once at this location."# |
| 138 | + ) |
| 139 | + } |
| 140 | + |
| 141 | + func testDifferentDuplicateDirectivesInOneLocation() throws { |
| 142 | + let errors = try assertInvalid( |
| 143 | + errorCount: 2, |
| 144 | + query: |
| 145 | + """ |
| 146 | + fragment Test on Type { |
| 147 | + field @directiveA @directiveB @directiveA @directiveB |
| 148 | + } |
| 149 | + """, |
| 150 | + schema: schema |
| 151 | + ) |
| 152 | + try assertValidationError( |
| 153 | + error: errors[0], |
| 154 | + locations: [ |
| 155 | + (line: 2, column: 9), |
| 156 | + (line: 2, column: 33), |
| 157 | + ], |
| 158 | + message: #"The directive "@directiveA" can only be used once at this location."# |
| 159 | + ) |
| 160 | + try assertValidationError( |
| 161 | + error: errors[1], |
| 162 | + locations: [ |
| 163 | + (line: 2, column: 21), |
| 164 | + (line: 2, column: 45), |
| 165 | + ], |
| 166 | + message: #"The directive "@directiveB" can only be used once at this location."# |
| 167 | + ) |
| 168 | + } |
| 169 | + |
| 170 | + // TODO: Add SDL tests |
| 171 | + |
| 172 | + let schema = try! GraphQLSchema( |
| 173 | + query: ValidationExampleQueryRoot, |
| 174 | + types: [ |
| 175 | + ValidationExampleCat, |
| 176 | + ValidationExampleDog, |
| 177 | + ValidationExampleHuman, |
| 178 | + ValidationExampleAlien, |
| 179 | + ], |
| 180 | + directives: { |
| 181 | + var directives = specifiedDirectives |
| 182 | + directives.append(contentsOf: [ |
| 183 | + ValidationFieldDirective, |
| 184 | + try! GraphQLDirective(name: "directive", locations: [.field, .fragmentDefinition]), |
| 185 | + try! GraphQLDirective(name: "directiveA", locations: [.field, .fragmentDefinition]), |
| 186 | + try! GraphQLDirective(name: "directiveB", locations: [.field, .fragmentDefinition]), |
| 187 | + try! GraphQLDirective( |
| 188 | + name: "repeatable", |
| 189 | + locations: [.field, .fragmentDefinition], |
| 190 | + isRepeatable: true |
| 191 | + ), |
| 192 | + ]) |
| 193 | + return directives |
| 194 | + }() |
| 195 | + ) |
| 196 | +} |
0 commit comments