"""Worksheet Adjustment detail model tests.""" from datetime import datetime from moneyhub.models.worksheet_adjustment_detail import WorksheetAdjustmentDetail from tests.unit.conftest import insert_mock_data def _insert_mock_data(): """Insert mock data for the tests.""" insert_mock_data({ 'account': [ {'account_id': 24601}, {'account_id': 999}, ], 'reference_payment_entity': {'reference_payment_entity_id': 1}, 'reference_sap_profit_center': { 'reference_sap_profit_center_id': 1, 'profit_center': 'UK1234', 'company_code': '1234', 'business_group': 'ORC' }, 'reference_signing_entity': { 'reference_signing_entity_id': 1, 'reference_payment_entity_id': 1, 'reference_sap_profit_center_id': 1, 'company_code': '1234', 'tax_entity_company_code': '1234', 'legal_name': 'Company', 'vat_number': '12341234', 'company_registration_number': '12341234', 'address': None, }, 'contract': [ { 'contract_id': 1001, 'reference_signing_entity_id': 1, }, { 'contract_id': 999, 'reference_signing_entity_id': 1, }, ], 'abacus_event': [ {'abacus_event_id': 1}, {'abacus_event_id': 2}, {'abacus_event_id': 3}, {'abacus_event_id': 4}, {'abacus_event_id': 5}, ], 'reference_adjustment_type': [ { 'reference_adjustment_type_id': 1, 'type_name': 'Label Earnings' }, { 'reference_adjustment_type_id': 2, 'type_name': 'Publisher Earnings' } ], 'statement_period': [ {'statement_period_id': 111, 'statement_period_status': 'closed'}, {'statement_period_id': 123, 'statement_period_status': 'closed'}, {'statement_period_id': 124, 'statement_period_status': 'current'}, ], 'statement_period_adjustment_file': { 'statement_period_adjustment_file_id': 1, 'statement_period_id': 123, }, 'worksheet_adjustment': [ { 'worksheet_adjustment_id': 1, 'abacus_event_id': 1, 'statement_period_adjustment_file_id': 1, 'account_id': 24601, 'contract_id': 1001, 'activity_statement_period_id': 123, 'apply_to_statement_period_id': 123, 'reference_adjustment_type_id': 1, 'adjustment_amount': 1000, 'adjustment_currency_code': 'USD', }, { 'worksheet_adjustment_id': 2, 'abacus_event_id': 2, 'statement_period_adjustment_file_id': 1, 'account_id': 24601, 'contract_id': 1001, 'activity_statement_period_id': 124, 'apply_to_statement_period_id': 124, 'reference_adjustment_type_id': 1, 'adjustment_amount': 1000, 'adjustment_currency_code': 'USD', }, { 'worksheet_adjustment_id': 3, 'abacus_event_id': 3, 'statement_period_adjustment_file_id': 1, 'account_id': 24601, 'contract_id': 1001, 'activity_statement_period_id': 123, 'apply_to_statement_period_id': 123, 'reference_adjustment_type_id': 1, 'adjustment_amount': 1000, 'adjustment_currency_code': 'USD', }, { 'worksheet_adjustment_id': 4, 'abacus_event_id': 4, 'statement_period_adjustment_file_id': 1, 'account_id': 999, 'contract_id': 1001, 'activity_statement_period_id': 123, 'apply_to_statement_period_id': 123, 'reference_adjustment_type_id': 1, 'adjustment_amount': 1000, 'adjustment_currency_code': 'USD', }, { 'worksheet_adjustment_id': 5, 'abacus_event_id': 5, 'statement_period_adjustment_file_id': 1, 'account_id': 24601, 'contract_id': 1001, 'activity_statement_period_id': 124, 'apply_to_statement_period_id': 124, 'reference_adjustment_type_id': 1, 'adjustment_amount': 1000, 'adjustment_currency_code': 'USD', }, ], 'worksheet_adjustment_detail': [ { 'worksheet_adjustment_detail_id': 1, 'worksheet_adjustment_id': 1, 'statement_period_adjustment_file_id': 1, 'account_id': 24601, 'contract_id': 1001, 'activity_statement_period_id': 123, 'amount': '1000.00', 'apply_to_statement_period_id': 123, 'currency_code': 'USD', 'created_at': datetime(2022, 1, 1), 'created_by': 'someone', 'distribution_type': 'digital', 'last_modified': datetime(2022, 5, 1), 'last_modified_by': 'someone', 'reference_adjustment_type_id': 1, 'note': 'mock adjustment', 'upc': '12345678' }, { 'worksheet_adjustment_detail_id': 2, 'worksheet_adjustment_id': 2, 'statement_period_adjustment_file_id': 1, 'account_id': 24601, 'contract_id': 1001, 'activity_statement_period_id': 124, 'amount': '1000.00', 'apply_to_statement_period_id': 124, 'currency_code': 'USD', 'created_at': datetime(2022, 1, 1), 'created_by': 'someone', 'distribution_type': 'physical', 'last_modified': datetime(2022, 5, 1), 'last_modified_by': 'someone', 'reference_adjustment_type_id': 1, 'note': 'mock adjustment', 'upc': '087654321' }, { 'worksheet_adjustment_detail_id': 3, 'worksheet_adjustment_id': 3, 'statement_period_adjustment_file_id': 1, 'account_id': 24601, 'contract_id': 1001, 'activity_statement_period_id': 123, 'amount': '1000.00', 'apply_to_statement_period_id': 123, 'currency_code': 'USD', 'created_at': datetime(2022, 1, 1), 'created_by': 'someone', 'distribution_type': 'digital', 'last_modified': datetime(2022, 5, 1), 'last_modified_by': 'someone', 'reference_adjustment_type_id': 1, 'note': 'mock adjustment', 'upc': '12345678' }, { 'worksheet_adjustment_detail_id': 4, 'worksheet_adjustment_id': 4, 'statement_period_adjustment_file_id': 1, 'account_id': 999, 'contract_id': 1001, 'activity_statement_period_id': 123, 'amount': '1000.00', 'apply_to_statement_period_id': 123, 'currency_code': 'USD', 'created_at': datetime(2022, 1, 1), 'created_by': 'someone', 'distribution_type': 'digital', 'last_modified': datetime(2022, 5, 1), 'last_modified_by': 'someone', 'reference_adjustment_type_id': 1, 'note': 'mock adjustment', 'upc': '43214321' }, { 'worksheet_adjustment_detail_id': 5, 'worksheet_adjustment_id': 5, 'statement_period_adjustment_file_id': 1, 'account_id': 24601, 'contract_id': 1001, 'activity_statement_period_id': 124, 'amount': '1000.00', 'apply_to_statement_period_id': 124, 'currency_code': 'USD', 'created_at': datetime(2022, 1, 1), 'created_by': 'someone', 'distribution_type': 'physical', 'last_modified': datetime(2022, 5, 1), 'last_modified_by': 'someone', 'reference_adjustment_type_id': 2, 'note': 'mock adjustment', 'upc': '12345678' }, ], 'ledger_adjustment_applied': [ { 'ledger_adjustment_applied_id': 1, 'worksheet_adjustment_id': 1, 'abacus_event_id': 1, 'account_id': 24601, 'contract_id': 1001, 'statement_period_id': 123, 'adjustment_amount': 1000, 'adjustment_currency_code': 'USD', 'adjustment_amount_payee_currency': 800, 'adjustment_payee_currency_code': 'GBP', }, { 'ledger_adjustment_applied_id': 2, 'worksheet_adjustment_id': 2, 'abacus_event_id': 2, 'account_id': 24601, 'contract_id': 1001, 'statement_period_id': 124, 'adjustment_amount': 1000, 'adjustment_currency_code': 'USD', 'adjustment_amount_payee_currency': 800, 'adjustment_payee_currency_code': 'GBP', }, { 'ledger_adjustment_applied_id': 3, 'worksheet_adjustment_id': 3, 'abacus_event_id': 3, 'account_id': 24601, 'contract_id': 1001, 'statement_period_id': 123, 'adjustment_amount': 1000, 'adjustment_currency_code': 'USD', 'adjustment_amount_payee_currency': 800, 'adjustment_payee_currency_code': 'GBP', }, { 'ledger_adjustment_applied_id': 4, 'worksheet_adjustment_id': 4, 'abacus_event_id': 4, 'account_id': 999, 'contract_id': 1001, 'statement_period_id': 123, 'adjustment_amount': 1000, 'adjustment_currency_code': 'USD', 'adjustment_amount_payee_currency': 800, 'adjustment_payee_currency_code': 'GBP', }, { 'ledger_adjustment_applied_id': 5, 'worksheet_adjustment_id': 5, 'abacus_event_id': 5, 'account_id': 24601, 'contract_id': 1001, 'statement_period_id': 124, 'adjustment_amount': 1000, 'adjustment_currency_code': 'USD', 'adjustment_amount_payee_currency': 800, 'adjustment_payee_currency_code': 'GBP', }, ], 'ledger_adjustment_detail_applied': [ { 'ledger_adjustment_detail_applied_id': 1, 'worksheet_adjustment_detail_id': 1, 'adjustment_amount': 1000, 'adjustment_currency_code': 'USD', 'adjustment_amount_payee_currency': 800, 'adjustment_payee_currency_code': 'GBP', }, { 'ledger_adjustment_detail_applied_id': 2, 'worksheet_adjustment_detail_id': 2, 'adjustment_amount': 1000, 'adjustment_currency_code': 'USD', 'adjustment_amount_payee_currency': 800, 'adjustment_payee_currency_code': 'GBP', }, { 'ledger_adjustment_detail_applied_id': 3, 'worksheet_adjustment_detail_id': 3, 'adjustment_amount': 1000, 'adjustment_currency_code': 'USD', 'adjustment_amount_payee_currency': 800, 'adjustment_payee_currency_code': 'GBP', }, { 'ledger_adjustment_detail_applied_id': 4, 'worksheet_adjustment_detail_id': 4, 'adjustment_amount': 1000, 'adjustment_currency_code': 'USD', 'adjustment_amount_payee_currency': 800, 'adjustment_payee_currency_code': 'GBP', }, { 'ledger_adjustment_detail_applied_id': 5, 'worksheet_adjustment_detail_id': 5, 'adjustment_amount': 1000, 'adjustment_currency_code': 'USD', 'adjustment_amount_payee_currency': 800, 'adjustment_payee_currency_code': 'GBP', }, ] }) def test_get_by_account_id(): """Test to get ledger adjustments details for a specified account.""" account_id = 24601 visible_periods = [111, 123, 124, 125] _insert_mock_data() result = WorksheetAdjustmentDetail.get_by_account_id( account_id, visible_periods, None, None, None, None, None, None) assert [item.worksheet_adjustment_detail_id for item in result] == [5, 3, 2, 1] def test_get_by_account_id_and_statement_period_range(): """Test ledger adjustments details for a specified account with statement period range.""" account_id = 24601 visible_periods = [111, 123, 124, 125] statement_period_id_start = 120 statement_period_id_end = 130 _insert_mock_data() result = WorksheetAdjustmentDetail.get_by_account_id( account_id, visible_periods, None, statement_period_id_start, statement_period_id_end, None, None, None) assert [item.worksheet_adjustment_detail_id for item in result] == [5, 3, 2, 1] def test_get_by_account_id_with_upc_distribution_type(): """Test to get ledger adjustments details for a specified account filtered by UPC and type.""" account_id = 24601 upc = '12345678' visible_periods = [111, 123, 124, 125] _insert_mock_data() result = WorksheetAdjustmentDetail.get_by_account_id( account_id, visible_periods, None, None, None, upc, None, 'digital') assert len(result) == 2 assert [item.distribution_type for item in result] == ['digital', 'digital'] assert [item.upc for item in result] == [upc, upc] def test_get_by_account_id_with_group_by(): """Test to get ledger adjustments details grouped by UPC.""" account_id = 24601 upc = '12345678' group_by = 'upc' visible_periods = [111, 123, 124, 125] _insert_mock_data() result = WorksheetAdjustmentDetail.get_by_account_id( account_id, visible_periods, None, None, None, upc, None, None, group_by) assert [item.upc for item in result] == [upc, upc] assert [item.distribution_type for item in result] == ['physical', 'digital'] assert len(result) == 2 def test_get_by_account_id_with_group_by_expense_type_id(): """Test to get ledger adjustments details grouped by expense type id.""" account_id = 24601 visible_periods = [111, 123, 124, 125] group_by = 'expense_type_id' _insert_mock_data() result = WorksheetAdjustmentDetail.get_by_account_id( account_id, visible_periods, None, None, None, None, None, None, group_by) assert len(result) == 2 def test_get_by_account_id_with_expense_type(): """Test to get ledger adjustments details for a specified account filtered by expenses type.""" account_id = 24601 visible_periods = [111, 123, 124, 125] expense_type_id = 2 _insert_mock_data() result = WorksheetAdjustmentDetail.get_by_account_id( account_id, visible_periods, None, None, None, None, expense_type_id, None) assert [item.reference_adjustment_type_id for item in result] == [2] def test_get_upcs_by_account(): """Test getting UPCs based on an account.""" account_id = 24601 _insert_mock_data() result = WorksheetAdjustmentDetail.get_upcs_by_account(account_id) assert [dict(item) for item in result] == [ {'account_id': account_id, 'distribution_type': 'physical', 'upc': '087654321'}, {'account_id': account_id, 'distribution_type': 'digital', 'upc': '12345678'}, {'account_id': account_id, 'distribution_type': 'physical', 'upc': '12345678'}, ] def test_get_account_adjustment_detail_activity(): """Test getting adjustment detail activity.""" account_id = 24601 _insert_mock_data() result = WorksheetAdjustmentDetail.get_account_adjustment_detail_activity(account_id) assert bool(result) is True def test_get_account_adjustment_detail_activity_no_activity(): """Test getting adjustment detail activity no activity.""" account_id = 24601 insert_mock_data({ 'account': {'account_id': account_id}, }) result = WorksheetAdjustmentDetail.get_account_adjustment_detail_activity(account_id) assert bool(result) is False