"""Functional tests for Ledger Accounting Run Balance.""" from decimal import Decimal from ledger.constants.constants import VAT_CATEGORIES_OLD from ledger.constants.error import INVALID_VAT_CATEGORY from tests.utils.factories import LedgerAccountingRunBalanceFactory def test_bulk_create_ledger_accounting_run_balances( fixture_client, mock_bulk_ledger_accounting_run_balances_body, mock_event_fixtures ): """Test bulk create Ledger Accounting Run Balance.""" res = fixture_client.post( '/ledger-accounting-run-balance/1/bulk', json=mock_bulk_ledger_accounting_run_balances_body, ) assert res.status_code == 201 items = res.json assert len(items) == 3 for i in items: del i['ledger_accounting_run_balance_id'] assert sorted(items, key=lambda x: x['currency_code']) == [ { 'accounting_run_id': 1, 'abacus_event_id': 1, 'adjusted_net_revenue': '-430.69', 'contract_id': 1, 'currency_code': 'EUR', 'distribution_fee': '95.00', 'mechanical_deduction_admin_fee_total': '534.00', 'mechanical_deduction_total': '333.00', 'total_gross_revenue_amount': '531.31', 'total_net_revenue_amount': '436.31', }, { 'accounting_run_id': 1, 'abacus_event_id': 1, 'adjusted_net_revenue': '418.31', 'contract_id': 1, 'currency_code': 'GBP', 'distribution_fee': '200.00', 'mechanical_deduction_admin_fee_total': '50.00', 'mechanical_deduction_total': '33.00', 'total_gross_revenue_amount': '701.31', 'total_net_revenue_amount': '501.31', }, { 'accounting_run_id': 1, 'abacus_event_id': 1, 'adjusted_net_revenue': '609.09', 'contract_id': 1, 'currency_code': 'USD', 'distribution_fee': '50.72', 'mechanical_deduction_admin_fee_total': '27.22', 'mechanical_deduction_total': '23.00', 'total_gross_revenue_amount': '710.03', 'total_net_revenue_amount': '659.31', }, ] def test_get_ledger_accounting_run_balances(fixture_client, mock_event_fixtures): """Test get list of Ledger Accounting Run Balance.""" larb = LedgerAccountingRunBalanceFactory.create( total_gross_revenue_amount=Decimal('200.00'), total_net_revenue_amount=Decimal('150.00'), mechanical_deduction_total=Decimal('40.00'), mechanical_deduction_admin_fee_total=Decimal('10.00'), ) res = fixture_client.get('/ledger-accounting-run-balance/1/') account_name = 'Test 1' contract_name = f'foo-{larb.contract_id}-1' assert res.status_code == 200 assert res.json['items'] == [ { 'ledger_accounting_run_balance_id': larb.ledger_accounting_run_balance_id, 'abacus_event_id': larb.abacus_event_id, 'account_id': 1, 'account_name': account_name, 'accounting_run_id': larb.accounting_run_id, 'adjusted_net_revenue': str(larb.adjusted_net_revenue), 'contract_id': larb.contract_id, 'contract_name': contract_name, 'currency_code': larb.currency_code, 'distribution_fee': str(larb.distribution_fee), 'mechanical_deduction_admin_fee_total': str( larb.mechanical_deduction_admin_fee_total ), 'mechanical_deduction_total': str(larb.mechanical_deduction_total), 'total_gross_revenue_amount': str(larb.total_gross_revenue_amount), 'total_net_revenue_amount': str(larb.total_net_revenue_amount), } ] assert res.json['total_count'] == 1 def test_bulk_create_error_unknown_currency( fixture_client, mock_bulk_ledger_accounting_run_balances_body ): """Test unknown currency code.""" records = mock_bulk_ledger_accounting_run_balances_body records[0]['currency_code'] = 'TEST' res = fixture_client.post('/ledger-accounting-run-balance/1/bulk', json=records) assert res.status_code == 400 assert res.json['code'] == 'error' assert 'Currency code not recognized: TEST' in res.json['message'] def test_get_ledger_acc_run_balance_by_acc_period_id( fixture_client, mock_event_fixtures ): """Test to get Ledger Accounting Run Balance entries by accounting_period_id.""" entry1 = LedgerAccountingRunBalanceFactory.create( accounting_run_id=2, contract_id=4 ) res = fixture_client.get( '/ledger-accounting-run-balance/accounting-period/1/vat-category/vat_applied/' ) assert res.status_code == 200 assert res.json['total_count'] == 1 assert res.json['items'][0]['accounting_run_id'] == entry1.accounting_run_id assert res.json['items'][0]['payment_entity_id'] == 1 assert res.json['items'][0]['country_of_tax_residence'] == 'GBR' assert res.json['items'][0]['is_vat_exempt'] is False def test_get_ledger_acc_run_balance_for_invalid_vat_category( fixture_client, mock_event_fixtures ): """Test to get list of ledger account run balance for an invalid vat category.""" LedgerAccountingRunBalanceFactory.create() res = fixture_client.get( '/ledger-accounting-run-balance/accounting-period/1/vat-category/test/' ) assert res.status_code == 400 assert res.json['message'] == INVALID_VAT_CATEGORY.format( VAT_CATEGORIES_OLD=(', '.join(VAT_CATEGORIES_OLD)) ) def test_get_ledger_acc_run_balance_by_accounting_period_only_committed_runs( fixture_client, mock_event_fixtures ): """Test GET ledger_accounting_run_balance_entries for committed runs only.""" LedgerAccountingRunBalanceFactory.create(accounting_run_id=3, contract_id=2) res = fixture_client.get( '/ledger-accounting-run-balance/accounting-period/1/vat-category/vat_applied/' ) assert res.status_code == 200 assert res.json['total_count'] == 0 assert not res.json['items'] def test_post_contract_count_dataloader_returns_expected_data( fixture_client, mock_event_fixtures ): """Test dataloader returns contract count per accounting_run_id.""" LedgerAccountingRunBalanceFactory.create(accounting_run_id=3, contract_id=2) LedgerAccountingRunBalanceFactory.create(accounting_run_id=2, contract_id=4) payload = [2, 1, 3] res = fixture_client.post( '/ledger-accounting-run-balance/contract-count/dataloader', json=payload ) assert res.status_code == 200 print(res.json) assert res.json == [ {'data': {'accounting_run_id': 2, 'contract_count': 1}}, {'data': {'accounting_run_id': 1, 'contract_count': 0}}, {'data': {'accounting_run_id': 3, 'contract_count': 1}}, ] def test_post_contract_count_dataloader_empty_result(fixture_client): """Test dataloader returns empty list when no records exist.""" res = fixture_client.post( '/ledger-accounting-run-balance/contract-count/dataloader', json=[999] ) assert res.status_code == 200 assert res.json == []