"""Functional tests for Ledger Adjustment.""" from urllib.parse import urlencode from tests.conftest import mock_worksheet_adjustment from tests.utils.factories import ( LedgerAdjustmentAppliedFactory, LedgerAdjustmentDetailFactory, LedgerAdjustmentFactory, ) def test_get_pending_ledger_adjustments_by_statement_period_id( fixture_client, mock_event_fixtures ): """Test to get pending ledger adjustments by statement period id.""" mock_worksheet_adjustment(worksheet_adjustment_id=2) statement_period_id = 2 detail = LedgerAdjustmentDetailFactory.create() ledger_adjustment = [ LedgerAdjustmentFactory.create( apply_to_statement_period_id=statement_period_id, ), LedgerAdjustmentFactory.create( apply_to_statement_period_id=statement_period_id, details=[detail] ), LedgerAdjustmentFactory.create( apply_to_statement_period_id=statement_period_id ), ] LedgerAdjustmentAppliedFactory.create(ledger_adjustment=ledger_adjustment[0]) LedgerAdjustmentAppliedFactory.create( ledger_adjustment=ledger_adjustment[2], worksheet_adjustment_id=2 ) result = fixture_client.get( f'/ledger-adjustment/statement-period/{statement_period_id}/pending' ) expected_result = ledger_adjustment[1] assert result.status_code == 200 assert result.json == { 'items': [ { 'ledger_adjustment_id': expected_result.ledger_adjustment_id, 'abacus_event_id': expected_result.abacus_event_id, 'account_id': expected_result.account_id, 'contract_id': expected_result.contract_id, 'activity_statement_period_id': expected_result.activity_statement_period_id, 'apply_to_statement_period_id': expected_result.apply_to_statement_period_id, 'reference_adjustment_type_id': expected_result.reference_adjustment_type_id, 'adjustment_amount': str(expected_result.adjustment_amount), 'adjustment_currency_code': expected_result.adjustment_currency_code, 'note': expected_result.note, 'account_currency_code': 'USD', '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, } ], } ], 'total_count': 1, } def test_get_ledger_adjustments(fixture_client, mock_event_fixtures): """Test to get a list of ledger_adjustments.""" statement_period_id = 2 detail = LedgerAdjustmentDetailFactory.create() ledger_adjustments = [ LedgerAdjustmentFactory.create( adjustment_amount='1001.9', account_id=1, contract_id=1, apply_to_statement_period_id=statement_period_id, ), LedgerAdjustmentFactory.create( adjustment_amount='500.9', account_id=50, contract_id=2, apply_to_statement_period_id=statement_period_id, details=[detail], ), ] LedgerAdjustmentAppliedFactory.create(ledger_adjustment=ledger_adjustments[0]) result = fixture_client.get('/ledger-adjustments/') assert result.status_code == 200 assert result.json == { 'items': [ { 'abacus_event_id': 1, 'adjustment_amount': '500.90', 'adjustment_currency_code': 'USD', 'adjustment_ledger_status': 'Pending', 'activity_statement_period_id': 1, 'ledger_adjustment_id': 2, 'note': 'informative message', 'apply_to_statement_period_id': 2, 'reference_adjustment_type_id': 1, 'account_id': 50, 'contract_id': 2, 'adjustment_type': 'Adjustment Type', 'details': [ { 'account_id': 1, 'activity_statement_period_id': 1, 'amount': '1500.00', 'apply_to_statement_period_id': 2, 'contract_id': 1, 'currency_code': 'USD', 'distribution_type': 'digital', 'ledger_adjustment_detail_id': 1, 'note': 'informative message', 'reference_adjustment_type_id': detail.reference_adjustment_type_id, 'upc': '555444333222111', } ], }, { 'abacus_event_id': 1, 'adjustment_amount': '1001.90', 'adjustment_currency_code': 'USD', 'adjustment_ledger_status': 'Applied', 'activity_statement_period_id': 1, 'reference_adjustment_type_id': 1, 'ledger_adjustment_id': 1, 'note': 'informative message', 'apply_to_statement_period_id': 2, 'account_id': 1, 'contract_id': 1, 'adjustment_type': 'Adjustment Type', 'details': [], }, ], 'total_count': 2, } def test_get_filtered_ledger_adjustments(fixture_client, mock_event_fixtures): """Test to get filtered list of Ledger Adjustments.""" detail = LedgerAdjustmentDetailFactory.create() statement_period_id = 2 ledger_adjustments = [ LedgerAdjustmentFactory.create( adjustment_amount='1001.9', account_id=1, contract_id=1, apply_to_statement_period_id=statement_period_id, details=[detail], ), LedgerAdjustmentFactory.create( adjustment_amount='500.9', account_id=50, contract_id=2, apply_to_statement_period_id=statement_period_id, ), ] LedgerAdjustmentAppliedFactory.create(ledger_adjustment=ledger_adjustments[0]) 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, } result = fixture_client.get(f'/ledger-adjustments/?{urlencode(params)}') assert result.status_code == 200 assert result.json == { 'items': [ { 'abacus_event_id': 1, 'adjustment_amount': '1001.90', 'adjustment_currency_code': 'USD', 'adjustment_ledger_status': 'Applied', 'activity_statement_period_id': 1, 'ledger_adjustment_id': 1, 'note': 'informative message', 'apply_to_statement_period_id': 2, 'reference_adjustment_type_id': 1, 'account_id': 1, 'contract_id': 1, 'adjustment_type': 'Adjustment Type', 'details': [ { 'account_id': 1, 'activity_statement_period_id': 1, 'amount': '1500.00', 'apply_to_statement_period_id': 2, 'contract_id': 1, 'currency_code': 'USD', 'distribution_type': 'digital', 'ledger_adjustment_detail_id': 1, 'note': 'informative message', 'reference_adjustment_type_id': detail.reference_adjustment_type_id, 'upc': '555444333222111', } ], } ], 'total_count': 1, } def test_get_filtered_ledger_adjustments_validation_error( fixture_client, mock_event_fixtures ): """Test to get filtered list of Ledger Adjustments.""" params = { 'limit': 25, 'offset': 0, 'account_id': 1, 'start_apply_to_statement_period_id': 7, 'end_apply_to_statement_period_id': 4, 'show_only_applied_adjustments': 1, } result = fixture_client.get(f'/ledger-adjustments/?{urlencode(params)}') assert result.status_code == 400 assert ( result.json['message'] == "{'_schema': ['Please enter a valid range of statement periods.']}" )