"""Tests for ows_royalties connector.""" import httpx import pytest from owsclient.test import OwsClientMock from adjustments_json_validation.connectors import ows_royalties from adjustments_json_validation.error_handling import ( NotFoundError, OwsServiceException, ) def test_get_current_statement_period( ows_client_mock: OwsClientMock, mock_statement_period ) -> None: """Test to get current statement period.""" mock_response = mock_statement_period ows_client_mock.get('ows-royalties', '/statement-period/current').mock( return_value=httpx.Response( 200, json=mock_response, ) ) response = ows_royalties.get_current_statement_period() assert response == mock_response def test_get_statement_period_error(ows_client_mock: OwsClientMock): """Test error is raised when getting statement period.""" ows_client_mock.get('ows-royalties', '/statement-period/current').mock( return_value=httpx.Response(500) ) with pytest.raises(OwsServiceException) as e: ows_royalties.get_current_statement_period() assert ( str(e.value) == 'ows-royalties failure: ERROR in GET /statement-period/current {}' ) def test_get_statement_period_adjustment_file( ows_client_mock: OwsClientMock, mock_statement_period_adjustment_file ) -> None: """Test to get statement period adjustment file.""" mock_response = mock_statement_period_adjustment_file statement_period_adjustment_file_id = 1 ows_client_mock.get( 'ows-royalties', f'/statement-period-adjustment-file/{statement_period_adjustment_file_id}', ).mock( return_value=httpx.Response( 200, json=mock_response, ) ) response = ows_royalties.get_statement_period_adjustment_file( statement_period_adjustment_file_id ) assert response == mock_response def test_get_statement_period_adjustment_file_error(ows_client_mock: OwsClientMock): """Test error is raised when getting statement period adjustment file.""" statement_adjustment_file_id = 1 ows_client_mock.get( 'ows-royalties', f'/statement-period-adjustment-file/{statement_adjustment_file_id}', ).mock(return_value=httpx.Response(500)) with pytest.raises(OwsServiceException) as e: ows_royalties.get_statement_period_adjustment_file(statement_adjustment_file_id) assert ( str(e.value) == 'ows-royalties failure: ERROR in GET /statement-period-adjustment-file/1 {}' ) def test_get_statement_period_adjustment_file_404_error(ows_client_mock: OwsClientMock): """Test error is raised when specified statement period adjustment file is not found.""" statement_adjustment_file_id = 1 mock_response = {'message': 'File not found.'} ows_client_mock.get( 'ows-royalties', f'/statement-period-adjustment-file/{statement_adjustment_file_id}', ).mock(return_value=httpx.Response(404, json=mock_response)) with pytest.raises(NotFoundError) as e: ows_royalties.get_statement_period_adjustment_file(statement_adjustment_file_id) assert str(e.value) == mock_response['message'] def test_update_statement_period_adjustment_file( ows_client_mock: OwsClientMock, mock_statement_period_adjustment_file ) -> None: """Test to update the statement period adjustment file.""" mock_response = mock_statement_period_adjustment_file statement_period_adjustment_file_id = 1 statement_period_id = 1 body = {'valid_row_count': 4} ows_client_mock.put( 'ows-royalties', f'/statement-period/{statement_period_id}/adjustment-file/{statement_period_adjustment_file_id}', ).mock( return_value=httpx.Response( 201, json=mock_response, ) ) response = ows_royalties.update_statement_period_adjustment_file( body, statement_period_id, statement_period_adjustment_file_id ) assert response == mock_response def test_update_statement_period_adjustment_file_error(ows_client_mock: OwsClientMock): """Test error is raised when updating adjustment file record.""" statement_period_adjustment_file_id = 1 statement_period_id = 1 body = {'valid_row_count': 4} ows_client_mock.put( 'ows-royalties', f'/statement-period/{statement_period_id}/adjustment-file/{statement_period_adjustment_file_id}', ).mock(return_value=httpx.Response(500)) with pytest.raises(OwsServiceException) as e: ows_royalties.update_statement_period_adjustment_file( body, statement_period_id, statement_period_adjustment_file_id ) assert ( str(e.value) == 'ows-royalties failure: ERROR in PUT /statement-period/1/adjustment-file/1 {}' ) def test_get_adjustment_batch_criteria_by_file_id_404_error( ows_client_mock: OwsClientMock, ): """Test error is raised when specified file is not found.""" statement_period_adjustment_file_id = 1 statement_period_id = 1 body = {'valid_row_count': 4} mock_response = 'file not found.' ows_client_mock.put( 'ows-royalties', f'/statement-period/{statement_period_id}/adjustment-file/{statement_period_adjustment_file_id}', ).mock(return_value=httpx.Response(404, text=mock_response)) with pytest.raises(OwsServiceException) as e: ows_royalties.update_statement_period_adjustment_file( body, statement_period_id, statement_period_adjustment_file_id ) assert ( str(e.value) == 'ows-royalties failure: ERROR in PUT /statement-period/1/adjustment-file/1 {}' )