"""Unit tests for ReportPayment model.""" from unittest.mock import patch from urllib.parse import urlparse from abacus_common_logic.connectors.database import db import pytest from sqlalchemy import select from sqlalchemy.exc import IntegrityError from payment.constants.constants import REPORT_TYPE from payment.models.report_payment import ReportPayment from tests.utils.factories import ReportPaymentFactory MOCK_REPORT_DATA = { 'target_type': 'worksheet_payment_custom', 'target_id': 1, 'report_export_url': f's3://abacus-payments/worksheet_payment_custom/approval/approval.pdf', 'report_type': REPORT_TYPE.APPROVAL, } def test_create(): """Create an instance.""" ReportPayment.create(**MOCK_REPORT_DATA) assert len(db.session.execute(select(ReportPayment)).scalars().all()) == 1 def test_create_unique(): """Check unique constraint.""" ReportPaymentFactory.create(**MOCK_REPORT_DATA) with pytest.raises(IntegrityError): ReportPaymentFactory.create(**MOCK_REPORT_DATA) @patch('payment.connectors.s3.get_object_metadata_by_s3_url') def test_original_filename(mock_get_metadata, faker): """Test original_filename property.""" mock_filename = faker.file_name(extension='pdf') mock_get_metadata.return_value = {'Metadata': {'original_filename': mock_filename}} instance = ReportPayment.create(**MOCK_REPORT_DATA) assert instance.original_filename == mock_filename @patch('payment.connectors.s3.create_presigned_url') @patch('payment.connectors.s3.get_s3_client') @patch('payment.config.Config.S3_PAYMENTS_BUCKET_NAME', 'test-bucket') def test_create_presigned_url(mock_get_s3_client, mock_create_presigned_url, faker): """Test create_presigned_url method.""" mock_client = faker.pydict() mock_presigned_url = faker.url() mock_get_s3_client.return_value = mock_client mock_create_presigned_url.return_value = mock_presigned_url instance = ReportPayment.create(**MOCK_REPORT_DATA) result = instance.create_presigned_url() assert result == mock_presigned_url mock_get_s3_client.assert_called_once() mock_create_presigned_url.assert_called_once_with( 'test-bucket', mock_client, urlparse(MOCK_REPORT_DATA['report_export_url']).path[1:], ) def test_get_by_target_and_type_exists(): """Test get_by_target_and_type when a record exists.""" report = ReportPaymentFactory.create() found_report = ReportPayment.get_by_target_and_type( target_type=report.target_type, target_id=report.target_id, report_type=report.report_type, ) assert found_report is not None assert found_report.report_payment_id == report.report_payment_id def test_get_by_target_and_type_not_exists(faker): """Test get_by_target_and_type when no record exists.""" found_report = ReportPayment.get_by_target_and_type( target_type=faker.word(), target_id=faker.pyint(), report_type=REPORT_TYPE.APPROVAL, ) assert found_report is None def test_get_all_by_target_exists(): """Test get_all_by_target when records exist.""" target_type = 'worksheet_payment_custom' target_id = 1 # Create multiple reports with same target but different report_types report1 = ReportPaymentFactory.create( target_type=target_type, target_id=target_id, report_type=REPORT_TYPE.APPROVAL, ) report2 = ReportPaymentFactory.create( target_type=target_type, target_id=target_id + 1, report_type=REPORT_TYPE.APPROVAL, ) # Create a report with different target to ensure it's not included ReportPaymentFactory.create(target_type='other_type', target_id=999) found_reports = ReportPayment.get_all_by_target( target_type=target_type, target_id=target_id, ) assert len(found_reports) == 1 assert report1 in found_reports assert report2 not in found_reports def test_get_all_by_target_empty(faker): """Test get_all_by_target when no records exist.""" found_reports = ReportPayment.get_all_by_target( target_type=faker.word(), target_id=faker.pyint(), ) assert found_reports == []