-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from derailed-dash/add-unit-tests
Add unit tests
- Loading branch information
Showing
2 changed files
with
42 additions
and
81 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
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 |
---|---|---|
@@ -1,77 +1,40 @@ | ||
import unittest | ||
from unittest.mock import MagicMock | ||
from ..main import extract_and_translate, detect_text, translate_text | ||
from google.cloud import vision | ||
from google.cloud import translate_v2 as translate | ||
|
||
class TestImageTextTranslator(unittest.TestCase): | ||
|
||
# def test_extract_and_translate_with_uploaded_file(self): | ||
# with open("test_image.jpg", "rb") as f: | ||
# test_image_data = f.read() | ||
|
||
# # Mock the request object using MagicMock | ||
# request_mock = MagicMock() | ||
# request_mock.method = 'POST' | ||
# request_mock.files = {"uploaded": io.BytesIO(test_image_data)} | ||
# request_mock.form = {"to_lang": "es"} | ||
|
||
# # Assuming detect_text and translate_text are working correctly | ||
# # and mocking their responses for this test | ||
# expected_detected_text = {"text": "Sample text", "src_lang": "en"} | ||
# expected_translated_text = {"text": "Texto de ejemplo", "src_lang": "en", "to_lang": "es"} | ||
|
||
# # Mock the responses from the Vision and Translate APIs | ||
# vision.ImageAnnotatorClient.text_detection = MagicMock(return_value=MagicMock(text_annotations=[MagicMock(description=expected_detected_text["text"])])) | ||
# translate.Client.detect_language = MagicMock(return_value={"language": expected_detected_text["src_lang"]}) | ||
# translate.Client.translate = MagicMock(return_value={"translatedText": expected_translated_text["text"]}) | ||
|
||
# response = extract_and_translate(request_mock) | ||
|
||
# self.assertEqual(response, expected_translated_text["text"]) | ||
|
||
def test_detect_text_with_text(self): | ||
""" Mock the response from the Vision API """ | ||
test_text = "Sample text" | ||
vision.ImageAnnotatorClient.text_detection = MagicMock(return_value=MagicMock(text_annotations=[MagicMock(description=test_text)])) | ||
translate.Client.detect_language = MagicMock(return_value={"language": "en"}) | ||
|
||
image_mock = MagicMock(spec=vision.Image) | ||
detected_text = detect_text(image_mock) | ||
|
||
self.assertEqual(detected_text["text"], test_text) | ||
|
||
def test_detect_text_without_text(self): | ||
""" Mock the response from the Vision API for no text detected """ | ||
vision.ImageAnnotatorClient.text_detection = MagicMock(return_value=MagicMock(text_annotations=[])) | ||
translate.Client.detect_language = MagicMock(return_value={"language": "und"}) | ||
|
||
image_mock = MagicMock(spec=vision.Image) | ||
detected_text = detect_text(image_mock) | ||
|
||
self.assertEqual(detected_text["text"], "") | ||
|
||
def test_translate_text(self): | ||
""" Mock the response from the Translate API """ | ||
message = {"text": "Hola mundo", "src_lang": "es"} | ||
to_lang = "en" | ||
expected_translated_text = {"text": "Hello world", "src_lang": "es", "to_lang": "en"} | ||
|
||
# Mock the response from the Translate API | ||
translate.Client.translate = MagicMock(return_value={"translatedText": expected_translated_text["text"]}) | ||
|
||
translated_text = translate_text(message, to_lang) | ||
|
||
self.assertEqual(translated_text, expected_translated_text) | ||
|
||
def test_translate_text_no_translation(self): | ||
message = {"text": "Hello world", "src_lang": "en"} | ||
to_lang = "en" | ||
expected_translated_text = {"text": "Hello world", "src_lang": "en", "to_lang": "en"} | ||
|
||
translated_text = translate_text(message, to_lang) | ||
|
||
self.assertEqual(translated_text, expected_translated_text) | ||
|
||
if __name__ == "__main__": | ||
unittest.main() | ||
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 - the request object is the one simulated in test_request_context | ||
response = extract_and_translate(request) | ||
|
||
# Check the result | ||
assert response == "Put a glass of rice and three glasses of water in a saucepan" |