from unittest.mock import call, MagicMock, patch import pytest from src import constants, utils from src.models import GetAccountsResponse from tests.unit.factories import AccountFactory, AccountPaymentTermFactory @pytest.mark.parametrize( 'agreement_type_ids', [ None, [1, 2], ], ) @patch('src.utils.account_payment_term_dataloader') @patch('src.utils.get_accounts') def test_fetch_all_accounts( mock_get_accounts: MagicMock, mock_account_payment_term_dataloader: MagicMock, agreement_type_ids: list[int] | None, ) -> None: """Test test_fetch_all_accounts function to fetch several batches and returns them in one list.""" account_1 = AccountFactory.build(account_id=1) account_2 = AccountFactory.build(account_id=2) term_1 = AccountPaymentTermFactory.build() term_2 = AccountPaymentTermFactory.build() first_batch: GetAccountsResponse = GetAccountsResponse( items=[account_1], total_count=constants.ACCOUNT_BATCH_SIZE + 1 ) second_batch: GetAccountsResponse = GetAccountsResponse( items=[account_2], total_count=1 ) mock_get_accounts.side_effect = [first_batch, second_batch] mock_account_payment_term_dataloader.return_value = {1: term_1, 2: term_2} res = utils.fetch_all_accounts( reference_payment_type_id=1, agreement_type_ids=agreement_type_ids ) assert mock_get_accounts.call_args_list == [ call( reference_payment_type_id=1, limit=constants.ACCOUNT_BATCH_SIZE, offset=0, agreement_type_ids=agreement_type_ids, ), call( reference_payment_type_id=1, limit=constants.ACCOUNT_BATCH_SIZE, offset=constants.ACCOUNT_BATCH_SIZE, agreement_type_ids=agreement_type_ids, ), ] assert res[0].account_id == account_1.account_id assert res[0].payment_minimum == term_1.payment_minimum assert res[0].currency_code == term_1.currency_code assert res[1].account_id == account_2.account_id assert res[1].payment_minimum == term_2.payment_minimum assert res[1].currency_code == term_2.currency_code