Skip to content

Commit 8fdafd1

Browse files
add tests
1 parent 63aee31 commit 8fdafd1

File tree

1 file changed

+46
-8
lines changed

1 file changed

+46
-8
lines changed

tests/integrations/gql/test_gql.py

Lines changed: 46 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,13 @@
33
import responses
44
from gql import gql
55
from gql import Client
6+
from gql import __version__
67
from gql.transport.exceptions import TransportQueryError
78
from gql.transport.requests import RequestsHTTPTransport
89
from sentry_sdk.integrations.gql import GQLIntegration
10+
from sentry_sdk.utils import parse_version
11+
12+
GQL_VERSION = parse_version(__version__)
913

1014

1115
@responses.activate
@@ -32,7 +36,36 @@ def _execute_mock_query(response_json):
3236
return client.execute(query)
3337

3438

35-
def _make_erroneous_query(capture_events):
39+
@responses.activate
40+
def _execute_mock_query_with_keyword_document(response_json):
41+
url = "http://example.com/graphql"
42+
query_string = """
43+
query Example {
44+
example
45+
}
46+
"""
47+
48+
# Mock the GraphQL server response
49+
responses.add(
50+
method=responses.POST,
51+
url=url,
52+
json=response_json,
53+
status=200,
54+
)
55+
56+
transport = RequestsHTTPTransport(url=url)
57+
client = Client(transport=transport)
58+
query = gql(query_string)
59+
60+
return client.execute(document=query)
61+
62+
63+
_execute_query_funcs = [_execute_mock_query]
64+
if GQL_VERSION < (4,):
65+
_execute_query_funcs.append(_execute_mock_query_with_keyword_document)
66+
67+
68+
def _make_erroneous_query(capture_events, execute_query):
3669
"""
3770
Make an erroneous GraphQL query, and assert that the error was reraised, that
3871
exactly one event was recorded, and that the exception recorded was a
@@ -42,7 +75,7 @@ def _make_erroneous_query(capture_events):
4275
response_json = {"errors": ["something bad happened"]}
4376

4477
with pytest.raises(TransportQueryError):
45-
_execute_mock_query(response_json)
78+
execute_query(response_json)
4679

4780
assert len(events) == 1, (
4881
"the sdk captured %d events, but 1 event was expected" % len(events)
@@ -67,7 +100,8 @@ def test_gql_init(sentry_init):
67100
sentry_init(integrations=[GQLIntegration()])
68101

69102

70-
def test_real_gql_request_no_error(sentry_init, capture_events):
103+
@pytest.mark.parametrize("execute_query", _execute_query_funcs)
104+
def test_real_gql_request_no_error(sentry_init, capture_events, execute_query):
71105
"""
72106
Integration test verifying that the GQLIntegration works as expected with successful query.
73107
"""
@@ -77,7 +111,7 @@ def test_real_gql_request_no_error(sentry_init, capture_events):
77111
response_data = {"example": "This is the example"}
78112
response_json = {"data": response_data}
79113

80-
result = _execute_mock_query(response_json)
114+
result = execute_query(response_json)
81115

82116
assert result == response_data, (
83117
"client.execute returned a different value from what it received from the server"
@@ -87,27 +121,31 @@ def test_real_gql_request_no_error(sentry_init, capture_events):
87121
)
88122

89123

90-
def test_real_gql_request_with_error_no_pii(sentry_init, capture_events):
124+
@pytest.mark.parametrize("execute_query", _execute_query_funcs)
125+
def test_real_gql_request_with_error_no_pii(sentry_init, capture_events, execute_query):
91126
"""
92127
Integration test verifying that the GQLIntegration works as expected with query resulting
93128
in a GraphQL error, and that PII is not sent.
94129
"""
95130
sentry_init(integrations=[GQLIntegration()])
96131

97-
event = _make_erroneous_query(capture_events)
132+
event = _make_erroneous_query(capture_events, execute_query)
98133

99134
assert "data" not in event["request"]
100135
assert "response" not in event["contexts"]
101136

102137

103-
def test_real_gql_request_with_error_with_pii(sentry_init, capture_events):
138+
@pytest.mark.parametrize("execute_query", _execute_query_funcs)
139+
def test_real_gql_request_with_error_with_pii(
140+
sentry_init, capture_events, execute_query
141+
):
104142
"""
105143
Integration test verifying that the GQLIntegration works as expected with query resulting
106144
in a GraphQL error, and that PII is not sent.
107145
"""
108146
sentry_init(integrations=[GQLIntegration()], send_default_pii=True)
109147

110-
event = _make_erroneous_query(capture_events)
148+
event = _make_erroneous_query(capture_events, execute_query)
111149

112150
assert "data" in event["request"]
113151
assert "response" in event["contexts"]

0 commit comments

Comments
 (0)