Esempio n. 1
0
 def test_realtime_messages_sent(
     self,
     monkeypatch,
     automatic_company_archive_feature_flag,
     created_on_delta,
     companies_to_create,
     expected_message,
 ):
     """
     Test that appropriate realtime messaging is sent which reflects the archiving
     actions.
     """
     created_on = timezone.now() - created_on_delta
     for _ in range(companies_to_create):
         with freeze_time(created_on):
             company = CompanyFactory()
         CompanyInteractionFactory(
             company=company,
             date=timezone.now() - relativedelta(years=8, days=1),
         )
     mock_send_realtime_message = mock.Mock()
     monkeypatch.setattr(
         'datahub.company.tasks.company.send_realtime_message',
         mock_send_realtime_message,
     )
     automatic_company_archive.apply_async(kwargs={'simulate': False})
     mock_send_realtime_message.assert_called_once_with(expected_message)
 def test_no_feature_flag(
     self,
     caplog,
 ):
     """
     Test that if the feature flag is not enabled, the
     task will not run.
     """
     caplog.set_level(logging.INFO, logger='datahub.company.tasks.company')
     automatic_company_archive.apply_async(kwargs={'simulate': False})
     assert caplog.messages == [
         f'Feature flag "{AUTOMATIC_COMPANY_ARCHIVE_FEATURE_FLAG}" is not active, exiting.',
     ]
 def test_no_interactions(
     self,
     caplog,
     automatic_company_archive_feature_flag,
     simulate,
 ):
     """
     Test that a company without interaction that fits
     all the other criteria is archived.
     """
     caplog.set_level(logging.INFO, logger='datahub.company.tasks.company')
     gt_3m_ago = timezone.now() - relativedelta(months=3, days=1)
     with freeze_time(gt_3m_ago):
         company = CompanyFactory()
     task_result = automatic_company_archive.apply_async(
         kwargs={'simulate': simulate}, )
     company.refresh_from_db()
     if simulate:
         assert caplog.messages == [
             f'[SIMULATION] Automatically archived company: {company.id}',
         ]
     else:
         assert task_result.successful()
         assert company.archived
         assert caplog.messages == [
             f'Automatically archived company: {company.id}',
         ]
 def test_modified_on(
     self,
     automatic_company_archive_feature_flag,
     modified_on_delta,
     expected_archived,
 ):
     """
     Test that a company modified_on dates around the 3m boundary
     are archived or not as expected.
     """
     gt_3m_ago = timezone.now() - relativedelta(months=3, days=1)
     with freeze_time(gt_3m_ago):
         company = CompanyFactory()
     CompanyInteractionFactory(
         company=company,
         date=timezone.now() - relativedelta(years=8, days=1),
     )
     with freeze_time(timezone.now() - modified_on_delta):
         company.save()
     task_result = automatic_company_archive.apply_async(
         kwargs={'simulate': False})
     assert task_result.successful()
     archived_company = Company.objects.get(pk=company.id)
     assert archived_company.archived == expected_archived
     assert archived_company.modified_on == company.modified_on
Esempio n. 5
0
    def test_limit(
        self,
        automatic_company_archive_feature_flag,
    ):
        """
        Test that we can set a limit to the number of companies
        that are automatically archived.
        """
        gt_3m_ago = timezone.now() - relativedelta(months=3, days=1)
        with freeze_time(gt_3m_ago):
            companies = CompanyFactory.create_batch(3)
        task_result = automatic_company_archive.apply_async(
            kwargs={
                'simulate': False,
                'limit': 2,
            },
        )
        assert task_result.successful()

        archived_companies_count = 0
        for company in companies:
            company.refresh_from_db()
            if company.archived:
                archived_companies_count += 1

        assert archived_companies_count == 2
Esempio n. 6
0
 def test_orders(
     self,
     automatic_company_archive_feature_flag,
 ):
     """
     Test that a company with OMIS orders is not archived.
     """
     gt_3m_ago = timezone.now() - relativedelta(months=3, days=1)
     with freeze_time(gt_3m_ago):
         company = CompanyFactory()
     OrderFactory(company=company)
     task_result = automatic_company_archive.apply_async(kwargs={'simulate': False})
     assert task_result.successful()
     company.refresh_from_db()
     assert not company.archived
Esempio n. 7
0
 def test_investment_projects(
     self,
     automatic_company_archive_feature_flag,
     investment_projects_status,
     expected_archived,
 ):
     """
     Test that a company with active investment projects is not
     archived.
     """
     gt_3m_ago = timezone.now() - relativedelta(months=3, days=1)
     with freeze_time(gt_3m_ago):
         company = CompanyFactory()
     for status in investment_projects_status:
         InvestmentProjectFactory(investor_company=company, status=status)
     task_result = automatic_company_archive.apply_async(kwargs={'simulate': False})
     assert task_result.successful()
     company.refresh_from_db()
     assert company.archived == expected_archived
Esempio n. 8
0
    def test_investor_profile(
        self,
        automatic_company_archive_feature_flag,
    ):
        """
        Test that a company with investor profile is not archived.
        """
        gt_3m_ago = timezone.now() - relativedelta(months=3, days=1)
        with freeze_time(gt_3m_ago):
            companies = CompanyFactory.create_batch(2)
        LargeCapitalInvestorProfileFactory(investor_company=companies[0])
        task_result = automatic_company_archive.apply_async(kwargs={'simulate': False})
        assert task_result.successful()

        archived_companies_count = 0
        for company in companies:
            company.refresh_from_db()
            if company.archived:
                archived_companies_count += 1

        assert archived_companies_count == 1
Esempio n. 9
0
 def test_created_on(
     self,
     automatic_company_archive_feature_flag,
     created_on_delta,
     expected_archived,
 ):
     """
     Test that a company created_on dates around the 3m boundary
     are archived or not as expected.
     """
     created_on = timezone.now() - created_on_delta
     with freeze_time(created_on):
         company = CompanyFactory()
     CompanyInteractionFactory(
         company=company,
         date=timezone.now() - relativedelta(years=8, days=1),
     )
     task_result = automatic_company_archive.apply_async(kwargs={'simulate': False})
     assert task_result.successful()
     company.refresh_from_db()
     assert company.archived == expected_archived
 def test_interactions(
     self,
     automatic_company_archive_feature_flag,
     interaction_date_delta,
     expected_archived,
 ):
     """
     Test that a company with interactions on various dates
     around the 8y boundary are archived or not as expected.
     """
     gt_3m_ago = timezone.now() - relativedelta(months=3, days=1)
     with freeze_time(gt_3m_ago):
         company = CompanyFactory()
     CompanyInteractionFactory(
         company=company,
         date=timezone.now() - interaction_date_delta,
     )
     task_result = automatic_company_archive.apply_async(
         kwargs={'simulate': False}, )
     assert task_result.successful()
     company.refresh_from_db()
     assert company.archived == expected_archived