RubénEu
commited on
Commit
·
62575a2
1
Parent(s):
41a7e8f
Added DeepL tests
Browse files
deep_translator/tests/test_deepl.py
ADDED
@@ -0,0 +1,29 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import pytest
|
2 |
+
from unittest.mock import Mock, PropertyMock, patch
|
3 |
+
from deep_translator.deepl import DeepL
|
4 |
+
from deep_translator.exceptions import AuthorizationException
|
5 |
+
|
6 |
+
|
7 |
+
@patch('deep_translator.deepl.requests')
|
8 |
+
def test_simple_translation(mock_requests):
|
9 |
+
translator = DeepL(api_key='imagine-this-is-an-valid-api-key')
|
10 |
+
# Set the request response mock.
|
11 |
+
mock_response = Mock()
|
12 |
+
mock_response.status_code = 200
|
13 |
+
mock_response.json.return_value = {
|
14 |
+
"translations": [{
|
15 |
+
"text": "hola"
|
16 |
+
}]
|
17 |
+
}
|
18 |
+
mock_requests.get.return_value = mock_response
|
19 |
+
translation = translator.translate('en', 'es', 'hello')
|
20 |
+
assert translation == 'hola'
|
21 |
+
|
22 |
+
|
23 |
+
@patch('deep_translator.deepl.requests.get')
|
24 |
+
def test_wrong_api_key(mock_requests):
|
25 |
+
translator = DeepL(api_key='this-is-a-wrong-api-key!')
|
26 |
+
# Set the response status_code only.
|
27 |
+
mock_requests.return_value = Mock(status_code=403)
|
28 |
+
with pytest.raises(AuthorizationException):
|
29 |
+
translator.translate('en', 'es', 'Hello')
|