"""Tests for ows_abacus_event.py.""" from unittest.mock import patch import httpx import pytest from owsclient.test import OwsClientMock from file_upload_complete.connectors import ows_abacus_event from file_upload_complete.exception import OwsServiceException class TestCreateAbacusEvent: """Tests for create_abacus_event function.""" def test_create_abacus_event_success_201( self, ows_client_mock: OwsClientMock, mock_abacus_event ) -> None: """Test successful create abacus event with 201 status.""" mock_response = mock_abacus_event mock_post_request_body = { 'event_name': 'test_event', 'target_id': 1, 'target_type': 'file_upload', } ows_client_mock.post( 'ows-abacus-event', f'/abacus-event', ).mock( return_value=httpx.Response( 201, json=mock_response, ) ) response = ows_abacus_event.create_abacus_event(mock_post_request_body) assert response == mock_response @patch('file_upload_complete.connectors.ows_abacus_event.logger') def test_create_abacus_event_failed_500( self, mock_logger, ows_client_mock: OwsClientMock ) -> None: """Test failed abacus event with 500 error.""" mock_response = { 'error': 'Internal server error', } mock_post_request_body = { 'event_name': 'test_event', 'target_id': 1, 'target_type': 'file_upload', } ows_client_mock.post( 'ows-abacus-event', f'/abacus-event', ).mock( return_value=httpx.Response( 500, json=mock_response, ) ) with pytest.raises(OwsServiceException) as exc_info: ows_abacus_event.create_abacus_event(mock_post_request_body) assert 'ows-abacus-event failure' in str(exc_info.value) assert 'Status 500' in str(exc_info.value) mock_logger.error.assert_called_once() @patch('file_upload_complete.connectors.ows_abacus_event.logger') def test_create_abacus_event_failed_404( self, mock_logger, ows_client_mock: OwsClientMock, ) -> None: """Test failed file upload complete with 404 not found.""" mock_response = {'error': 'File not found'} mock_post_request_body = { 'event_name': 'test_event', 'target_id': 1, 'target_type': 'file_upload', } ows_client_mock.post( 'ows-abacus-event', f'/abacus-event', ).mock( return_value=httpx.Response( 404, json=mock_response, ) ) with pytest.raises(OwsServiceException) as exc_info: ows_abacus_event.create_abacus_event(mock_post_request_body) assert 'Status 404' in str(exc_info.value) mock_logger.error.assert_called_once()