test: rearrange this for covering extensive number of codes

This commit is contained in:
SuHun Han 2017-03-10 19:20:59 +09:00 committed by ssut
parent 93e2cd9043
commit afc4006f3e
6 changed files with 173 additions and 38 deletions

18
pytest.ini Normal file
View File

@ -0,0 +1,18 @@
[pytest]
addopts = -v
omit =
tests/*
[run]
include = googletrans/*
[report]
exclude_lines =
pragma: no cover
def __repr__
if self.debug:
if settings.DEBUG
raise AssertionError
raise NotImplementedError
if 0:
if __name__ == .__main__.:

View File

@ -1,38 +0,0 @@
# -*- coding: utf-8 -*-
import unittest
from googletrans import translator, conversion
class TranslateTests(unittest.TestCase):
def setUp(self):
pass
def test_to_json(self):
text = '[,,"en",,,,0.96954316,,[["en"],,[0.96954316]]]'
approx = [None, None, 'en', None, None, None, 0.96954316, None, [['en'], None, [0.96954316]]]
assert conversion.format_json(text) == approx
def test_latin_to_english(self):
result = translator.translate('veritas lux mea', src='la', dest='en')
assert result.text == 'The truth is my light'
def test_unicode(self):
result = translator.translate('안녕하세요.', src='ko', dest='ja')
assert result.text == u'こんにちは。'
def test_list_translation(self):
translations = translator.translate(['The quick brown fox', 'jumps over', 'the lazy dog'],
src='en', dest='ko')
assert translations[0].text == u'빠른 갈색 여우'
assert translations[1].text == u'이상 점프'
assert translations[2].text == u'게으른 개'
def test_language_detection(self):
ko = translator.detect('한국어')
assert ko.lang == 'ko'
en = translator.detect('English')
assert en.lang == 'en'
if __name__ == '__main__':
unittest.main()

7
tests/conftest.py Normal file
View File

@ -0,0 +1,7 @@
from pytest import fixture
@fixture
def translator():
from googletrans import Translator
return Translator()

66
tests/test_client.py Normal file
View File

@ -0,0 +1,66 @@
# -*- coding: utf-8 -*-
from pytest import raises
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'こんにちは。'
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):
ko = translator.detect('한국어')
en = translator.detect('English')
assert ko.lang == 'ko'
assert en.lang == 'en'
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)
assert result.text == 'Hello'
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):
translator.translate(*args)

58
tests/test_gtoken.py Normal file
View File

@ -0,0 +1,58 @@
# -*- coding: utf-8 -*-
from googletrans import gtoken
from pytest import fixture
@fixture
def acquirer():
return gtoken.TokenAcquirer()
def test_acquire_token(acquirer):
text = 'test'
result = acquirer.do(text)
assert result
def test_acquire_token_ascii_less_than_2048(acquirer):
text = u'Ѐ'
result = acquirer.do(text)
assert result
def test_acquire_token_ascii_matches_special_condition(acquirer):
text = chr(55296) + chr(56320)
result = acquirer.do(text)
assert result
def test_acquire_token_ascii_else(acquirer):
text = u''
result = acquirer.do(text)
assert result
def test_reuse_valid_token(acquirer):
text = 'test'
first = acquirer.do(text)
second = acquirer.do(text)
assert first == second
def test_map_lazy_return(acquirer):
value = True
func = acquirer._lazy(value)
assert callable(func)
assert func() == value

24
tests/test_utils.py Normal file
View File

@ -0,0 +1,24 @@
from googletrans import utils
from pytest import raises
def test_format_json():
text = '[,,"en",,,,0.96954316,,[["en"],,[0.96954316]]]'
result = utils.format_json(text)
assert result == [None, None, 'en', None, None, None, 0.96954316, None,
[['en'], None, [0.96954316]]]
def test_format_malformed_json():
text = '[,,"en",,,,0.96954316,,[["en"],,0.96954316]]]'
with raises(ValueError):
utils.format_json(text)
def test_rshift():
value, n = 1000, 3
result = utils.rshift(value, n)
assert result == 125