"""Custom report model tests.""" from datetime import datetime from unittest.mock import patch from fastapi import HTTPException import pytest 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 RevenueDisplayType from moneyhub.constants.constants import RevenueType from moneyhub.constants.error import ENTITY_DOES_NOT_EXIST from moneyhub.models.report_custom import ReportCustom from tests.unit.conftest import insert_mock_account from tests.unit.conftest import insert_mock_data from tests.unit.conftest import insert_mock_report_custom @patch('moneyhub.models.report_custom.datetime') def test_init(datetime_mock): """Test creating a new model object.""" datetime_mock.now.return_value = datetime(2010, 4, 12) report = ReportCustom(account_id=24601) assert report.account_id == 24601 assert report.created_at == datetime(2010, 4, 12) def test_get_by_account_id(): """Test getting reports by account ID.""" account_id = 24601 insert_mock_account(account_id) insert_mock_report_custom(report_custom_id=1, account_id=account_id) insert_mock_report_custom(report_custom_id=2, account_id=account_id) insert_mock_account(account_id + 1) insert_mock_report_custom(report_custom_id=3, account_id=account_id + 1) result = ReportCustom.get_by_account_id(account_id, None) assert len(result) == 2 assert result[0].report_custom_id == 2 assert result[1].report_custom_id == 1 def test_get_by_account_id_and_contract_id(): """Test getting reports by account ID and contract ID.""" account_id = 24601 contract_id = 1 insert_mock_account(account_id) insert_mock_report_custom( report_custom_id=1, account_id=account_id, contract_id=contract_id) result = ReportCustom.get_by_account_id(account_id, contract_id) assert result[0].report_custom_id == 1 assert result[0].contract_id == 1 def test_get_by_account_id_for_d3(): """Test getting reports by account ID that has subaccounts.""" account_id = 24601 subaccount_id = 456 insert_mock_account(account_id) insert_mock_report_custom( report_custom_id=1, account_id=account_id, subaccount_id=subaccount_id ) insert_mock_report_custom(report_custom_id=2, account_id=account_id, subaccount_id=789) insert_mock_report_custom(report_custom_id=3, account_id=account_id, subaccount_id=None) result = ReportCustom.get_by_account_id(account_id, None, None, False) assert len(result) == 1 assert result[0].report_custom_id == 3 assert result[0].subaccount_id is None def test_get_by_account_id_with_subaccount_filter(): """Test getting reports by account ID filtered by subaccount_id.""" account_id = 24601 subaccount_id = 456 insert_mock_account(account_id) insert_mock_report_custom( report_custom_id=1, account_id=account_id, subaccount_id=subaccount_id ) insert_mock_report_custom(report_custom_id=2, account_id=account_id, subaccount_id=789) insert_mock_report_custom(report_custom_id=3, account_id=account_id, subaccount_id=None) result = ReportCustom.get_by_account_id(account_id, None, subaccount_id, True) assert len(result) == 1 assert result[0].report_custom_id == 1 assert result[0].subaccount_id == subaccount_id def test_update_custom_report(): """Test deleting a reports by ID.""" account_id = 24601 report_custom_id = 123 file_location = 's3://test.csv' report_custom_status = ReportCustomStatus.COMPLETE payload = { 'file_location': file_location, 'report_custom_status': report_custom_status, } insert_mock_account(account_id) insert_mock_report_custom(report_custom_id=report_custom_id, account_id=account_id) result = ReportCustom.update_custom_report(report_custom_id, payload) assert result.file_location == file_location assert result.report_custom_status == report_custom_status def test_delete_id(): """Test deleting a reports by ID.""" account_id = 24601 report_custom_id = 123 insert_mock_account(account_id) insert_mock_report_custom(report_custom_id=report_custom_id, account_id=account_id) existing_result = ReportCustom.get_by_id_or_error(report_custom_id) assert existing_result.report_custom_id == report_custom_id ReportCustom.delete_by_id(report_custom_id) with pytest.raises(HTTPException) as error: ReportCustom.get_by_id_or_error(report_custom_id) assert error.value.status_code == 404 assert error.value.detail == ENTITY_DOES_NOT_EXIST.format( object_type='ReportCustom', object_id=report_custom_id) def test_delete_not_found(): """Test deleting a report that doesn't exist.""" report_custom_id = 123 with pytest.raises(HTTPException) as error: ReportCustom.delete_by_id(report_custom_id) assert error.value.status_code == 404 assert error.value.detail == ENTITY_DOES_NOT_EXIST.format( object_type='ReportCustom', object_id=report_custom_id) @pytest.mark.parametrize('filters, expected_filters', [ (None, None), ( { 'store_ids': [286, 1, 348], 'country_codes': ['US', 'GB'], 'transaction_type_ids': [1, 2, 3], 'imprint_ids': [1, 2, 3], 'activity_period_ids': [1, 2, 3] }, { 'store_ids': [286, 1, 348], 'country_codes': ['US', 'GB'], 'transaction_type_ids': [1, 2, 3], 'imprint_ids': [1, 2, 3], 'activity_period_ids': [1, 2, 3] } ), ]) def test_creating_reports_then_retrieving_by_statement_period(filters, expected_filters): """Test inserting reports then getting them by statement periods.""" created_at = datetime.now() insert_mock_data({ 'account': {'account_id': 24601}, 'report_custom': [ { 'account_id': 24601, 'statement_period_ids': '123', 'revenue_type': 'distribution', 'dimension_column': 'territory', 'dimension_row': 'product', 'filters': filters, 'number_format': 'us', 'file_type': 'csv', 'report_custom_status': 'in_progress', 'created_at': created_at, 'created_by': 'test' }, { 'account_id': 24601, 'statement_period_ids': '123,456', 'revenue_type': 'distribution', 'dimension_column': 'statement_period', 'dimension_row': 'product', 'filters': filters, 'report_custom_status': 'in_progress', 'number_format': 'us', 'file_type': 'csv', 'created_at': created_at, 'created_by': 'test' }, { 'account_id': 24601, 'statement_period_ids': '456', 'revenue_type': 'distribution', 'dimension_column': 'territory', 'dimension_row': 'statement_period', 'filters': filters, 'number_format': 'us', 'file_type': 'csv', 'report_custom_status': 'in_progress', 'created_at': created_at, 'created_by': 'test' }, ], }) result = ReportCustom.get_by_statement_period(123, None) assert [item.to_dict() for item in result] == [ {'report_custom_id': 1, 'account_id': 24601, 'contract_id': None, 'subaccount_id': None, 'statement_period_ids': '123', 'revenue_type': RevenueType.DISTRIBUTION, 'revenue_display_type': RevenueDisplayType.NET, 'dimension_column': ReportCustomColumnDimension.TERRITORY, 'dimension_row': ReportCustomRowDimension.PRODUCT, 'filters': expected_filters, 'number_format': NumberFormat.US, 'file_type': ReportCustomFileType.CSV, 'file_location': None, 'report_custom_status': ReportCustomStatus.IN_PROGRESS, 'created_at': created_at, 'created_by': 'test'}, {'report_custom_id': 2, 'account_id': 24601, 'contract_id': None, 'subaccount_id': None, 'statement_period_ids': '123,456', 'revenue_type': RevenueType.DISTRIBUTION, 'revenue_display_type': RevenueDisplayType.NET, 'dimension_column': ReportCustomColumnDimension.STATEMENT_PERIOD, 'dimension_row': ReportCustomRowDimension.PRODUCT, 'filters': expected_filters, 'number_format': NumberFormat.US, 'file_type': ReportCustomFileType.CSV, 'file_location': None, 'report_custom_status': ReportCustomStatus.IN_PROGRESS, 'created_at': created_at, 'created_by': 'test'}, ] def test_get_by_statement_period_and_ids(): """Test getting reports by statement periods filtered by report ID.""" report_custom_ids = [1, 2] insert_mock_data({ 'account': {'account_id': 24601}, 'report_custom': [ { 'report_custom_id': 1, 'account_id': 24601, 'statement_period_ids': '123', 'revenue_type': 'distribution', 'dimension_column': 'territory', 'dimension_row': 'product', 'filters': None, 'number_format': 'us', 'file_type': 'csv', 'report_custom_status': 'in_progress', 'created_at': datetime.now(), 'created_by': 'test', }, { 'report_custom_id': 2, 'account_id': 24601, 'statement_period_ids': '123,456', 'revenue_type': 'distribution', 'dimension_column': 'statement_period', 'dimension_row': 'product', 'filters': None, 'number_format': 'us', 'file_type': 'csv', 'report_custom_status': 'in_progress', 'created_at': datetime.now(), 'created_by': 'test', }, { 'report_custom_id': 3, 'account_id': 24601, 'statement_period_ids': '100,123', 'revenue_type': 'distribution', 'dimension_column': 'territory', 'dimension_row': 'statement_period', 'filters': None, 'number_format': 'us', 'file_type': 'csv', 'report_custom_status': 'in_progress', 'created_at': datetime.now(), 'created_by': 'test', }, ], }) result = ReportCustom.get_by_statement_period(123, report_custom_ids) assert [item.report_custom_id for item in result] == report_custom_ids def test_get_stuck_in_progress(): """Test getting reports stuck in progress.""" account_id = 24601 insert_mock_account(account_id) insert_mock_report_custom( report_custom_id=1, account_id=account_id, report_custom_status=ReportCustomStatus.COMPLETE) insert_mock_report_custom(report_custom_id=2, account_id=account_id) insert_mock_account(account_id + 1) insert_mock_report_custom(report_custom_id=3, account_id=account_id + 1) result = ReportCustom.get_stuck_in_progress() assert len(result) == 2 assert result[0].report_custom_id == 2 assert result[1].report_custom_id == 3