Skip to content

Commit

Permalink
test: Added test cases for Azure ChatGPT.
Browse files Browse the repository at this point in the history
  • Loading branch information
bookfere committed Oct 11, 2023
1 parent d65ba7c commit 51bcae0
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 3 deletions.
36 changes: 33 additions & 3 deletions tests/test_engine.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import re
import json
import unittest
from unittest.mock import patch
from types import GeneratorType
from unittest.mock import patch, Mock

from ..engines.base import Base
from ..engines.deepl import DeeplTranslate
from ..engines.chatgpt import AzureChatgptTranslate
from ..engines.custom import (
create_engine_template, load_engine_data, CustomTranslate)

Expand Down Expand Up @@ -41,8 +43,8 @@ def setUp(self):
def test_get_usage(self, mock_browser):
result = mock_browser.return_value.response.return_value.read \
.return_value.decode.return_value.strip

result.return_value = '{"character_count": 30, "character_limit": 100}'

self.assertEqual(
_('{} total, {} used, {} left').format(100, 30, 70),
self.translator.get_usage(),)
Expand All @@ -53,9 +55,9 @@ def test_get_usage(self, mock_browser):
def test_translate(self, mock_browser):
result = mock_browser.return_value.response.return_value.read \
.return_value.decode.return_value.strip

result.return_value = '{"translations":[{' \
'"detected_source_language":"EN","text":"你好世界!"}]}'

self.assertEqual('你好世界!', self.translator.translate('Hello World!'))

# bad response
Expand All @@ -68,6 +70,34 @@ def test_translate(self, mock_browser):
str(e.exception))


class TestAzureChatgptTranslate(unittest.TestCase):
def setUp(self):
AzureChatgptTranslate.set_config({'api_keys': ['a', 'b', 'c']})
AzureChatgptTranslate.lang_codes = {
'source': {'English': 'EN'},
'target': {'Chinese': 'ZH'},
}

self.translator = AzureChatgptTranslate()
self.translator.set_source_lang('English')
self.translator.set_target_lang('Chinese')

@patch('calibre_plugins.ebook_translator.engines.base.Browser')
def test_translate(self, mock_browser):
template = b'data: {"choices":[{"delta":{"content":"%b"}}]}'
mock_response = Mock()
mock_response.readline.side_effect = [
template % i.encode() for i in '你好世界!'] \
+ ['data: [DONE]'.encode()]
mock_browser.return_value.response.return_value = mock_response

# TODO: validate the request information.

result = self.translator.translate('Hello World!')
self.assertIsInstance(result, GeneratorType)
self.assertEqual('你好世界!', ''.join(result))


class TestFunction(unittest.TestCase):
def test_create_engine_template(self):
expect = """{
Expand Down
3 changes: 3 additions & 0 deletions tests/test_translation.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
from ..engines.deepl import DeeplTranslate


load_translations()


class TestGlossary(unittest.TestCase):
@patch('calibre_plugins.ebook_translator.lib.translation.open')
def test_load_from_file(self, mock_open):
Expand Down

0 comments on commit 51bcae0

Please sign in to comment.