"""Ows-royalties module unit tests.""" from unittest.mock import MagicMock, patch from owsrequest.flask_request import flaskify from owsresponse import response import pytest from snapshot_contract.constants import ACCT_RUN_MSG from snapshot_contract.custom_exceptions import OwsRoyaltiesException from snapshot_contract.ows_royalties import get_accounting_run @patch('snapshot_contract.ows_royalties.app_logger') @patch('snapshot_contract.ows_royalties._request') def test_get_accounting_run_success(mock_request, mock_logger): """Test successfully getting accounting run detail from ows-royalties.""" accounting_run_id = 123 accounting_run = { 'accounting_period_id': 12, 'accounting_period_name': 'Period Name', 'accounting_run_id': accounting_run_id, 'run_controller_name': 'RC Name' } mock_response = MagicMock() mock_response.json.return_value = accounting_run mock_response.status_code = 200 mock_request.return_value = mock_response res = get_accounting_run(accounting_run_id) assert res == accounting_run mock_request.assert_called_once_with(f'/accounting-run/{accounting_run_id}') mock_logger.info.assert_called_once_with(ACCT_RUN_MSG.format(accounting_run_id)) @patch('snapshot_contract.ows_royalties.app_logger') @patch('snapshot_contract.ows_royalties._request') def test_get_accounting_run_failure(mock_request, mock_logger): """Test failure getting accounting run detail raises an exception.""" accounting_run_id = 123 mock_response = response.create_error_response( code='error', message='Error Msg', status=400 ) mock_request.return_value = flaskify(mock_response) with pytest.raises(OwsRoyaltiesException) as e: get_accounting_run(accounting_run_id) assert f'GET /accounting-run{accounting_run_id}/ failed' == str(e.value) mock_request.assert_called_once() mock_logger.info.assert_called_once()