-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
415 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
from typing import List | ||
from unittest import TestCase | ||
from unittest.mock import Mock, patch | ||
|
||
from hypha.apply.translate.translate import translate | ||
|
||
|
||
class TestTranslate(TestCase): | ||
@staticmethod | ||
def mocked_translate(string: str, from_code, to_code): | ||
"""Use pig latin for all test translations - ie. 'hypha is cool' -> 'yphahay isway oolcay' | ||
https://en.wikipedia.org/wiki/Pig_Latin | ||
""" | ||
vowels = {"a", "e", "i", "o", "u"} | ||
string = string.lower() | ||
pl = [ | ||
f"{word}way" if word[0] in vowels else f"{word[1:]}{word[0]}ay" | ||
for word in string.split() | ||
] | ||
return " ".join(pl) | ||
|
||
@staticmethod | ||
def mocked_get_available_translations(codes: List[str]): | ||
mocked_packages = [ | ||
Mock(from_code="ar", to_code="en"), | ||
Mock(from_code="fr", to_code="en"), | ||
Mock(from_code="en", to_code="ar"), | ||
Mock(from_code="zh", to_code="en"), | ||
Mock(from_code="en", to_code="fr"), | ||
] | ||
|
||
return list(filter(lambda x: x.from_code in codes, mocked_packages)) | ||
|
||
@classmethod | ||
def setUpClass(cls): | ||
"""Used to patch & mock all the methods called from argostranslate & hypha.apply.translate.utils""" | ||
|
||
cls.patcher = [ | ||
patch( | ||
"hypha.apply.translate.utils.get_available_translations", | ||
side_effect=cls.mocked_get_available_translations, | ||
), | ||
patch( | ||
"argostranslate.translate.translate", side_effect=cls.mocked_translate | ||
), | ||
] | ||
|
||
for patched in cls.patcher: | ||
patched.start() | ||
|
||
@classmethod | ||
def tearDownClass(cls): | ||
for patched in cls.patcher: | ||
patched.stop() | ||
|
||
def test_valid_translate(self): | ||
self.assertEqual(translate("hey there", "fr", "en"), "eyhay heretay") | ||
|
||
def test_duplicate_code_translate(self): | ||
with self.assertRaises(ValueError) as context: | ||
translate("hey there", "fr", "fr") | ||
|
||
self.assertEqual( | ||
"Translation from_code cannot match to_code", str(context.exception) | ||
) | ||
|
||
def test_invalid_code_translate(self): | ||
with self.assertRaises(ValueError) as context: | ||
translate("hey there", "test", "test2") | ||
|
||
self.assertIn("is not installed", str(context.exception)) |
Oops, something went wrong.