Saminul
commited on
Commit
·
9a69d6b
1
Parent(s):
5a8d2be
added payload check in LibreTranslator.translate and added tests in test_libre.py
Browse files- deep_translator/libre.py +13 -4
- tests/test_libre.py +49 -0
deep_translator/libre.py
CHANGED
|
@@ -10,7 +10,8 @@ from .constants import BASE_URLS,LIBRE_LANGUAGES_TO_CODES, LIBRE_CODES_TO_LANGUA
|
|
| 10 |
from .exceptions import (ServerException,
|
| 11 |
TranslationNotFound,
|
| 12 |
LanguageNotSupportedException,
|
| 13 |
-
AuthorizationException
|
|
|
|
| 14 |
|
| 15 |
|
| 16 |
class LibreTranslator(BaseTranslator):
|
|
@@ -31,7 +32,10 @@ class LibreTranslator(BaseTranslator):
|
|
| 31 |
raise ServerException(401)
|
| 32 |
self.__base_url = base_url
|
| 33 |
self.api_key = api_key
|
| 34 |
-
|
|
|
|
|
|
|
|
|
|
| 35 |
self.target = self._map_language_to_code(target)
|
| 36 |
|
| 37 |
|
|
@@ -62,7 +66,10 @@ class LibreTranslator(BaseTranslator):
|
|
| 62 |
@param language: a string for 1 language
|
| 63 |
@return: bool or raise an Exception
|
| 64 |
"""
|
| 65 |
-
|
|
|
|
|
|
|
|
|
|
| 66 |
|
| 67 |
def translate(self, text, **kwargs):
|
| 68 |
"""
|
|
@@ -71,6 +78,9 @@ class LibreTranslator(BaseTranslator):
|
|
| 71 |
@return: str: translated text
|
| 72 |
"""
|
| 73 |
# Create the request parameters.
|
|
|
|
|
|
|
|
|
|
| 74 |
translate_endpoint = 'translate'
|
| 75 |
params = {
|
| 76 |
"q": text,
|
|
@@ -95,4 +105,3 @@ class LibreTranslator(BaseTranslator):
|
|
| 95 |
raise TranslationNotFound(text)
|
| 96 |
# Process and return the response.
|
| 97 |
return res['translatedText']
|
| 98 |
-
|
|
|
|
| 10 |
from .exceptions import (ServerException,
|
| 11 |
TranslationNotFound,
|
| 12 |
LanguageNotSupportedException,
|
| 13 |
+
AuthorizationException,
|
| 14 |
+
NotValidPayload)
|
| 15 |
|
| 16 |
|
| 17 |
class LibreTranslator(BaseTranslator):
|
|
|
|
| 32 |
raise ServerException(401)
|
| 33 |
self.__base_url = base_url
|
| 34 |
self.api_key = api_key
|
| 35 |
+
if source == "auto":
|
| 36 |
+
self.source = "auto"
|
| 37 |
+
else:
|
| 38 |
+
self.source = self._map_language_to_code(source)
|
| 39 |
self.target = self._map_language_to_code(target)
|
| 40 |
|
| 41 |
|
|
|
|
| 66 |
@param language: a string for 1 language
|
| 67 |
@return: bool or raise an Exception
|
| 68 |
"""
|
| 69 |
+
if language == 'auto' or language in self._languages.keys() or language in self._languages.values():
|
| 70 |
+
return True
|
| 71 |
+
else:
|
| 72 |
+
raise LanguageNotSupportedException(language)
|
| 73 |
|
| 74 |
def translate(self, text, **kwargs):
|
| 75 |
"""
|
|
|
|
| 78 |
@return: str: translated text
|
| 79 |
"""
|
| 80 |
# Create the request parameters.
|
| 81 |
+
if type(text) != str or text == "":
|
| 82 |
+
raise NotValidPayload(text)
|
| 83 |
+
|
| 84 |
translate_endpoint = 'translate'
|
| 85 |
params = {
|
| 86 |
"q": text,
|
|
|
|
| 105 |
raise TranslationNotFound(text)
|
| 106 |
# Process and return the response.
|
| 107 |
return res['translatedText']
|
|
|
tests/test_libre.py
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python
|
| 2 |
+
|
| 3 |
+
"""Tests for `deep_translator` package."""
|
| 4 |
+
|
| 5 |
+
import pytest
|
| 6 |
+
from deep_translator import exceptions, LibreTranslator
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
@pytest.fixture
|
| 10 |
+
def libre():
|
| 11 |
+
return LibreTranslator(source="en", target='fr')
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
def test_content(libre):
|
| 15 |
+
"""Sample pytest test function with the pytest fixture as an argument."""
|
| 16 |
+
# from bs4 import BeautifulSoup
|
| 17 |
+
# assert 'GitHub' in BeautifulSoup(response.content).title.string
|
| 18 |
+
assert libre.translate(text='good') is not None
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
def test_inputs():
|
| 22 |
+
with pytest.raises(exceptions.LanguageNotSupportedException):
|
| 23 |
+
LibreTranslator(source="", target="")
|
| 24 |
+
|
| 25 |
+
with pytest.raises(exceptions.LanguageNotSupportedException):
|
| 26 |
+
LibreTranslator(source="auto", target="nothing")
|
| 27 |
+
|
| 28 |
+
l1 = LibreTranslator("en", "fr")
|
| 29 |
+
l2 = LibreTranslator("english", "french")
|
| 30 |
+
assert l1.source == l2.source
|
| 31 |
+
assert l1.target == l2.target
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
def test_payload(libre):
|
| 35 |
+
with pytest.raises(exceptions.NotValidPayload):
|
| 36 |
+
libre.translate("")
|
| 37 |
+
|
| 38 |
+
with pytest.raises(exceptions.NotValidPayload):
|
| 39 |
+
libre.translate(123)
|
| 40 |
+
|
| 41 |
+
with pytest.raises(exceptions.NotValidPayload):
|
| 42 |
+
libre.translate({})
|
| 43 |
+
|
| 44 |
+
with pytest.raises(exceptions.NotValidPayload):
|
| 45 |
+
libre.translate([])
|
| 46 |
+
|
| 47 |
+
|
| 48 |
+
def test_one_character_words():
|
| 49 |
+
assert LibreTranslator(source='es', target='en').translate('y') == 'and'
|