|
| 1 | +from graphql.core.language.location import SourceLocation |
| 2 | +from graphql.core.validation.rules import UniqueFragmentNames |
| 3 | +from utils import expect_passes_rule, expect_fails_rule |
| 4 | + |
| 5 | + |
| 6 | +def duplicate_fragment(fragment_name, l1, c1, l2, c2): |
| 7 | + return { |
| 8 | + 'message': UniqueFragmentNames.duplicate_fragment_name_message(fragment_name), |
| 9 | + 'locations': [SourceLocation(l1, c1), SourceLocation(l2, c2)] |
| 10 | + } |
| 11 | + |
| 12 | + |
| 13 | +def test_no_fragments(): |
| 14 | + expect_passes_rule(UniqueFragmentNames, ''' |
| 15 | + { |
| 16 | + field |
| 17 | + } |
| 18 | + ''') |
| 19 | + |
| 20 | +def test_one_fragment(): |
| 21 | + expect_passes_rule(UniqueFragmentNames, ''' |
| 22 | + { |
| 23 | + ...fragA |
| 24 | + } |
| 25 | + fragment fragA on Type { |
| 26 | + field |
| 27 | + } |
| 28 | + ''') |
| 29 | + |
| 30 | +def test_many_fragments(): |
| 31 | + expect_passes_rule(UniqueFragmentNames, ''' |
| 32 | + { |
| 33 | + ...fragA |
| 34 | + ...fragB |
| 35 | + ...fragC |
| 36 | + } |
| 37 | + fragment fragA on Type { |
| 38 | + fieldA |
| 39 | + } |
| 40 | + fragment fragB on Type { |
| 41 | + fieldB |
| 42 | + } |
| 43 | + fragment fragC on Type { |
| 44 | + fieldC |
| 45 | + } |
| 46 | + ''') |
| 47 | +def test_inline_fragments(): |
| 48 | + expect_passes_rule(UniqueFragmentNames, ''' |
| 49 | + { |
| 50 | + ...on Type { |
| 51 | + fieldA |
| 52 | + } |
| 53 | + ...on Type { |
| 54 | + fieldB |
| 55 | + } |
| 56 | + } |
| 57 | + ''') |
| 58 | +def test_fragment_operation_same_name(): |
| 59 | + expect_passes_rule(UniqueFragmentNames, ''' |
| 60 | + query Foo { |
| 61 | + ...Foo |
| 62 | + } |
| 63 | + fragment Foo on Type { |
| 64 | + field |
| 65 | + } |
| 66 | + ''') |
| 67 | +def test_fragments_same_name(): |
| 68 | + expect_fails_rule(UniqueFragmentNames, ''' |
| 69 | + { |
| 70 | + ...fragA |
| 71 | + } |
| 72 | + fragment fragA on Type { |
| 73 | + fieldA |
| 74 | + } |
| 75 | + fragment fragA on Type { |
| 76 | + fieldB |
| 77 | + } |
| 78 | + ''', [duplicate_fragment('fragA', 5, 16, 8, 16)] |
| 79 | + ) |
| 80 | + |
| 81 | +def test_fragments_same_name_no_ref(): |
| 82 | + expect_fails_rule(UniqueFragmentNames, ''' |
| 83 | + fragment fragA on Type { |
| 84 | + fieldA |
| 85 | + } |
| 86 | + fragment fragA on Type { |
| 87 | + fieldB |
| 88 | + } |
| 89 | + ''', [ |
| 90 | + duplicate_fragment('fragA', 2, 16, 5, 16) |
| 91 | + ] |
| 92 | + ) |
0 commit comments