"""Accounting period report logic tests.""" from unittest.mock import MagicMock, patch from royalties.constants.constants import REPORT_TYPE from royalties.constants.error import INVALID_REPORT_TYPE from royalties.logic import accounting_period_report as logic from royalties.tests.utils.factories import ( AccountingPeriodFactory, AccountingPeriodReportFactory, ) @patch('royalties.logic.accounting_period_report.models') def test_get_acc_period_reports_by_acc_period_id(mock_models): """Test to get accounting period reports by period id.""" accounting_period = AccountingPeriodFactory.create() report = AccountingPeriodReportFactory.create(accounting_period=accounting_period) mock_models.AccountingPeriod.get_by_id_or_error.return_value = accounting_period accounting_period_id = accounting_period.accounting_period_id res = logic.get_acc_period_reports_by_acc_period_id(accounting_period_id) assert res.status == 200 assert ( res.message[0]['accounting_period_report_id'] == report.accounting_period_report_id ) @patch('royalties.logic.accounting_period_report.create_presigned_url') @patch('royalties.logic.accounting_period_report.get_s3_client') @patch('royalties.logic.accounting_period_report.models') def test_get_acc_period_report(mock_models, mock_client, mock_url): """Test to get accounting period reports by period id and report type.""" accounting_period = AccountingPeriodFactory.create() AccountingPeriodReportFactory.create( accounting_period=accounting_period, report_export_url='s3://main/report_file_1/', report_type=REPORT_TYPE.VAT_EXEMPT, ) report_2 = AccountingPeriodReportFactory.create( accounting_period=accounting_period, report_export_url='s3://main/report_file_2/', report_type=REPORT_TYPE.VAT_APPLIED_GBR, ) mock_client.return_value = 'client' mock_url.return_value = report_2.report_export_url accounting_period_id = accounting_period.accounting_period_id report_type = REPORT_TYPE.VAT_APPLIED_GBR mock_models.AccountingPeriod.get_by_id_or_error.return_value = accounting_period mock_models.AccountingPeriodReport.get_acc_period_reports.return_value.first.return_value = report_2 res = logic.get_acc_period_report(accounting_period_id, report_type) assert res.status == 200 assert res.message == { 'accounting_period_report_id': report_2.accounting_period_report_id, 'accounting_period_id': report_2.accounting_period_id, 'report_type': report_2.report_type, 'report_export_url': report_2.report_export_url, } mock_client.assert_called_once() mock_models.AccountingPeriodReport.get_acc_period_reports.assert_called_once_with( accounting_period_id, report_type ) @patch('royalties.logic.accounting_period_report.create_presigned_url') @patch('royalties.logic.accounting_period_report.get_s3_client') @patch('royalties.logic.accounting_period_report.models') def test_get_acc_period_report_invalid_report_type(mock_models, mock_client, mock_url): """Test to get accounting period reports by period id and invalid report type.""" accounting_period = AccountingPeriodFactory.create() mock_client.return_value = 'client' res = logic.get_acc_period_report(accounting_period.accounting_period_id, 'test') assert res.status == 400 assert res.errors['message'] == INVALID_REPORT_TYPE.format( report_type=', '.join([*REPORT_TYPE]) ) mock_models.AccountingPeriodReport.get_acc_period_reports.assert_not_called() @patch('royalties.logic.accounting_period_report.models') def test_create_or_update_accounting_period_report_update(mock_models): """Test for accounting period report creation logic.""" mock_models.AccountingPeriod.get_by_id_or_error.return_value = True accounting_period_report_mock = MagicMock() report_model = mock_models.AccountingPeriodReport report_model.get_acc_period_reports.return_value.first.return_value = ( accounting_period_report_mock ) vat_exempt_url = 's3://vat/exempt/export/url' logic.create_or_update_accounting_period_report(1, 'vat_exempt', vat_exempt_url) accounting_period_report_mock.update_attributes.assert_called_once_with( report_export_url=vat_exempt_url ) @patch('royalties.logic.accounting_period_report.models') def test_create_or_update_accounting_period_report_create(mock_models): """Test for accounting period report creation logic.""" mock_models.AccountingPeriod.get_by_id_or_error.return_value = True report_model = mock_models.AccountingPeriodReport report_model.get_acc_period_reports.return_value.first.return_value = None vat_exempt_url = 's3://vat/exempt/export/url' logic.create_or_update_accounting_period_report(1, 'vat_exempt', vat_exempt_url) report_model.create.assert_called_once_with( accounting_period_id=1, report_type='vat_exempt', report_export_url=vat_exempt_url, )