|
| 1 | +@testable import GraphQL |
| 2 | +import XCTest |
| 3 | + |
| 4 | +class UniqueDirectiveNamesRuleTests: SDLValidationTestCase { |
| 5 | + override func setUp() { |
| 6 | + rule = UniqueDirectiveNamesRule |
| 7 | + } |
| 8 | + |
| 9 | + func testNoDirective() throws { |
| 10 | + try assertValidationErrors( |
| 11 | + """ |
| 12 | + type Foo |
| 13 | + """, |
| 14 | + [] |
| 15 | + ) |
| 16 | + } |
| 17 | + |
| 18 | + func testOneDirective() throws { |
| 19 | + try assertValidationErrors( |
| 20 | + """ |
| 21 | + directive @foo on SCHEMA |
| 22 | + """, |
| 23 | + [] |
| 24 | + ) |
| 25 | + } |
| 26 | + |
| 27 | + func testManyDirectives() throws { |
| 28 | + try assertValidationErrors( |
| 29 | + """ |
| 30 | + directive @foo on SCHEMA |
| 31 | + directive @bar on SCHEMA |
| 32 | + directive @baz on SCHEMA |
| 33 | + """, |
| 34 | + [] |
| 35 | + ) |
| 36 | + } |
| 37 | + |
| 38 | + func testDirectiveAndNonDirectiveDefinitionsNamedTheSame() throws { |
| 39 | + try assertValidationErrors( |
| 40 | + """ |
| 41 | + query foo { __typename } |
| 42 | + fragment foo on foo { __typename } |
| 43 | + type foo |
| 44 | +
|
| 45 | + directive @foo on SCHEMA |
| 46 | + """, |
| 47 | + [] |
| 48 | + ) |
| 49 | + } |
| 50 | + |
| 51 | + func testDirectivesNamedTheSame() throws { |
| 52 | + try assertValidationErrors( |
| 53 | + """ |
| 54 | + directive @foo on SCHEMA |
| 55 | +
|
| 56 | + directive @foo on SCHEMA |
| 57 | + """, |
| 58 | + [ |
| 59 | + GraphQLError( |
| 60 | + message: #"There can be only one directive named "@foo"."#, |
| 61 | + locations: [ |
| 62 | + .init(line: 1, column: 12), |
| 63 | + .init(line: 3, column: 12), |
| 64 | + ] |
| 65 | + ), |
| 66 | + ] |
| 67 | + ) |
| 68 | + } |
| 69 | + |
| 70 | + func testAddingNewDirectiveToExistingSchema() throws { |
| 71 | + let schema = try buildSchema(source: "directive @foo on SCHEMA") |
| 72 | + try assertValidationErrors("directive @bar on SCHEMA", schema: schema, []) |
| 73 | + } |
| 74 | + |
| 75 | + func testAddingNewDirectiveWithStandardNameToExistingSchema() throws { |
| 76 | + let schema = try buildSchema(source: "type foo") |
| 77 | + try assertValidationErrors( |
| 78 | + "directive @skip on SCHEMA", |
| 79 | + schema: schema, |
| 80 | + [ |
| 81 | + GraphQLError( |
| 82 | + message: #"Directive "@skip" already exists in the schema. It cannot be redefined."#, |
| 83 | + locations: [.init(line: 1, column: 12)] |
| 84 | + ), |
| 85 | + ] |
| 86 | + ) |
| 87 | + } |
| 88 | + |
| 89 | + func testAddingNewDirectiveToExistingSchemaWithSameNamedType() throws { |
| 90 | + let schema = try buildSchema(source: "type foo") |
| 91 | + try assertValidationErrors("directive @foo on SCHEMA", schema: schema, []) |
| 92 | + } |
| 93 | + |
| 94 | + func testAddingConflictingDirectiveToExistingSchema() throws { |
| 95 | + let schema = try buildSchema(source: "directive @foo on SCHEMA") |
| 96 | + try assertValidationErrors( |
| 97 | + "directive @foo on SCHEMA", |
| 98 | + schema: schema, |
| 99 | + [ |
| 100 | + GraphQLError( |
| 101 | + message: #"Directive "@foo" already exists in the schema. It cannot be redefined."#, |
| 102 | + locations: [.init(line: 1, column: 12)] |
| 103 | + ), |
| 104 | + ] |
| 105 | + ) |
| 106 | + } |
| 107 | +} |
0 commit comments