-
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.
Add unit tests for extract_and_translate function
- Loading branch information
gda
committed
Nov 11, 2024
1 parent
0ae1f2a
commit 144503d
Showing
4 changed files
with
41 additions
and
0 deletions.
There are no files selected for viewing
Empty file.
Empty file.
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,40 @@ | ||
import pytest | ||
from unittest.mock import patch, MagicMock # To mock external API calls | ||
from flask import Flask, request # To simulate HTTP requests | ||
from io import BytesIO | ||
|
||
# Import the function from main.py | ||
from backend_gcf.main import extract_and_translate | ||
|
||
@pytest.fixture | ||
def app(): | ||
""" Fixture for Flask app context, for creating HTTP requests """ | ||
app = Flask(__name__) | ||
return app | ||
|
||
# Use patch to replace the Google clients with mock objects | ||
@patch('backend_gcf.main.vision_client') | ||
@patch('backend_gcf.main.translate_client') | ||
def test_extract_and_translate_with_posted_image(mock_translate_client, mock_vision_client, app: Flask): | ||
with app.test_request_context( | ||
method='POST', | ||
data={ | ||
'uploaded': (BytesIO(b'sample image data'), 'test_image.jpg'), | ||
'to_lang': 'en' | ||
}, | ||
content_type='multipart/form-data' | ||
): | ||
# Mock Vision API response | ||
mock_vision_client.text_detection.return_value = MagicMock( | ||
text_annotations=[MagicMock(description="Put a glass of rice and three glasses of water in a saucepan")] | ||
) | ||
|
||
# Mock Translate API response | ||
mock_translate_client.detect_language.return_value = {"language": "uk"} | ||
mock_translate_client.translate.return_value = {"translatedText": "Put a glass of rice and three glasses of water in a saucepan"} | ||
|
||
# Call the function | ||
response = extract_and_translate(request) | ||
|
||
# Check the result | ||
assert response == "Put a glass of rice and three glasses of water in a saucepan" |
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