|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +from dataclasses import dataclass |
| 4 | +from typing import TYPE_CHECKING |
| 5 | + |
| 6 | +from tests.helpers.graphql import graphql_query |
| 7 | + |
| 8 | +if TYPE_CHECKING: |
| 9 | + from infrahub.core.branch import Branch |
| 10 | + from infrahub.core.schema.schema_branch import SchemaBranch |
| 11 | + from infrahub.database import InfrahubDatabase |
| 12 | + |
| 13 | +QUERY = """ |
| 14 | +query ($q: String!) { |
| 15 | + InfrahubGraphQLQueryReport(query: $q) { |
| 16 | + targets_unique_nodes |
| 17 | + } |
| 18 | +} |
| 19 | +""" |
| 20 | + |
| 21 | + |
| 22 | +@dataclass |
| 23 | +class UniqueTargetsTestCase: |
| 24 | + analyzed_query: str |
| 25 | + expected: bool |
| 26 | + description: str |
| 27 | + |
| 28 | + |
| 29 | +UNIQUE_TARGETS_TEST_CASES = [ |
| 30 | + UniqueTargetsTestCase( |
| 31 | + description="required variable matching uniqueness constraint", |
| 32 | + analyzed_query=""" |
| 33 | + query ($name: String!) { |
| 34 | + TestCar(name__value: $name) { |
| 35 | + edges { node { id } } |
| 36 | + } |
| 37 | + } |
| 38 | + """, |
| 39 | + expected=True, |
| 40 | + ), |
| 41 | + UniqueTargetsTestCase( |
| 42 | + description="hardcoded value matching uniqueness constraint", |
| 43 | + analyzed_query=""" |
| 44 | + query { |
| 45 | + TestCar(name__value: "mycar") { |
| 46 | + edges { node { id } } |
| 47 | + } |
| 48 | + } |
| 49 | + """, |
| 50 | + expected=True, |
| 51 | + ), |
| 52 | + UniqueTargetsTestCase( |
| 53 | + description="no filter returns all nodes", |
| 54 | + analyzed_query=""" |
| 55 | + query { |
| 56 | + TestCar { |
| 57 | + edges { node { id } } |
| 58 | + } |
| 59 | + } |
| 60 | + """, |
| 61 | + expected=False, |
| 62 | + ), |
| 63 | + UniqueTargetsTestCase( |
| 64 | + description="optional (nullable) variable does not guarantee uniqueness", |
| 65 | + analyzed_query=""" |
| 66 | + query ($name: String) { |
| 67 | + TestCar(name__value: $name) { |
| 68 | + edges { node { id } } |
| 69 | + } |
| 70 | + } |
| 71 | + """, |
| 72 | + expected=False, |
| 73 | + ), |
| 74 | +] |
| 75 | + |
| 76 | + |
| 77 | +async def test_targets_unique_nodes( |
| 78 | + db: InfrahubDatabase, |
| 79 | + default_branch: Branch, |
| 80 | + car_person_schema: SchemaBranch, |
| 81 | +) -> None: |
| 82 | + assert UNIQUE_TARGETS_TEST_CASES, "No test cases defined for unique targets test" |
| 83 | + for case in UNIQUE_TARGETS_TEST_CASES: |
| 84 | + response = await graphql_query(query=QUERY, db=db, branch=default_branch, variables={"q": case.analyzed_query}) |
| 85 | + |
| 86 | + assert not response.errors, f"Unexpected errors for case '{case.description}': {response.errors}" |
| 87 | + assert response.data |
| 88 | + result = response.data["InfrahubGraphQLQueryReport"]["targets_unique_nodes"] |
| 89 | + assert result is case.expected, f"Case '{case.description}': expected {case.expected}, got {result}" |
| 90 | + |
| 91 | + |
| 92 | +async def test_error_on_empty_query_string( |
| 93 | + db: InfrahubDatabase, |
| 94 | + default_branch: Branch, |
| 95 | + car_person_schema: SchemaBranch, |
| 96 | +) -> None: |
| 97 | + response = await graphql_query(query=QUERY, db=db, branch=default_branch, variables={"q": ""}) |
| 98 | + |
| 99 | + assert response.errors |
| 100 | + error = response.errors[0] |
| 101 | + assert "Syntax Error: Unexpected <EOF>." in error.message |
| 102 | + assert error.locations |
| 103 | + assert error.locations[0].line == 1 |
| 104 | + |
| 105 | + |
| 106 | +async def test_error_on_invalid_graphql_syntax( |
| 107 | + db: InfrahubDatabase, |
| 108 | + default_branch: Branch, |
| 109 | + car_person_schema: SchemaBranch, |
| 110 | +) -> None: |
| 111 | + response = await graphql_query(query=QUERY, db=db, branch=default_branch, variables={"q": "not valid graphql {"}) |
| 112 | + |
| 113 | + assert response.errors |
| 114 | + error = response.errors[0] |
| 115 | + assert "Syntax Error: Unexpected Name 'not'." in error.message |
| 116 | + # Locations must point into the analyzed query string (line 1), not the |
| 117 | + # outer wrapper query — proves the inner GraphQLSyntaxError is re-raised |
| 118 | + # rather than wrapped in a fresh, location-less GraphQLError. |
| 119 | + assert error.locations |
| 120 | + assert error.locations[0].line == 1 |
| 121 | + |
| 122 | + |
| 123 | +async def test_error_on_nonexistent_node_type( |
| 124 | + db: InfrahubDatabase, |
| 125 | + default_branch: Branch, |
| 126 | + car_person_schema: SchemaBranch, |
| 127 | +) -> None: |
| 128 | + inner_query = "query { NonExistentType123 { edges { node { id } } } }" |
| 129 | + response = await graphql_query( |
| 130 | + query=QUERY, |
| 131 | + db=db, |
| 132 | + branch=default_branch, |
| 133 | + variables={"q": inner_query}, |
| 134 | + ) |
| 135 | + |
| 136 | + assert response.errors |
| 137 | + error = response.errors[0] |
| 138 | + assert "Cannot query field 'NonExistentType123' on type 'Query'." in error.message |
| 139 | + assert error.locations |
| 140 | + assert error.locations[0].line == 1 |
| 141 | + assert error.locations[0].column == inner_query.index("NonExistentType123") + 1 |
0 commit comments