def test_sync_outdated_companies_sync_task_failure_logs_error( caplog, monkeypatch): """ Test that when the sync_company_with_dnb sub-task fails, an error log is generated. """ caplog.set_level('WARNING') company = CompanyFactory( duns_number='123456789', dnb_modified_on=now() - timedelta(days=5), ) mocked_sync_company_with_dnb = mock.Mock(side_effect=Exception()) monkeypatch.setattr( 'datahub.dnb_api.tasks.sync_company_with_dnb.apply', mocked_sync_company_with_dnb, ) task_result = sync_outdated_companies_with_dnb.apply_async(kwargs={ 'fields_to_update': ['global_ultimate_duns_number'], 'dnb_modified_on_before': now() - timedelta(days=1), 'simulate': False, }, ) assert task_result.successful() expected_message = f'Syncing dnb-linked company "{company.id}" Failed' assert expected_message in caplog.text
def test_sync_outdated_companies_nothing_to_update( requests_mock, dnb_response_uk, ): """ Add two companies (one with dnb_modified_on>dnb_modified_on_before) and assert that only the outdated one is synced. """ company = CompanyFactory( duns_number='123456789', dnb_modified_on=now() - timedelta(days=5), ) original_company = Company.objects.get(id=company.id) task_result = sync_outdated_companies_with_dnb.apply_async(kwargs={ 'fields_to_update': ['global_ultimate_duns_number'], 'dnb_modified_on_before': now() - timedelta(days=1), 'simulate': False, 'limit': 1, }, ) assert task_result.successful() company.refresh_from_db() # We expect the company to be unmodified assert company.dnb_modified_on == original_company.dnb_modified_on
def test_sync_outdated_companies_with_dnb_all_fields( requests_mock, dnb_response_uk, base_company_dict, existing_company_dnb_modified_on, caplog, ): """ Test the sync_outdated_companies_with_dnb task when all fields should be synced. """ caplog.set_level('INFO') if callable(existing_company_dnb_modified_on): existing_company_dnb_modified_on = existing_company_dnb_modified_on() requests_mock.post( DNB_SEARCH_URL, json=dnb_response_uk, ) company = CompanyFactory( duns_number='123456789', dnb_modified_on=existing_company_dnb_modified_on, ) original_company = Company.objects.get(id=company.id) task_result = sync_outdated_companies_with_dnb.apply_async( kwargs={ 'dnb_modified_on_before': now() + timedelta(days=1), 'simulate': False, }, ) assert task_result.successful() company.refresh_from_db() uk_country = Country.objects.get(iso_alpha2_code='GB') assert model_to_dict_company(company) == { **base_company_dict, 'address_1': 'Unit 10, Ockham Drive', 'address_2': '', 'address_country': uk_country.id, 'address_county': '', 'address_postcode': 'UB6 0F2', 'address_town': 'GREENFORD', 'archived_documents_url_path': original_company.archived_documents_url_path, 'business_type': original_company.business_type_id, 'company_number': '01261539', 'created_by': original_company.created_by_id, 'duns_number': '123456789', 'employee_range': original_company.employee_range_id, 'export_experience_category': original_company.export_experience_category_id, 'global_ultimate_duns_number': '291332174', 'id': original_company.id, 'is_number_of_employees_estimated': True, 'modified_by': original_company.modified_by_id, 'name': 'FOO BICYCLE LIMITED', 'number_of_employees': 260, 'sector': original_company.sector_id, 'turnover': 50651895, 'turnover_range': original_company.turnover_range_id, 'uk_region': original_company.uk_region_id, 'dnb_modified_on': now(), } expected_message = f'Syncing dnb-linked company "{company.id}" Succeeded' assert expected_message in caplog.text
def test_sync_outdated_companies_limit_most_recently_interacted_updated( requests_mock, dnb_response_uk, ): """ Test that running sync_outdated_companies_with_dnb with a limit will update the most recently interacted company. """ requests_mock.post( DNB_SEARCH_URL, json=dnb_response_uk, ) company_most_recent_interaction = CompanyFactory( duns_number='123456789', dnb_modified_on=now() - timedelta(days=1), ) CompanyInteractionFactory(company=company_most_recent_interaction, date=now()) company_least_recent_interaction = CompanyFactory( duns_number='123456788', dnb_modified_on=now() - timedelta(days=1), ) CompanyInteractionFactory( company=company_least_recent_interaction, date=now() - timedelta(days=1), ) task_result = sync_outdated_companies_with_dnb.apply_async(kwargs={ 'fields_to_update': ['global_ultimate_duns_number'], 'dnb_modified_on_before': now() + timedelta(days=1), 'simulate': False, 'limit': 1, }, ) company_least_recent_interaction.refresh_from_db() company_most_recent_interaction.refresh_from_db() assert task_result.successful() # We expect the least recently interacted company to be unmodified assert company_least_recent_interaction.dnb_modified_on == now( ) - timedelta(days=1) # We expect most recently interacted company to be modified assert company_most_recent_interaction.dnb_modified_on == now()
def test_sync_outdated_companies_limit_least_recently_synced_is_updated( requests_mock, dnb_response_uk, ): """ Test that running sync_outdated_companies_with_dnb with a limit will update the least recently synced company. """ requests_mock.post( DNB_SEARCH_URL, json=dnb_response_uk, ) company_1 = CompanyFactory( duns_number='123456788', dnb_modified_on=now() - timedelta(days=1), ) original_company_1 = Company.objects.get(id=company_1.id) company_2 = CompanyFactory( duns_number='123456789', dnb_modified_on=now() - timedelta(days=2), ) task_result = sync_outdated_companies_with_dnb.apply_async(kwargs={ 'fields_to_update': ['global_ultimate_duns_number'], 'dnb_modified_on_before': now() + timedelta(days=1), 'simulate': False, 'limit': 1, }, ) assert task_result.successful() company_1.refresh_from_db() company_2.refresh_from_db() # We expect company_1 to be unmodified assert company_1.dnb_modified_on == original_company_1.dnb_modified_on # We expect company_2 to be modified, as it was least recently synced with D&B assert company_2.dnb_modified_on == now()
def test_sync_outdated_companies_simulation(caplog): """ Test that using simulation mode does not modify companies and logs correctly. """ caplog.set_level('INFO') company = CompanyFactory( duns_number='123456789', dnb_modified_on=now() - timedelta(days=5), ) original_company = Company.objects.get(id=company.id) task_result = sync_outdated_companies_with_dnb.apply_async(kwargs={ 'fields_to_update': ['global_ultimate_duns_number'], 'dnb_modified_on_before': now() - timedelta(days=1), }, ) assert task_result.successful() company.refresh_from_db() # We expect the company to be unmodified assert company.dnb_modified_on == original_company.dnb_modified_on expected_message = f'[SIMULATION] Syncing dnb-linked company "{company.id}" Succeeded' assert expected_message in caplog.text
def test_sync_outdated_companies_with_dnb_partial_fields( requests_mock, dnb_response_uk, base_company_dict, existing_company_dnb_modified_on, caplog, ): """ Test the sync_outdated_companies_with_dnb task when only a subset of fields should be synced. """ caplog.set_level('INFO') if callable(existing_company_dnb_modified_on): existing_company_dnb_modified_on = existing_company_dnb_modified_on() requests_mock.post( DNB_SEARCH_URL, json=dnb_response_uk, ) company = CompanyFactory( duns_number='123456789', dnb_modified_on=existing_company_dnb_modified_on, ) original_company = Company.objects.get(id=company.id) task_result = sync_outdated_companies_with_dnb.apply_async(kwargs={ 'fields_to_update': ['global_ultimate_duns_number'], 'dnb_modified_on_before': now() + timedelta(days=1), 'simulate': False, }, ) assert task_result.successful() company.refresh_from_db() assert model_to_dict(company) == { **base_company_dict, 'address_1': original_company.address_1, 'address_2': original_company.address_2, 'address_country': original_company.address_country_id, 'address_county': original_company.address_county, 'address_postcode': original_company.address_postcode, 'address_town': original_company.address_town, 'archived_documents_url_path': original_company.archived_documents_url_path, 'business_type': original_company.business_type_id, 'company_number': original_company.company_number, 'created_by': original_company.created_by_id, 'duns_number': original_company.duns_number, 'employee_range': original_company.employee_range_id, 'export_experience_category': original_company.export_experience_category_id, 'global_ultimate_duns_number': '291332174', 'id': original_company.id, 'is_number_of_employees_estimated': original_company.is_number_of_employees_estimated, 'is_turnover_estimated': original_company.is_turnover_estimated, 'modified_by': original_company.modified_by_id, 'name': original_company.name, 'number_of_employees': original_company.number_of_employees, 'registered_address_1': original_company.registered_address_1, 'registered_address_2': original_company.registered_address_2, 'registered_address_country': original_company.registered_address_country_id, 'registered_address_county': original_company.registered_address_county, 'registered_address_postcode': original_company.registered_address_postcode, 'registered_address_town': original_company.registered_address_town, 'sector': original_company.sector_id, 'trading_names': original_company.trading_names, 'turnover': original_company.turnover, 'turnover_range': original_company.turnover_range_id, 'uk_region': original_company.uk_region_id, 'website': original_company.website, 'dnb_modified_on': now(), } expected_message = f'Syncing dnb-linked company "{company.id}" Succeeded' assert expected_message in caplog.text