-
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 #3 from derailed-dash/feature/add-unit-tests
Add unit tests for post_valid_image
- Loading branch information
Showing
4 changed files
with
94 additions
and
0 deletions.
There are no files selected for viewing
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
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,91 @@ | ||
import os | ||
import pytest | ||
import logging | ||
from unittest.mock import patch, MagicMock | ||
from io import BytesIO | ||
from PIL import Image | ||
|
||
@pytest.fixture | ||
def app(): | ||
"""Flask test app configuration with logging enabled.""" | ||
from ui_cr.app import app # Import the app | ||
app.config['TESTING'] = True | ||
app.logger.setLevel(logging.DEBUG) # Enable debug logging | ||
|
||
# Enable Flask's logger propagation to the root logger | ||
app.logger.propagate = True | ||
|
||
return app | ||
|
||
@pytest.fixture | ||
def client(app): | ||
"""Fixture to create a test client for the Flask app.""" | ||
return app.test_client() | ||
|
||
@patch('ui_cr.app.make_authorized_post_request') | ||
def test_entry_post_valid_image(mock_make_post_request, client, app): | ||
"""Test the POST request with a valid image and form.""" | ||
|
||
# Create a test image and save it to a BytesIO object | ||
img_io = BytesIO() | ||
test_image = Image.new('RGB', (10, 10), color='red') | ||
test_image.save(img_io, format='JPEG') | ||
img_io.seek(0) # Move to the start of the BytesIO object | ||
|
||
# Ensure the BACKEND_GCF environment variable is set for this test | ||
backend_gcf = os.environ.get('BACKEND_GCF', 'undefined') | ||
app.logger.debug(f"BACKEND_GCF: {backend_gcf}") | ||
|
||
# Mocking the response from the backend function | ||
mock_response = MagicMock() | ||
mock_response.status_code = 200 | ||
mock_response.text = "Test translated text" | ||
mock_make_post_request.return_value = mock_response | ||
|
||
# Create a mock image file to simulate form data | ||
img_data = (img_io, 'test_image.jpg') | ||
|
||
# Send POST request | ||
response = client.post( | ||
'/', | ||
data={'file': img_data, 'to_lang': 'en'}, | ||
content_type='multipart/form-data' | ||
) | ||
|
||
# Check the response status | ||
assert response.status_code == 200 | ||
|
||
# Debugging logs | ||
app.logger.debug(f"Test response data: {response.data}") | ||
|
||
# Check if the mock function was called, ensuring the image was processed correctly | ||
try: | ||
mock_make_post_request.assert_called_once() | ||
called_args, called_kwargs = mock_make_post_request.call_args | ||
|
||
# Check that the arguments are as expected | ||
assert called_kwargs['endpoint'] == backend_gcf | ||
assert called_kwargs['to_lang'] == 'en' | ||
assert called_kwargs['filename'] == 'test_image.jpg' | ||
assert called_kwargs['content_type'] in ['image/jpeg', 'image/png'] | ||
|
||
# Optionally check that image_data is not empty | ||
assert called_kwargs['image_data'], "Image data is empty" | ||
|
||
# Add additional check for content length | ||
assert len(called_kwargs['image_data']) > 0, "Image data length is zero" | ||
|
||
# Log the mock function call arguments | ||
app.logger.debug(f"make_authorized_post_request called with: {called_kwargs}") | ||
|
||
except AssertionError as e: | ||
app.logger.error(f"make_authorized_post_request was not called correctly: {str(e)}") | ||
raise e | ||
|
||
# Check that there are no flash error messages | ||
assert b"No file selected for uploading." not in response.data | ||
assert b"Unable to process image." not in response.data | ||
assert b"Unsupported image format." not in response.data | ||
|
||
# Check for the translation text in the response | ||
assert b"Test translated text" in response.data |