-
Notifications
You must be signed in to change notification settings - Fork 52
Expand file tree
/
Copy pathtest_graphql_query_report.py
More file actions
127 lines (107 loc) · 3.62 KB
/
test_graphql_query_report.py
File metadata and controls
127 lines (107 loc) · 3.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
from __future__ import annotations
from dataclasses import dataclass
from typing import TYPE_CHECKING
from tests.helpers.graphql import graphql_query
if TYPE_CHECKING:
from infrahub.core.branch import Branch
from infrahub.core.schema.schema_branch import SchemaBranch
from infrahub.database import InfrahubDatabase
QUERY = """
query ($q: String!) {
InfrahubGraphQLQueryReport(query: $q) {
targets_unique_nodes
}
}
"""
@dataclass
class UniqueTargetsTestCase:
analyzed_query: str
expected: bool
description: str
UNIQUE_TARGETS_TEST_CASES = [
UniqueTargetsTestCase(
description="required variable matching uniqueness constraint",
analyzed_query="""
query ($name: String!) {
TestCar(name__value: $name) {
edges { node { id } }
}
}
""",
expected=True,
),
UniqueTargetsTestCase(
description="hardcoded value matching uniqueness constraint",
analyzed_query="""
query {
TestCar(name__value: "mycar") {
edges { node { id } }
}
}
""",
expected=True,
),
UniqueTargetsTestCase(
description="no filter returns all nodes",
analyzed_query="""
query {
TestCar {
edges { node { id } }
}
}
""",
expected=False,
),
UniqueTargetsTestCase(
description="optional (nullable) variable does not guarantee uniqueness",
analyzed_query="""
query ($name: String) {
TestCar(name__value: $name) {
edges { node { id } }
}
}
""",
expected=False,
),
]
async def test_targets_unique_nodes(
db: InfrahubDatabase,
default_branch: Branch,
car_person_schema: SchemaBranch,
) -> None:
assert UNIQUE_TARGETS_TEST_CASES, "No test cases defined for unique targets test"
for case in UNIQUE_TARGETS_TEST_CASES:
response = await graphql_query(query=QUERY, db=db, branch=default_branch, variables={"q": case.analyzed_query})
assert not response.errors, f"Unexpected errors for case '{case.description}': {response.errors}"
assert response.data
result = response.data["InfrahubGraphQLQueryReport"]["targets_unique_nodes"]
assert result is case.expected, f"Case '{case.description}': expected {case.expected}, got {result}"
async def test_error_on_empty_query_string(
db: InfrahubDatabase,
default_branch: Branch,
car_person_schema: SchemaBranch,
) -> None:
response = await graphql_query(query=QUERY, db=db, branch=default_branch, variables={"q": ""})
assert response.errors
assert "Syntax Error: Unexpected <EOF>." in response.errors[0].message
async def test_error_on_invalid_graphql_syntax(
db: InfrahubDatabase,
default_branch: Branch,
car_person_schema: SchemaBranch,
) -> None:
response = await graphql_query(query=QUERY, db=db, branch=default_branch, variables={"q": "not valid graphql {"})
assert response.errors
assert "Syntax Error: Unexpected Name 'not'." in response.errors[0].message
async def test_error_on_nonexistent_node_type(
db: InfrahubDatabase,
default_branch: Branch,
car_person_schema: SchemaBranch,
) -> None:
response = await graphql_query(
query=QUERY,
db=db,
branch=default_branch,
variables={"q": "query { NonExistentType123 { edges { node { id } } } }"},
)
assert response.errors
assert "Cannot query field 'NonExistentType123' on type 'Query'." in response.errors[0].message