"""Functional tests for custom reports.""" from contextlib import contextmanager from unittest.mock import patch import httpx from moneyhub.connectors.snowflake import db from moneyhub.constants.constants import NumberFormat from moneyhub.constants.constants import ReportCustomColumnDimension from moneyhub.constants.constants import ReportCustomFileType from moneyhub.constants.constants import ReportCustomRowDimension from moneyhub.constants.constants import ReportCustomStatus from moneyhub.constants.constants import RevenueType from moneyhub.models.account_statement_periods import AccountStatementPeriods from tests.functional.conftest import insert_mock_data @contextmanager def _using_mock_data(mock_data: dict): """Create the necessary mock tables and fills them with the data. Args: mock_data (dict): Data to enter. """ AccountStatementPeriods.__table__.drop(db.engine, checkfirst=True) db.session.commit() AccountStatementPeriods.__table__.create(db.engine) insert_mock_data(mock_data, database='snowflake') yield db.session.commit() AccountStatementPeriods.__table__.drop(db.engine) db.session.close() @patch('moneyhub.logic.report_custom.sqs') def test_create_custom_report(mock_sqs, fixture_client): """Test creating a custom report.""" statement_period_id = 123 account_id = 24601 dimension_column = ReportCustomColumnDimension.TERRITORY dimension_row = ReportCustomRowDimension.PRODUCT revenue_type = RevenueType.DISTRIBUTION insert_mock_data({'account': {'account_id': account_id}}) with _using_mock_data( { 'account_statement_periods_dbt': { 'account_id': account_id, 'contract_id': 1111, 'currency_code': 'USD', 'statement_period_id': statement_period_id, 'statement_period_status': 'closed' } } ): result = fixture_client.post( f'/reports/custom/account/{account_id}', json={ 'statement_period_ids': [statement_period_id], 'revenue_type': revenue_type, 'dimension_column': dimension_column, 'dimension_row': dimension_row, 'filters': { 'store_ids': [286, 1, 348], 'country_codes': ['US', 'GB'], 'transaction_type_ids': [1, 2, 3], 'imprint_ids': [1, 2, 3], 'activity_period_ids': [], 'artist_ids': [2, 3, 4], 'product_ids': [3, 4, 5], 'subaccount_ids': [4, 5, 6], 'recording_ids': ['5', '6', '7'], 'track_unique_ids': [6, 7, 8], 'project_ids': [1, 2, 3], }, 'number_format': NumberFormat.EU, 'file_type': ReportCustomFileType.XLS, } ) assert result.status_code == 200 payload = result.json() del payload['report_custom_id'] # autoincrement ID payload['created_at'] = 'now' assert payload == { 'account_id': account_id, 'contract_id': None, 'subaccount_id': None, 'statement_period_ids': [statement_period_id], 'revenue_type': 'distribution', 'revenue_display_type': 'net', 'dimension_column': dimension_column.value, 'dimension_row': dimension_row.value, 'filters': { 'store_ids': [286, 1, 348], 'country_codes': ['US', 'GB'], 'transaction_type_ids': [1, 2, 3], 'imprint_ids': [1, 2, 3], 'activity_period_ids': [], 'artist_ids': [2, 3, 4], 'product_ids': [3, 4, 5], 'subaccount_ids': [4, 5, 6], 'recording_ids': ['5', '6', '7'], 'track_unique_ids': [6, 7, 8], 'project_ids': [1, 2, 3], }, 'number_format': NumberFormat.EU, 'file_type': ReportCustomFileType.XLS, 'report_custom_status': ReportCustomStatus.IN_PROGRESS.value, 'file_location': None, 'created_at': 'now', 'created_by': 'me', } mock_sqs.send_message.assert_called_once() def test_get_custom_report(fixture_client, ows_client_mock): """Test getting a custom report.""" report_custom_id = 111 profile_type = 'MoneyhubProfile' profile_id = '123' ows_client_mock.get( 'ows-permissions', f'/admin/profile-type/{profile_type}/profile/{profile_id}/resource/all', ).mock( return_value=httpx.Response( 200, json={'items': [{'type': 'Vendor', 'vendorId': '*'}], 'total_count': 1}, ) ) insert_mock_data({ 'account': {'account_id': 24601}, 'report_custom': [ { 'report_custom_id': report_custom_id, 'account_id': 24601, 'statement_period_ids': '123', 'revenue_type': 'distribution', 'dimension_column': ReportCustomColumnDimension.TERRITORY, 'dimension_row': ReportCustomRowDimension.PROJECT, 'number_format': NumberFormat.EU, 'filters': { 'country_codes': ['Denmark', 'GB'], 'artist_ids': [2, 3, 4], 'recording_ids': ['5', '6', '7'], }, 'report_custom_status': ReportCustomStatus.COMPLETE, 'created_at': '2010-09-08T07:06:05', }, ], }) result = fixture_client.get( f'/reports/custom/{report_custom_id}', headers={'orchard-profile-id': profile_id, 'orchard-profile-type': profile_type} ) assert result.status_code == 200 assert result.json() == { 'report_custom_id': report_custom_id, 'account_id': 24601, 'contract_id': None, 'subaccount_id': None, 'file_location': None, 'statement_period_ids': [123], 'revenue_type': 'distribution', 'revenue_display_type': 'net', 'dimension_column': ReportCustomColumnDimension.TERRITORY.value, 'dimension_row': ReportCustomRowDimension.PROJECT.value, 'filters': { 'store_ids': [], 'country_codes': ['Denmark', 'GB'], 'transaction_type_ids': [], 'imprint_ids': [], 'activity_period_ids': [], 'artist_ids': [2, 3, 4], 'product_ids': [], 'subaccount_ids': [], 'recording_ids': ['5', '6', '7'], 'track_unique_ids': [], 'project_ids': [], }, 'number_format': NumberFormat.EU.value, 'file_type': ReportCustomFileType.CSV.value, 'report_custom_status': ReportCustomStatus.COMPLETE.value, 'created_by': '', 'created_at': '2010-09-08T07:06:05', } def test_update_custom_report(fixture_client, ows_client_mock): """Test updating a custom report.""" report_custom_id = 111 profile_type = 'MoneyhubProfile' profile_id = '123' ows_client_mock.get( 'ows-permissions', f'/admin/profile-type/{profile_type}/profile/{profile_id}/resource/all', ).mock( return_value=httpx.Response( 200, json={'items': [{'type': 'Vendor', 'vendorId': '*'}], 'total_count': 1}, ) ) insert_mock_data({ 'account': {'account_id': 24601}, 'report_custom': [ { 'report_custom_id': report_custom_id, 'account_id': 24601, 'statement_period_ids': '123', 'revenue_type': 'distribution', 'dimension_column': ReportCustomColumnDimension.TERRITORY, 'dimension_row': ReportCustomRowDimension.PRODUCT, 'number_format': NumberFormat.US, 'file_type': ReportCustomFileType.TXT, 'report_custom_status': ReportCustomStatus.IN_PROGRESS, 'created_at': '2010-09-08T07:06:05', }, ], }) result = fixture_client.put( f'/reports/custom/{report_custom_id}', headers={'orchard-profile-id': profile_id, 'orchard-profile-type': profile_type}, json={ 'file_location': 's3://somewhere.wav', 'report_custom_status': ReportCustomStatus.COMPLETE.value, } ) assert result.status_code == 200 assert result.json() == { 'report_custom_id': report_custom_id, 'account_id': 24601, 'contract_id': None, 'subaccount_id': None, 'file_location': 's3://somewhere.wav', 'statement_period_ids': [123], 'revenue_type': 'distribution', 'revenue_display_type': 'net', 'dimension_column': ReportCustomColumnDimension.TERRITORY.value, 'dimension_row': ReportCustomRowDimension.PRODUCT.value, 'filters': None, 'number_format': NumberFormat.US, 'file_type': ReportCustomFileType.TXT, 'report_custom_status': ReportCustomStatus.COMPLETE.value, 'created_by': '', 'created_at': '2010-09-08T07:06:05', } def test_delete_custom_report(fixture_client, ows_client_mock): """Test deleting a custom report.""" report_custom_id = 111 profile_type = 'MoneyhubProfile' profile_id = '123' ows_client_mock.get( 'ows-permissions', f'/admin/profile-type/{profile_type}/profile/{profile_id}/resource/all', ).mock( return_value=httpx.Response( 200, json={'items': [{'type': 'Vendor', 'vendorId': '*'}], 'total_count': 1}, ) ) insert_mock_data({ 'account': {'account_id': 24601}, 'report_custom': [ { 'report_custom_id': report_custom_id, 'account_id': 24601, 'subaccount_id': None, 'statement_period_ids': '123', 'revenue_type': 'distribution', 'dimension_column': ReportCustomColumnDimension.TERRITORY, 'dimension_row': ReportCustomRowDimension.PRODUCT, 'report_custom_status': ReportCustomStatus.IN_PROGRESS, 'created_at': '2010-09-08T07:06:05', }, ], }) result = fixture_client.delete( f'/reports/custom/{report_custom_id}', headers={'orchard-profile-id': profile_id, 'orchard-profile-type': profile_type}) assert result.status_code == 204 assert result.text == '' def test_get_custom_reports(fixture_client): """Test getting multiple custom reports.""" account_id = 24601 insert_mock_data({ 'account': {'account_id': account_id}, 'report_custom': [ { 'report_custom_id': 1, 'account_id': account_id, 'statement_period_ids': '123', 'revenue_type': 'distribution', 'dimension_column': ReportCustomColumnDimension.TERRITORY, 'dimension_row': ReportCustomRowDimension.PRODUCT, 'number_format': NumberFormat.EU, 'file_type': ReportCustomFileType.XLS, 'report_custom_status': ReportCustomStatus.IN_PROGRESS, 'created_at': '2010-09-08T07:06:05', }, { 'report_custom_id': 2, 'account_id': account_id, 'statement_period_ids': '123,456', 'revenue_type': 'distribution', 'dimension_column': ReportCustomColumnDimension.TERRITORY, 'dimension_row': ReportCustomRowDimension.PRODUCT, 'number_format': NumberFormat.EU, 'file_type': ReportCustomFileType.XLS, 'report_custom_status': ReportCustomStatus.COMPLETE, 'created_at': '2010-09-08T07:06:05', }, ], }) result = fixture_client.get(f'/reports/custom/account/{account_id}') assert result.status_code == 200 assert result.json() == [ { 'report_custom_id': 2, 'account_id': account_id, 'contract_id': None, 'subaccount_id': None, 'file_location': None, 'statement_period_ids': [123, 456], 'revenue_type': 'distribution', 'revenue_display_type': 'net', 'dimension_column': ReportCustomColumnDimension.TERRITORY.value, 'dimension_row': ReportCustomRowDimension.PRODUCT.value, 'filters': None, 'number_format': NumberFormat.EU, 'file_type': ReportCustomFileType.XLS, 'report_custom_status': ReportCustomStatus.COMPLETE.value, 'created_by': '', 'created_at': '2010-09-08T07:06:05', }, { 'report_custom_id': 1, 'account_id': account_id, 'contract_id': None, 'subaccount_id': None, 'file_location': None, 'statement_period_ids': [123], 'revenue_type': 'distribution', 'revenue_display_type': 'net', 'dimension_column': ReportCustomColumnDimension.TERRITORY.value, 'dimension_row': ReportCustomRowDimension.PRODUCT.value, 'filters': None, 'number_format': NumberFormat.EU, 'file_type': ReportCustomFileType.XLS, 'report_custom_status': ReportCustomStatus.IN_PROGRESS.value, 'created_by': '', 'created_at': '2010-09-08T07:06:05', }, ] @patch('moneyhub.logic.report_custom.create_presigned_url') def test_get_report_presigned_url(mock_create_presigned_url, fixture_client, ows_client_mock): """Test getting a report presigned URL.""" report_custom_id = 111 profile_type = 'MoneyhubProfile' profile_id = '123' secure_url = 's3://place/something.wav?secure=yes' ows_client_mock.get( 'ows-permissions', f'/admin/profile-type/{profile_type}/profile/{profile_id}/resource/all', ).mock( return_value=httpx.Response( 200, json={'items': [{'type': 'Vendor', 'vendorId': '*'}], 'total_count': 1}, ) ) mock_create_presigned_url.return_value = secure_url insert_mock_data({ 'account': {'account_id': 24601}, 'report_custom': [ { 'report_custom_id': report_custom_id, 'account_id': 24601, 'statement_period_ids': '123', 'revenue_type': 'distribution', 'file_location': 's3://place/something.wav', 'dimension_column': ReportCustomColumnDimension.TERRITORY, 'dimension_row': ReportCustomRowDimension.PRODUCT, 'report_custom_status': ReportCustomStatus.COMPLETE, 'created_at': '2010-09-08T07:06:05', }, ], }) result = fixture_client.get( f'/reports/custom/{report_custom_id}/download', headers={'orchard-profile-id': profile_id, 'orchard-profile-type': profile_type}) assert result.status_code == 200 assert result.json() == {'url': secure_url} @patch('moneyhub.logic.report_custom.sqs') def test_regenerate_custom_reports(mock_sqs, fixture_client): """Test to regenerate custom reports for a specified statement period.""" statement_period_id = 123 insert_mock_data({ 'account': {'account_id': 24601}, 'report_custom': [ { 'account_id': 24601, 'statement_period_ids': '123', 'revenue_type': 'distribution', 'number_format': NumberFormat.EU, 'dimension_column': ReportCustomColumnDimension.TERRITORY, 'dimension_row': ReportCustomRowDimension.PRODUCT, 'report_custom_status': ReportCustomStatus.COMPLETE, 'created_at': '2010-09-08T07:06:05', }, { 'account_id': 24601, 'statement_period_ids': '123,456', 'revenue_type': 'distribution', 'number_format': NumberFormat.EU, 'dimension_column': ReportCustomColumnDimension.STATEMENT_PERIOD, 'dimension_row': ReportCustomRowDimension.PRODUCT, 'report_custom_status': ReportCustomStatus.ERROR, 'created_at': '2010-09-08T07:06:05', }, { 'account_id': 24601, 'statement_period_ids': '456', 'revenue_type': 'distribution', 'number_format': NumberFormat.EU, 'dimension_column': ReportCustomColumnDimension.TERRITORY, 'dimension_row': ReportCustomRowDimension.STATEMENT_PERIOD, 'report_custom_status': ReportCustomStatus.IN_PROGRESS, 'created_at': '2010-09-08T07:06:05', }, ], }) endpoint_url = f'/reports/custom/statement-period/{statement_period_id}/regenerate' result = fixture_client.post(endpoint_url) assert result.status_code == 200 result_json = result.json() for item in result_json: del item['report_custom_id'] # autoincrement ID assert result_json == [ { 'account_id': 24601, 'contract_id': None, 'subaccount_id': None, 'statement_period_ids': [statement_period_id], 'revenue_type': 'distribution', 'revenue_display_type': 'net', 'dimension_column': ReportCustomColumnDimension.TERRITORY.value, 'dimension_row': ReportCustomRowDimension.PRODUCT.value, 'filters': None, 'number_format': NumberFormat.EU.value, 'file_type': ReportCustomFileType.CSV.value, 'report_custom_status': ReportCustomStatus.IN_PROGRESS.value, 'file_location': None, 'created_at': '2010-09-08T07:06:05', 'created_by': '', }, { 'account_id': 24601, 'contract_id': None, 'subaccount_id': None, 'statement_period_ids': [statement_period_id, 456], 'revenue_type': 'distribution', 'revenue_display_type': 'net', 'dimension_column': ReportCustomColumnDimension.STATEMENT_PERIOD.value, 'dimension_row': ReportCustomRowDimension.PRODUCT.value, 'filters': None, 'number_format': NumberFormat.EU.value, 'file_type': ReportCustomFileType.CSV.value, 'report_custom_status': ReportCustomStatus.IN_PROGRESS.value, 'file_location': None, 'created_at': '2010-09-08T07:06:05', 'created_by': '', }, ] mock_sqs.send_messages.assert_called_once()