"""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_adjustments import ows_royalties from snapshot_adjustments.constants import ACCT_RUN_MSG, ADJUSTMENTS_MSG @patch('snapshot_adjustments.ows_royalties.app_logger') @patch('snapshot_adjustments.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 = ows_royalties.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_adjustments.ows_royalties.app_logger') @patch('snapshot_adjustments.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(ows_royalties.OwsRoyaltiesException) as e: ows_royalties.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() @patch('snapshot_adjustments.ows_royalties.app_logger') @patch('snapshot_adjustments.ows_royalties._request') def test_get_adjustments_by_acc_run_id_success( mock_request, mock_logger, mock_adjustments ): """Test successfully getting adjustments from ows-royalties.""" accounting_run_id = 123 mock_response = MagicMock() mock_response.json.return_value = mock_adjustments mock_response.status_code = 200 mock_request.return_value = mock_response res = ows_royalties.get_adjustments_by_acc_run_id(accounting_run_id) assert res == mock_adjustments mock_request.assert_called_once_with( f'/accounting-run/{accounting_run_id}/adjustments' ) mock_logger.info.assert_called_once_with(ADJUSTMENTS_MSG.format(accounting_run_id)) @patch('snapshot_adjustments.ows_royalties.app_logger') @patch('snapshot_adjustments.ows_royalties._request') def test_get_adjustments_by_acc_run_id_failure(mock_request, mock_logger): """Test failure getting adjustments 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(ows_royalties.OwsRoyaltiesException) as e: ows_royalties.get_adjustments_by_acc_run_id(accounting_run_id) assert f'GET /accounting-run{accounting_run_id}/adjustments failed' == \ str(e.value) mock_request.assert_called_once() mock_logger.info.assert_called_once()