"""Unit tests for Payment Group Payment Batch model.""" from abacus_common_logic.connectors.database import db from sqlalchemy import select from payment.models.payment_group_payment_batch import PaymentGroupPaymentBatch from tests.utils.factories import ( PaymentGroupPaymentBatchFactory, PaymentGroupPaymentFactory, ) def test_create(mock_reference_payoneer_program): """Create a PaymentGroupPaymentBatch.""" payment_group_payment = PaymentGroupPaymentFactory.create() payment_group_payment_id = payment_group_payment.payment_group_payment_id batch_num = 1 payoneer_program_id = 1001 PaymentGroupPaymentBatch.create( batch_num=batch_num, payment_group_payment_id=payment_group_payment_id, payoneer_program_id=payoneer_program_id, ) payment_batches = ( db.session.execute(select(PaymentGroupPaymentBatch)).scalars().all() ) assert len(payment_batches) == 1 assert payment_batches[0].batch_num == batch_num assert payment_batches[0].payment_group_payment_id == payment_group_payment_id assert ( payment_batches[0].payment_group_payment_batch_id == payment_batches[0].payment_group_payment_batch_id ) assert payment_batches[0].payoneer_program_id == payoneer_program_id def test_get_by_payment_group_payment_and_batch_status( mock_reference_payoneer_program, create_mock_payment_state ): """Test get a list of payment batches by payment_group_payment and batch status.""" payment_group_payment = PaymentGroupPaymentFactory.create() payment_group_payment_id = payment_group_payment.payment_group_payment_id [ PaymentGroupPaymentBatchFactory.create( payment_group_payment_batch_id=i, batch_num=1, payment_group_payment=payment_group_payment, payoneer_program_id=100158970, ) for i in [1, 2, 3] ] failed_batches = ( PaymentGroupPaymentBatch.get_by_payment_group_payment_and_batch_status( payment_group_payment_id, 'failed' ) ) assert len(failed_batches) == 1 assert failed_batches[0].payment_group_payment_batch_id == 1 assert failed_batches[0].error_message == 'Batch Failed' success_batches = ( PaymentGroupPaymentBatch.get_by_payment_group_payment_and_batch_status( payment_group_payment_id, 'success' ) ) assert len(success_batches) == 2 assert success_batches[0].payment_group_payment_batch_id == 2 assert success_batches[1].payment_group_payment_batch_id == 3 pending_batches = ( PaymentGroupPaymentBatch.get_by_payment_group_payment_and_batch_status( payment_group_payment_id, 'pending' ) ) assert len(pending_batches) == 0 all_batches = ( PaymentGroupPaymentBatch.get_by_payment_group_payment_and_batch_status( payment_group_payment_id ) ) assert len(all_batches) == 3 assert all_batches[0].payment_group_payment_batch_id == 1 assert all_batches[1].payment_group_payment_batch_id == 2 assert all_batches[2].payment_group_payment_batch_id == 3