"""Unit tests for vendor_dms_master_restriction logic.""" from unittest.mock import patch from owsresponse import response from abacus_legacy_sync.logic import vendor_dms_master_restriction as logic from tests.utils.factories import ( CustomerMasterMasterFactory, DistributionTypeFactory, VendorContractFactory, VendorDmsMasterRestrictionFactory, ) @patch( 'abacus_legacy_sync.logic.vendor_dms_master_restriction.carveouts_python.' 'save_account_carveouts' ) def test_create_vendor_dms_master_restriction_success(mock_save): """Test create_vendor_dms_master_restriction function for successful execution.""" vendor_contract_id = 77 distribution_type_id = 1 store_ids = ['1', '2'] params = { 'vendor_contract_id': vendor_contract_id, 'distribution_type_id': distribution_type_id, 'store_ids': store_ids, } result = logic.create_vendor_dms_master_restriction(**params) # On success, the function doesn't return anything. assert result is None # Assert that the mocked function was called the correct number of times assert mock_save.call_count == 1 # Verify the arguments for each call expected_calls = [ ( vendor_contract_id, { 'service': [ {'service_id': '1', 'distribution_types': [1]}, {'service_id': '2', 'distribution_types': [1]}, ] }, ) ] for i, call_args in enumerate(mock_save.call_args_list): assert call_args.args == expected_calls[0] @patch( 'abacus_legacy_sync.logic.vendor_dms_master_restriction.carveouts_python.' 'save_account_carveouts', side_effect=Exception('Test error message'), ) def test_create_vendor_dms_master_restriction_exception_handling(mock_save): """Test create_vendor_dms_master_restriction handles an exception correctly.""" params = {'vendor_contract_id': 77, 'distribution_type_id': 1, 'store_ids': ['1']} result = logic.create_vendor_dms_master_restriction(**params) # The function should catch the exception and return an error response assert result is not None assert isinstance(result, response.Response) assert result.status == 400 assert result.errors['code'] == 'error' assert result.errors['message'] == 'Test error message' @patch('abacus_legacy_sync.logic.vendor_dms_master_restriction.models') def test_update_dms_master_restriction_no_restriction(mock_models): """Test update_vendor_dms_master_restriction function return true. When there is no entries in vendor_dms_master_restriction table and store_ids field is empty. """ mock_models.VendorDmsMasterRestriction.get_by_criteria.return_value = [] params = {'vendor_contract_id': 1, 'distribution_type_id': 1, 'store_ids': None} result = logic.update_vendor_dms_master_restriction(**params) assert result mock_models.VendorDmsMasterRestriction.get_by_criteria.assert_called_once() @patch('abacus_legacy_sync.logic.vendor_dms_master_restriction.models') def test_update_dms_master_restriction_delete_restriction(mock_models): """Test update_vendor_dms_master_restriction function. delete existing entries in vendor_dms_master_restriction table when store_ids field is None. """ vendor_contract = VendorContractFactory.create() distribution_type = DistributionTypeFactory.create() customer_master_master = CustomerMasterMasterFactory.create() dms_master_restriction = VendorDmsMasterRestrictionFactory.create( customer_master_master=customer_master_master, distribution_type=distribution_type, vendor_contract=vendor_contract, ) mock_models.VendorDmsMasterRestriction.get_by_criteria.return_value = [ dms_master_restriction ] params = { 'vendor_contract_id': vendor_contract.vendor_contract_id, 'distribution_type_id': distribution_type.distribution_type_id, 'store_ids': None, } logic.update_vendor_dms_master_restriction(**params) mock_models.VendorDmsMasterRestriction.get_by_criteria.assert_called_once() mock_models.VendorDmsMasterRestriction.delete_by_ids.assert_called_once_with( [dms_master_restriction.restriction_id] ) @patch( 'abacus_legacy_sync.logic.vendor_dms_master_restriction.' '_update_or_create_vendor_dms_master_restriction' ) @patch('abacus_legacy_sync.logic.vendor_dms_master_restriction.models') def test_update_dms_master_restriction(mock_models, mock_update_logic): """Test update_vendor_dms_master_restriction function. update/create/delete the store ids when store_ids field has values """ vendor_contract = VendorContractFactory.create() distribution_type = DistributionTypeFactory.create() customer_master_master = CustomerMasterMasterFactory.create() dms_master_restriction = VendorDmsMasterRestrictionFactory.create( customer_master_master=customer_master_master, distribution_type=distribution_type, vendor_contract=vendor_contract, ) mock_models.VendorDmsMasterRestriction.get_by_criteria.return_value = [ dms_master_restriction ] mock_update_logic.return_value = True params = { 'vendor_contract_id': vendor_contract.vendor_contract_id, 'distribution_type_id': distribution_type.distribution_type_id, 'store_ids': ['11', '123'], } logic.update_vendor_dms_master_restriction(**params) params.update({'vendor_dms_master_restrictions': [dms_master_restriction]}) mock_models.VendorDmsMasterRestriction.get_by_criteria.assert_called_once() mock_update_logic.assert_called_once_with(**params) @patch('abacus_legacy_sync.logic.vendor_dms_master_restriction.models') def test_update_or_create_dms_master_restriction_delete_restrictions(mock_models): """Test _update_or_create_vendor_dms_master_restriction function. Update the existing restriction with new store ids and delete additional restrictions. """ # noqa: E501 vendor_contract = VendorContractFactory.create() distribution_type = DistributionTypeFactory.create() customer_master_masters_1 = [ CustomerMasterMasterFactory.create(customer_name=customer_name) for customer_name in ['eMusic', 'Buymusic', 'Audio Lunchbox'] ] customer_master_masters_2 = [ CustomerMasterMasterFactory.create(customer_name=customer_name) for customer_name in ['7 Digital', 'Secury Cast', 'iMusica'] ] vendor_dms_master_restrictions = [ VendorDmsMasterRestrictionFactory.create( customer_master_master=customer_master_master, distribution_type=distribution_type, vendor_contract=vendor_contract, ) for customer_master_master in customer_master_masters_1 ] mock_models.VendorDmsMasterRestriction.update_by_ids.return_value = True mock_models.VendorDmsMasterRestriction.delete_by_ids.return_value = True params = { 'vendor_contract_id': vendor_contract.vendor_contract_id, 'distribution_type_id': distribution_type.distribution_type_id, 'store_ids': [str(customer_master_masters_2[0].customer_master_master_id)], 'vendor_dms_master_restrictions': vendor_dms_master_restrictions, } result = logic._update_or_create_vendor_dms_master_restriction(**params) assert result mock_models.VendorDmsMasterRestriction.update_by_ids.assert_called_once_with( { vendor_dms_master_restrictions[0].restriction_id: str( customer_master_masters_2[0].customer_master_master_id ) } ) mock_models.VendorDmsMasterRestriction.delete_by_ids.assert_called_once_with( [ vendor_dms_master_restrictions[1].restriction_id, vendor_dms_master_restrictions[2].restriction_id, ] ) @patch( 'abacus_legacy_sync.logic.vendor_dms_master_restriction' '._create_vendor_dms_master_restriction' ) @patch('abacus_legacy_sync.logic.vendor_dms_master_restriction.models') def test_update_or_create_vendor_dms_master_restrictions( mock_models, mock_create_logic ): """Test _update_or_create_vendor_dms_master_restriction function. Update the existing restriction with new store ids and create additional restrictions. """ # noqa: E501 vendor_contract = VendorContractFactory.create() distribution_type = DistributionTypeFactory.create() customer_master_masters_1 = [ CustomerMasterMasterFactory.create(customer_name=customer_name) for customer_name in ['eMusic', 'Buymusic'] ] customer_master_masters_2 = [ CustomerMasterMasterFactory.create(customer_name=customer_name) for customer_name in ['7 Digital', 'Secury Cast', 'iMusica'] ] vendor_dms_master_restrictions = [ VendorDmsMasterRestrictionFactory.create( customer_master_master=customer_master_master, distribution_type=distribution_type, vendor_contract=vendor_contract, ) for customer_master_master in customer_master_masters_1 ] mock_models.VendorDmsMasterRestriction.update_by_ids.return_value = True mock_create_logic.return_value = True params = { 'vendor_contract_id': vendor_contract.vendor_contract_id, 'distribution_type_id': distribution_type.distribution_type_id, 'store_ids': [ str(customer_master_master.customer_master_master_id) for customer_master_master in customer_master_masters_2 ], 'vendor_dms_master_restrictions': vendor_dms_master_restrictions, } result = logic._update_or_create_vendor_dms_master_restriction(**params) assert result mock_models.VendorDmsMasterRestriction.update_by_ids.assert_called_once_with( { vendor_dms_master_restrictions[0].restriction_id: str( customer_master_masters_2[0].customer_master_master_id ), vendor_dms_master_restrictions[1].restriction_id: str( customer_master_masters_2[1].customer_master_master_id ), } ) mock_create_logic.assert_called_once_with( distribution_type_id=distribution_type.distribution_type_id, store_ids=[str(customer_master_masters_2[2].customer_master_master_id)], vendor_contract_id=vendor_contract.vendor_contract_id, ) mock_models.VendorDmsMasterRestriction.delete_by_ids.assert_not_called()