"""Unit tests for Payment Group Payment Batch handlers.""" from unittest.mock import call, patch import pytest from payment.constants import constants from payment.schemas.payment_group_payment_account_detail import ( PaymentDebitCreditDataEntrySchema, ) from payment.schemas.payment_group_payment_batch import PaymentGroupPaymentBatchSchema from tests.utils.factories import ( PaymentDebitCreditDataEntryFactory, PaymentGroupPaymentBatchFactory, ) @patch('payment.blueprints.payment_group_payment_batch.logic') def test_create_payment_group_payment_batch(mock_logic, fixture_client): """Test POST /payment-group-payment-batch.""" mock_batch = PaymentGroupPaymentBatchFactory.build() mock_logic.create_payment_group_payment_batch.return_value = mock_batch create_params = { 'payment_group_payment_id': 1, 'payoneer_program_id': 1001, 'batch_num': 1, } res = fixture_client.post('/payment-group-payment-batch', json=create_params) assert res.status_code == 201 mock_logic.create_payment_group_payment_batch.assert_called_once_with( **create_params ) @pytest.mark.parametrize('payment_type', constants.PAYMENT_TYPES) @patch('payment.blueprints.payment_group_payment_batch.logic') def test_get_batch_debit_data_success(mock_logic, payment_type, fixture_client, faker): """Test GET /payment-group-payment-batch///debit.""" mock_data_entry = PaymentDebitCreditDataEntryFactory.build() mock_logic.get_batch_debit_data.return_value = [mock_data_entry] batch_id = faker.pyint() res = fixture_client.get( f'/payment-group-payment-batch/{batch_id}/{payment_type}/debit' ) assert res.status_code == 200 assert mock_logic.get_batch_debit_data.call_args_list == [ call(batch_id, payment_type) ] assert res.json == PaymentDebitCreditDataEntrySchema().dump( [mock_data_entry], many=True ) @patch('payment.blueprints.payment_group_payment_batch.logic') def test_get_batch_debit_data_failure(mock_logic, fixture_client, faker): """Test GET /payment-group-payment-batch///debit.""" mock_data_entry = PaymentDebitCreditDataEntryFactory.build() mock_logic.get_batch_debit_data.return_value = [mock_data_entry] 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 assert not mock_logic.get_batch_debit_data.called @patch('payment.blueprints.payment_group_payment_batch.logic') def test_get_payment_group_payment_batch_success(mock_logic, fixture_client, faker): """Test GET /payment-group-payment-batch/.""" mock_entry = PaymentGroupPaymentBatchFactory.build() mock_logic.get_payment_batch_by_id.return_value = mock_entry batch_id = faker.pyint() res = fixture_client.get(f'/payment-group-payment-batch/{batch_id}') assert res.status_code == 200 assert mock_logic.get_payment_batch_by_id.call_args_list == [call(batch_id)] assert res.json == PaymentGroupPaymentBatchSchema().dump(mock_entry)