"""Tests payment_group serialization.""" from marshmallow.exceptions import ValidationError import pytest from payment.constants.constants import PAYMENT_SCHEDULE from payment.schemas.payment_group import ( PaymentGroupCriteriaSchema, PaymentGroupDetailSchema, PaymentGroupListSchema, PaymentGroupPostSchema, PaymentGroupPutSchema, ) from tests.utils.factories import PaymentGroupFactory def test_payment_group_criteria_schema(): """Test payment group criteria schema.""" currency_criteria = {'currency_codes': ['USD', 'EUR']} mixed_criteria = { 'reference_payment_entities': [1], 'currency_codes': ['USD'], 'payment_schedules': [PAYMENT_SCHEDULE.SCHEDULE_30_DAYS_MONTH], } ref_payment_entities_criteria = {'reference_payment_entities': [3]} schedule_criteria = { 'payment_schedules': [ PAYMENT_SCHEDULE.SCHEDULE_30_DAYS_MONTH, PAYMENT_SCHEDULE.SCHEDULE_30_DAYS_QUARTER, ] } payment_type_criteria = {'reference_payment_type_id': 1} reference_agreement_types_criteria = {'reference_agreement_types': [1, 2, 3]} schema = PaymentGroupCriteriaSchema() assert schema.dump(currency_criteria) == currency_criteria assert schema.dump(mixed_criteria) == mixed_criteria assert schema.dump(ref_payment_entities_criteria) == ref_payment_entities_criteria assert schema.dump(schedule_criteria) == schedule_criteria assert schema.dump(payment_type_criteria) == payment_type_criteria assert ( schema.dump(reference_agreement_types_criteria) == reference_agreement_types_criteria ) def test_payment_group_detail_schema(): """Test payment group detail schema.""" group_criteria = { 'reference_payment_entities': [1], 'currency_codes': ['USD'], 'payment_schedules': [PAYMENT_SCHEDULE.SCHEDULE_30_DAYS_MONTH], 'reference_payment_type_id': 1, 'reference_agreement_types': [1, 2], } payment_group = PaymentGroupFactory.build(group_criteria=group_criteria) res = PaymentGroupDetailSchema().dump(payment_group) assert res['payment_group_id'] == payment_group.payment_group_id assert res['group_criteria'] == payment_group.group_criteria assert res['group_name'] == payment_group.group_name assert res['is_reusable'] == payment_group.is_reusable @pytest.mark.parametrize( 'valid_input', [ ({'account_id': 123}), ({'account_ids': [1, 2, 3]}), ({'account_ids': [3]}), ], ) def test_payment_group_detail_schema_with_account_ids(valid_input): """Test payment group detail schema with account_id in group_criteria.""" payment_group = PaymentGroupFactory.build(group_criteria=valid_input) res = PaymentGroupDetailSchema().dump(payment_group) assert res.get('group_criteria') == payment_group.group_criteria @pytest.mark.parametrize( 'invalid_input,expected_error', [ ({'account_id': -123}, 'account_id'), ({'account_ids': [1, -2, 3]}, 'account_ids'), ({'account_ids': [i for i in range(1, 102)]}, 'account_ids'), ({'account_ids': []}, 'account_ids'), ({'account_ids': [1, 1]}, 'schema'), ({'account_ids': [1, 2], 'account_id': 3}, 'schema'), ], ) def test_payment_group_detail_schema_with_invalid_account_ids( invalid_input, expected_error ): """Test payment group detail schema with invalid account_id(s) in group_criteria.""" schema = PaymentGroupCriteriaSchema() with pytest.raises(ValidationError) as exc_info: schema.load(invalid_input) assert expected_error in str(exc_info.value) def test_payment_group_post_schema_success(): """Test payment group post schema.""" group_criteria = { 'reference_payment_entities': [1], 'currency_codes': ['USD'], 'payment_schedules': [PAYMENT_SCHEDULE.SCHEDULE_30_DAYS_MONTH], } payment_group = PaymentGroupFactory.build(group_criteria=group_criteria) payment_name = 'Month Year' data = PaymentGroupDetailSchema().dump(payment_group) data.update({'payment_name': payment_name}) res = PaymentGroupPostSchema().dump(data) assert res['payment_name'] == payment_name def test_payment_group_post_schema_failure_payment_type() -> None: """Test payment group post schema.""" group_criteria = { 'reference_payment_entities': [1], 'currency_codes': ['USD'], 'payment_schedules': [PAYMENT_SCHEDULE.SCHEDULE_30_DAYS_MONTH], 'reference_payment_type_id': 1, } with pytest.raises(ValidationError) as exc_info: PaymentGroupPostSchema().load( { 'payment_group_id': 1, 'group_name': 'test name', 'is_reusable': True, 'group_criteria': group_criteria, } ) assert exc_info.value.messages == { 'group_criteria': {'reference_payment_type_id': ['Unknown field.']} } def test_payment_group_put_schema() -> None: """Test PaymentGroupPutSchema.""" assert PaymentGroupPutSchema().load({}) == {} assert PaymentGroupPutSchema().load({'group_name': 'test_name'}) == { 'group_name': 'test_name' } assert PaymentGroupPutSchema().load({'is_reusable': False}) == { 'is_reusable': False } with pytest.raises(ValidationError) as exc_info: PaymentGroupPutSchema().load({'group_name': ''}) assert exc_info.value.messages_dict == {'group_name': ['Must be specified.']} with pytest.raises(ValidationError) as exc_info: PaymentGroupPutSchema().load({'group_name': None}) assert exc_info.value.messages_dict == {'group_name': ['Field may not be null.']} with pytest.raises(ValidationError) as exc_info: PaymentGroupPutSchema().load({'is_reusable': True}) assert exc_info.value.messages_dict == {'is_reusable': ['Must be equal to False.']} def test_payment_group_list_schema(): """Test payment group list schema.""" payment_groups = PaymentGroupFactory.build_batch(2) data = {'items': payment_groups, 'total_count': 2} res = PaymentGroupListSchema().dump(data) assert res['total_count'] == data['total_count'] assert len(res['items']) == len(data['items']) assert res['items'][0]['payment_group_id'] == payment_groups[0].payment_group_id # Test with empty list data = {'items': [], 'total_count': 0} res = PaymentGroupListSchema().dump(data) assert res['total_count'] == 0 assert res['items'] == []