"""Payment Group Payment Batch functional tests.""" import pytest from payment.constants import constants from tests.utils.factories import ( PaymentGroupPaymentAccountDetailFactory, PaymentGroupPaymentBatchAccountFactory, PaymentGroupPaymentBatchFactory, PaymentGroupPaymentFactory, WorksheetAccountContractClosingBalanceFactory, WorksheetPayableBalanceAfterTaxFactory, ) def test_create_payment_batch_success(fixture_client, mock_accounts): """Test successful creation of payment_group_payment_batch.""" payment_group_payment = PaymentGroupPaymentFactory.create() payment_group_payment_id = payment_group_payment.payment_group_payment_id batch_num = 1 payoneer_program_id = 1001 post_body = { 'payment_group_payment_id': payment_group_payment_id, 'payoneer_program_id': payoneer_program_id, 'batch_num': batch_num, } response = fixture_client.post('/payment-group-payment-batch', json=post_body) assert response.status_code == 201 assert response.json['payment_group_payment_batch_id'] assert response.json['payment_group_payment_id'] == payment_group_payment_id assert response.json['payoneer_program_id'] == payoneer_program_id assert response.json['batch_num'] == batch_num def test_create_payment_batch_failure(fixture_client): """Test payment batch creation returns error when field is missing.""" post_body = {'payment_group_payment_id': 1} response = fixture_client.post('/payment-group-payment-batch', json=post_body) assert response.status_code == 400 assert response.json['message']['json'] == { 'batch_num': ['Must be specified.'], 'payoneer_program_id': ['Must be specified.'], } @pytest.mark.parametrize('payment_type', constants.PAYMENT_TYPES) def test_get_batch_debit_data_success( payment_type, fixture_client, mock_accounts, mock_contracts, mock_abacus_event, mock_ledger_account_contracts, ): """Test GET /payment-group-payment-batch///debit.""" worksheet_closing_balance = WorksheetAccountContractClosingBalanceFactory.create() worksheet_after_tax1 = WorksheetPayableBalanceAfterTaxFactory.create( # noqa worksheet_account_contract_closing_balance_id=worksheet_closing_balance.worksheet_account_contract_closing_balance_id ) detail1 = PaymentGroupPaymentAccountDetailFactory.create( # noqa worksheet_account_contract_payable_after_tax_id=worksheet_after_tax1.worksheet_account_contract_payable_after_tax_id ) batch1 = PaymentGroupPaymentBatchAccountFactory.create( payment_group_payment_account=detail1.payment_group_payment_account ) worksheet_after_tax2 = WorksheetPayableBalanceAfterTaxFactory.create( # noqa worksheet_account_contract_closing_balance_id=worksheet_closing_balance.worksheet_account_contract_closing_balance_id ) detail2 = PaymentGroupPaymentAccountDetailFactory.create( # noqa worksheet_account_contract_payable_after_tax_id=worksheet_after_tax2.worksheet_account_contract_payable_after_tax_id ) PaymentGroupPaymentBatchAccountFactory.create( payment_group_payment_account=detail2.payment_group_payment_account ) res = fixture_client.get( f'/payment-group-payment-batch/' f'{batch1.payment_group_payment_batch_id}/{payment_type}/debit' ) match payment_type: case constants.PAYMENT_TYPES.WHT: # negative according to formula, return as is amount = detail1.tax_withholding_amount case constants.PAYMENT_TYPES.VAT: # positive according to formula, return as is amount = detail1.vat_amount case _: # positive according to formula, make negative amount = ( detail1.payable_amount_post_tax * -1 if detail1.payable_amount_post_tax else detail1.payable_amount_post_tax ) assert res.status_code == 200 assert res.json == [ { 'account_id': detail1.account_id, 'amount': str(amount), 'contract_id': detail1.contract_id, 'currency_code': detail1.currency_code, 'worksheet_account_contract_payable_after_tax_id': detail1.worksheet_account_contract_payable_after_tax_id, } ] @pytest.mark.parametrize('payment_type', constants.PAYMENT_TYPES) def test_get_batch_debit_data_no_zero_amounts( payment_type, fixture_client, mock_accounts, mock_contracts, mock_abacus_event, mock_ledger_account_contracts, ): """Test GET /payment-group-payment-batch///debit.""" worksheet_closing_balance = WorksheetAccountContractClosingBalanceFactory.create() worksheet_after_tax1 = WorksheetPayableBalanceAfterTaxFactory.create( worksheet_account_contract_closing_balance_id=worksheet_closing_balance.worksheet_account_contract_closing_balance_id ) detail1 = PaymentGroupPaymentAccountDetailFactory.create( worksheet_account_contract_payable_after_tax_id=worksheet_after_tax1.worksheet_account_contract_payable_after_tax_id, payable_amount_pre_tax=0, tax_withholding_amount=0, payable_amount_post_tax=0, vat_amount=0, ) batch1 = PaymentGroupPaymentBatchAccountFactory.create( payment_group_payment_account=detail1.payment_group_payment_account ) worksheet_after_tax2 = WorksheetPayableBalanceAfterTaxFactory.create( worksheet_account_contract_closing_balance_id=worksheet_closing_balance.worksheet_account_contract_closing_balance_id ) detail2 = PaymentGroupPaymentAccountDetailFactory.create( worksheet_account_contract_payable_after_tax_id=worksheet_after_tax2.worksheet_account_contract_payable_after_tax_id ) PaymentGroupPaymentBatchAccountFactory.create( payment_group_payment_account=detail2.payment_group_payment_account ) res = fixture_client.get( f'/payment-group-payment-batch/' f'{batch1.payment_group_payment_batch_id}/{payment_type}/debit' ) assert res.status_code == 200 assert res.json == [] def test_get_batch_debit_data_failure(fixture_client, faker): """Test GET /payment-group-payment-batch///debit.""" batch_id = faker.pyint() payment_type = faker.pystr(prefix='test_') res = fixture_client.get( f'/payment-group-payment-batch/{batch_id}/{payment_type}/debit' ) assert res.status_code == 404 def test_get_payment_group_payment_batch_success( fixture_client, mock_accounts, mock_contracts, mock_abacus_event, mock_ledger_account_contracts, ): """Test GET /payment-group-payment-batch/.""" batch1 = PaymentGroupPaymentBatchFactory.create() res = fixture_client.get( f'/payment-group-payment-batch/' f'{batch1.payment_group_payment_batch_id}' ) assert res.status_code == 200 assert res.json == { 'payment_group_payment_batch_id': batch1.payment_group_payment_batch_id, 'payment_group_payment_id': batch1.payment_group_payment_id, 'payoneer_program_id': batch1.payoneer_program_id, 'batch_num': batch1.batch_num, } def test_get_payment_group_payment_batch_failure(fixture_client, faker): """Test GET /payment-group-payment-batch/.""" batch_id = faker.pyint() res = fixture_client.get(f'/payment-group-payment-batch/{batch_id}') assert res.status_code == 400