"""Test ows-royalties requests.""" from unittest.mock import patch import httpx from owsclient.test import OwsClientMock from commit_royalties.constants.constants import GET_ACCOUNTING_PERIOD_MSG from commit_royalties.constants.constants import GET_ACCOUNTING_RUN_MSG from commit_royalties.ows_royalties import get_accounting_period_detail from commit_royalties.ows_royalties import get_accounting_run_detail @patch('commit_royalties.ows_royalties.raise_service_error') @patch('commit_royalties.ows_royalties.app_logger') def test_get_accounting_period_detail( mock_logger, mock_raise_service_error, ows_client_mock: OwsClientMock ): """Test successfully getting accounting period.""" accounting_period_id = 100 path = f'/accounting-period/{accounting_period_id}' mock_response = { 'accounting_period_status': 'open', 'closed_date': None, 'contract_type': 'neighbouring_rights', 'accounting_period_name': 'test', 'statement_period_id': 282, 'accounting_period_id': 1337 } ows_client_mock.get( 'ows-royalties', path, ).mock(return_value=httpx.Response(200, json=mock_response)) res = get_accounting_period_detail(accounting_period_id) assert res == mock_response mock_logger.info.assert_called_once_with( GET_ACCOUNTING_PERIOD_MSG.format(accounting_period_id) ) mock_logger.error.assert_not_called() mock_raise_service_error.assert_not_called() @patch('commit_royalties.ows_royalties.raise_service_error') @patch('commit_royalties.ows_royalties.app_logger') def test_get_accounting_period_detail_error( mock_logger, mock_raise_service_error, ows_client_mock: OwsClientMock ): """Test unsuccessfully getting accounting period.""" accounting_period_id = 100 path = f'/accounting-period/{accounting_period_id}' ows_client_mock.get( 'ows-royalties', path ).mock(return_value=httpx.Response(404, json={'message': 'Not Found'})) res = get_accounting_period_detail(accounting_period_id) assert res is None mock_logger.info.assert_called_once_with( GET_ACCOUNTING_PERIOD_MSG.format(accounting_period_id) ) mock_logger.error.assert_called_once_with( "ows-royalties error response: {'message': 'Not Found'}" ) mock_raise_service_error.assert_called_once_with( f'ERROR in GET {path}', 'ows-royalties' ) @patch('commit_royalties.ows_royalties.raise_service_error') @patch('commit_royalties.ows_royalties.app_logger') def test_get_accounting_run_detail_success( mock_logger, mock_raise_service_error, mock_accounting_run, ows_client_mock: OwsClientMock ): """Test successfully getting an accounting_run by accounting_run_id.""" accounting_run_id = mock_accounting_run.get('accounting_run_id') path = f'/accounting-run/{accounting_run_id}' ows_client_mock.get( 'ows-royalties', path, ).mock(return_value=httpx.Response(200, json=mock_accounting_run)) res = get_accounting_run_detail(accounting_run_id) assert res == mock_accounting_run mock_logger.info.assert_called_once_with( GET_ACCOUNTING_RUN_MSG.format(accounting_run_id) ) mock_logger.error.assert_not_called() mock_raise_service_error.assert_not_called() @patch('commit_royalties.ows_royalties.raise_service_error') @patch('commit_royalties.ows_royalties.app_logger') def test_get_accounting_run_detail_error( mock_logger, mock_raise_service_error, ows_client_mock: OwsClientMock ): """Test error is raised when getting an accounting run fails.""" accounting_run_id = 18 path = f'/accounting-run/{accounting_run_id}' ows_client_mock.get( 'ows-royalties', path, ).mock(return_value=httpx.Response(404, json={'message': 'Not Found'})) res = get_accounting_run_detail(accounting_run_id) assert res is None mock_logger.info.assert_called_once_with( GET_ACCOUNTING_RUN_MSG.format(accounting_run_id) ) mock_logger.error.assert_called_once_with( "ows-royalties error response: {'message': 'Not Found'}" ) mock_raise_service_error.assert_called_once_with( f'ERROR in GET {path}', 'ows-royalties' )