def test_extract_data(self): translator = GoogleTranslator() # Check the keys with invalid input basic_dictionary = { "translation": "", "original_text": "", "romanization": "", "src_lang": "", "match": 1.0, "has_typo": False, "extra": {} } self.assertEqual(translator._extract_data(1234), basic_dictionary) # Check with valid input inputs = [ [[["T1", "o1", "", "", 0], ["", "", "r1", "R2"]], "", "fr"], [[["t1", "O1", "", "", 0], ["", "", "r1"]], "", "en"], [[["t1", "o1", "", "", 0]], ["extra"], "en", "", "", "", 0.5, ["", "", "", "", "", True]], [[["t1", "o1", "", "", 0]], [[ "nouns", ["w1", "w2"], [["w1", ["t1", "t2"], "", 0.44], ["w2", ["t3", "t4"]]], "ooo", 1 ], [ "adjectives", ["a1"], [["a1", ["t1", "t5"], "", 0.05]], "ooo", 3 ], ["verbs", ["v1"], [["v1", ["t6"], "", 0.0001]], "ooo", 2]], "en", "", "", "", 1], [[["t1", "o1", "", "", 0]], [["nouns", ["w1"], [["w1", ["t1", "t2"], "", 0.666]], "ooo", 1], ["verbs", ["v1"], [["v1", ["t8"], "", 0.15]], "ooo", 2]], "en", "", "", "", 1], [], [ [ [ "Αυτή είναι μια πολύ μακρά σειρά που\n", "This is a very long line that\n", "", "", 0 ], [ "συνεχίζει στην επόμενη.", "continues to the next one.", "", "", 0 ], [ "", "", "Aftí eínai mia polý makrá seirá pou\nsynechízei stin epómeni." ] # Romanization ], "", "en" ], # New input type see https://github.com/MrS0m30n3/google-translate/issues/5 [[["t1", "o1", None, None, 0], [None, None, None, "r1"]], None, "ru", None, None, None, 0.666, None, [["ru"], None, [0.666], ["ru"]]], [[["t1", "o1", "", "", 0]], [[ "nouns", ["w1", "w2"], [["w1", ["t1", "t2"], "", 0.44], ["w2", ["t3", "t4"]]], "ooo", 1 ], ["adverbs", ["adv1"], [["adv1", ["t10"], "", 0.123]], "ooo", 4], [ "prepositions", ["pre1", "pre2"], [["pre1", ["t8", "t9"], "", 0.0001], ["pre2", ["t3"], "", 0.005]], "ooo", 5 ]], "en", "", "", "", 1] ] outputs = [{ "translation": "T1", "original_text": "o1", "romanization": "R2", "src_lang": "fr", "match": 1.0, "extra": {}, "has_typo": False }, { "translation": "t1", "original_text": "O1", "romanization": "O1", "src_lang": "en", "match": 1.0, "extra": {}, "has_typo": False }, { "translation": "t1", "original_text": "o1", "romanization": "o1", "src_lang": "en", "match": 0.5, "extra": {}, "has_typo": True }, { "translation": "t1", "original_text": "o1", "romanization": "o1", "src_lang": "en", "match": 1.0, "extra": { "nouns": { "w1": ["t1", "t2"], "w2": ["t3", "t4"] }, "adjectives": { "a1": ["t1", "t5"] }, "verbs": { "v1": ["t6"] } }, "has_typo": False }, { "translation": "t1", "original_text": "o1", "romanization": "o1", "src_lang": "en", "match": 1.0, "extra": { "nouns": { "w1": ["t1", "t2"] }, "verbs": { "v1": ["t8"] } }, "has_typo": False }, { "translation": "", "original_text": "", "romanization": "", "src_lang": "", "match": 1.0, "extra": {}, "has_typo": False }, { "translation": "Αυτή είναι μια πολύ μακρά σειρά που\nσυνεχίζει στην επόμενη.", "original_text": "This is a very long line that\ncontinues to the next one.", "romanization": "This is a very long line that\ncontinues to the next one.", "src_lang": "en", "match": 1.0, "extra": {}, "has_typo": False }, { "translation": "t1", "original_text": "o1", "romanization": "r1", "src_lang": "ru", "match": 0.666, "extra": {}, "has_typo": False }, { "translation": "t1", "original_text": "o1", "romanization": "o1", "src_lang": "en", "match": 1.0, "extra": { "nouns": { "w1": ["t1", "t2"], "w2": ["t3", "t4"] }, "adverbs": { "adv1": ["t10"] }, "prepositions": { "pre1": ["t8", "t9"], "pre2": ["t3"] } }, "has_typo": False }] for index, item in enumerate(inputs): self.assertEqual(translator._extract_data(item), outputs[index])
def setUp(self): self.translator = GoogleTranslator()
def test_detect_full(self): translator = GoogleTranslator() self.assertEqual(translator.detect("hi"), "english") self.assertEqual(translator.detect("犬"), "japanese") self.assertEqual(translator.detect("σπίτη"), "greek") # With typo
class TestGetInfoDict(unittest.TestCase): def setUp(self): self.translator = GoogleTranslator() @mock.patch.object(GoogleTranslator, "_validate_language") @mock.patch.object(GoogleTranslator, "_do_work") def test_get_info_dict_default_params(self, mock_do_work, mock_val_lang): self.assertEqual(self.translator.get_info_dict("hello", "de"), mock_do_work.return_value) mock_do_work.assert_called_once_with(self.translator._get_info, "hello", mock_val_lang.return_value, mock_val_lang.return_value, "text") mock_val_lang.assert_has_calls( [mock.call("auto"), mock.call("de", allow_auto=False)]) @mock.patch.object(GoogleTranslator, "_validate_language") @mock.patch.object(GoogleTranslator, "_do_work") def test_get_info_dict_non_default_params(self, mock_do_work, mock_val_lang): self.assertEqual( self.translator.get_info_dict("hello", "de", "en", "json"), mock_do_work.return_value) mock_do_work.assert_called_once_with(self.translator._get_info, "hello", mock_val_lang.return_value, mock_val_lang.return_value, "json") mock_val_lang.assert_has_calls( [mock.call("en"), mock.call("de", allow_auto=False)]) @unittest.skipUnless(TEST_FULL, b"TEST_FULL False") def test_get_info_dict_full(self): expected_output = { "original_text": "house", "extra": { "verbs": { "στεγάζω": ["house", "roof"], "εστιώ": ["house"] }, "nouns": { "σπίτι": ["home", "house"], "κατοικία": [ "residence", "house", "home", "dwelling", "domicile", "residency" ], "οικία": ["house", "home"], "οίκος": ["house", "home"], "βουλή": [ "parliament", "house", "legislature", "diet", "State house" ] } }, "has_typo": False, "romanization": "house", "src_lang": "en", "translation": "σπίτι", "match": 1 } self.assertEqual(self.translator.get_info_dict("house", "el"), expected_output)
def test_get_headers_default(self): translator = GoogleTranslator() self.assertListEqual(translator._get_headers(), translator._default_headers.items())