"""Unit tests for Ledger Adjustment schema.""" from decimal import Decimal import pytest from marshmallow import ValidationError from ledger.schemas.ledger_adjustment import ( LedgerAdjustmentDetailSchema, LedgerAdjustmentExtendedSchema, LedgerAdjustmentFilterSchema, LedgerAdjustmentListSchema, LedgerAdjustmentSchema, ) from tests.utils.factories import LedgerAdjustmentDetailFactory, LedgerAdjustmentFactory def test_ledger_adjustment_schema_load(): """Test Ledger Adjustment schema loading.""" test_data = { 'ledger_adjustment_id': 1, 'abacus_event_id': 1, 'account_id': 1, 'contract_id': 1, 'activity_statement_period_id': 1, 'apply_to_statement_period_id': 2, 'reference_adjustment_type_id': 1, 'adjustment_amount': '125.42', 'adjustment_currency_code': 'USD', 'note': 'informative message', 'details': [ { 'ledger_adjustment_detail_id': 1, 'account_id': 1, 'contract_id': 1, 'activity_statement_period_id': 1, 'apply_to_statement_period_id': 2, 'reference_adjustment_type_id': 1, 'currency_code': 'USD', 'amount': '125.42', 'upc': '555444333222111', 'distribution_type': 'digital', 'note': 'informative message', } ], } result = LedgerAdjustmentSchema().load(test_data) assert result == { 'ledger_adjustment_id': 1, 'abacus_event_id': 1, 'account_id': 1, 'contract_id': 1, 'activity_statement_period_id': 1, 'apply_to_statement_period_id': 2, 'reference_adjustment_type_id': 1, 'adjustment_amount': Decimal('125.42'), 'adjustment_currency_code': 'USD', 'note': 'informative message', 'details': [ { 'ledger_adjustment_detail_id': 1, 'account_id': 1, 'contract_id': 1, 'activity_statement_period_id': 1, 'apply_to_statement_period_id': 2, 'reference_adjustment_type_id': 1, 'currency_code': 'USD', 'amount': Decimal(str('125.42')), 'upc': '555444333222111', 'distribution_type': 'digital', 'note': 'informative message', } ], } def test_ledger_adjustment_schema_dump(): """Test Ledger Adjustment schema dumping.""" detail = LedgerAdjustmentDetailFactory.build() entry = LedgerAdjustmentFactory.build(details=[detail]) res = LedgerAdjustmentSchema().dump(entry) assert res == { 'ledger_adjustment_id': entry.ledger_adjustment_id, 'abacus_event_id': entry.abacus_event_id, 'account_id': entry.account_id, 'contract_id': entry.contract_id, 'activity_statement_period_id': entry.activity_statement_period_id, 'apply_to_statement_period_id': entry.apply_to_statement_period_id, 'reference_adjustment_type_id': entry.reference_adjustment_type_id, 'adjustment_amount': str(entry.adjustment_amount), 'adjustment_currency_code': entry.adjustment_currency_code, 'note': entry.note, 'details': [ { 'ledger_adjustment_detail_id': detail.ledger_adjustment_detail_id, 'account_id': detail.account_id, 'contract_id': detail.contract_id, 'activity_statement_period_id': detail.activity_statement_period_id, 'apply_to_statement_period_id': detail.apply_to_statement_period_id, 'reference_adjustment_type_id': detail.reference_adjustment_type_id, 'currency_code': detail.currency_code, 'amount': str(detail.amount), 'upc': detail.upc, 'distribution_type': detail.distribution_type, 'note': detail.note, } ], } def test_ledger_adjustment_list_schema_dump(): """Test Ledger Adjustment List schema dumping.""" mock_data = { 'ledger_adjustment_id': 10, 'account_id': 35968, 'activity_statement_period_id': 260, 'adjustment_ledger_status': 'Applied', 'adjustment_amount': '101.00', 'adjustment_currency_code': 'AUD', 'adjustment_type': 'Admin Fees', 'apply_to_statement_period_id': 269, 'contract_id': 5, 'note': None, 'details': [ { 'ledger_adjustment_detail_id': 1, 'account_id': 1, 'contract_id': 1, 'activity_statement_period_id': 1, 'apply_to_statement_period_id': 2, 'reference_adjustment_type_id': 1, 'currency_code': 'USD', 'amount': '125.42', 'upc': '555444333222111', 'distribution_type': 'digital', 'note': 'informative message', } ], } res = LedgerAdjustmentListSchema().dump(mock_data) assert res == mock_data def test_ledger_adjustment_extended_schema_load(): """Test Ledger Adjustment Extended With Details schema loading.""" test_data = { 'ledger_adjustment_id': 1, 'abacus_event_id': 1, 'account_id': 1, 'contract_id': 1, 'activity_statement_period_id': 1, 'apply_to_statement_period_id': 2, 'reference_adjustment_type_id': 1, 'adjustment_amount': '125.42', 'adjustment_currency_code': 'GBP', 'note': 'informative message', 'account_currency_code': 'USD', 'details': [ { 'ledger_adjustment_detail_id': 1, 'account_id': 1, 'contract_id': 1, 'activity_statement_period_id': 1, 'apply_to_statement_period_id': 2, 'reference_adjustment_type_id': 1, 'currency_code': 'USD', 'amount': '125.42', 'upc': '555444333222111', 'distribution_type': 'digital', 'note': 'informative message', } ], } result = LedgerAdjustmentExtendedSchema().load(test_data) assert result == { 'ledger_adjustment_id': 1, 'abacus_event_id': 1, 'account_id': 1, 'contract_id': 1, 'activity_statement_period_id': 1, 'apply_to_statement_period_id': 2, 'reference_adjustment_type_id': 1, 'adjustment_amount': Decimal('125.42'), 'adjustment_currency_code': 'GBP', 'note': 'informative message', 'account_currency_code': 'USD', 'details': [ { 'ledger_adjustment_detail_id': 1, 'account_id': 1, 'contract_id': 1, 'activity_statement_period_id': 1, 'apply_to_statement_period_id': 2, 'reference_adjustment_type_id': 1, 'currency_code': 'USD', 'amount': Decimal('125.42'), 'upc': '555444333222111', 'distribution_type': 'digital', 'note': 'informative message', } ], } def test_ledger_adjustment_extended_schema_dump(): """Test Ledger Adjustment Extended dumping.""" detail = LedgerAdjustmentDetailFactory.build() entry = LedgerAdjustmentFactory.build(details=[detail]) res = LedgerAdjustmentExtendedSchema().dump(entry) assert res == { 'ledger_adjustment_id': entry.ledger_adjustment_id, 'abacus_event_id': entry.abacus_event_id, 'account_id': entry.account_id, 'contract_id': entry.contract_id, 'activity_statement_period_id': entry.activity_statement_period_id, 'apply_to_statement_period_id': entry.apply_to_statement_period_id, 'reference_adjustment_type_id': entry.reference_adjustment_type_id, 'adjustment_amount': str(entry.adjustment_amount), 'adjustment_currency_code': entry.adjustment_currency_code, 'note': entry.note, 'account_currency_code': entry.account_currency_code, 'details': [ { 'ledger_adjustment_detail_id': detail.ledger_adjustment_detail_id, 'account_id': detail.account_id, 'contract_id': detail.contract_id, 'activity_statement_period_id': detail.activity_statement_period_id, 'apply_to_statement_period_id': detail.apply_to_statement_period_id, 'reference_adjustment_type_id': detail.reference_adjustment_type_id, 'currency_code': detail.currency_code, 'amount': str(detail.amount), 'upc': detail.upc, 'distribution_type': detail.distribution_type, 'note': detail.note, } ], } def test_ledger_adjustment_filter_schema_dump(): """Test Ledger Adjustment filter schema dumping.""" mock_filter_params = { 'limit': 25, 'offset': 0, 'account_id': 1, 'start_apply_to_statement_period_id': 1, 'end_apply_to_statement_period_id': 4, 'show_only_applied_adjustments': 1, } res = LedgerAdjustmentFilterSchema().dump(mock_filter_params) assert res == mock_filter_params def test_ledger_adjustment_filter_schema_validation(): """Test Ledger Adjustment filter schema validation.""" mock_filter_params = { 'limit': 25, 'offset': 0, 'account_id': 1, 'start_apply_to_statement_period_id': 5, 'end_apply_to_statement_period_id': 4, 'show_only_applied_adjustments': 1, } with pytest.raises(ValidationError) as exc_info: res = LedgerAdjustmentFilterSchema().load(mock_filter_params) assert res is None assert exc_info.value.args[0] == { '_schema': ['Please enter a valid range of statement periods.'] } def test_ledger_adjustment_detail_schema_load(): """Test Ledger Adjustment schema loading.""" test_data = { 'ledger_adjustment_detail_id': 1, 'account_id': 1, 'contract_id': 1, 'activity_statement_period_id': 1, 'apply_to_statement_period_id': 2, 'reference_adjustment_type_id': 1, 'currency_code': 'USD', 'amount': '125.42', 'upc': '555444333222111', 'distribution_type': 'digital', 'note': 'informative message', } result = LedgerAdjustmentDetailSchema().load(test_data) assert result == { 'ledger_adjustment_detail_id': 1, 'account_id': 1, 'contract_id': 1, 'activity_statement_period_id': 1, 'apply_to_statement_period_id': 2, 'reference_adjustment_type_id': 1, 'currency_code': 'USD', 'amount': Decimal('125.42'), 'upc': '555444333222111', 'distribution_type': 'digital', 'note': 'informative message', } def test_ledger_adjustment_detail_schema_dump(): """Test Ledger Adjustment schema dumping.""" entry = LedgerAdjustmentDetailFactory.build() res = LedgerAdjustmentDetailSchema().dump(entry) assert res == { 'ledger_adjustment_detail_id': entry.ledger_adjustment_detail_id, 'account_id': entry.account_id, 'contract_id': entry.contract_id, 'activity_statement_period_id': entry.activity_statement_period_id, 'apply_to_statement_period_id': entry.apply_to_statement_period_id, 'reference_adjustment_type_id': entry.reference_adjustment_type_id, 'currency_code': entry.currency_code, 'amount': str(entry.amount), 'upc': entry.upc, 'distribution_type': entry.distribution_type, 'note': entry.note, }