Skip to content

Commit 1904841

Browse files
committed
tests: Add basic write API test
1 parent c512702 commit 1904841

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed

tests/conftest.py

+28
Original file line numberDiff line numberDiff line change
@@ -150,13 +150,41 @@ def _make_translator(server, auth_key=None, proxy=None):
150150
return translator
151151

152152

153+
def _make_deepl_client(server, auth_key=None, proxy=None):
154+
"""Returns a deepl.DeepLClient for the specified server test fixture.
155+
The server auth_key is used unless specifically overridden."""
156+
if auth_key is None:
157+
auth_key = server.auth_key
158+
deepl_client = deepl.DeepLClient(
159+
auth_key, server_url=server.server_url, proxy=proxy
160+
)
161+
162+
# If the server test fixture has custom headers defined, update the
163+
# translator headers and replace with the server headers dictionary.
164+
# Note: changing the underlying object is necessary because some tests
165+
# make changes to the headers during tests.
166+
if server.headers:
167+
server.headers.update(deepl_client.headers)
168+
deepl_client.headers = server.headers
169+
return deepl_client
170+
171+
153172
@pytest.fixture
154173
def translator(server):
155174
"""Returns a deepl.Translator to use in all tests taking a parameter
156175
'translator'."""
157176
return _make_translator(server)
158177

159178

179+
# TODO Replace all uses of `translator` above with this and delete the
180+
# duplicated code.
181+
@pytest.fixture
182+
def deepl_client(server):
183+
"""Returns a deepl.DeepLClient to use in all tests taking a parameter
184+
'deepl_client'."""
185+
return _make_deepl_client(server)
186+
187+
160188
@pytest.fixture
161189
def translator_with_random_auth_key(server):
162190
"""Returns a deepl.Translator with randomized authentication key,

tests/test_rephrase_text.py

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Copyright 2025 DeepL SE (https://www.deepl.com)
2+
# Use of this source code is governed by an MIT
3+
# license that can be found in the LICENSE file.
4+
5+
from .conftest import example_text
6+
7+
8+
def test_single_text(deepl_client):
9+
result = deepl_client.rephrase_text(
10+
example_text["EN"], target_lang="EN-GB"
11+
)
12+
assert result.detected_source_language.upper() == "EN"
13+
epsilon = 0.2
14+
n_original = len(example_text["EN"])
15+
n_improved = len(result.text)
16+
assert 1 / (1.0 + epsilon) <= n_improved / n_original <= (1.0 + epsilon)

0 commit comments

Comments
 (0)