|
1 | 1 | import os |
2 | | -from pathlib import Path |
3 | | -from tempfile import TemporaryDirectory |
4 | 2 | from time import time |
5 | 3 | from typing import Dict |
6 | | -from unittest import mock |
7 | 4 |
|
8 | | -import graphql |
9 | 5 | import pytest |
10 | 6 | import pytest_mock |
11 | 7 | from gql import Client |
@@ -35,88 +31,6 @@ def test_graphql_client_cache_cant_get_kili_version(mocker): |
35 | 31 | ) |
36 | 32 |
|
37 | 33 |
|
38 | | -@pytest.mark.parametrize( |
39 | | - "query", |
40 | | - [ |
41 | | - "query MyQuery { my_query_is_clearly_not_valid_according_to_the_kili_schema { id } }", |
42 | | - "query { projects { this_field_does_not_exist } }", |
43 | | - ], |
44 | | -) |
45 | | -def test_gql_bad_query_local_validation(query, mocker): |
46 | | - """Test gql validation against local schema.""" |
47 | | - api_endpoint = os.getenv( |
48 | | - "KILI_API_ENDPOINT", "https://cloud.kili-technology.com/api/label/v2/graphql" |
49 | | - ) |
50 | | - |
51 | | - # we need to remove "Authorization" api key from the header |
52 | | - # if not, the backend will refuse the introspection query |
53 | | - mocker.patch.object(GraphQLClient, "_get_headers", return_value={}) |
54 | | - |
55 | | - client = GraphQLClient( |
56 | | - endpoint=api_endpoint, |
57 | | - api_key="", |
58 | | - client_name=GraphQLClientName.SDK, |
59 | | - verify=True, |
60 | | - http_client=HttpClient( |
61 | | - kili_endpoint="https://fake_endpoint.kili-technology.com", api_key="", verify=True |
62 | | - ), |
63 | | - ) |
64 | | - |
65 | | - with pytest.raises(GraphQLError) as exc_info: |
66 | | - client.execute(query) |
67 | | - |
68 | | - assert isinstance(exc_info.value.__cause__, graphql.GraphQLError) |
69 | | - |
70 | | - |
71 | | -def test_graphql_client_cache(mocker): |
72 | | - with TemporaryDirectory() as temp_dir: |
73 | | - schema_path = Path(temp_dir) / "schema.graphql" |
74 | | - mocker.patch.object(GraphQLClient, "_get_graphql_schema_path", return_value=schema_path) |
75 | | - |
76 | | - api_endpoint = os.getenv( |
77 | | - "KILI_API_ENDPOINT", "https://cloud.kili-technology.com/api/label/v2/graphql" |
78 | | - ) |
79 | | - |
80 | | - # we need to remove "Authorization" api key from the header |
81 | | - # if not, the backend will refuse the introspection query |
82 | | - mocker.patch.object(GraphQLClient, "_get_headers", return_value={}) |
83 | | - |
84 | | - # initialize a client with schema caching enabled |
85 | | - _ = GraphQLClient( |
86 | | - endpoint=api_endpoint, |
87 | | - api_key="", |
88 | | - client_name=GraphQLClientName.SDK, |
89 | | - verify=True, |
90 | | - http_client=HttpClient( |
91 | | - kili_endpoint="https://fake_endpoint.kili-technology.com", api_key="", verify=True |
92 | | - ), |
93 | | - enable_schema_caching=True, |
94 | | - graphql_schema_cache_dir=temp_dir, |
95 | | - ) |
96 | | - |
97 | | - # schema should be cached |
98 | | - files_in_cache_dir = list(schema_path.parent.glob("*")) |
99 | | - assert schema_path.is_file(), f"{files_in_cache_dir}, {schema_path}" |
100 | | - assert schema_path.stat().st_size > 0 |
101 | | - |
102 | | - # check that the schema is not fetched again when we initialize a new client |
103 | | - with mock.patch("kili.core.graphql.graphql_client.print_schema") as mocked_print_schema: |
104 | | - _ = GraphQLClient( |
105 | | - endpoint=api_endpoint, |
106 | | - api_key="", |
107 | | - client_name=GraphQLClientName.SDK, |
108 | | - verify=True, |
109 | | - http_client=HttpClient( |
110 | | - kili_endpoint="https://fake_endpoint.kili-technology.com", |
111 | | - api_key="", |
112 | | - verify=True, |
113 | | - ), |
114 | | - enable_schema_caching=True, |
115 | | - graphql_schema_cache_dir=temp_dir, |
116 | | - ) |
117 | | - mocked_print_schema.assert_not_called() |
118 | | - |
119 | | - |
120 | 34 | def test_schema_caching_requires_cache_dir(): |
121 | 35 | with pytest.raises( |
122 | 36 | Exception, match="must specify a cache directory if you want to enable schema caching" |
@@ -190,50 +104,6 @@ def mock_execute(*args, **kwargs): |
190 | 104 | assert last_call_timestamp - before_last_call_timestamp > 1 |
191 | 105 |
|
192 | 106 |
|
193 | | -def test_given_gql_client_when_i_send_wrong_query_then_it_refreshes_the_schema_and_retry_once_only( |
194 | | - mocker: pytest_mock.MockerFixture, |
195 | | -): |
196 | | - api_endpoint = os.getenv( |
197 | | - "KILI_API_ENDPOINT", "https://cloud.kili-technology.com/api/label/v2/graphql" |
198 | | - ) |
199 | | - |
200 | | - # we need to remove "Authorization" api key from the header |
201 | | - # if not, the backend will refuse the introspection query |
202 | | - mocker.patch.object(GraphQLClient, "_get_headers", return_value={}) |
203 | | - |
204 | | - gql_execute_spy = mocker.spy(Client, "execute") |
205 | | - |
206 | | - # Given |
207 | | - client = GraphQLClient( |
208 | | - endpoint=api_endpoint, |
209 | | - api_key="", |
210 | | - client_name=GraphQLClientName.SDK, |
211 | | - http_client=HttpClient( |
212 | | - kili_endpoint="https://fake_endpoint.kili-technology.com", api_key="", verify=True |
213 | | - ), |
214 | | - enable_schema_caching=True, |
215 | | - ) |
216 | | - |
217 | | - # When I send wrong query, the local validation fails |
218 | | - # the client fetches a fresh schema from the backend |
219 | | - # and we retry the query once only |
220 | | - with pytest.raises( |
221 | | - GraphQLError, |
222 | | - match=( |
223 | | - 'GraphQL error: "Cannot query field' |
224 | | - " 'my_query_is_clearly_not_valid_according_to_the_kili_schema'" |
225 | | - ), |
226 | | - ): |
227 | | - client.execute( |
228 | | - query=( |
229 | | - "query MyQuery { my_query_is_clearly_not_valid_according_to_the_kili_schema {" |
230 | | - " id } }" |
231 | | - ) |
232 | | - ) |
233 | | - |
234 | | - assert gql_execute_spy.call_count == 2 # first call + one retry |
235 | | - |
236 | | - |
237 | 107 | def test_given_gql_client_when_the_server_refuses_wrong_query_then_it_does_no_retry( |
238 | 108 | mocker: pytest_mock.MockerFixture, |
239 | 109 | ): |
|
0 commit comments