forked from langchain-ai/langchain
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Made the review changes, added an example, and unit test
- Loading branch information
Showing
3 changed files
with
140 additions
and
21 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
Binary file not shown.
105 changes: 105 additions & 0 deletions
105
libs/community/tests/unit_tests/tools/azure_ai_services/test_azure_file_translation.py
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,105 @@ | ||
from pathlib import Path | ||
from typing import Any | ||
|
||
import pytest | ||
from langchain_community.tools.azure_ai_services.azure_file_translation import ( | ||
AzureFileTranslateTool | ||
) | ||
|
||
_THIS_DIR = Path(__file__).parents[3] | ||
|
||
_EXAMPLES_DIR = _THIS_DIR / "examples" | ||
AZURE_PDF = _EXAMPLES_DIR / "test_azure.pdf" | ||
|
||
|
||
@pytest.mark.requires("azure-ai-translation-text") | ||
def test_tool_initialization(mocker: Any) -> None: | ||
mocker.patch("azure.core.credentials.AzureKeyCredential", autospec=True) | ||
|
||
mock_translate_client = mocker.Mock() | ||
mocker.patch("azure.ai.translation.text.TextTranslationClient", | ||
return_value=mock_translate_client) | ||
|
||
key = "key" | ||
endpoint = "endpoint" | ||
region = "westus2" | ||
|
||
tool = AzureFileTranslateTool( | ||
text_translation_key=key, | ||
text_translation_endpoint=endpoint, | ||
region=region, | ||
translate_client=mock_translate_client | ||
) | ||
|
||
assert tool.text_translation_key == key | ||
assert tool.text_translation_endpoint == endpoint | ||
assert tool.region == region | ||
assert tool.translate_client == mock_translate_client | ||
|
||
|
||
@pytest.mark.requires("azure-ai-translation-text") | ||
def test_translation_with_file(mocker: Any) -> None: | ||
key = "key" | ||
endpoint = "endpoint" | ||
region = "westus2" | ||
|
||
mocker.patch("azure.core.credentials.AzureKeyCredential", | ||
autospec=True) | ||
|
||
mock_translate_client = mocker.Mock() | ||
mocker.patch("azure.ai.translation.text.TextTranslationClient", | ||
return_value=mock_translate_client) | ||
|
||
tool = AzureFileTranslateTool( | ||
text_translation_key=key, | ||
text_translation_endpoint=endpoint, | ||
region=region, | ||
translate_client=mock_translate_client | ||
) | ||
|
||
mock_translate_client.translate.return_value = [ | ||
{ | ||
'detectedLanguage': {'language': 'en', 'score': 1.0}, | ||
'translations': [ | ||
{'text': 'Hola, mi nombre es Azure', 'to': 'es'} | ||
] | ||
} | ||
] | ||
|
||
file_input: str = str(AZURE_PDF) | ||
expected_output = "Hola, mi nombre es Azure" | ||
|
||
result = tool._run(file_input) | ||
|
||
assert result == expected_output | ||
|
||
|
||
@pytest.mark.requires("azure-ai-translation-text") | ||
def test_translation_with_no_file(mocker: Any) -> None: | ||
key = "key" | ||
endpoint = "endpoint" | ||
region = "westus2" | ||
|
||
mocker.patch("azure.core.credentials.AzureKeyCredential", | ||
autospec=True) | ||
|
||
mock_translate_client = mocker.Mock() | ||
mocker.patch("azure.ai.translation.text.TextTranslationClient", | ||
return_value=mock_translate_client) | ||
|
||
tool = AzureFileTranslateTool( | ||
text_translation_key=key, | ||
text_translation_endpoint=endpoint, | ||
region=region, | ||
translate_client=mock_translate_client | ||
) | ||
|
||
file_input: str = "" | ||
expected_output = "Error while running AzureFileTranslateTool" | ||
|
||
try: | ||
result = tool._run(file_input) | ||
except RuntimeError as e: | ||
result = str(e) | ||
|
||
assert expected_output in result |