"""Tests for ows_royalties.py.""" from unittest.mock import patch import httpx import pytest from owsclient.test import OwsClientMock from file_upload_initialize.connectors import ows_royalties from file_upload_initialize.exception import OwsServiceException class TestUpdateFileUploadStatus: """Tests for update_file_upload_status function.""" def test_update_file_upload_status_success_200( self, ows_client_mock: OwsClientMock, ) -> None: """Test successful updating upload status with 200 status.""" mock_response = {'status': 'success', 'message': 'File processed'} file_key = 'test-file-key-123' put_request_body = {'upload_status': 'scanning'} ows_client_mock.put('ows-royalties', f'/file-upload/{file_key}/status').mock( return_value=httpx.Response( 200, json=mock_response, ) ) response = ows_royalties.update_file_upload_status(file_key, put_request_body) assert response == mock_response @patch('file_upload_initialize.connectors.ows_royalties.logger') def test_update_file_upload_status_failed_500( self, mock_logger, ows_client_mock: OwsClientMock, ) -> None: """Test failed updating status with 500 error.""" mock_response = { 'error': 'Internal server error', } file_key = 'test-file-key-789' put_request_body = {'upload_status': 'scanning'} ows_client_mock.put( 'ows-royalties', f'/file-upload/{file_key}/status', ).mock( return_value=httpx.Response( 500, json=mock_response, ) ) with pytest.raises(OwsServiceException) as exc_info: ows_royalties.update_file_upload_status(file_key, put_request_body) assert 'ows-royalties failure' in str(exc_info.value) assert 'Status 500' in str(exc_info.value) assert mock_logger.error.called @patch('file_upload_initialize.connectors.ows_royalties.logger') def test_update_file_upload_status_failed_404( self, mock_logger, ows_client_mock: OwsClientMock, ) -> None: """Test failed updating status with 404 not found.""" mock_response = {'error': 'File not found'} file_key = 'nonexistent-file-key' put_request_body = {'upload_status': 'scanning'} ows_client_mock.put( 'ows-royalties', f'/file-upload/{file_key}/status', ).mock( return_value=httpx.Response( 404, json=mock_response, ) ) with pytest.raises(OwsServiceException) as exc_info: ows_royalties.update_file_upload_status(file_key, put_request_body) assert 'Status 404' in str(exc_info.value) @patch('file_upload_initialize.connectors.ows_royalties.logger') def test_update_file_upload_status_non_json_error_response( self, mock_logger, ows_client_mock: OwsClientMock, ) -> None: """Test updating status with non-JSON error response.""" file_key = 'test-file-key' error_text = 'Internal Server Error' put_request_body = {'upload_status': 'scanning'} ows_client_mock.put( 'ows-royalties', f'/file-upload/{file_key}/status', ).mock( return_value=httpx.Response( 500, text=error_text, headers={'content-type': 'text/plain'}, ) ) with pytest.raises(OwsServiceException): ows_royalties.update_file_upload_status(file_key, put_request_body) # Verify logger was called with text content assert mock_logger.error.called @patch('file_upload_initialize.connectors.ows_royalties.logger') def test_update_file_upload_status_logs_info( self, mock_logger, ows_client_mock: OwsClientMock, ) -> None: """Test updating status logs appropriate info messages.""" mock_response = {'status': 'success'} file_key = 'test-file-key' put_request_body = {'upload_status': 'scanning'} ows_client_mock.put( 'ows-royalties', f'/file-upload/{file_key}/status', ).mock( return_value=httpx.Response( 200, json=mock_response, ) ) ows_royalties.update_file_upload_status(file_key, put_request_body) # Verify info logging was called assert mock_logger.info.called mock_logger.info.assert_called_with( f'File upload status updated successfully: {file_key}' )