|
| 1 | +import pytest |
| 2 | + |
| 3 | +from graphql.core.language.location import SourceLocation |
| 4 | +from graphql.core.validation.rules import KnownDirectives |
| 5 | +from utils import expect_passes_rule, expect_fails_rule |
| 6 | + |
| 7 | + |
| 8 | +def unknown_directive(directive_name, line, column): |
| 9 | + return { |
| 10 | + 'message': KnownDirectives.message(directive_name), |
| 11 | + 'locations': [SourceLocation(line, column)] |
| 12 | + } |
| 13 | + |
| 14 | + |
| 15 | +def misplaced_directive(directive_name, placement, line, column): |
| 16 | + return { |
| 17 | + 'message': KnownDirectives.misplaced_directive_message(directive_name, |
| 18 | + placement), |
| 19 | + 'locations': [SourceLocation(line, column)] |
| 20 | + } |
| 21 | + |
| 22 | + |
| 23 | +def test_with_no_directives(): |
| 24 | + expect_passes_rule(KnownDirectives, ''' |
| 25 | + query Foo { |
| 26 | + name |
| 27 | + ...Frag |
| 28 | + } |
| 29 | +
|
| 30 | + fragment Frag on Dog { |
| 31 | + name |
| 32 | + } |
| 33 | + ''') |
| 34 | + |
| 35 | + |
| 36 | +def test_with_known_directives(): |
| 37 | + expect_passes_rule(KnownDirectives, ''' |
| 38 | + { |
| 39 | + dog @include(if: true) { |
| 40 | + name |
| 41 | + } |
| 42 | + human @skip(if: false) { |
| 43 | + name |
| 44 | + } |
| 45 | + } |
| 46 | + ''') |
| 47 | + |
| 48 | + |
| 49 | +@pytest.mark.skipif(not hasattr(KnownDirectives, "message"), |
| 50 | + reason=("KnownDirectives.message has not yet been " |
| 51 | + "implemented")) |
| 52 | +def test_with_unknown_directive(): |
| 53 | + expect_fails_rule(KnownDirectives, ''' |
| 54 | + { |
| 55 | + dog @unknown(directive: "value") { |
| 56 | + name |
| 57 | + } |
| 58 | + } |
| 59 | + ''', [unknown_directive('unknown', 3, 13)]) |
| 60 | + |
| 61 | + |
| 62 | +@pytest.mark.skipif(not hasattr(KnownDirectives, "message"), |
| 63 | + reason=("KnownDirectives.message has not yet been " |
| 64 | + "implemented")) |
| 65 | +def test_with_many_unknown_directives(): |
| 66 | + expect_fails_rule(KnownDirectives, ''' |
| 67 | + { |
| 68 | + dog @unknown(directive: "value") { |
| 69 | + name |
| 70 | + } |
| 71 | + human @unknown(directive: "value") { |
| 72 | + name |
| 73 | + pets @unknown(directive: "value") { |
| 74 | + name |
| 75 | + } |
| 76 | + } |
| 77 | + } |
| 78 | + ''', [unknown_directive('unknown', 3, 13), |
| 79 | + unknown_directive('unknown', 6, 15), |
| 80 | + unknown_directive('unknown', 8, 16)]) |
| 81 | + |
| 82 | + |
| 83 | +def test_with_well_placed_directives(): |
| 84 | + expect_passes_rule(KnownDirectives, ''' |
| 85 | + query Foo { |
| 86 | + name @include(if: true) |
| 87 | + ...Frag @include(if: true) |
| 88 | + skippedField @skip(if: true) |
| 89 | + ...SkippedFrag @skip(if: true) |
| 90 | + } |
| 91 | + ''') |
| 92 | + |
| 93 | + |
| 94 | +@pytest.mark.skipif(not hasattr(KnownDirectives, |
| 95 | + "misplaced_directive_message"), |
| 96 | + reason=("KnownDirectives.misplaced_directive_message has " |
| 97 | + "not yet been implemented")) |
| 98 | +def test_with_misplaced_directives(): |
| 99 | + expect_fails_rule(KnownDirectives, ''' |
| 100 | + query Foo @include(if: true) { |
| 101 | + name |
| 102 | + ...Frag |
| 103 | + } |
| 104 | + ''', [misplaced_directive('include', 'operation', 2, 17)]) |
0 commit comments