"""Unit tests for ReportPayment schemas.""" from unittest.mock import patch from abacus_common_logic.constants.constants import DATETIME_FORMAT from payment.constants.constants import REPORT_TYPE from payment.schemas.report_payment import ( ReportPaymentDetailSchema, ReportPaymentDownloadSchema, ReportPaymentPutSchema, ) from tests.utils.factories import ReportPaymentFactory def test_report_payment_schema_validation_success(faker): """Test ReportPaymentPutSchema.""" data = { 'report_export_url': faker.uri(), 'report_type': REPORT_TYPE.APPROVAL, } res = ReportPaymentPutSchema().load(data) assert res assert res == data def test_report_payment_schema_validation_failure(): """Test ReportPaymentPutSchema.""" res = ReportPaymentPutSchema().validate({}) assert len(res) == 2 res = ReportPaymentPutSchema().validate( { 'report_export_url': None, 'report_type': 'test', } ) assert len(res) == 2 @patch('payment.connectors.s3.get_object_metadata_by_s3_url') def test_report_payment_detail_schema_dump(mock_get_metadata, fresh_db, faker): """Test ReportPaymentDetailSchema.""" mock_filename = faker.file_name(extension='pdf') mock_get_metadata.return_value = {'Metadata': {'original_filename': mock_filename}} report_payment = ReportPaymentFactory() res = ReportPaymentDetailSchema().dump(report_payment) expected = { 'report_payment_id': report_payment.report_payment_id, 'target_type': report_payment.target_type, 'target_id': report_payment.target_id, 'report_export_url': report_payment.report_export_url, 'report_type': report_payment.report_type, 'original_filename': mock_filename, 'created_at': report_payment.created_at.strftime(DATETIME_FORMAT), 'last_modified': report_payment.last_modified.strftime(DATETIME_FORMAT), 'created_by': report_payment.created_by, 'last_modified_by': report_payment.last_modified_by, } assert res == expected def test_report_payment_download_schema_dump(faker): """Test ReportPaymentDownloadSchema dump.""" data = { 'download_url': faker.uri(), } res = ReportPaymentDownloadSchema().dump(data) assert res == data