|
| 1 | +import pytest |
| 2 | + |
| 3 | +from graphql.core.language.location import SourceLocation |
| 4 | +from graphql.core.validation.rules import KnownArgumentNames |
| 5 | +from utils import expect_passes_rule, expect_fails_rule |
| 6 | + |
| 7 | + |
| 8 | +def unknown_arg(arg_name, field_name, type_name, line, column): |
| 9 | + return { |
| 10 | + 'message': KnownArgumentNames.message(arg_name, field_name, type_name), |
| 11 | + 'locations': [SourceLocation(line, column)] |
| 12 | + } |
| 13 | + |
| 14 | + |
| 15 | +def unknown_directive_arg(arg_name, directive_name, line, column): |
| 16 | + return { |
| 17 | + 'message': KnownArgumentNames.unknown_directive_message( |
| 18 | + arg_name, directive_name), |
| 19 | + 'locations': [SourceLocation(line, column)] |
| 20 | + } |
| 21 | + |
| 22 | + |
| 23 | +def test_single_arg_is_known(): |
| 24 | + expect_passes_rule(KnownArgumentNames, ''' |
| 25 | + fragment argOnRequiredArg on Dog { |
| 26 | + doesKnowCommand(dogCommand: SIT) |
| 27 | + } |
| 28 | + ''') |
| 29 | + |
| 30 | + |
| 31 | +def test_multiple_args_are_known(): |
| 32 | + expect_passes_rule(KnownArgumentNames, ''' |
| 33 | + fragment multipleArgs on ComplicatedArgs { |
| 34 | + multipleReqs(req1: 1, req2: 2) |
| 35 | + } |
| 36 | + ''') |
| 37 | + |
| 38 | + |
| 39 | +def test_ignore_args_of_unknown_fields(): |
| 40 | + expect_passes_rule(KnownArgumentNames, ''' |
| 41 | + fragment argOnUnknownField on Dog { |
| 42 | + unknownField(unknownArg: SIT) |
| 43 | + } |
| 44 | + ''') |
| 45 | + |
| 46 | + |
| 47 | +def test_multiple_args_in_reverse_order_are_known(): |
| 48 | + expect_passes_rule(KnownArgumentNames, ''' |
| 49 | + fragment multipleArgsReverseOrder on ComplicatedArgs { |
| 50 | + multipleReqs(req2: 2, req1: 1) |
| 51 | + } |
| 52 | + ''') |
| 53 | + |
| 54 | + |
| 55 | +def test_no_args_on_optional_arg(): |
| 56 | + expect_passes_rule(KnownArgumentNames, ''' |
| 57 | + fragment noArgOnOptionalArg on Dog { |
| 58 | + isHousetrained |
| 59 | + } |
| 60 | + ''') |
| 61 | + |
| 62 | + |
| 63 | +def test_args_are_known_deeply(): |
| 64 | + expect_passes_rule(KnownArgumentNames, ''' |
| 65 | + { |
| 66 | + dog { |
| 67 | + doesKnowCommand(dogCommand: SIT) |
| 68 | + } |
| 69 | + human { |
| 70 | + pet { |
| 71 | + ... on Dog { |
| 72 | + doesKnowCommand(dogCommand: SIT) |
| 73 | + } |
| 74 | + } |
| 75 | + } |
| 76 | + } |
| 77 | + ''') |
| 78 | + |
| 79 | + |
| 80 | +def test_directive_args_are_known(): |
| 81 | + expect_passes_rule(KnownArgumentNames, ''' |
| 82 | + { |
| 83 | + dog @skip(if: true) |
| 84 | + } |
| 85 | + ''') |
| 86 | + |
| 87 | + |
| 88 | +@pytest.mark.skipif(not hasattr(KnownArgumentNames, |
| 89 | + "unknown_directive_message"), |
| 90 | + reason=("KnownDirectives.unknown_directive_message not " |
| 91 | + "yet implemented")) |
| 92 | +def test_undirective_args_are_invalid(): |
| 93 | + expect_fails_rule(KnownArgumentNames, ''' |
| 94 | + { |
| 95 | + dog @skip(unless: true) |
| 96 | + } |
| 97 | + ''', [unknown_directive_arg('unless', 'skip', 3, 19)]) |
| 98 | + |
| 99 | + |
| 100 | +@pytest.mark.skipif(not hasattr(KnownArgumentNames, "message"), |
| 101 | + reason="KnownArgumentNames.message not yet implemented") |
| 102 | +def test_invalid_arg_name(): |
| 103 | + expect_fails_rule(KnownArgumentNames, ''' |
| 104 | + fragment invalidArgName on Dog { |
| 105 | + doesKnowCommand(unknown: true) |
| 106 | + } |
| 107 | + ''', [unknown_arg('unknown', 'doesKnowCommand', 'Dog', 3, 25)]) |
| 108 | + |
| 109 | + |
| 110 | +@pytest.mark.skipif(not hasattr(KnownArgumentNames, "message"), |
| 111 | + reason="KnownArgumentNames.message not yet implemented") |
| 112 | +def test_unknown_args_amongst_known_args(): |
| 113 | + expect_fails_rule(KnownArgumentNames, ''' |
| 114 | + fragment oneGoodArgOneInvalidArg on Dog { |
| 115 | + doesKnowCommand(whoknows: 1, dogCommand: SIT, unknown: true) |
| 116 | + } |
| 117 | + ''', [unknown_arg('whoknows', 'doesKnowCommand', 'Dog', 3, 25), |
| 118 | + unknown_arg('unknown', 'doesKnowCommand', 'Dog', 3, 55)]) |
| 119 | + |
| 120 | + |
| 121 | +@pytest.mark.skipif(not hasattr(KnownArgumentNames, "message"), |
| 122 | + reason="KnownArgumentNames.message not yet implemented") |
| 123 | +def test_unknown_args_deeply(): |
| 124 | + expect_fails_rule(KnownArgumentNames, ''' |
| 125 | + { |
| 126 | + dog { |
| 127 | + doesKnowCommand(unknown: true) |
| 128 | + } |
| 129 | + human { |
| 130 | + pet { |
| 131 | + ... on Dog { |
| 132 | + doesKnowCommand(unknown: true) |
| 133 | + } |
| 134 | + } |
| 135 | + } |
| 136 | + } |
| 137 | + ''', [unknown_arg('unknown', 'doesKnowCommand', 'Dog', 4, 27), |
| 138 | + unknown_arg('unknown', 'doesKnowCommand', 'Dog', 9, 31)]) |
0 commit comments