2017-03-10 11:20:59 +01:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
from pytest import raises
|
2017-12-30 14:56:50 +01:00
|
|
|
from requests.exceptions import ConnectionError
|
|
|
|
from requests.exceptions import ReadTimeout
|
2017-03-10 11:20:59 +01:00
|
|
|
|
2017-03-11 14:31:57 +01:00
|
|
|
from googletrans import Translator
|
|
|
|
|
|
|
|
|
|
|
|
def test_bind_multiple_service_urls():
|
|
|
|
service_urls = [
|
|
|
|
'translate.google.com',
|
|
|
|
'translate.google.co.kr',
|
|
|
|
]
|
|
|
|
|
|
|
|
translator = Translator(service_urls=service_urls)
|
|
|
|
assert translator.service_urls == service_urls
|
|
|
|
|
|
|
|
assert translator.translate('test', dest='ko')
|
|
|
|
assert translator.detect('Hello')
|
|
|
|
|
2017-03-10 11:20:59 +01:00
|
|
|
|
2017-08-08 13:42:22 +02:00
|
|
|
def test_source_language(translator):
|
|
|
|
result = translator.translate('안녕하세요.')
|
|
|
|
assert result.src == 'ko'
|
|
|
|
|
|
|
|
|
|
|
|
def test_pronunciation(translator):
|
|
|
|
result = translator.translate('안녕하세요.', dest='ja')
|
|
|
|
assert result.pronunciation == 'Kon\'nichiwa.'
|
|
|
|
|
|
|
|
|
2020-06-09 02:35:17 +02:00
|
|
|
def test_pronunciation_issue_175(translator):
|
|
|
|
result = translator.translate('Hello', src='en', dest='ru')
|
|
|
|
|
|
|
|
assert result.pronunciation is not None
|
|
|
|
|
|
|
|
|
2017-03-10 11:20:59 +01:00
|
|
|
def test_latin_to_english(translator):
|
|
|
|
result = translator.translate('veritas lux mea', src='la', dest='en')
|
|
|
|
assert result.text == 'The truth is my light'
|
|
|
|
|
|
|
|
|
|
|
|
def test_unicode(translator):
|
|
|
|
result = translator.translate(u'안녕하세요.', src='ko', dest='ja')
|
|
|
|
assert result.text == u'こんにちは。'
|
|
|
|
|
|
|
|
|
2020-06-08 17:26:29 +02:00
|
|
|
def test_emoji(translator):
|
|
|
|
result = translator.translate('😀')
|
|
|
|
assert result.text == u'😀'
|
|
|
|
|
|
|
|
|
2017-06-01 15:50:13 +02:00
|
|
|
def test_language_name(translator):
|
|
|
|
result = translator.translate(u'Hello', src='ENGLISH', dest='iRiSh')
|
|
|
|
assert result.text == u'Dia dhuit'
|
|
|
|
|
|
|
|
|
2017-08-09 13:58:08 +02:00
|
|
|
def test_language_name_with_space(translator):
|
|
|
|
result = translator.translate(u'Hello', src='en', dest='chinese (simplified)')
|
|
|
|
assert result.dest == 'zh-cn'
|
|
|
|
|
|
|
|
|
2017-06-01 15:50:13 +02:00
|
|
|
def test_language_rfc1766(translator):
|
|
|
|
result = translator.translate(u'luna', src='it_ch@euro', dest='en')
|
|
|
|
assert result.text == u'moon'
|
|
|
|
|
|
|
|
|
2017-04-03 16:05:19 +02:00
|
|
|
def test_special_chars(translator):
|
2017-04-07 15:43:48 +02:00
|
|
|
text = u"©×《》"
|
2017-04-03 16:05:19 +02:00
|
|
|
|
2017-04-07 15:43:48 +02:00
|
|
|
result = translator.translate(text, src='en', dest='en')
|
2017-04-03 16:05:19 +02:00
|
|
|
assert result.text == text
|
|
|
|
|
|
|
|
|
2017-03-10 11:20:59 +01:00
|
|
|
def test_translate_list(translator):
|
|
|
|
args = (['test', 'exam'], 'ko', 'en')
|
|
|
|
translations = translator.translate(*args)
|
|
|
|
|
|
|
|
assert translations[0].text == u'테스트'
|
|
|
|
assert translations[1].text == u'시험'
|
|
|
|
|
|
|
|
|
|
|
|
def test_detect_language(translator):
|
2017-03-10 14:53:00 +01:00
|
|
|
ko = translator.detect(u'한국어')
|
2017-03-10 11:20:59 +01:00
|
|
|
en = translator.detect('English')
|
|
|
|
|
|
|
|
assert ko.lang == 'ko'
|
|
|
|
assert en.lang == 'en'
|
|
|
|
|
2017-03-10 14:53:00 +01:00
|
|
|
|
2017-03-10 11:20:59 +01:00
|
|
|
def test_detect_list(translator):
|
|
|
|
items = [u'한국어', ' English']
|
|
|
|
|
|
|
|
result = translator.detect(items)
|
|
|
|
|
|
|
|
assert result[0].lang == 'ko'
|
|
|
|
assert result[1].lang == 'en'
|
|
|
|
|
|
|
|
|
|
|
|
def test_src_in_special_cases(translator):
|
|
|
|
args = ('Tere', 'en', 'ee')
|
|
|
|
|
|
|
|
result = translator.translate(*args)
|
|
|
|
|
2020-06-08 17:23:30 +02:00
|
|
|
assert result.text in ('Hello', 'Hi,')
|
2017-03-10 11:20:59 +01:00
|
|
|
|
|
|
|
|
|
|
|
def test_src_not_in_supported_languages(translator):
|
|
|
|
args = ('Hello', 'en', 'zzz')
|
|
|
|
|
|
|
|
with raises(ValueError):
|
|
|
|
translator.translate(*args)
|
|
|
|
|
|
|
|
|
|
|
|
def test_dest_in_special_cases(translator):
|
|
|
|
args = ('hello', 'ee', 'en')
|
|
|
|
|
|
|
|
result = translator.translate(*args)
|
|
|
|
|
|
|
|
assert result.text == 'Tere'
|
|
|
|
|
|
|
|
|
|
|
|
def test_dest_not_in_supported_languages(translator):
|
|
|
|
args = ('Hello', 'zzz', 'en')
|
|
|
|
|
|
|
|
with raises(ValueError):
|
2017-03-10 14:53:00 +01:00
|
|
|
translator.translate(*args)
|
2017-12-30 14:56:50 +01:00
|
|
|
|
|
|
|
|
|
|
|
def test_connection_timeout():
|
|
|
|
# Requests library specifies two timeouts: connection and read
|
|
|
|
|
2018-05-06 14:17:21 +02:00
|
|
|
with raises((ConnectionError, ReadTimeout)):
|
2017-12-30 14:56:50 +01:00
|
|
|
"""If a number is passed to timeout parameter, both connection
|
|
|
|
and read timeouts will be set to it.
|
|
|
|
Firstly, the connection timeout will fail.
|
|
|
|
"""
|
|
|
|
translator = Translator(timeout=0.00001)
|
|
|
|
translator.translate('안녕하세요.')
|
|
|
|
|
|
|
|
|
|
|
|
def test_read_timeout():
|
|
|
|
|
|
|
|
with raises(ReadTimeout):
|
|
|
|
translator = Translator(timeout=(10, 0.00001))
|
|
|
|
translator.translate('안녕하세요.')
|