-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathtest_settings.py
355 lines (280 loc) · 11 KB
/
test_settings.py
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
import os
from pathlib import Path
from textwrap import dedent
import pytest
from ariadne_codegen.client_generators.dependencies import (
async_base_client,
async_base_client_open_telemetry,
base_client,
base_client_open_telemetry,
)
from ariadne_codegen.config import ClientSettings, GraphQLSchemaSettings
from ariadne_codegen.exceptions import InvalidConfiguration
def test_client_settings_instance_is_created_with_base_client_defined_in_file(tmp_path):
schema_path = tmp_path / "schema.graphql"
schema_path.touch()
queries_path = tmp_path / "queries.graphql"
queries_path.touch()
base_client_file_content = """
class BaseClient:
pass
"""
base_client_file_path = tmp_path / "base_client.py"
base_client_file_path.write_text(dedent(base_client_file_content))
ClientSettings(
schema_path=schema_path.as_posix(),
queries_path=queries_path.as_posix(),
base_client_name="BaseClient",
base_client_file_path=base_client_file_path.as_posix(),
)
def test_client_settings_with_invalid_base_client_file_raises_invalid_configuration(
tmp_path,
):
schema_path = tmp_path / "schema.graphql"
schema_path.touch()
queries_path = tmp_path / "queries.graphql"
queries_path.touch()
base_client_file_path = tmp_path / "invalid_file.txt"
base_client_file_path.write_text("invalid content")
with pytest.raises(InvalidConfiguration):
ClientSettings(
schema_path=schema_path.as_posix(),
queries_path=queries_path.as_posix(),
base_client_file_path=base_client_file_path.as_posix(),
base_client_name="BaseClient",
)
def test_client_settings_with_invalid_base_client_name_raises_configuration_exception(
tmp_path,
):
schema_path = tmp_path / "schema.graphql"
schema_path.touch()
queries_path = tmp_path / "queries.graphql"
queries_path.touch()
base_client_file_content = """
class BaseClient:
pass
"""
base_client_file_path = tmp_path / "base_client.py"
base_client_file_path.write_text(dedent(base_client_file_content))
with pytest.raises(InvalidConfiguration):
ClientSettings(
schema_path=schema_path.as_posix(),
queries_path=queries_path.as_posix(),
base_client_name="OtherClient",
base_client_file_path=base_client_file_path.as_posix(),
)
@pytest.mark.parametrize(
"async_client, opentelemetry_client, expected_name, expected_path",
[
(
True,
True,
"AsyncBaseClientOpenTelemetry",
async_base_client_open_telemetry.__file__,
),
(True, False, "AsyncBaseClient", async_base_client.__file__),
(False, True, "BaseClientOpenTelemetry", base_client_open_telemetry.__file__),
(False, False, "BaseClient", base_client.__file__),
],
)
def test_client_settings_sets_correct_default_values_for_base_client_name_and_path(
tmp_path, async_client, opentelemetry_client, expected_name, expected_path
):
schema_path = tmp_path / "schema.graphql"
schema_path.touch()
queries_path = tmp_path / "queries.graphql"
queries_path.touch()
settings = ClientSettings(
schema_path=schema_path,
queries_path=queries_path,
async_client=async_client,
opentelemetry_client=opentelemetry_client,
)
assert settings.base_client_name == expected_name
assert settings.base_client_file_path == Path(expected_path).as_posix()
def test_client_settings_without_schema_path_with_remote_schema_url_is_valid(tmp_path):
queries_path = tmp_path / "queries.graphql"
queries_path.touch()
settings = ClientSettings(
remote_schema_url="http://testserver/graphq/", queries_path=queries_path
)
assert not settings.schema_path
def test_client_settings_without_schema_path_or_remote_schema_url_raises_exception(
tmp_path,
):
queries_path = tmp_path / "queries.graphql"
queries_path.touch()
with pytest.raises(InvalidConfiguration):
ClientSettings(queries_path=queries_path)
@pytest.mark.parametrize(
"configured_header, expected_header",
[
("$TEST_VAR", "test_value"),
("Bearer: $TEST_VAR", "Bearer: test_value"),
("Bearer: ${TEST_VAR}", "Bearer: test_value"),
pytest.param(
"$NOT_SET_VAR",
"",
marks=pytest.mark.xfail(raises=InvalidConfiguration),
),
],
)
def test_client_settings_resolves_env_variable_for_remote_schema_header_with_prefix(
tmp_path,
mocker,
configured_header,
expected_header,
):
queries_path = tmp_path / "queries.graphql"
queries_path.touch()
mocker.patch.dict(os.environ, {"TEST_VAR": "test_value"})
settings = ClientSettings(
queries_path=queries_path,
remote_schema_url="https://test",
remote_schema_headers={"Authorization": configured_header},
)
assert settings.remote_schema_headers["Authorization"] == expected_header
@pytest.mark.parametrize(
"configured_url, expected_url",
[
("$TEST_VAR", "test_value"),
("https://${TEST_VAR}/graphql", "https://test_value/graphql"),
("https://$TEST_VAR/graphql", "https://test_value/graphql"),
("https://TEST_VAR/graphql", "https://TEST_VAR/graphql"),
pytest.param(
"https://${NOT_SET_VAR}/graphql",
"",
marks=pytest.mark.xfail(raises=InvalidConfiguration),
),
],
)
def test_client_settings_resolves_env_variable_for_remote_schema(
tmp_path,
mocker,
configured_url,
expected_url,
):
queries_path = tmp_path / "queries.graphql"
queries_path.touch()
mocker.patch.dict(os.environ, {"TEST_VAR": "test_value"})
settings = ClientSettings(
queries_path=queries_path,
remote_schema_url=configured_url,
)
assert settings.remote_schema_url == expected_url
def test_client_settings_doesnt_resolve_remote_schema_header_without_prefix(tmp_path):
queries_path = tmp_path / "queries.graphql"
queries_path.touch()
settings = ClientSettings(
queries_path=queries_path,
remote_schema_url="https://test",
remote_schema_headers={"Authorization": "no_prefix"},
)
assert settings.remote_schema_headers["Authorization"] == "no_prefix"
def test_client_settings_raises_invalid_configuration_for_not_found_env_variable(
tmp_path,
):
queries_path = tmp_path / "queries.graphql"
queries_path.touch()
with pytest.raises(InvalidConfiguration):
ClientSettings(
queries_path=queries_path,
remote_schema_url="https://test",
remote_schema_headers={"Authorization": "$TEST_VAR"},
)
def test_client_settings_used_settings_message_returns_string_with_summary_of_data(
tmp_path,
):
schema_path = tmp_path / "schema.graphql"
schema_path.touch()
queries_path = tmp_path / "queries.graphql"
queries_path.touch()
base_client_file_content = """
class BaseClient:
pass
"""
base_client_file_path = tmp_path / "base_client.py"
base_client_file_path.write_text(dedent(base_client_file_content))
settings = ClientSettings(
schema_path=schema_path.as_posix(),
queries_path=queries_path.as_posix(),
base_client_name="BaseClient",
base_client_file_path=base_client_file_path.as_posix(),
)
result = settings.used_settings_message
assert settings.schema_path in result
assert settings.queries_path in result
assert settings.target_package_name in result
assert settings.target_package_path in result
assert settings.client_name in result
assert settings.base_client_name in result
assert settings.base_client_file_path in result
assert settings.enums_module_name in result
assert settings.input_types_module_name in result
def test_graphq_schema_settings_without_remote_schema_url_with_schema_path_is_valid(
tmp_path,
):
schema_path = tmp_path / "schema.graphql"
schema_path.touch()
settings = GraphQLSchemaSettings(schema_path=schema_path.as_posix())
assert not settings.remote_schema_url
def test_graphq_schema_settings_without_schema_path_with_remote_schema_url_is_valid():
settings = GraphQLSchemaSettings(remote_schema_url="http://testserver/graphq/")
assert not settings.schema_path
def test_graphql_schema_settings_with_target_file_path_with_py_extension_is_valid():
settings = GraphQLSchemaSettings(
remote_schema_url="http://testserver/graphq/",
target_file_path="schema_file.py",
)
assert settings.target_file_path == "schema_file.py"
assert settings.target_file_format == "py"
def test_graphql_schema_settings_with_target_file_with_graphql_extension_is_valid():
settings = GraphQLSchemaSettings(
remote_schema_url="http://testserver/graphq/",
target_file_path="schema_file.graphql",
)
assert settings.target_file_path == "schema_file.graphql"
assert settings.target_file_format == "graphql"
def test_graphql_schema_settings_with_target_file_path_with_gql_extension_is_valid():
settings = GraphQLSchemaSettings(
remote_schema_url="http://testserver/graphq/",
target_file_path="schema_file.gql",
)
assert settings.target_file_path == "schema_file.gql"
assert settings.target_file_format == "gql"
def test_graphql_schema_settings_target_file_format_is_lowercased():
settings = GraphQLSchemaSettings(
remote_schema_url="http://testserver/graphq/",
target_file_path="schema_file.GQL",
)
assert settings.target_file_format == "gql"
def test_graphq_schema_settings_without_schema_path_or_remote_schema_url_is_not_valid():
with pytest.raises(InvalidConfiguration):
GraphQLSchemaSettings()
def test_graphql_schema_settings_raises_invalid_configuration_for_invalid_schema_path():
with pytest.raises(InvalidConfiguration):
GraphQLSchemaSettings(schema_path="not_exisitng.graphql")
def test_graphql_schema_settings_with_target_file_missing_extension_raises_exception():
with pytest.raises(InvalidConfiguration):
GraphQLSchemaSettings(
remote_schema_url="http://testserver/graphq/",
target_file_path="schema_file",
)
def test_graphql_schema_settings_with_target_file_invalid_extension_raises_exception():
with pytest.raises(InvalidConfiguration):
GraphQLSchemaSettings(
remote_schema_url="http://testserver/graphq/",
target_file_path="schema_file.invalid",
)
def test_graphql_schema_settings_with_invalid_schema_variable_name_raises_exception():
with pytest.raises(InvalidConfiguration):
GraphQLSchemaSettings(
remote_schema_url="http://testserver/graphq/",
schema_variable_name="!schema?",
)
def test_graphql_schema_settings_with_invalid_type_map_variable_name_raises_exception():
with pytest.raises(InvalidConfiguration):
GraphQLSchemaSettings(
remote_schema_url="http://testserver/graphq/",
type_map_variable_name="1type_map",
)