diff --git a/pytest.ini b/pytest.ini new file mode 100644 index 0000000..3f22e41 --- /dev/null +++ b/pytest.ini @@ -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__.: diff --git a/tests.py b/tests.py deleted file mode 100644 index 0db481b..0000000 --- a/tests.py +++ /dev/null @@ -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() diff --git a/tests/conftest.py b/tests/conftest.py new file mode 100644 index 0000000..f1695ad --- /dev/null +++ b/tests/conftest.py @@ -0,0 +1,7 @@ +from pytest import fixture + + +@fixture +def translator(): + from googletrans import Translator + return Translator() \ No newline at end of file diff --git a/tests/test_client.py b/tests/test_client.py new file mode 100644 index 0000000..3d83623 --- /dev/null +++ b/tests/test_client.py @@ -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) \ No newline at end of file diff --git a/tests/test_gtoken.py b/tests/test_gtoken.py new file mode 100644 index 0000000..8649fd6 --- /dev/null +++ b/tests/test_gtoken.py @@ -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 \ No newline at end of file diff --git a/tests/test_utils.py b/tests/test_utils.py new file mode 100644 index 0000000..6ca1745 --- /dev/null +++ b/tests/test_utils.py @@ -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 \ No newline at end of file