Skip to content

Commit

Permalink
Test our workflow runs unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
derailed-dash committed Sep 13, 2024
1 parent c549bc5 commit f180ffa
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 0 deletions.
Empty file added app/backend_gcf/__init__.py
Empty file.
Empty file.
77 changes: 77 additions & 0 deletions app/backend_gcf/tests/test_main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import unittest
import io
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):
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()
2 changes: 2 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ charset-normalizer==3.3.2
click==8.1.7
cloudevents==1.10.1
deprecation==2.1.0
flake8
Flask==3.0.3
functions-framework==3.7.0
google-api-core==2.19.0
Expand All @@ -29,6 +30,7 @@ proto-plus==1.23.0
protobuf==4.25.3
pyasn1==0.6.0
pyasn1_modules==0.4.0
pytest
requests==2.32.3
rsa==4.9
urllib3==2.2.1
Expand Down

0 comments on commit f180ffa

Please sign in to comment.