"""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_exchange_rates import ows_royalties from snapshot_exchange_rates.constants import ACCT_RUN_MSG, EXCHANGE_RATES_MSG @patch('snapshot_exchange_rates.ows_royalties.app_logger') @patch('snapshot_exchange_rates.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 = 1337 accounting_run = { 'run_controller_name': 'Jen Priority', 'start_date': '2020-05-27', 'accounting_period_name': 'January 2020', 'accounting_run_id': 1337, 'accounting_period_id': 1021, 'payee_count': 21, 'accounting_run_status': 'Committed', 'run_controller_id': 2722, 'summary_export_url': 's3://qa-royalties-sales-files/summary.tsv' } 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_exchange_rates.ows_royalties.app_logger') @patch('snapshot_exchange_rates.ows_royalties._request') def test_get_accounting_run_failure(mock_request, mock_logger): """Test failure getting accounting period detail raises an exception.""" accounting_run_id = 1021 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_exchange_rates.ows_royalties.app_logger') @patch('snapshot_exchange_rates.ows_royalties._request') def test_get_exchange_rates_by_period_id_success( mock_request, mock_logger, mock_exchange_rates ): """Test successfully getting exchange rates from ows-royalties.""" accounting_period_id = 123 mock_response = MagicMock() mock_response.json.return_value = mock_exchange_rates mock_response.status_code = 200 mock_request.return_value = mock_response res = ows_royalties.get_exchange_rates_by_period_id(accounting_period_id) assert res == mock_exchange_rates mock_request.assert_called_once_with( f'/accounting-period/{accounting_period_id}/bulk-exchange-rates?format=json' ) mock_logger.info.assert_called_once_with( EXCHANGE_RATES_MSG.format(accounting_period_id) ) @patch('snapshot_exchange_rates.ows_royalties.app_logger') @patch('snapshot_exchange_rates.ows_royalties._request') def test_get_exchange_rates_by_period_id_failure(mock_request, mock_logger): """Test failure getting exchange rates raises an exception.""" period_id = 1021 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_exchange_rates_by_period_id(period_id) assert f'GET /accounting-period/{period_id}/bulk-exchange-rates failed' == \ str(e.value) mock_request.assert_called_once() mock_logger.info.assert_called_once()