Skip to content

Commit

Permalink
✅ test(test_translator): update tests for async translator
Browse files Browse the repository at this point in the history
- refactor TestTranslator to use IsolatedAsyncioTestCase
- add async test cases for AutoIncreaseAsyncTranslator
  • Loading branch information
awwaawwa committed Dec 23, 2024
1 parent 2dcc250 commit 026b611
Showing 1 changed file with 36 additions and 2 deletions.
38 changes: 36 additions & 2 deletions test/test_translator.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import unittest
from pdf2zh.translator import BaseTranslator
from pdf2zh import cache
import asyncio


class AutoIncreaseTranslator(BaseTranslator):
Expand All @@ -12,7 +13,16 @@ def do_translate(self, text):
return str(self.n)


class TestTranslator(unittest.TestCase):
class AutoIncreaseAsyncTranslator(BaseTranslator):
name = "auto_increase_async"
n = 0

async def do_translate_async(self, text):
self.n += 1
return str(self.n)


class TestTranslator(unittest.IsolatedAsyncioTestCase):
def setUp(self):
self.test_db = cache.init_test_db()

Expand Down Expand Up @@ -67,8 +77,32 @@ def test_add_cache_impact_parameters(self):
another_result = translator.translate(text)
self.assertNotEqual(second_result, another_result)

def test_base_translator_throw(self):
async def test_base_translator_throw(self):
translator = BaseTranslator("en", "zh", "test")
with self.assertRaises(NotImplementedError):
translator.do_translate("Hello World")
with self.assertRaises(NotImplementedError):
await translator.do_translate_async("Hello World")

async def test_async_and_sync_translator(self):
async_translator = AutoIncreaseAsyncTranslator("en", "zh", "test")
sync_translator = AutoIncreaseTranslator("en", "zh", "test")

# call async from async
self.assertEqual(await async_translator.translate_async("Hello World"), "1")

# call sync from async
self.assertEqual(await sync_translator.translate_async("Hello World"), "1")

# call sync from sync
self.assertEqual(sync_translator.translate("Hello World"), "1")

# call async from sync
with self.assertRaises(NotImplementedError):
self.assertEqual(async_translator.translate("Hello World", ignore_cache=True), "1")

async def test_call_async_from_sync_inside_running_loop(self):
translator = AutoIncreaseAsyncTranslator("en", "zh", "test")
with self.assertRaises(NotImplementedError):
translator.translate("Hello World")

Expand Down

0 comments on commit 026b611

Please sign in to comment.