"""Functional tests for Accounting Run VAT.""" from ledger.constants.constants import VAT_CATEGORIES_OLD from ledger.constants.error import ERROR_UNKNOWN_COUNTRY, INVALID_VAT_CATEGORY from tests.utils.factories import LedgerAccountingRunVatFactory def test_get_contracts_count_by_vat_category(fixture_client, mock_event_fixtures): """Test to get count of contracts by VAT category for a specific accounting_period.""" (LedgerAccountingRunVatFactory.create_batch(2),) LedgerAccountingRunVatFactory.create( accounting_run_id=2, currency_code='USA', country_of_tax_residence='USA', gross_revenue=0, net_revenue=0, distribution_fee=0, gross_vat_rate=0, distribution_vat_rate=0, gross_vat=0, distribution_vat=0, adjusted_net_revenue=0, exempt_reason='Non-UK citizen', ) res = fixture_client.get('/ledger-accounting-run-vat/accounting-period/1/overview') assert res.status_code == 200 assert res.json == [ { 'vat_category': VAT_CATEGORIES_OLD.VAT_APPLIED, 'country_of_tax_residence': 'GBR', 'contract_count': 2, }, { 'vat_category': VAT_CATEGORIES_OLD.VAT_EXEMPT, 'country_of_tax_residence': None, 'contract_count': 1, }, ] def test_get_ledger_vat_list(fixture_client, mock_event_fixtures): """Test to get list of ledger run vat by an accounting_period_id and vat_category.""" entry1 = LedgerAccountingRunVatFactory.create() res = fixture_client.get( '/ledger-accounting-run-vat/accounting-period/1/vat-category/vat_applied/?country_code=GBR' ) assert res.status_code == 200 assert res.json['total_count'] == 1 assert res.json['items'][0]['accounting_run_id'] == entry1.accounting_run_id def test_get_ledger_vat_list_for_invalid_country(fixture_client, mock_event_fixtures): """Test to get list of ledger run vat for an invalid country code.""" LedgerAccountingRunVatFactory.create() res = fixture_client.get( '/ledger-accounting-run-vat/accounting-period/1/vat-category/vat_applied/?country_code=test' ) assert res.status_code == 400 assert res.json['message'] == ERROR_UNKNOWN_COUNTRY.format(code='test') def test_get_ledger_vat_list_for_invalid_vat_category( fixture_client, mock_event_fixtures ): """Test to get list of ledger run vat for an invalid vat category.""" LedgerAccountingRunVatFactory.create() res = fixture_client.get( '/ledger-accounting-run-vat/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_bulk_create_ledger_accounting_run_vat( fixture_client, mock_bulk_ledger_accounting_run_vat_body, mock_event_fixtures ): """Test bulk create Ledger Accounting Run Vat.""" res = fixture_client.post( '/ledger-accounting-run-vat/bulk', json=mock_bulk_ledger_accounting_run_vat_body ) assert res.status_code == 201 assert res.json['total_count'] == 2 items = res.json['items'] for i in items: del i['ledger_accounting_run_vat_id'] assert sorted(items, key=lambda x: x['currency_code']) == [ { 'accounting_run_id': 1, 'abacus_event_id': 1, 'contract_id': 1, 'currency_code': 'GBP', 'country_of_tax_residence': 'GBR', 'gross_revenue': '200.00', 'net_revenue': '180.00', 'distribution_fee': '2.00', 'gross_vat_rate': '20.00', 'distribution_vat_rate': '20.00', 'gross_vat': '20.00', 'distribution_vat': '0.25', 'adjusted_net_revenue': '175.00', 'exempt_reason': None, }, { 'accounting_run_id': 1, 'abacus_event_id': 1, 'contract_id': 1, 'currency_code': 'USD', 'country_of_tax_residence': 'USA', 'gross_revenue': None, 'net_revenue': None, 'distribution_fee': None, 'gross_vat_rate': None, 'distribution_vat_rate': None, 'gross_vat': None, 'distribution_vat': None, 'adjusted_net_revenue': None, 'exempt_reason': 'US citizenship', }, ] def test_bulk_create_error_unknown_currency( fixture_client, mock_bulk_ledger_accounting_run_vat_body ): """Test unknown currency code.""" records = mock_bulk_ledger_accounting_run_vat_body records[0]['currency_code'] = 'TEST' res = fixture_client.post('/ledger-accounting-run-vat/bulk', json=records) assert res.status_code == 400 assert res.json['code'] == 'error' assert res.json['message'] == 'Currency code not recognized: TEST' def test_bulk_create_error_unknown_country( fixture_client, mock_bulk_ledger_accounting_run_vat_body ): """Test unknown country code.""" records = mock_bulk_ledger_accounting_run_vat_body records[0]['country_of_tax_residence'] = 'TEST' res = fixture_client.post('/ledger-accounting-run-vat/bulk', json=records) assert res.status_code == 400 assert res.json['code'] == 'error' assert res.json['message'] == 'Country code not recognized: TEST'