"""Logic layer test for image status.""" from unittest import mock import pytest from images import response from images.logic import image_status from images.models import image_assets as image_assets_model from images.models import image_status as image_status_model @pytest.fixture def import_asset_id(): """Fixture to return asset id.""" return 1234567 @pytest.fixture def filename(): """Fixture to return filename.""" return '123456789012.jpg' @pytest.fixture def successful_image_status_response_message(filename): """Fixture to return complete image status response.""" return { 'import_asset_id': 1, 'filename': filename, 'status': 200, 'result': 'success'} @pytest.fixture def unsuccessful_image_status_response_message(filename): """Fixture to return not complete image status response.""" return { 'import_asset_id': 1, 'filename': filename, 'status': 200, 'result': None} @pytest.fixture def expected_path(): """Fixture to return complete image url.""" return ('https://images.qaorch.com/release/large_cover/123456/789/' '123456789012.jpg') @pytest.fixture def image_assets_response(): """Fixture to return image path response message.""" return {'path': ('https://images.qaorch.com/release/large_cover' '/123456/789/123456789012.jpg')} def test_get_import_asset_for_image(monkeypatch, import_asset_id): """Test that the logic layer is called with the given import asset id.""" monkeypatch.setattr( image_status_model, 'get_import_asset_for_image', mock.MagicMock()) image_status.get_import_asset_for_image(import_asset_id) image_status_model.get_import_asset_for_image.assert_called_with( import_asset_id) def test_get_import_asset_success_result( successful_image_status_response_message, monkeypatch, import_asset_id, image_assets_response, expected_path): """Test that logic layer calls image_assets model. Calls in the case that result = succcess, and updates the path property in the response. """ monkeypatch.setattr( image_status_model, 'get_import_asset_for_image', mock.MagicMock(return_value=response.Response( message=successful_image_status_response_message))) monkeypatch.setattr( image_assets_model, 'get_file_path_for_image', mock.MagicMock(return_value=response.Response( message=image_assets_response))) image_response = image_status.get_import_asset_for_image(import_asset_id) image_assets_model.get_file_path_for_image.assert_called_with( import_asset_id) assert image_response.message.get('path') == expected_path def test_get_import_asset_not_success_result( unsuccessful_image_status_response_message, monkeypatch, import_asset_id): """Test logic does not call image_assets model if result != success.""" monkeypatch.setattr( image_status_model, 'get_import_asset_for_image', mock.MagicMock(return_value=response.Response( message=unsuccessful_image_status_response_message))) monkeypatch.setattr( image_assets_model, 'get_file_path_for_image', mock.MagicMock()) image_status.get_import_asset_for_image(import_asset_id) image_assets_model.get_file_path_for_image.not_assert_called_with(filename) def test_get_import_asset_when_asset_path_not_found( monkeypatch, import_asset_id, successful_image_status_response_message): """Test that a not found result is returned when asset path not found.""" monkeypatch.setattr( image_status_model, 'get_import_asset_for_image', mock.MagicMock(return_value=response.Response( message=successful_image_status_response_message))) monkeypatch.setattr( image_assets_model, 'get_file_path_for_image', mock.MagicMock(return_value=response.Response( status=404))) image_response = image_status.get_import_asset_for_image(import_asset_id) assert image_response.status == 404