"""Unit tests for Ledger Correction model.""" from decimal import Decimal from ledger.constants.constants import CORRECTION_TYPES from ledger.models.ledger_correction import LedgerCorrection from tests.utils.factories import LedgerCorrectionFactory def test_create_ledger_correction(mock_event_fixtures, mock_worksheet_correction): """Create a Ledger Correction entry.""" records = LedgerCorrection.query.all() assert len(records) == 0 LedgerCorrectionFactory.create( abacus_event_id=1, worksheet_correction_id=1, account_id=1, contract_id=1, statement_period_id=1, correction_statement_period_id=2, correction_type=CORRECTION_TYPES.ROYALTY_CORRECTION, currency_code='USD', gross_revenue=Decimal('2000.11'), distribution_fee=Decimal('-500.00'), net_revenue=Decimal('1500.11'), ) records = LedgerCorrection.query.all() assert len(records) == 1 assert records[0].net_revenue == Decimal('1500.11') assert records[0].mechanical_deduction_total is None assert records[0].mechanical_deduction_admin_fee_total is None def test_create_ledger_correction_with_mech_columns( mock_event_fixtures, mock_worksheet_correction ): """Create a Ledger Correction entry including mech deduction columns.""" records = LedgerCorrection.query.all() assert len(records) == 0 LedgerCorrectionFactory.create( abacus_event_id=1, worksheet_correction_id=1, account_id=1, contract_id=1, statement_period_id=1, correction_statement_period_id=2, correction_type=CORRECTION_TYPES.ROYALTY_CORRECTION, currency_code='USD', gross_revenue=Decimal('2000.11'), mechanical_deduction_total=Decimal('11.90'), mechanical_deduction_admin_fee_total=Decimal('5.90'), distribution_fee=Decimal('-500.00'), net_revenue=Decimal('1500.11'), ) records = LedgerCorrection.query.all() assert len(records) == 1 assert records[0].net_revenue == Decimal('1500.11') assert records[0].mechanical_deduction_total == Decimal('11.90') assert records[0].mechanical_deduction_admin_fee_total == Decimal('5.90') def test_get_by_worksheet_correction_ids( mock_event_fixtures, mock_worksheet_correction ): """Test to get ledger_correction records.""" worksheet_correction_id = 1 LedgerCorrectionFactory.create( abacus_event_id=1, worksheet_correction_id=worksheet_correction_id, account_id=1, contract_id=1, statement_period_id=1, correction_statement_period_id=2, correction_type=CORRECTION_TYPES.ROYALTY_CORRECTION, currency_code='USD', gross_revenue=Decimal('2000.11'), distribution_fee=Decimal('-500.00'), net_revenue=Decimal('1500.11'), ) LedgerCorrectionFactory.create( abacus_event_id=1, worksheet_correction_id=worksheet_correction_id, account_id=50, contract_id=2, statement_period_id=1, correction_statement_period_id=2, correction_type=CORRECTION_TYPES.ROYALTY_CORRECTION, currency_code='AUD', gross_revenue=Decimal('67.99'), distribution_fee=Decimal('0.00'), net_revenue=Decimal('67.99'), ) items = LedgerCorrection.get_by_worksheet_correction_ids([1]) assert len(items) == 2 assert all( [item.worksheet_correction_id == worksheet_correction_id for item in items] )