|
5 | 5 | from gql import Client |
6 | 6 | from gql.transport.exceptions import TransportQueryError |
7 | 7 | from gql.transport.requests import RequestsHTTPTransport |
8 | | -from graphql import DocumentNode |
9 | 8 | from sentry_sdk.integrations.gql import GQLIntegration |
10 | | -from unittest.mock import MagicMock, patch |
11 | | - |
12 | | - |
13 | | -class _MockClientBase(MagicMock): |
14 | | - """ |
15 | | - Mocked version of GQL Client class, following same spec as GQL Client. |
16 | | - """ |
17 | | - |
18 | | - def __init__(self, *args, **kwargs): |
19 | | - kwargs["spec"] = Client |
20 | | - super().__init__(*args, **kwargs) |
21 | | - |
22 | | - transport = MagicMock() |
23 | 9 |
|
24 | 10 |
|
25 | 11 | @responses.activate |
@@ -81,95 +67,6 @@ def test_gql_init(sentry_init): |
81 | 67 | sentry_init(integrations=[GQLIntegration()]) |
82 | 68 |
|
83 | 69 |
|
84 | | -@patch("sentry_sdk.integrations.gql.Hub") |
85 | | -def test_setup_once_patches_execute_and_patched_function_calls_original(_): |
86 | | - """ |
87 | | - Unit test which ensures the following: |
88 | | - 1. The GQLIntegration setup_once function patches the gql.Client.execute method |
89 | | - 2. The patched gql.Client.execute method still calls the original method, and it |
90 | | - forwards its arguments to the original method. |
91 | | - 3. The patched gql.Client.execute method returns the same value that the original |
92 | | - method returns. |
93 | | - """ |
94 | | - original_method_return_value = MagicMock() |
95 | | - |
96 | | - class OriginalMockClient(_MockClientBase): |
97 | | - """ |
98 | | - This mock client always returns the mock original_method_return_value when a query |
99 | | - is executed. This can be used to simulate successful GraphQL queries. |
100 | | - """ |
101 | | - |
102 | | - execute = MagicMock( |
103 | | - spec=Client.execute, return_value=original_method_return_value |
104 | | - ) |
105 | | - |
106 | | - original_execute_method = OriginalMockClient.execute |
107 | | - |
108 | | - with patch( |
109 | | - "sentry_sdk.integrations.gql.gql.Client", new=OriginalMockClient |
110 | | - ) as PatchedMockClient: # noqa: N806 |
111 | | - # Below line should patch the PatchedMockClient with Sentry SDK magic |
112 | | - GQLIntegration.setup_once() |
113 | | - |
114 | | - # We expect GQLIntegration.setup_once to patch the execute method. |
115 | | - assert ( |
116 | | - PatchedMockClient.execute is not original_execute_method |
117 | | - ), "execute method not patched" |
118 | | - |
119 | | - # Now, let's instantiate a client and send it a query. Original execute still should get called. |
120 | | - mock_query = MagicMock(spec=DocumentNode) |
121 | | - client_instance = PatchedMockClient() |
122 | | - patched_method_return_value = client_instance.execute(mock_query) |
123 | | - |
124 | | - # Here, we check that the original execute was called |
125 | | - original_execute_method.assert_called_once_with(client_instance, mock_query) |
126 | | - |
127 | | - # Also, let's verify that the patched execute returns the expected value. |
128 | | - assert ( |
129 | | - patched_method_return_value is original_method_return_value |
130 | | - ), "pathced execute method returns a different value than the original execute method" |
131 | | - |
132 | | - |
133 | | -@patch("sentry_sdk.integrations.gql.event_from_exception") |
134 | | -@patch("sentry_sdk.integrations.gql.Hub") |
135 | | -def test_patched_gql_execute_captures_and_reraises_graphql_exception( |
136 | | - mock_hub, mock_event_from_exception |
137 | | -): |
138 | | - """ |
139 | | - Unit test which ensures that in the case that calling the execute method results in a |
140 | | - TransportQueryError (which gql raises when a GraphQL error occurs), the patched method |
141 | | - captures the event on the current Hub and it reraises the error. |
142 | | - """ |
143 | | - mock_event_from_exception.return_value = (dict(), MagicMock()) |
144 | | - |
145 | | - class OriginalMockClient(_MockClientBase): |
146 | | - """ |
147 | | - This mock client always raises a TransportQueryError when a GraphQL query is attempted. |
148 | | - This simulates a GraphQL query which results in errors. |
149 | | - """ |
150 | | - |
151 | | - execute = MagicMock( |
152 | | - spec=Client.execute, side_effect=TransportQueryError("query failed") |
153 | | - ) |
154 | | - |
155 | | - with patch( |
156 | | - "sentry_sdk.integrations.gql.gql.Client", new=OriginalMockClient |
157 | | - ) as PatchedMockClient: # noqa: N806 |
158 | | - # Below line should patch the PatchedMockClient with Sentry SDK magic |
159 | | - GQLIntegration.setup_once() |
160 | | - |
161 | | - mock_query = MagicMock(spec=DocumentNode) |
162 | | - client_instance = PatchedMockClient() |
163 | | - |
164 | | - # The error should still get raised even though we have instrumented the execute method. |
165 | | - with pytest.raises(TransportQueryError): |
166 | | - client_instance.execute(mock_query) |
167 | | - |
168 | | - # However, we should have also captured the error on the hub. |
169 | | - mock_capture_event = mock_hub.current.capture_event |
170 | | - mock_capture_event.assert_called_once() |
171 | | - |
172 | | - |
173 | 70 | def test_real_gql_request_no_error(sentry_init, capture_events): |
174 | 71 | """ |
175 | 72 | Integration test verifying that the GQLIntegration works as expected with successful query. |
|
0 commit comments