harry
commited on
Commit
·
292c906
1
Parent(s):
d01fa51
update tencent translator api.
Browse files- deep_translator/tencent.py +2 -5
- tests/test_tencent.py +29 -10
deep_translator/tencent.py
CHANGED
@@ -22,7 +22,6 @@ from deep_translator.constants import (
|
|
22 |
)
|
23 |
from deep_translator.exceptions import (
|
24 |
ApiKeyException,
|
25 |
-
AuthorizationException,
|
26 |
ServerException,
|
27 |
TencentAPIerror,
|
28 |
TranslationNotFound,
|
@@ -108,9 +107,7 @@ class TencentTranslator(BaseTranslator):
|
|
108 |
except ConnectionError:
|
109 |
raise ServerException(503)
|
110 |
# If the answer is not success, raise server exception.
|
111 |
-
if response.status_code
|
112 |
-
raise AuthorizationException(self.secret_id)
|
113 |
-
elif response.status_code != 200:
|
114 |
raise ServerException(response.status_code)
|
115 |
# Get the response and check is not empty.
|
116 |
res = response.json()
|
@@ -136,5 +133,5 @@ if __name__ == "__main__":
|
|
136 |
d = TencentTranslator(
|
137 |
target="zh", secret_id="some-id", secret_key="some-key"
|
138 |
)
|
139 |
-
t = d.translate("
|
140 |
print("text: ", t)
|
|
|
22 |
)
|
23 |
from deep_translator.exceptions import (
|
24 |
ApiKeyException,
|
|
|
25 |
ServerException,
|
26 |
TencentAPIerror,
|
27 |
TranslationNotFound,
|
|
|
107 |
except ConnectionError:
|
108 |
raise ServerException(503)
|
109 |
# If the answer is not success, raise server exception.
|
110 |
+
if response.status_code != 200:
|
|
|
|
|
111 |
raise ServerException(response.status_code)
|
112 |
# Get the response and check is not empty.
|
113 |
res = response.json()
|
|
|
133 |
d = TencentTranslator(
|
134 |
target="zh", secret_id="some-id", secret_key="some-key"
|
135 |
)
|
136 |
+
t = d.translate("Hello\nHow are you?")
|
137 |
print("text: ", t)
|
tests/test_tencent.py
CHANGED
@@ -3,35 +3,54 @@ from unittest.mock import Mock, patch
|
|
3 |
import pytest
|
4 |
|
5 |
from deep_translator import TencentTranslator
|
6 |
-
from deep_translator.exceptions import
|
7 |
|
8 |
|
9 |
@patch("deep_translator.tencent.requests")
|
10 |
def test_simple_translation(mock_requests):
|
11 |
translator = TencentTranslator(
|
12 |
-
secret_id="this-is-an-valid-
|
|
|
13 |
source="en",
|
14 |
target="zh",
|
15 |
-
secret_key="this-is-an-valid-api-key",
|
16 |
)
|
17 |
# Set the request response mock.
|
18 |
mock_response = Mock()
|
19 |
mock_response.status_code = 200
|
20 |
-
mock_response.json.return_value = {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
21 |
mock_requests.get.return_value = mock_response
|
22 |
translation = translator.translate("hello")
|
23 |
assert translation == "你好"
|
24 |
|
25 |
|
26 |
-
@patch("deep_translator.tencent.requests
|
27 |
def test_wrong_api_key(mock_requests):
|
28 |
translator = TencentTranslator(
|
29 |
-
secret_id="this-is-a-wrong-
|
|
|
30 |
source="en",
|
31 |
target="zh",
|
32 |
-
secret_key="this-is-a-wrong-api-key",
|
33 |
)
|
34 |
-
|
35 |
-
|
36 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
37 |
translator.translate("Hello")
|
|
|
3 |
import pytest
|
4 |
|
5 |
from deep_translator import TencentTranslator
|
6 |
+
from deep_translator.exceptions import TencentAPIerror
|
7 |
|
8 |
|
9 |
@patch("deep_translator.tencent.requests")
|
10 |
def test_simple_translation(mock_requests):
|
11 |
translator = TencentTranslator(
|
12 |
+
secret_id="imagine-this-is-an-valid-secret-id",
|
13 |
+
secret_key="imagine-this-is-an-valid-secret-key",
|
14 |
source="en",
|
15 |
target="zh",
|
|
|
16 |
)
|
17 |
# Set the request response mock.
|
18 |
mock_response = Mock()
|
19 |
mock_response.status_code = 200
|
20 |
+
mock_response.json.return_value = {
|
21 |
+
"Response": {
|
22 |
+
"TargetText": "你好",
|
23 |
+
"Source": "en",
|
24 |
+
"Target": "zh",
|
25 |
+
"RequestId": "000ee211-f19e-4a34-a214-e2bb1122d248",
|
26 |
+
}
|
27 |
+
}
|
28 |
mock_requests.get.return_value = mock_response
|
29 |
translation = translator.translate("hello")
|
30 |
assert translation == "你好"
|
31 |
|
32 |
|
33 |
+
@patch("deep_translator.tencent.requests")
|
34 |
def test_wrong_api_key(mock_requests):
|
35 |
translator = TencentTranslator(
|
36 |
+
secret_id="imagine-this-is-a-wrong-secret-id",
|
37 |
+
secret_key="imagine-this-is-a-wrong-secret-id",
|
38 |
source="en",
|
39 |
target="zh",
|
|
|
40 |
)
|
41 |
+
|
42 |
+
mock_response = Mock()
|
43 |
+
mock_response.status_code = 200
|
44 |
+
mock_response.json.return_value = {
|
45 |
+
"Response": {
|
46 |
+
"Error": {
|
47 |
+
"Code": "AuthFailure.SignatureFailure",
|
48 |
+
"Message": "The provided credentials could not be validated. \
|
49 |
+
Please check your signature is correct.",
|
50 |
+
},
|
51 |
+
"RequestId": "ed93f3cb-f35e-473f-b9f3-0d451b8b79c6",
|
52 |
+
}
|
53 |
+
}
|
54 |
+
mock_requests.get.return_value = mock_response
|
55 |
+
with pytest.raises(TencentAPIerror):
|
56 |
translator.translate("Hello")
|