Skip to content

Commit 057cb23

Browse files
committed
Adding providers tests
1 parent de68dba commit 057cb23

File tree

7 files changed

+142
-43
lines changed

7 files changed

+142
-43
lines changed

python_gpt_po/tests/providers/__init__.py

Whitespace-only changes.
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import pytest
2+
from unittest.mock import MagicMock
3+
4+
from python_gpt_po.models.provider_clients import ProviderClients
5+
from python_gpt_po.services.providers.anthropic_provider import AnthropicProvider
6+
7+
@pytest.fixture
8+
def mock_provider_clients() -> ProviderClients:
9+
"""Mock provider clients for testing."""
10+
clients = ProviderClients()
11+
clients.anthropic_client = MagicMock()
12+
clients.anthropic_client.api_key = "sk-ant-mock-key"
13+
return clients
14+
15+
def test_translate(mock_provider_clients: ProviderClients) -> None:
16+
"""Test bulk translation with Anthropic."""
17+
# Setup mock response
18+
mock_chatcompletion = MagicMock()
19+
mock_chatcompletion.content = [MagicMock()]
20+
mock_chatcompletion.content[0].text = '["Bonjour", "Monde", "Bienvenue dans notre application", "Au revoir"]'
21+
mock_provider_clients.anthropic_client.messages.create.return_value = mock_chatcompletion
22+
23+
provider = AnthropicProvider()
24+
translations = provider.translate(
25+
provider_clients=mock_provider_clients,
26+
model="gpt-4",
27+
content="['Hello', 'World', 'Welcome to our application', 'Goodbye']"
28+
)
29+
30+
assert translations == '["Bonjour", "Monde", "Bienvenue dans notre application", "Au revoir"]'
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import pytest
2+
from unittest.mock import MagicMock
3+
4+
from python_gpt_po.models.provider_clients import ProviderClients
5+
from python_gpt_po.services.providers.azure_openai_provider import AzureOpenAIProvider
6+
7+
@pytest.fixture
8+
def mock_provider_clients() -> ProviderClients:
9+
"""Mock provider clients for testing."""
10+
clients = ProviderClients()
11+
clients.azure_openai_client = MagicMock()
12+
clients.azure_openai_client.api_key = "sk-aoi-mock-key"
13+
return clients
14+
15+
def test_translate(mock_provider_clients: ProviderClients) -> None:
16+
"""Test bulk translation with Azure OpenAI."""
17+
# Setup mock response
18+
mock_chatcompletion = MagicMock()
19+
mock_chatcompletion.choices = [MagicMock()]
20+
mock_chatcompletion.choices[0].message.content = '["Bonjour", "Monde", "Bienvenue dans notre application", "Au revoir"]'
21+
mock_provider_clients.azure_openai_client.chat.completions.create.return_value = mock_chatcompletion
22+
23+
provider = AzureOpenAIProvider()
24+
translations = provider.translate(
25+
provider_clients=mock_provider_clients,
26+
model="gpt-4",
27+
content="['Hello', 'World', 'Welcome to our application', 'Goodbye']"
28+
)
29+
30+
assert translations == '["Bonjour", "Monde", "Bienvenue dans notre application", "Au revoir"]'
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import pytest
2+
from unittest.mock import MagicMock, patch
3+
4+
from python_gpt_po.models.provider_clients import ProviderClients
5+
from python_gpt_po.services.providers.deepseek_provider import DeepSeekProvider
6+
7+
DEEPSEEK_TRANSLATION_RESPONSE = {
8+
"choices": [
9+
{
10+
"message": {
11+
"content": "```json\n[\"Bonjour\", \"Monde\", \"Bienvenue dans notre application\", \"Au revoir\"]\n```"
12+
}
13+
}
14+
]
15+
}
16+
17+
@pytest.fixture
18+
def mock_provider_clients() -> ProviderClients:
19+
"""Mock provider clients for testing."""
20+
clients = ProviderClients()
21+
clients.deepseek_api_key = "sk-deepseek-mock-key"
22+
clients.deepseek_base_url = "https://api.deepseek.com/v1"
23+
return clients
24+
25+
@patch('python_gpt_po.services.providers.deepseek_provider.requests.post')
26+
def test_translate(mock_post: MagicMock, mock_provider_clients: ProviderClients) -> None:
27+
"""Test translation with DeepSeek."""
28+
# Setup mock response
29+
mock_response = MagicMock()
30+
mock_response.json.return_value = DEEPSEEK_TRANSLATION_RESPONSE
31+
mock_post.return_value = mock_response
32+
33+
provider = DeepSeekProvider()
34+
translations = provider.translate(
35+
provider_clients=mock_provider_clients,
36+
model="deepseek-chat",
37+
content="['Hello', 'World', 'Welcome to our application', 'Goodbye']"
38+
)
39+
40+
print(type(translations))
41+
assert translations == '```json\n["Bonjour", "Monde", "Bienvenue dans notre application", "Au revoir"]\n```'
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import pytest
2+
from unittest.mock import MagicMock
3+
4+
from python_gpt_po.models.provider_clients import ProviderClients
5+
from python_gpt_po.services.providers.openai_provider import OpenAIProvider
6+
7+
@pytest.fixture
8+
def mock_provider_clients() -> ProviderClients:
9+
"""Mock provider clients for testing."""
10+
clients = ProviderClients()
11+
clients.openai_client = MagicMock()
12+
return clients
13+
14+
def test_translate(mock_provider_clients: ProviderClients) -> None:
15+
"""Test bulk translation with OpenAI."""
16+
# Setup mock response
17+
mock_chatcompletion = MagicMock()
18+
mock_chatcompletion.choices = [MagicMock()]
19+
mock_chatcompletion.choices[0].message.content = '["Bonjour", "Monde", "Bienvenue dans notre application", "Au revoir"]'
20+
mock_provider_clients.openai_client.chat.completions.create.return_value = mock_chatcompletion
21+
22+
provider = OpenAIProvider()
23+
translations = provider.translate(
24+
provider_clients=mock_provider_clients,
25+
model="gpt-4",
26+
content="['Hello', 'World', 'Welcome to our application', 'Goodbye']"
27+
)
28+
29+
assert translations == '["Bonjour", "Monde", "Bienvenue dans notre application", "Au revoir"]'

python_gpt_po/tests/test_multi_provider.py

Lines changed: 6 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
from python_gpt_po.services.model_manager import ModelManager
1717
from python_gpt_po.services.po_file_handler import POFileHandler
1818
from python_gpt_po.services.translation_service import TranslationService
19+
from providers.test_deepseek_provider import DEEPSEEK_TRANSLATION_RESPONSE
1920

2021
logging.basicConfig(level=logging.INFO)
2122

@@ -99,38 +100,6 @@
99100
]
100101
}
101102

102-
# Translation responses for different providers
103-
OPENAI_TRANSLATION_RESPONSE = {
104-
"choices": [
105-
{
106-
"message": {
107-
"content": '["Bonjour", "Monde", "Bienvenue dans notre application", "Au revoir"]'
108-
}
109-
}
110-
]
111-
}
112-
113-
AZURE_OPENAI_TRANSLATION_RESPONSE = OPENAI_TRANSLATION_RESPONSE
114-
115-
ANTHROPIC_TRANSLATION_RESPONSE = {
116-
"content": [
117-
{
118-
"text": '["Bonjour", "Monde", "Bienvenue dans notre application", "Au revoir"]'
119-
}
120-
]
121-
}
122-
123-
DEEPSEEK_TRANSLATION_RESPONSE = {
124-
"choices": [
125-
{
126-
"message": {
127-
"content": "```json\n[\"Bonjour\", \"Monde\", \"Bienvenue dans notre application\", \"Au revoir\"]\n```"
128-
}
129-
}
130-
]
131-
}
132-
133-
134103
@pytest.fixture
135104
def temp_po_file(tmp_path: str) -> str:
136105
"""Create a temporary PO file for testing."""
@@ -227,7 +196,7 @@ def translation_service_deepseek(translation_config_deepseek: TranslationConfig)
227196

228197

229198
@patch('requests.get')
230-
def test_get_openai_models(mock_get, mock_provider_clients: ProviderClients):
199+
def test_get_openai_models(mock_get: MagicMock, mock_provider_clients: ProviderClients):
231200
"""Test getting OpenAI models."""
232201
# Setup mock response
233202
mock_response = MagicMock()
@@ -249,7 +218,7 @@ def test_get_openai_models(mock_get, mock_provider_clients: ProviderClients):
249218

250219

251220
@patch('requests.get')
252-
def test_get_ayure_openai_models(mock_get, mock_provider_clients: ProviderClients):
221+
def test_get_ayure_openai_models(mock_get: MagicMock, mock_provider_clients: ProviderClients):
253222
"""Test getting OpenAI models."""
254223
# Setup mock response
255224
mock_response = MagicMock()
@@ -415,7 +384,7 @@ def test_clean_json_response(translation_service_deepseek: TranslationService):
415384

416385

417386
@patch('polib.pofile')
418-
def test_process_po_file_all_providers(mock_pofile,
387+
def test_process_po_file_all_providers(mock_pofile: MagicMock,
419388
translation_service_openai: TranslationService,
420389
translation_service_anthropic: TranslationService,
421390
translation_service_deepseek: TranslationService,
@@ -462,7 +431,7 @@ def test_process_po_file_all_providers(mock_pofile,
462431

463432

464433
@patch('python_gpt_po.services.po_file_handler.POFileHandler.disable_fuzzy_translations')
465-
def test_fuzzy_flag_handling(mock_disable_fuzzy, translation_service_openai: TranslationService, temp_po_file):
434+
def test_fuzzy_flag_handling(mock_disable_fuzzy: MagicMock, translation_service_openai: TranslationService, temp_po_file: MagicMock):
466435
"""Test handling of fuzzy translations."""
467436
# Enable fuzzy flag
468437
translation_service_openai.config.flags.fuzzy = True
@@ -520,7 +489,7 @@ def test_validation_model_connection_all_providers(
520489

521490
@patch('os.walk')
522491
@patch('polib.pofile')
523-
def test_scan_and_process_po_files(mock_pofile, mock_walk, translation_service_openai: TranslationService):
492+
def test_scan_and_process_po_files(mock_pofile: MagicMock, mock_walk: MagicMock, translation_service_openai: TranslationService):
524493
"""Test scanning and processing PO files."""
525494
# Setup mock directory structure
526495
mock_walk.return_value = [

python_gpt_po/tests/test_po_translator.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ def fixture_mock_openai_client():
2828

2929

3030
@pytest.fixture(name='translation_config')
31-
def fixture_translation_config(mock_openai_client):
31+
def fixture_translation_config(mock_openai_client: MagicMock):
3232
"""
3333
Fixture to create a TranslationConfig instance.
3434
"""
@@ -50,7 +50,7 @@ def fixture_translation_config(mock_openai_client):
5050

5151

5252
@pytest.fixture(name='translation_service')
53-
def fixture_translation_service(translation_config):
53+
def fixture_translation_service(translation_config: MagicMock):
5454
"""
5555
Fixture to create a TranslationService instance.
5656
"""
@@ -65,14 +65,14 @@ def fixture_mock_po_file_handler():
6565
return MagicMock(spec=POFileHandler)
6666

6767

68-
def test_validate_openai_connection(translation_service):
68+
def test_validate_openai_connection(translation_service: MagicMock):
6969
"""Test to validate the connection."""
7070
# The new method is validate_provider_connection instead of validate_openai_connection
7171
assert translation_service.validate_provider_connection() is True
7272

7373

7474
@patch('python_gpt_po.services.po_file_handler.POFileHandler')
75-
def test_process_po_file(mock_po_file_handler_class, translation_service, tmp_path):
75+
def test_process_po_file(mock_po_file_handler_class: MagicMock, translation_service: MagicMock, tmp_path: MagicMock):
7676
"""
7777
Test the process_po_file method.
7878
"""
@@ -131,7 +131,7 @@ def mock_prepare(po_file_path, languages):
131131
translation_service._prepare_po_file = original_prepare
132132

133133

134-
def test_translate_bulk(translation_service, tmp_path):
134+
def test_translate_bulk(translation_service: MagicMock, tmp_path: MagicMock):
135135
"""Test the bulk translation functionality."""
136136
texts_to_translate = ["HR", "TENANT", "HEALTHCARE", "TRANSPORT", "SERVICES"]
137137
po_file_path = str(tmp_path / "django.po")
@@ -145,7 +145,7 @@ def test_translate_bulk(translation_service, tmp_path):
145145
assert translated_texts == ["HR", "Inquilino", "Salud", "Transporte", "Servicios"]
146146

147147

148-
def test_translate_single(translation_service):
148+
def test_translate_single(translation_service: MagicMock):
149149
"""Test the single translation functionality."""
150150
text_to_translate = "HEALTHCARE"
151151

0 commit comments

Comments
 (0)