File size: 1,761 Bytes
4524f4c
 
 
 
 
7a05cd7
 
76aa3b2
4524f4c
 
 
 
 
 
 
78d0ff1
4524f4c
 
 
 
78d0ff1
4524f4c
 
 
76aa3b2
4524f4c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
78d0ff1
4524f4c
 
 
cb15d3a
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#!/usr/bin/env python

"""Tests for `deep_translator` package."""

import pytest

from deep_translator import GoogleTranslator, exceptions
from deep_translator.constants import GOOGLE_LANGUAGES_TO_CODES


@pytest.fixture
def google_translator():
    """Sample pytest fixture.
    See more at: http://doc.pytest.org/en/latest/fixture.html
    """
    return GoogleTranslator(target="en")


def test_content(google_translator):
    """Sample pytest test function with the pytest fixture as an argument."""
    assert google_translator.translate(text="좋은") == "good"


def test_abbreviations_and_languages_mapping():
    for abb, lang in GOOGLE_LANGUAGES_TO_CODES.items():
        g1 = GoogleTranslator(abb)
        g2 = GoogleTranslator(lang)
        assert g1._source == g2._source


def test_inputs():
    with pytest.raises(exceptions.InvalidSourceOrTargetLanguage):
        GoogleTranslator(source="", target="")

    with pytest.raises(exceptions.InvalidSourceOrTargetLanguage):
        GoogleTranslator(source="auto", target="")

    with pytest.raises(exceptions.InvalidSourceOrTargetLanguage):
        GoogleTranslator(source="", target="en")


def test_empty_text(google_translator):
    empty_txt = ""
    res = google_translator.translate(text=empty_txt)
    assert res == empty_txt


def test_payload(google_translator):
    with pytest.raises(exceptions.NotValidPayload):
        google_translator.translate(text={})

    with pytest.raises(exceptions.NotValidPayload):
        google_translator.translate(text=[])

    with pytest.raises(exceptions.NotValidLength):
        google_translator.translate("a" * 5001)


def test_one_character_words():
    assert (
        GoogleTranslator(source="es", target="en").translate("o") is not None
    )