"""Test for statement_period_adjustment_file serialization.""" from datetime import datetime from decimal import Decimal import pytest from marshmallow import ValidationError from royalties.schemas.statement_period_adjustment_file import ( AutoGenerationInProgressOrErrorSchema, StatementPeriodAdjustmentFileDetailSchema, StatementPeriodAdjustmentFileFilterSchema, StatementPeriodAdjustmentFileListSchema, StatementPeriodAdjustmentFilePostSchema, StatementPeriodAdjustmentFilePutSchema, StatementPeriodAdjustmentFileUserListSchema, ) from royalties.tests.utils.factories import StatementPeriodAdjustmentFileFactory def test_statement_period_adjustment_file_post_schema(): """Test statement period adjustment file post schema.""" mock_data = { 'file_name': 'test_file.csv', 'valid_file_location': 'valid/location', 'invalid_file_location': 'invalid/location', 'valid_row_count': 1, 'invalid_row_count': 2, 'total_file_amount_multicurrency': '3.000000000001', 'total_rounded_amount_multicurrency': '4.02', 'md5sum': 'b6579ec2950296ed6a04f08f67f64422', 'error_type': 'content_error', } loaded_data = StatementPeriodAdjustmentFilePostSchema().load(mock_data) expected_data = { **mock_data, 'total_file_amount_multicurrency': Decimal( mock_data['total_file_amount_multicurrency'] ), 'total_rounded_amount_multicurrency': Decimal( mock_data['total_rounded_amount_multicurrency'] ), } assert loaded_data == expected_data def test_statement_period_adjustment_file_post_schema_required_fields(): """Test statement period adjustment file post schema only required fields.""" mock_data = {'file_name': 'test_file.csv'} loaded_data = StatementPeriodAdjustmentFilePostSchema().load(mock_data) expected_data = {**mock_data} assert loaded_data == expected_data def test_statement_period_adjustment_file_put_schema(): """Test statement period adjustment file put schema.""" mock_data = { 'file_name': 'test_file.csv', 'valid_file_location': 'valid/location', 'invalid_file_location': 'invalid/location', 'valid_row_count': 1, 'invalid_row_count': 2, 'total_file_amount_multicurrency': '3.000000000001', 'total_rounded_amount_multicurrency': '4.02', 'md5sum': 'b6579ec2950296ed6a04f08f67f64422', 'error_type': 'content_error', } loaded_data = StatementPeriodAdjustmentFilePutSchema().load(mock_data) expected_data = { **mock_data, 'total_file_amount_multicurrency': Decimal( mock_data['total_file_amount_multicurrency'] ), 'total_rounded_amount_multicurrency': Decimal( mock_data['total_rounded_amount_multicurrency'] ), } assert loaded_data == expected_data def test_statement_period_adjustment_file_put_schema_required_fields(): """Test statement period adjustment file put schema only required fields.""" mock_data = {'file_name': 'test_file.csv'} loaded_data = StatementPeriodAdjustmentFilePutSchema().load(mock_data) expected_data = {**mock_data} assert loaded_data == expected_data def test_statement_period_adjustment_file_detail_schema(): """Test statement period adjustment file detail schema.""" instance = StatementPeriodAdjustmentFileFactory.build( file_name='test_file.csv', valid_file_location='valid/location', invalid_file_location='invalid/location', valid_row_count=2, invalid_row_count=3, total_file_amount_multicurrency=Decimal('3.000000000001'), total_rounded_amount_multicurrency=Decimal('4.02'), md5sum='b6579ec2950296ed6a04f08f67f64422', error_type='content_error', ) dumped_data = StatementPeriodAdjustmentFileDetailSchema().dump(instance) assert dumped_data == { 'batch_type': 'upload', 'statement_period_adjustment_file_id': instance.statement_period_adjustment_file_id, 'statement_period_id': instance.statement_period_id, 'file_name': instance.file_name, 'valid_file_location': instance.valid_file_location, 'invalid_file_location': instance.invalid_file_location, 'valid_row_count': instance.valid_row_count, 'invalid_row_count': instance.invalid_row_count, 'total_file_amount_multicurrency': str( instance.total_file_amount_multicurrency ), 'total_rounded_amount_multicurrency': str( instance.total_rounded_amount_multicurrency ), 'md5sum': instance.md5sum, 'error_type': 'content_error', 'source_file_upload_id': None, 'created_by': 'default_user_id', } def test_statement_period_adjustment_file_list_schema(): """Test StatementPeriodAdjustmentFileList Schema.""" mock_adjustment_files_list = { 'status': 'approved', 'valid_row_count': 67, 'statement_period_adjustment_file_id': 1, 'created_at': datetime(2023, 10, 20), 'created_by': 'Test User', 'date_approved': datetime(2023, 11, 11), 'approved_by': 'Joe User', 'date_applied': datetime(2023, 11, 20), } result = StatementPeriodAdjustmentFileListSchema().dump(mock_adjustment_files_list) assert result == { 'status': 'approved', 'valid_row_count': 67, 'statement_period_adjustment_file_id': 1, 'created_at': '2023-10-20', 'created_by': 'Test User', 'date_approved': '2023-11-11', 'approved_by': 'Joe User', 'date_applied': '2023-11-20', } def test_statement_period_adjustment_file_filter_schema(): """Test StatementPeriodAdjustmentFileFilter schema.""" mock_filter_params = { 'created_by': 'd5ca8ac3-7e51-4793-8775-50d11282504c', 'file_name': 'test_adjustment_file.xlsx', 'limit': 25, 'offset': 0, 'sort_by': 'status', 'statement_period_adjustment_file_id': 1, 'statement_period_id': 307, 'status': 'applied', } res = StatementPeriodAdjustmentFileFilterSchema().load(mock_filter_params) mock_filter_params.update({'sort_order': 'desc'}) assert res == mock_filter_params def test_statement_period_adjustment_file_filter_schema_validation(): """Test StatementPeriodAdjustmentFileFilter schema validation.""" mock_filter_params = { 'limit': 25, 'offset': 0, 'sort_by': 'status', 'statement_period_adjustment_file_id': 1, 'status': 'applied, test', } with pytest.raises(ValidationError) as exc_info: StatementPeriodAdjustmentFileFilterSchema().load(mock_filter_params) assert exc_info.value.args[0] == { '_schema': ['Must be applied, approved, not_approved, failed_to_generate'] } def test_statement_period_adjustment_file_user_list_schema(): """Test StatementPeriodAdjustmentFileUserList Schema.""" mock_data = [ { 'file_name': 'test_file_1.xlsx', 'created_by': 'd5ca8ac3-7e51-4793-8775-50d11282504c', }, { 'file_name': 'test_file_2.xlsx', 'created_by': 'e5ca8bc3-7e52-4793-8775-50d11282504c', }, ] result = StatementPeriodAdjustmentFileUserListSchema(many=True).dump(mock_data) assert result == [ {'created_by': 'd5ca8ac3-7e51-4793-8775-50d11282504c'}, {'created_by': 'e5ca8bc3-7e52-4793-8775-50d11282504c'}, ] def test_auto_generation_in_progress_or_error_schema(): """Test auto-generated in progress/error adjustment file schema.""" mock_data = { 'batch_type': 'auto', 'status': 'generating', 'statement_period_id': 1, 'statement_period_adjustment_file_id': 1, } result = AutoGenerationInProgressOrErrorSchema().dump(mock_data) assert result == mock_data