"""Tests for reference_signing_entity model.""" from abacus_contract import models from abacus_contract.models.reference_signing_entity import ReferenceSigningEntity from abacus_contract.tests.utils.factories import ( ReferencePaymentEntityFactory, ReferenceSapProfitCenterFactory, ReferenceSigningEntityFactory, SigningEntitySapProfitCenterFactory, ) def test_create_reference_signing_entity(): """Test to create reference_signing_entity record.""" reference_payment_entity = ReferencePaymentEntityFactory.create() reference_sap_profit_center = ReferenceSapProfitCenterFactory.create() reference_sap = ReferenceSigningEntity.create( reference_payment_entity=reference_payment_entity, reference_sap_profit_center_id=reference_sap_profit_center.reference_sap_profit_center_id, company_code='4915', legal_name='AWAL Recordings Licensing Ltd', ) result = ReferenceSigningEntity.query.all() assert len(result) == 1 assert result[0] == reference_sap # --- get_authorized_for_sap_profit_centers --- def test_get_authorized_for_sap_profit_centers_returns_only_live_mappings(): """Soft-deleted junction rows are excluded; live mappings are returned.""" pc = ReferenceSapProfitCenterFactory.create() live_se = ReferenceSigningEntityFactory.create( legal_name='Live Inc.', with_junction_row=False ) deleted_se = ReferenceSigningEntityFactory.create( legal_name='Was authorized', with_junction_row=False ) _unrelated_se = ReferenceSigningEntityFactory.create( legal_name='Unrelated LLC', with_junction_row=False ) live_junction = SigningEntitySapProfitCenterFactory.create( reference_signing_entity=live_se, reference_sap_profit_center=pc ) soft_deleted = SigningEntitySapProfitCenterFactory.create( reference_signing_entity=deleted_se, reference_sap_profit_center=pc ) models.SigningEntitySapProfitCenter.delete_by_id_or_error( soft_deleted.signing_entity_sap_profit_center_id, soft_delete=True ) items, total_count = ReferenceSigningEntity.get_authorized_for_sap_profit_centers( [pc.reference_sap_profit_center_id], limit=50, offset=0 ) assert total_count == 1 [item] = items assert item['signing_entity_sap_profit_center_id'] == ( live_junction.signing_entity_sap_profit_center_id ) assert 'created_at' in item assert item['signing_entity'].reference_signing_entity_id == ( live_se.reference_signing_entity_id ) assert item['signing_entity'].legal_name == 'Live Inc.' def test_get_authorized_for_sap_profit_centers_search_filters_legal_name(): """search_term matches case-insensitively against SE legal_name only.""" pc = ReferenceSapProfitCenterFactory.create() match = ReferenceSigningEntityFactory.create( legal_name='Foundation Media SAS', with_junction_row=False ) no_match = ReferenceSigningEntityFactory.create( legal_name='Santa Anna LLC', with_junction_row=False ) for se in (match, no_match): SigningEntitySapProfitCenterFactory.create( reference_signing_entity=se, reference_sap_profit_center=pc ) items, _ = ReferenceSigningEntity.get_authorized_for_sap_profit_centers( [pc.reference_sap_profit_center_id], limit=50, offset=0, search_term='foundation', ) assert {item['signing_entity'].reference_signing_entity_id for item in items} == { match.reference_signing_entity_id } def test_get_authorized_for_sap_profit_centers_pagination(): """Limit caps the page; total_count reflects the full match set.""" pc = ReferenceSapProfitCenterFactory.create() for n in range(5): se = ReferenceSigningEntityFactory.create( legal_name=f'SE {n}', with_junction_row=False ) SigningEntitySapProfitCenterFactory.create( reference_signing_entity=se, reference_sap_profit_center=pc ) page, total_count = ReferenceSigningEntity.get_authorized_for_sap_profit_centers( [pc.reference_sap_profit_center_id], limit=2, offset=0 ) assert total_count == 5 assert len(page) == 2 def test_get_authorized_for_sap_profit_centers_orders_by_legal_name(): """Results are ordered by legal_name ascending.""" pc = ReferenceSapProfitCenterFactory.create() for name in ('Charlie LLC', 'Alpha Inc.', 'Bravo Co.'): se = ReferenceSigningEntityFactory.create( legal_name=name, with_junction_row=False ) SigningEntitySapProfitCenterFactory.create( reference_signing_entity=se, reference_sap_profit_center=pc ) items, _ = ReferenceSigningEntity.get_authorized_for_sap_profit_centers( [pc.reference_sap_profit_center_id], limit=50, offset=0 ) assert [item['signing_entity'].legal_name for item in items] == [ 'Alpha Inc.', 'Bravo Co.', 'Charlie LLC', ] def test_get_authorized_for_sap_profit_centers_returns_active_mappings(): """Test that the method retrieves all active, live signing entities mappings for the given SAP profit center IDs.""" # noqa: E501 mock_sap_profit_centers = [ ReferenceSapProfitCenterFactory.create(), ReferenceSapProfitCenterFactory.create(), ReferenceSapProfitCenterFactory.create(), ] mock_signing_entities = [ ReferenceSigningEntityFactory.create( legal_name='Live Inc.', with_junction_row=False ), ReferenceSigningEntityFactory.create( legal_name='Was authorized', with_junction_row=False ), ReferenceSigningEntityFactory.create( legal_name='Unrelated LLC', with_junction_row=False ), ] for se in mock_signing_entities[0:2]: SigningEntitySapProfitCenterFactory.create( reference_signing_entity=se, reference_sap_profit_center=mock_sap_profit_centers[0], ) SigningEntitySapProfitCenterFactory.create( reference_signing_entity=mock_signing_entities[2], reference_sap_profit_center=mock_sap_profit_centers[1], ) profit_center_ids = [ mock_sap_profit_center.reference_sap_profit_center_id for mock_sap_profit_center in mock_sap_profit_centers ] items, total_count = ReferenceSigningEntity.get_authorized_for_sap_profit_centers( profit_center_ids, 10, 0 ) legal_names = ['Live Inc.', 'Unrelated LLC', 'Different', 'Was authorized'] assert total_count == 3 assert ( all([item['signing_entity'].legal_name in legal_names for item in items]) is True ) assert ( all( item['reference_sap_profit_center_id'] in profit_center_ids[0:2] for item in items ) is True ) def test_get_authorized_for_sap_profit_centers_returns_empty_when_unmapped(): """Test that an empty list is returned when the provided profit center IDs have no active signing entity mappings.""" # noqa: E501 mock_sap_profit_centers = [ ReferenceSapProfitCenterFactory.create(), ReferenceSapProfitCenterFactory.create(), ReferenceSapProfitCenterFactory.create(), ] mock_signing_entities = [ ReferenceSigningEntityFactory.create( legal_name='Live Inc.', with_junction_row=False ), ReferenceSigningEntityFactory.create( legal_name='Was authorized', with_junction_row=False ), ReferenceSigningEntityFactory.create( legal_name='Unrelated LLC', with_junction_row=False ), ] for se in mock_signing_entities[0:2]: SigningEntitySapProfitCenterFactory.create( reference_signing_entity=se, reference_sap_profit_center=mock_sap_profit_centers[0], deleted_by='Test User', ) SigningEntitySapProfitCenterFactory.create( reference_signing_entity=mock_signing_entities[2], reference_sap_profit_center=mock_sap_profit_centers[1], deleted_by='Test User', ) profit_center_ids = [ mock_sap_profit_center.reference_sap_profit_center_id for mock_sap_profit_center in mock_sap_profit_centers ] _, total_count = ReferenceSigningEntity.get_authorized_for_sap_profit_centers( profit_center_ids, 10, 0 ) assert total_count == 0 def test_get_by_ids(): """Test that existing signing entities are retrieved and non-existent IDs are ignored.""" mock_signing_entities = [ ReferenceSigningEntityFactory.create(with_junction_row=False) for _ in range(3) ] signing_entity_ids = [ entity.reference_signing_entity_id for entity in mock_signing_entities ] non_existent_id = 99999 ids = signing_entity_ids + [non_existent_id] items = ReferenceSigningEntity.get_by_ids(ids) assert len(items) == 3 returned_ids = {item.reference_signing_entity_id for item in items} assert returned_ids == set(signing_entity_ids) assert non_existent_id not in returned_ids def test_get_by_ids_for_empty_list(): """Test that passing an empty list short-circuits and returns an empty list.""" items = ReferenceSigningEntity.get_by_ids([]) assert len(items) == 0 def test_get_by_ids_for_non_matching_ids(): """Test that searching for completely non-existent IDs returns an empty list.""" items = ReferenceSigningEntity.get_by_ids([1111, 2222]) assert len(items) == 0 def test_get_reference_signing_entities(): """Test to get a list of signing entities.""" ReferenceSigningEntityFactory.create( company_code='0001', legal_name='ScreenMedia', with_junction_row=False ) ReferenceSigningEntityFactory.create( company_code='0002', legal_name='JIVE Records', with_junction_row=False ) ReferenceSigningEntityFactory.create( company_code='0003', legal_name='Other label', with_junction_row=False ) items, total_count = ReferenceSigningEntity.get_reference_signing_entities( limit=10, offset=0, sort_by='legal_name', sort_order='asc' ) assert total_count == 3 assert items[0].legal_name == 'JIVE Records' assert items[1].legal_name == 'Other label' assert items[2].legal_name == 'ScreenMedia'