Skip to content

Commit 4cd410f

Browse files
committed
Fix tests; use str.format rather than string concatenation
Also add @EpicJhon to AUTHORS
1 parent cbae51c commit 4cd410f

File tree

3 files changed

+15
-9
lines changed

3 files changed

+15
-9
lines changed

AUTHORS.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,4 @@ Contributors (chronological)
2121
- Lage Ragnarsson `@lragnarsson <https://github.com/lragnarsson>`_
2222
- Jonathon Coe `@jonmcoe <https://github.com/jonmcoe>`_
2323
- Adrián López Calvo `@AdrianLC <https://github.com/AdrianLC>`_
24+
- Jhon Eslava `@EpicJhon <https://github.com/EpicJhon>`_

tests/test_translate.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,13 @@ def test_failed_translation_raises_not_translated(self, mock_request):
3838
assert_equal(mock_request.call_count, len(failed_responses))
3939

4040
@mock.patch("textblob.translate.Translator._request")
41-
def test_tk_parameter_included_in_requests(self, mock_request):
41+
def test_tk_parameter_included_in_request_url(self, mock_request):
4242
mock_request.return_value = '["Esta es una frase.","en"]'
4343
self.translator.translate(self.sentence, to_lang="es")
4444
assert_true(mock_request.called_once)
4545
args, kwargs = mock_request.call_args
46-
tk = kwargs['data']['tk']
47-
assert_true(re.match(r'^\d+\.\d+$', tk))
46+
url = args[0]
47+
assert_true(re.match('.+&tk=\d+\.\d+$', url))
4848

4949
@mock.patch('textblob.translate.Translator._request')
5050
def test_detect(self, mock_request):
@@ -84,7 +84,7 @@ def test_translate_spaces(self):
8484
assert_equal(to_en, "Hello, my name is Adrian! How are you? I am good")
8585

8686
def test_translate_missing_from_language_auto_detects(self):
87-
text = "Ich besorge das Bier"
87+
text = "Ich hole das Bier"
8888
translated = self.translator.translate(text, to_lang="en")
8989
assert_equal(translated, "I'll get the beer")
9090

@@ -94,16 +94,16 @@ def test_translate_text(self):
9494
assert_equal(translated, "Esta es una frase.")
9595
es_text = "Esta es una frase."
9696
to_en = self.translator.translate(es_text, from_lang="es", to_lang="en")
97-
assert_equal(to_en, "This is a sentence.")
97+
assert_equal(to_en, "This is a phrase.")
9898

9999
def test_translate_non_ascii(self):
100100
text = "ذات سيادة كاملة"
101101
translated = self.translator.translate(text, from_lang='ar', to_lang='en')
102102
assert_equal(translated, "With full sovereignty")
103103

104-
text2 = "美丽优于丑陋"
104+
text2 = "美丽比丑陋更好"
105105
translated = self.translator.translate(text2, from_lang="zh-CN", to_lang='en')
106-
assert_equal(translated, "Beautiful is better than ugly")
106+
assert_equal(translated, "Beauty is better than ugly")
107107

108108
@mock.patch('textblob.translate.Translator._validate_translation', mock.MagicMock())
109109
def test_translate_unicode_escape(self):

textblob/translate.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,12 @@ def translate(self, source, from_lang='auto', to_lang='en', host=None, type_=Non
4545
if PY2:
4646
source = source.encode('utf-8')
4747
data = {"q": source}
48-
url = self.url + "&sl=" + from_lang + "&tl=" + to_lang + "&hl=" + to_lang + "&tk=" + _calculate_tk(source)
48+
url = u'{url}&sl={from_lang}&tl={to_lang}&hl={to_lang}&tk={tk}'.format(
49+
url=self.url,
50+
from_lang=from_lang,
51+
to_lang=to_lang,
52+
tk=_calculate_tk(source),
53+
)
4954
response = self._request(url, host=host, type_=type_, data=data)
5055
result = json.loads(response)
5156
if isinstance(result, list):
@@ -63,7 +68,7 @@ def detect(self, source, host=None, type_=None):
6368
if len(source) < 3:
6469
raise TranslatorError('Must provide a string with at least 3 characters.')
6570
data = {"q": source}
66-
url = self.url + "&sl=auto&tk=" + _calculate_tk(source)
71+
url = u'{url}&sl=auto&tk={tk}'.format(url=self.url, tk=_calculate_tk(source))
6772
response = self._request(url, host=host, type_=type_, data=data)
6873
result, language = json.loads(response)
6974
return language

0 commit comments

Comments
 (0)