def test_updates_max_update_limit(self, monkeypatch, data): """ Test if the update_company task is called with the right parameters for all the records spread across pages. """ mock_get_company_update_page = mock.Mock( side_effect=lambda _, next_page: data[next_page], ) monkeypatch.setattr( 'datahub.dnb_api.tasks.get_company_update_page', mock_get_company_update_page, ) mock_update_company = mock.Mock() monkeypatch.setattr( 'datahub.dnb_api.tasks.update_company_from_dnb_data', mock_update_company, ) task_result = get_company_updates.apply() assert mock_update_company.apply_async.call_count == 2 expected_kwargs = { 'fields_to_update': None, 'update_descriptor': f'celery:get_company_updates:{task_result.id}', } mock_update_company.apply_async.assert_any_call( args=({'foo': 1},), kwargs=expected_kwargs, ) mock_update_company.apply_async.assert_any_call( args=({'bar': 2},), kwargs=expected_kwargs, )
def test_updates(self, monkeypatch, caplog, data, fields_to_update): """ Test if the update_company task is called with the right parameters for all the records spread across pages. """ caplog.set_level('INFO') mock_get_company_update_page = mock.Mock( side_effect=lambda _, next_page: data[next_page], ) monkeypatch.setattr( 'datahub.dnb_api.tasks.update.get_company_update_page', mock_get_company_update_page, ) mock_update_company = mock.Mock() monkeypatch.setattr( 'datahub.dnb_api.tasks.update.update_company_from_dnb_data', mock_update_company, ) task_result = get_company_updates.apply( kwargs={'fields_to_update': fields_to_update}) assert mock_get_company_update_page.call_count == 2 mock_get_company_update_page.assert_any_call( '2019-01-01T00:00:00', None, ) mock_get_company_update_page.assert_any_call( '2019-01-01T00:00:00', 'http://foo.bar/companies?cursor=page2', ) assert mock_update_company.apply_async.call_count == 3 expected_kwargs = { 'fields_to_update': fields_to_update, 'update_descriptor': f'celery:get_company_updates:{task_result.id}', } mock_update_company.apply_async.assert_any_call( args=({ 'foo': 1 }, ), kwargs=expected_kwargs, ) mock_update_company.apply_async.assert_any_call( args=({ 'bar': 2 }, ), kwargs=expected_kwargs, ) mock_update_company.apply_async.assert_any_call( args=({ 'baz': 3 }, ), kwargs=expected_kwargs, ) assert 'get_company_updates total update count: 3' in caplog.text
def test_updates_with_update_company_from_dnb_data_with_failure( self, mocked_log_to_sentry, mocked_send_realtime_message, monkeypatch, dnb_company_updates_response_uk, ): """ Test full integration for the `get_company_updates` task with the `update_company_from_dnb_data` task when all fields are updated and one company in the dnb-service result does not exist in Data Hub. """ company = CompanyFactory(duns_number='123456789') missing_dnb_company = { **dnb_company_updates_response_uk['results'][0], 'duns_number': '999999999', } dnb_company_updates_response_uk['results'].append(missing_dnb_company) mock_get_company_update_page = mock.Mock( return_value=dnb_company_updates_response_uk, ) monkeypatch.setattr( 'datahub.dnb_api.tasks.update.get_company_update_page', mock_get_company_update_page, ) task_result = get_company_updates.apply() company.refresh_from_db() dnb_company = dnb_company_updates_response_uk['results'][0] assert company.name == dnb_company['primary_name'] expected_gu_number = dnb_company['global_ultimate_duns_number'] assert company.global_ultimate_duns_number == expected_gu_number mocked_log_to_sentry.assert_called_with( 'get_company_updates task completed.', extra={ 'success_count': 1, 'failure_count': 1, 'updated_company_ids': [str(company.pk)], 'producer_task_id': task_result.id, 'start_time': '2019-01-02T02:00:00+00:00', 'end_time': '2019-01-02T02:00:00+00:00', }, ) expected_message = ( 'datahub.dnb_api.tasks.update.get_company_updates ' 'updated: 1; failed to update: 1' ) mocked_send_realtime_message.assert_called_once_with(expected_message)
def test_updates_with_update_company_from_dnb_data_partial_fields( self, mocked_log_to_sentry, mocked_send_realtime_message, monkeypatch, dnb_company_updates_response_uk, ): """ Test full integration for the `get_company_updates` task with the `update_company_from_dnb_data` task when the fields are only partially updated. """ company = CompanyFactory(duns_number='123456789') mock_get_company_update_page = mock.Mock( return_value=dnb_company_updates_response_uk, ) monkeypatch.setattr( 'datahub.dnb_api.tasks.update.get_company_update_page', mock_get_company_update_page, ) task_result = get_company_updates.apply(kwargs={'fields_to_update': ['name']}) company.refresh_from_db() dnb_company = dnb_company_updates_response_uk['results'][0] assert company.name == dnb_company['primary_name'] assert company.global_ultimate_duns_number == '' mocked_log_to_sentry.assert_called_with( 'get_company_updates task completed.', extra={ 'success_count': 1, 'failure_count': 0, 'updated_company_ids': [str(company.pk)], 'producer_task_id': task_result.id, 'start_time': '2019-01-02T02:00:00+00:00', 'end_time': '2019-01-02T02:00:00+00:00', }, ) expected_message = ( 'datahub.dnb_api.tasks.update.get_company_updates ' 'updated: 1; failed to update: 0' ) mocked_send_realtime_message.assert_called_once_with(expected_message)