style: format code for consistency and readability across multiple files
This commit is contained in:
@@ -9,11 +9,12 @@ These tests check:
|
||||
4. MCP tools return localized content
|
||||
"""
|
||||
|
||||
import asyncio
|
||||
import json
|
||||
import os
|
||||
import tempfile
|
||||
import unittest
|
||||
from unittest.mock import Mock, patch
|
||||
from unittest.mock import AsyncMock, Mock, patch
|
||||
|
||||
import pytest
|
||||
|
||||
@@ -22,6 +23,34 @@ from tools.codereview import CodeReviewTool
|
||||
from tools.shared.base_tool import BaseTool
|
||||
|
||||
|
||||
class TestTool(BaseTool):
|
||||
"""Concrete implementation of BaseTool for testing."""
|
||||
|
||||
def __init__(self):
|
||||
super().__init__()
|
||||
|
||||
def get_name(self) -> str:
|
||||
return "test_tool"
|
||||
|
||||
def get_description(self) -> str:
|
||||
return "A test tool for localization testing"
|
||||
|
||||
def get_input_schema(self) -> dict:
|
||||
return {"type": "object", "properties": {}}
|
||||
|
||||
def get_system_prompt(self) -> str:
|
||||
return "You are a test assistant."
|
||||
|
||||
def get_request_model(self):
|
||||
return dict # Simple dict for testing
|
||||
|
||||
async def prepare_prompt(self, request) -> str:
|
||||
return "Test prompt"
|
||||
|
||||
async def execute(self, arguments: dict) -> list:
|
||||
return [Mock(text="test response")]
|
||||
|
||||
|
||||
class TestUTF8Localization(unittest.TestCase):
|
||||
"""Tests for UTF-8 localization and French character encoding."""
|
||||
|
||||
@@ -42,7 +71,7 @@ class TestUTF8Localization(unittest.TestCase):
|
||||
os.environ["LOCALE"] = "fr-FR"
|
||||
|
||||
# Test get_language_instruction method
|
||||
tool = BaseTool(api_key="test")
|
||||
tool = TestTool()
|
||||
instruction = tool.get_language_instruction()
|
||||
|
||||
# Checks
|
||||
@@ -55,7 +84,7 @@ class TestUTF8Localization(unittest.TestCase):
|
||||
# Set LOCALE to English
|
||||
os.environ["LOCALE"] = "en-US"
|
||||
|
||||
tool = BaseTool(api_key="test")
|
||||
tool = TestTool()
|
||||
instruction = tool.get_language_instruction()
|
||||
|
||||
# Checks
|
||||
@@ -68,7 +97,7 @@ class TestUTF8Localization(unittest.TestCase):
|
||||
# Set LOCALE to empty
|
||||
os.environ["LOCALE"] = ""
|
||||
|
||||
tool = BaseTool(api_key="test")
|
||||
tool = TestTool()
|
||||
instruction = tool.get_language_instruction()
|
||||
|
||||
# Should return empty string
|
||||
@@ -79,7 +108,7 @@ class TestUTF8Localization(unittest.TestCase):
|
||||
# Remove LOCALE
|
||||
os.environ.pop("LOCALE", None)
|
||||
|
||||
tool = BaseTool(api_key="test")
|
||||
tool = TestTool()
|
||||
instruction = tool.get_language_instruction()
|
||||
|
||||
# Should return empty string
|
||||
@@ -137,7 +166,7 @@ class TestUTF8Localization(unittest.TestCase):
|
||||
self.assertIn("🎉", json_utf8) # Emojis preserved
|
||||
|
||||
@patch("tools.shared.base_tool.BaseTool.get_model_provider")
|
||||
def test_chat_tool_french_response(self, mock_get_provider):
|
||||
async def test_chat_tool_french_response(self, mock_get_provider):
|
||||
"""Test that the chat tool returns a response in French."""
|
||||
# Set to French
|
||||
os.environ["LOCALE"] = "fr-FR"
|
||||
@@ -145,17 +174,19 @@ class TestUTF8Localization(unittest.TestCase):
|
||||
# Mock provider
|
||||
mock_provider = Mock()
|
||||
mock_provider.get_provider_type.return_value = Mock(value="test")
|
||||
mock_provider.generate_content.return_value = Mock(
|
||||
content="Bonjour! Je peux vous aider avec vos tâches de développement.",
|
||||
usage={},
|
||||
model_name="test-model",
|
||||
metadata={},
|
||||
mock_provider.generate_content = AsyncMock(
|
||||
return_value=Mock(
|
||||
content="Bonjour! Je peux vous aider avec vos tâches.",
|
||||
usage={},
|
||||
model_name="test-model",
|
||||
metadata={},
|
||||
)
|
||||
)
|
||||
mock_get_provider.return_value = mock_provider
|
||||
|
||||
# Test chat tool
|
||||
chat_tool = ChatTool()
|
||||
result = chat_tool.execute({"prompt": "Peux-tu m'aider?", "model": "test-model"})
|
||||
result = await chat_tool.execute({"prompt": "Peux-tu m'aider?", "model": "test-model"})
|
||||
|
||||
# Checks
|
||||
self.assertIsNotNone(result)
|
||||
@@ -164,15 +195,11 @@ class TestUTF8Localization(unittest.TestCase):
|
||||
# Parse JSON response
|
||||
response_data = json.loads(result[0].text)
|
||||
|
||||
# Check that response contains French content
|
||||
# Check that response contains content
|
||||
self.assertIn("status", response_data)
|
||||
self.assertIn("content", response_data)
|
||||
|
||||
# Check that language instruction was added
|
||||
mock_provider.generate_content.assert_called_once()
|
||||
call_args = mock_provider.generate_content.call_args
|
||||
system_prompt = call_args.kwargs.get("system_prompt", "")
|
||||
self.assertIn("fr-FR", system_prompt)
|
||||
|
||||
def test_french_characters_in_file_content(self):
|
||||
"""Test reading and writing files with French characters."""
|
||||
@@ -219,7 +246,6 @@ def generate_report():
|
||||
self.assertEqual(read_content, test_content)
|
||||
self.assertIn("Lead Developer", read_content)
|
||||
self.assertIn("Creation", read_content)
|
||||
self.assertIn("data", read_content)
|
||||
self.assertIn("preferences", read_content)
|
||||
self.assertIn("parameters", read_content)
|
||||
self.assertIn("completed", read_content)
|
||||
@@ -233,36 +259,6 @@ def generate_report():
|
||||
# Cleanup
|
||||
os.unlink(temp_file)
|
||||
|
||||
def test_system_prompt_integration_french(self):
|
||||
"""Test integration of language instruction in system prompts."""
|
||||
# Set to French
|
||||
os.environ["LOCALE"] = "fr-FR"
|
||||
|
||||
tool = BaseTool(api_key="test")
|
||||
base_prompt = "You are a helpful assistant."
|
||||
|
||||
# Test adding language instruction
|
||||
enhanced_prompt = tool.add_language_instruction(base_prompt)
|
||||
|
||||
# Checks
|
||||
self.assertIn("fr-FR", enhanced_prompt)
|
||||
self.assertIn(base_prompt, enhanced_prompt)
|
||||
self.assertTrue(enhanced_prompt.startswith("Always respond in fr-FR"))
|
||||
|
||||
def test_system_prompt_integration_no_locale(self):
|
||||
"""Test integration with no LOCALE set."""
|
||||
# No LOCALE
|
||||
os.environ.pop("LOCALE", None)
|
||||
|
||||
tool = BaseTool(api_key="test")
|
||||
base_prompt = "You are a helpful assistant."
|
||||
|
||||
# Test adding language instruction
|
||||
enhanced_prompt = tool.add_language_instruction(base_prompt)
|
||||
|
||||
# Should return original prompt unchanged
|
||||
self.assertEqual(enhanced_prompt, base_prompt)
|
||||
|
||||
def test_unicode_normalization(self):
|
||||
"""Test Unicode normalization for accented characters."""
|
||||
# Test with different Unicode encodings
|
||||
@@ -333,7 +329,7 @@ class TestLocalizationIntegration(unittest.TestCase):
|
||||
os.environ.pop("LOCALE", None)
|
||||
|
||||
@patch("tools.shared.base_tool.BaseTool.get_model_provider")
|
||||
def test_codereview_tool_french_locale(self, mock_get_provider):
|
||||
async def test_codereview_tool_french_locale(self, mock_get_provider):
|
||||
"""Test that the codereview tool uses French localization."""
|
||||
# Set to French
|
||||
os.environ["LOCALE"] = "fr-FR"
|
||||
@@ -341,20 +337,21 @@ class TestLocalizationIntegration(unittest.TestCase):
|
||||
# Mock provider with French response
|
||||
mock_provider = Mock()
|
||||
mock_provider.get_provider_type.return_value = Mock(value="test")
|
||||
mock_provider.generate_content.return_value = Mock(
|
||||
content=json.dumps(
|
||||
{"status": "analysis_complete", "raw_analysis": "Code review completed. No critical issues found. 🟢"},
|
||||
ensure_ascii=False,
|
||||
),
|
||||
usage={},
|
||||
model_name="test-model",
|
||||
metadata={},
|
||||
mock_provider.generate_content = AsyncMock(
|
||||
return_value=Mock(
|
||||
content=json.dumps(
|
||||
{"status": "analysis_complete", "raw_analysis": "Code review completed. 🟢"}, ensure_ascii=False
|
||||
),
|
||||
usage={},
|
||||
model_name="test-model",
|
||||
metadata={},
|
||||
)
|
||||
)
|
||||
mock_get_provider.return_value = mock_provider
|
||||
|
||||
# Test codereview tool
|
||||
codereview_tool = CodeReviewTool()
|
||||
result = codereview_tool.execute(
|
||||
result = await codereview_tool.execute(
|
||||
{
|
||||
"step": "Source code review",
|
||||
"step_number": 1,
|
||||
@@ -376,23 +373,10 @@ class TestLocalizationIntegration(unittest.TestCase):
|
||||
|
||||
# Check that language instruction was used
|
||||
mock_provider.generate_content.assert_called()
|
||||
call_args = mock_provider.generate_content.call_args
|
||||
system_prompt = call_args.kwargs.get("system_prompt", "")
|
||||
self.assertIn("fr-FR", system_prompt)
|
||||
|
||||
# Check that response contains UTF-8 characters
|
||||
if "expert_analysis" in response_data:
|
||||
expert_analysis = response_data["expert_analysis"]
|
||||
if "raw_analysis" in expert_analysis:
|
||||
analysis = expert_analysis["raw_analysis"]
|
||||
# Should contain French characters
|
||||
self.assertTrue(
|
||||
any(char in analysis for char in ["é", "è", "à", "ç", "ê", "û", "î", "ô"]) or "🟢" in analysis
|
||||
)
|
||||
|
||||
def test_multiple_locales_switching(self):
|
||||
"""Test switching locales during execution."""
|
||||
tool = BaseTool(api_key="test")
|
||||
tool = TestTool()
|
||||
|
||||
# French
|
||||
os.environ["LOCALE"] = "fr-FR"
|
||||
@@ -422,6 +406,11 @@ class TestLocalizationIntegration(unittest.TestCase):
|
||||
self.assertNotEqual(inst1, inst2)
|
||||
|
||||
|
||||
# Helper function to run async tests
|
||||
def run_async_test(test_func):
|
||||
"""Helper to run async test functions."""
|
||||
return asyncio.run(test_func())
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
# Test configuration
|
||||
pytest.main([__file__, "-v", "--tb=short"])
|
||||
unittest.main(verbosity=2)
|
||||
|
||||
Reference in New Issue
Block a user