"""PaymentHoldProcessor tests.""" from datetime import date from unittest.mock import call, MagicMock, patch from faker import Faker import pytest import config from src.models import PaymentHold from src.processors import payment_hold from src.processors.exceptions import ProcessingError from tests.unit.factories import PaymentHoldFactory from tests.utils import create_sample_csv_buffer class TestPaymentHoldProcessor: """PaymentHoldProcessor test suite.""" def test_process(self, faker: Faker) -> None: """Test process method.""" processor = payment_hold.PaymentHoldProcessor(faker.pystr(), faker.pystr()) processor._load_data = MagicMock() # type: ignore processor._create_or_update_payment_holds = MagicMock() # type: ignore processor.process() assert processor._load_data.called assert processor._create_or_update_payment_holds.called def test_load_data_success(self) -> None: """Test loading data from CSV file success.""" with patch( 'src.processors.payment_hold.payment_hold.datetime' ) as mock_datetime: mock_datetime.date.today.return_value = date(2024, 11, 1) processor = payment_hold.PaymentHoldProcessor('', '') processor._input_file_buffer = create_sample_csv_buffer( (('account_id',), (36031, 'reason1'), (18, 'reason2')) ) processor._load_data() assert processor._data == [ PaymentHold( account_id=36031, is_on_hold=True, start_date=date(2024, 11, 1), reason='reason1', created_by=config.APPLICATION_NAME, last_modified_by=config.APPLICATION_NAME, ), PaymentHold( account_id=18, is_on_hold=True, start_date=date(2024, 11, 1), reason='reason2', created_by=config.APPLICATION_NAME, last_modified_by=config.APPLICATION_NAME, ), ] def test_load_data_no_header_success(self) -> None: """Test loading data from CSV file without header success.""" with patch( 'src.processors.payment_hold.payment_hold.datetime' ) as mock_datetime: mock_datetime.date.today.return_value = date(2024, 11, 2) processor = payment_hold.PaymentHoldProcessor('', '') processor._input_file_buffer = create_sample_csv_buffer( (('21', 'test reason'),) ) processor._load_data() assert processor._data == [ PaymentHold( account_id=21, is_on_hold=True, start_date=date(2024, 11, 2), reason='test reason', created_by=config.APPLICATION_NAME, last_modified_by=config.APPLICATION_NAME, ), ] def test_load_data_failure_no_file(self) -> None: """Test loading data from CSV file failure.""" processor = payment_hold.PaymentHoldProcessor('', '') with pytest.raises(ProcessingError, match='Unable to read the file'): processor._load_data() def test_load_data_failure_invalid_content(self) -> None: """Test loading data from CSV file failure.""" processor = payment_hold.PaymentHoldProcessor('', '') processor._input_file_buffer = create_sample_csv_buffer( ( ('account_id', 'reason'), ('36034a', 'test'), ) ) with pytest.raises(ProcessingError, match='Invalid file content'): processor._load_data() def test_load_data_failure_invalid_reason(self) -> None: """Test loading data from CSV file failure.""" processor = payment_hold.PaymentHoldProcessor('', '') processor._input_file_buffer = create_sample_csv_buffer( ( ('account_id', 'reason'), ('36034',), ) ) with pytest.raises(ProcessingError, match='Invalid file content'): processor._load_data() def test_load_data_failure_no_items(self) -> None: """Test loading data from CSV file failure.""" processor = payment_hold.PaymentHoldProcessor('', '') processor._input_file_buffer = create_sample_csv_buffer((('account_id',),)) with pytest.raises(ProcessingError, match='No items in file to post'): processor._load_data() @patch('src.processors.payment_hold.payment_hold.ows_abacus_account') def test_create_or_update_payment_hold( self, mock_ows_abacus_account: MagicMock, faker: Faker ) -> None: """Test _create_tax_corrections method.""" processor = payment_hold.PaymentHoldProcessor(faker.pystr(), faker.pystr()) processor._data = PaymentHoldFactory.batch(faker.pyint(1, 10)) processor._create_or_update_payment_holds() assert mock_ows_abacus_account.create_or_update_payment_hold.call_args_list == [ call(item) for item in processor._data ] class TestPaymentUnholdProcessor: """PaymentUnholdProcessor test suite.""" def test_load_data_success(self) -> None: """Test loading data from CSV file success.""" with patch( 'src.processors.payment_hold.payment_hold.datetime' ) as mock_datetime: mock_datetime.date.today.return_value = date(2024, 11, 1) processor = payment_hold.PaymentUnholdProcessor('', '') processor._input_file_buffer = create_sample_csv_buffer( (('36031', 'reason 36031'), ('18', '18')) ) processor._load_data() assert processor._data == [ PaymentHold( account_id=36031, is_on_hold=False, start_date=date(2024, 11, 1), reason='reason 36031', created_by=config.APPLICATION_NAME, last_modified_by=config.APPLICATION_NAME, ), PaymentHold( account_id=18, is_on_hold=False, start_date=date(2024, 11, 1), reason='18', created_by=config.APPLICATION_NAME, last_modified_by=config.APPLICATION_NAME, ), ] @patch('src.processors.payment_hold.payment_hold.ows_abacus_account') def test_create_or_update_payment_hold( self, mock_ows_abacus_account: MagicMock, faker: Faker ) -> None: """Test _create_tax_corrections method.""" processor = payment_hold.PaymentUnholdProcessor(faker.pystr(), faker.pystr()) processor._data = PaymentHoldFactory.batch(faker.pyint(1, 10)) processor._create_or_update_payment_holds() assert mock_ows_abacus_account.create_or_update_payment_hold.call_args_list == [ call(item) for item in processor._data ]