def test_archive_with_updates(self): """ Test contact archiving with updates on correct contacts """ date = timezone.now() - relativedelta(days=10) with freeze_time(date): company1 = CompanyFactory() company2 = CompanyFactory(archived=True) contact1 = ContactFactory(company=company1) contact2 = ContactFactory(company=company2) contact3 = ContactFactory(company=company2) for c in [contact1, contact2, contact3]: assert c.archived is False assert c.archived_reason is None assert c.archived_on is None # run task twice expecting same result for _ in range(2): task_result = automatic_contact_archive.apply_async( kwargs={'limit': 200}) assert task_result.successful() contact1.refresh_from_db() contact2.refresh_from_db() contact3.refresh_from_db() assert contact1.archived is False assert contact2.archived is True assert contact3.archived is True assert contact1.archived_reason is None assert contact2.archived_reason is not None assert contact3.archived_reason is not None assert contact1.archived_on is None assert contact2.archived_on == date assert contact3.archived_on == date # run again at later time expecting no changes task_result = automatic_contact_archive.apply_async( kwargs={'limit': 200}) assert task_result.successful() contact1.refresh_from_db() contact2.refresh_from_db() contact3.refresh_from_db() assert contact1.archived is False assert contact2.archived is True assert contact3.archived is True assert contact1.archived_reason is None assert contact2.archived_reason is not None assert contact3.archived_reason is not None assert contact1.archived_on is None assert contact2.archived_on == date assert contact3.archived_on == date
def test_simulate(self, caplog, simulate): """ Test contact archiving simulate flag """ caplog.set_level(logging.INFO, logger='datahub.company.tasks.contact') date = timezone.now() - relativedelta(days=10) with freeze_time(date): company1 = CompanyFactory() company2 = CompanyFactory(archived=True) contact1 = ContactFactory(company=company1) contact2 = ContactFactory(company=company2) task_result = automatic_contact_archive.apply_async( kwargs={'simulate': simulate}) contact1.refresh_from_db() contact2.refresh_from_db() if simulate: assert caplog.messages == [ f'[SIMULATION] Automatically archived contact: {contact2.id}', ] else: assert task_result.successful() assert contact1.archived is False assert contact2.archived is True assert caplog.messages == [ f'Automatically archived contact: {contact2.id}' ]
def test_realtime_messages_sent( self, monkeypatch, contacts, message, ): """ Test that appropriate realtime messaging is sent which reflects the archiving actions """ for is_archived in contacts: company = CompanyFactory(archived=is_archived) ContactFactory(company=company) mock_send_realtime_message = mock.Mock() monkeypatch.setattr( 'datahub.company.tasks.contact.send_realtime_message', mock_send_realtime_message, ) automatic_contact_archive.apply_async() mock_send_realtime_message.assert_called_once_with(message)
def test_limit(self): """ Test contact archiving query limit """ limit = 2 contacts = [ ContactFactory(company=CompanyFactory(archived=True)) for _ in range(3) ] task_result = automatic_contact_archive.apply_async( kwargs={'limit': limit}) assert task_result.successful() count = 0 for contact in contacts: contact.refresh_from_db() if contact.archived: count += 1 assert count == limit