"""Tests for reference_sap_profit_center serialization.""" import pytest from marshmallow import ValidationError from abacus_contract.constants.constants import ( SAP_PROFIT_CENTER_SORT_OPTIONS, SORT_ORDER_OPTIONS, ) from abacus_contract.schemas.reference_sap_profit_center import ( ReferenceSapProfitCenterDataloaderSchema, ReferenceSapProfitCenterFilterSchema, ReferenceSapProfitCenterPostSchema, ReferenceSapProfitCenterPutSchema, ReferenceSapProfitCenterSchema, ) from abacus_contract.tests.utils.factories import ReferenceSapProfitCenterFactory def test_reference_sap_profit_center_schema_dumps_all_fields(): """Serialization includes display_name alongside the other fields.""" pc = ReferenceSapProfitCenterFactory.create() result = ReferenceSapProfitCenterSchema().dump(pc) assert result == { 'reference_sap_profit_center_id': pc.reference_sap_profit_center_id, 'profit_center': pc.profit_center, 'company_code': pc.company_code, 'business_group': pc.business_group, 'display_name': pc.display_name, } def test_reference_sap_profit_center_schema_load_rejects_missing_display_name(): """display_name is required; missing it raises ValidationError on load.""" payload = { 'reference_sap_profit_center_id': 1, 'profit_center': 'PC001', 'company_code': '4914', 'business_group': 'ORC', } with pytest.raises(ValidationError) as exc_info: ReferenceSapProfitCenterSchema().load(payload) assert 'display_name' in exc_info.value.messages def test_reference_sap_profit_center_schema_load_rejects_empty_display_name(): """display_name must be non-empty (uses NonemptyString).""" payload = { 'reference_sap_profit_center_id': 1, 'profit_center': 'PC001', 'company_code': '4914', 'business_group': 'ORC', 'display_name': '', } with pytest.raises(ValidationError) as exc_info: ReferenceSapProfitCenterSchema().load(payload) assert 'display_name' in exc_info.value.messages # --- PUT schema (ReferenceSapProfitCenterPutSchema) --- def test_put_schema_load_accepts_valid_display_name(): """Valid payload with only display_name loads cleanly.""" result = ReferenceSapProfitCenterPutSchema().load({'display_name': 'New label'}) assert result == {'display_name': 'New label'} def test_put_schema_load_rejects_missing_display_name(): """display_name is required; PUT body without it -> ValidationError.""" with pytest.raises(ValidationError) as exc_info: ReferenceSapProfitCenterPutSchema().load({}) assert 'display_name' in exc_info.value.messages def test_put_schema_load_rejects_empty_display_name(): """Empty string -> ValidationError (NonemptyString).""" with pytest.raises(ValidationError) as exc_info: ReferenceSapProfitCenterPutSchema().load({'display_name': ''}) assert 'display_name' in exc_info.value.messages def test_put_schema_load_rejects_null_display_name(): """Explicit null -> ValidationError (NonemptyString implies not-nullable).""" with pytest.raises(ValidationError) as exc_info: ReferenceSapProfitCenterPutSchema().load({'display_name': None}) assert 'display_name' in exc_info.value.messages def test_put_schema_load_rejects_unknown_field(): """unknown=RAISE: extra fields are rejected so the endpoint stays narrow.""" with pytest.raises(ValidationError) as exc_info: ReferenceSapProfitCenterPutSchema().load( {'display_name': 'New label', 'profit_center': 'attempt to mutate'} ) assert 'profit_center' in exc_info.value.messages def test_sap_profit_center_filter_schema(): """Test filter schema for profit centers.""" mock_input = { 'limit': 10, 'offset': 0, 'sort_by': SAP_PROFIT_CENTER_SORT_OPTIONS.COMPANY_CODE, 'sort_order': SORT_ORDER_OPTIONS.DESC, 'search_term': '123', 'orphan': False, 'signing_entity_ids': '1,2,3', } result = ReferenceSapProfitCenterFilterSchema().dump(mock_input) assert result == { 'limit': 10, 'offset': 0, 'sort_by': SAP_PROFIT_CENTER_SORT_OPTIONS.COMPANY_CODE, 'sort_order': SORT_ORDER_OPTIONS.DESC, 'search_term': '123', 'orphan': False, 'signing_entity_ids': '1,2,3', } def test_sap_profit_center_filter_schema_with_null_values(): """Test profit centers filter schema with null values .""" mock_input = { 'limit': 10, 'offset': 0, 'sort_by': SAP_PROFIT_CENTER_SORT_OPTIONS.PROFIT_CENTER, 'sort_order': SORT_ORDER_OPTIONS.DESC, 'search_term': None, 'orphan': None, 'signing_entity_ids': None, } result = ReferenceSapProfitCenterFilterSchema().dump(mock_input) assert result == { 'limit': 10, 'offset': 0, 'sort_by': SAP_PROFIT_CENTER_SORT_OPTIONS.PROFIT_CENTER, 'sort_order': SORT_ORDER_OPTIONS.DESC, 'search_term': None, 'orphan': None, 'signing_entity_ids': None, } def test_sap_profit_center_filter_schema_with_missing_fields(): """Test profit centers filter schema with missing fields.""" mock_input = {'search_term': 'test', 'orphan': None} result = ReferenceSapProfitCenterFilterSchema().load(mock_input) assert result == { 'limit': 100, 'offset': 0, 'sort_by': SAP_PROFIT_CENTER_SORT_OPTIONS.DISPLAY_NAME, 'sort_order': SORT_ORDER_OPTIONS.ASC, 'search_term': 'test', 'orphan': None, } def test_sap_profit_center_filter_schema_with_invalid_value(): """Test profit centers filter schema with invalid values.""" mock_input = { 'limit': 't12', 'offset': 'O0', 'sort_by': 'test sort', 'sort_order': 'test order', 'search_term': 'test', 'orphan': 'test', 'signing_entity_ids': 'test', } with pytest.raises(ValidationError) as exc_info: ReferenceSapProfitCenterFilterSchema().load(mock_input) assert exc_info.value.messages == { 'limit': ['Must be an integer greater or equal to 0.'], 'offset': ['Must be an integer greater or equal to 0.'], 'sort_by': [ 'Must be one of reference_sap_profit_center_id, profit_center, company_code, display_name, business_group' ], 'sort_order': ['Must be one of asc, desc'], 'orphan': ['Not a valid boolean.'], 'signing_entity_ids': [ "signing_entity_ids must be a comma-separated list of numbers (e.g., '1,2,3')." ], } def test_reference_sap_profit_center_dataloader_schema(): """Test dataloader schema for reference_sap_profit_center.""" mock_profit_centers = [ { 'data': [ { 'reference_sap_profit_center_id': 1, 'profit_center': 'PC001', 'company_code': '4914', 'business_group': 'ORC', 'display_name': 'Test', } ], 'data': None, } ] result = ReferenceSapProfitCenterDataloaderSchema(many=True).dump( mock_profit_centers ) assert result == mock_profit_centers def test_reference_sap_profit_center_post_schema(): """Test POST schema for reference_sap_profit_center.""" mock_request_body = { 'profit_center': 'PC001', 'company_code': '4914', 'business_group': 'ORC', 'display_name': 'Test', 'reference_signing_entity_ids': [1, 2, 3], } result = ReferenceSapProfitCenterPostSchema().dump(mock_request_body) assert result == mock_request_body def test_reference_sap_profit_center_post_invalid_signing_entity_schema(): """Test thrown an error for invalid signing entity ids.""" mock_request_body = { 'profit_center': 'PC001', 'company_code': '4914', 'business_group': 'ORC', 'display_name': 'Test', 'reference_signing_entity_ids': [1, 2, 3, 'test'], } with pytest.raises(ValidationError) as exc_info: ReferenceSapProfitCenterPostSchema().load(mock_request_body) assert exc_info.value.messages == { 'reference_signing_entity_ids': { 3: ['Must be an integer greater or equal to 0.'] } }