Skip to content
Navigation Menu
{{ message }}
-
Notifications
You must be signed in to change notification settings - Fork 87
Expand file tree
/
Copy pathtest_cli.py
More file actions
425 lines (351 loc) · 14.6 KB
/
Copy pathtest_cli.py
File metadata and controls
425 lines (351 loc) · 14.6 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
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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
# Copyright 2022 DeepL SE (https://www.deepl.com)
# Use of this source code is governed by an MIT
# license that can be found in the LICENSE file.
from click.testing import CliRunner
from .conftest import example_text, needs_real_server
# flake8: noqa: F401
from deepl import __main__
import deepl
import pathlib
import pytest
import re
from unittest.mock import patch
main_function = deepl.__main__
@pytest.fixture
def runner(server):
env = {
"DEEPL_SERVER_URL": server.server_url,
"DEEPL_AUTH_KEY": server.auth_key,
}
print(f"env: {env}")
return CliRunner(env=env)
def test_help(runner):
result = runner.invoke(main_function, "--help")
assert result.exit_code == 0, f"exit: {result.exit_code}\n {result.output}"
assert "usage" in result.output
def test_version(runner):
result = runner.invoke(main_function, "--version")
assert result.exit_code == 0, f"exit: {result.exit_code}\n {result.output}"
assert "deepl-python v" in result.output
version_regex = re.compile(r"deepl-python v\d+\.\d+\.\d+")
assert version_regex.match(result.output) is not None
def test_verbose(runner):
# verbose = info
result = runner.invoke(main_function, "--verbose usage")
assert result.exit_code == 0, f"exit: {result.exit_code}\n {result.output}"
assert "Request to DeepL API" in result.output
# verbose = debug
result = runner.invoke(main_function, "-vv usage")
assert result.exit_code == 0, f"exit: {result.exit_code}\n {result.output}"
assert "Request to DeepL API" in result.output
assert "Request details" in result.output
def test_no_auth(runner):
result = runner.invoke(
main_function, "usage", env={"DEEPL_AUTH_KEY": None}
)
assert result.exit_code == 1, f"exit: {result.exit_code}\n {result.output}"
assert "DEEPL_AUTH_KEY" in result.output
# Unfortunately there is no secure way to use the keyring module in our CI,
# so we have to mock the module's behavior here
# For the reason, see https://github.com/jaraco/keyring/issues/477
@patch("deepl.__main__._optional_import")
def test_keyring_auth(import_mock, runner):
mocked_keyring = {
"deepl": {"DEEPL_AUTH_KEY": runner.env["DEEPL_AUTH_KEY"]}
}
def get_pw_mock(service_name, username):
return mocked_keyring[service_name][username]
import_mock.return_value.get_password.side_effect = get_pw_mock
result = runner.invoke(
main_function, "usage", env={"DEEPL_AUTH_KEY": None}
)
assert result.exit_code == 0, f"exit: {result.exit_code}\n {result.output}"
assert "Usage this billing period" in result.output
@patch("importlib.import_module")
def test_no_auth_no_keyring(mock, runner):
mock.side_effect = ImportError("Keyring module not available in this test")
result = runner.invoke(
main_function, "usage", env={"DEEPL_AUTH_KEY": None}
)
assert result.exit_code == 1, f"exit: {result.exit_code}\n {result.output}"
assert "DEEPL_AUTH_KEY" in result.output
@patch("importlib.import_module")
def test_env_auth_no_keyring(mock, runner):
mock.side_effect = ImportError("Keyring module not available in this test")
result = runner.invoke(main_function, "usage")
assert result.exit_code == 0, f"exit: {result.exit_code}\n {result.output}"
assert "Usage this billing period" in result.output
def test_no_command(runner):
result = runner.invoke(main_function, "")
assert result.exit_code == 1, f"exit: {result.exit_code}\n {result.output}"
assert "command is required" in result.output
def test_usage(runner):
result = runner.invoke(main_function, "usage")
assert result.exit_code == 0, f"exit: {result.exit_code}\n {result.output}"
assert "Usage this billing period" in result.output
def test_languages(runner):
result = runner.invoke(main_function, "languages")
assert result.exit_code == 0, f"exit: {result.exit_code}\n {result.output}"
assert "Source languages" in result.output
assert "Target languages" in result.output
assert "DE: German" in result.output
assert "EN: English" in result.output
result = runner.invoke(deepl.__main__, "languages --glossary")
assert result.exit_code == 0, f"exit: {result.exit_code}\n {result.output}"
assert "supported for glossaries" in result.output
assert "de, en" in result.output
def test_text(runner):
result = runner.invoke(
main_function, 'text --to DE "proton beam" --show-detected-source'
)
assert result.exit_code == 0, f"exit: {result.exit_code}\n {result.output}"
assert example_text["DE"] in result.output
assert "Detected source" in result.output
# Test text options
extra_options = [
("--formality more", "'formality': 'more'"),
("--formality prefer_less", "'formality': 'prefer_less'"),
("--split-sentences 0", "'split_sentences': '0'"),
("--preserve-formatting", "'preserve_formatting': True"),
("--tag-handling xml", "'tag_handling': 'xml'"),
("--outline-detection-off", "'outline_detection': False"),
(
"--ignore-tags a,b --ignore-tags c",
"'ignore_tags': ['a', 'b', 'c']",
),
(
"--splitting-tags a,b --splitting-tags c",
"'splitting_tags': ['a', 'b', 'c']",
),
(
"--non-splitting-tags a,b --non-splitting-tags c",
"'non_splitting_tags': ['a', 'b', 'c']",
),
]
for args, search_str in extra_options:
result = runner.invoke(
main_function, f'-vv text --to DE "proton beam" {args}'
)
assert (
result.exit_code == 0
), f"exit: {result.exit_code}\n {result.output}"
request_details = next(
line
for line in result.output.splitlines()
if line.startswith("Request details data")
)
assert search_str in request_details
def test_text_stdin(runner):
result = runner.invoke(
main_function,
"text --to DE --show-detected-source -",
input=example_text["EN"],
)
assert result.exit_code == 0, f"exit: {result.exit_code}\n {result.output}"
assert example_text["DE"] in result.output
assert "Detected source" in result.output
@needs_real_server
def test_text_preserve_formatting(runner):
result = runner.invoke(
main_function, 'text --to DE --preserve-formatting "proton beam"'
)
assert result.exit_code == 0, f"exit: {result.exit_code}\n {result.output}"
assert example_text["DE"].lower() in result.output
def test_text_split_sentences(runner):
result = runner.invoke(
main_function,
'-vv text --to DE --split-sentences nonewlines "proton beam"',
)
assert result.exit_code == 0, f"exit: {result.exit_code}\n {result.output}"
# Check split_sentences parameter is sent in HTTP request
regex = re.compile("Request details.*split_sentences.*nonewlines.*")
assert any(
regex.match(line) is not None for line in result.output.split("\n")
), f"output:\n{result.output}"
def test_text_tags(runner):
result = runner.invoke(
main_function,
"-vv text --to DE --tag-handling xml --splitting-tags split "
'--ignore-tags a,b --ignore-tags c --ignore-tags d "proton beam"',
)
assert result.exit_code == 0, f"exit: {result.exit_code}\n {result.output}"
# Check ignore_tags parameter is sent in HTTP request
regex = re.compile(
"Request details.*'ignore_tags': \\['a', 'b', 'c', 'd']"
)
assert any(
regex.match(line) is not None for line in result.output.split("\n")
), f"output:\n{result.output}"
# Check splitting_tags parameter is sent in HTTP request
regex = re.compile("Request details.*'splitting_tags': \\['split']")
assert any(
regex.match(line) is not None for line in result.output.split("\n")
), f"output:\n{result.output}"
def test_text_html_tag_handling(runner):
result = runner.invoke(
main_function,
'-vv text --to DE --tag-handling html "<html><p>Test</p></html>"',
)
assert result.exit_code == 0, f"exit: {result.exit_code}\n {result.output}"
def test_document(runner, tmpdir):
tmpdir = pathlib.Path(tmpdir)
output_dir = tmpdir / "output"
input_document = tmpdir / "document.txt"
input_document.write_text(example_text["EN"])
output_document = output_dir / "document.txt"
result = runner.invoke(
main_function, f"-vv document --to DE {input_document} {output_dir}"
)
assert result.exit_code == 0, f"exit: {result.exit_code}\n {result.output}"
assert example_text["DE"] == output_document.read_text()
def test_document_occupied_output(runner, tmpdir):
tmpdir = pathlib.Path(tmpdir)
output_dir = tmpdir / "output"
input_document = tmpdir / "document.txt"
input_document.write_text(example_text["EN"])
# Create a file in place of the output directory
output_dir.touch()
result = runner.invoke(
main_function, f"-vv document --to DE {input_document} {output_dir}"
)
assert result.exit_code == 1, f"exit: {result.exit_code}\n {result.output}"
assert "already exists" in result.output
def test_invalid_document(runner, tmpdir):
tmpdir = pathlib.Path(tmpdir)
output_dir = tmpdir / "output"
input_document = tmpdir / "document.invalid"
input_document.write_text(example_text["EN"])
result = runner.invoke(
main_function, f"-vv document --to DE {input_document} {output_dir}"
)
assert result.exit_code == 1, f"exit: {result.exit_code}\n {result.output}"
assert "Invalid file" in result.output or "file extension" in result.output
def test_glossary_no_subcommand(runner):
result = runner.invoke(main_function, "glossary")
assert result.exit_code == 1, f"exit: {result.exit_code}\n {result.output}"
assert "subcommand is required" in result.output
def test_glossary_create(
runner,
glossary_name,
tmpdir,
cleanup_matching_glossaries,
example_glossary_csv,
):
name_cli = f"{glossary_name}-cli"
name_stdin = f"{glossary_name}-stdin"
name_file = f"{glossary_name}-file"
name_csv = f"{glossary_name}-csv"
entries = {"Hallo": "Hello", "Maler": "Artist"}
entries_tsv = deepl.convert_dict_to_tsv(entries)
entries_cli = "\n".join(f"{s}={t}" for s, t in entries.items())
file = tmpdir / "glossary_entries"
file.write(entries_tsv)
try:
result = runner.invoke(
main_function,
f'-vv glossary create --name "{name_cli}" --from DE --to EN '
f"{entries_cli}",
)
assert (
result.exit_code == 0
), f"exit: {result.exit_code}\n {result.output}"
result = runner.invoke(
main_function,
f'-vv glossary create --name "{name_stdin}" --from DE --to EN -',
input=entries_tsv,
)
assert (
result.exit_code == 0
), f"exit: {result.exit_code}\n {result.output}"
result = runner.invoke(
main_function,
f'-vv glossary create --name "{name_file}" --from DE --to EN '
f"--file {file}",
)
assert (
result.exit_code == 0
), f"exit: {result.exit_code}\n {result.output}"
result = runner.invoke(
main_function,
f'-vv glossary create --name "{name_csv}" --from EN --to DE '
f"--file {example_glossary_csv} --csv",
)
assert (
result.exit_code == 0
), f"exit: {result.exit_code}\n {result.output}"
result = runner.invoke(main_function, "-vv glossary list")
assert (
result.exit_code == 0
), f"exit: {result.exit_code}\n {result.output}"
assert name_cli in result.output
assert name_stdin in result.output
assert name_file in result.output
assert name_csv in result.output
# Cannot use --file option together with entries
result = runner.invoke(
main_function,
f'-vv glossary create --name "{name_file}" --from DE --to EN '
f"--file {file} {entries_cli}",
)
assert (
result.exit_code == 1
), f"exit: {result.exit_code}\n {result.output}"
assert "--file argument" in result.output
finally:
cleanup_matching_glossaries(
lambda glossary: glossary.name
in [name_file, name_cli, name_stdin, name_csv]
)
def test_glossary_get(translator, runner, glossary_manager):
with glossary_manager() as created_glossary:
created_id = created_glossary.glossary_id
result = runner.invoke(main_function, f"-vv glossary get {created_id}")
print(result.output)
assert (
result.exit_code == 0
), f"exit: {result.exit_code}\n {result.output}"
assert created_id in result.output
assert created_glossary.name in result.output
def test_glossary_list(translator, runner, glossary_manager):
with glossary_manager(glossary_name_suffix="1") as g1, glossary_manager(
glossary_name_suffix="2"
) as g2, glossary_manager(glossary_name_suffix="3") as g3:
glossary_list = [g1, g2, g3]
result = runner.invoke(main_function, "-vv glossary list")
assert (
result.exit_code == 0
), f"exit: {result.exit_code}\n {result.output}"
for glossary in glossary_list:
assert glossary.name in result.output
def test_glossary_entries(translator, runner, glossary_manager):
entries = {"Hallo": "Hello", "Maler": "Artist"}
with glossary_manager(entries=entries) as created_glossary:
created_id = created_glossary.glossary_id
result = runner.invoke(
main_function, f"-vv glossary entries {created_id}"
)
assert (
result.exit_code == 0
), f"exit: {result.exit_code}\n {result.output}"
for source, target in entries.items():
assert f"{source}\t{target}" in result.output
def test_glossary_delete(translator, runner, glossary_manager):
with glossary_manager() as created_glossary:
created_id = created_glossary.glossary_id
result = runner.invoke(main_function, "glossary list")
assert (
result.exit_code == 0
), f"exit: {result.exit_code}\n {result.output}"
assert created_id in result.output
# Remove the created glossary
result = runner.invoke(
main_function, f'glossary delete "{created_id}"'
)
assert (
result.exit_code == 0
), f"exit: {result.exit_code}\n {result.output}"
result = runner.invoke(main_function, "glossary list")
assert (
result.exit_code == 0
), f"exit: {result.exit_code}\n {result.output}"
assert created_id not in result.output
You can’t perform that action at this time.
