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') 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') 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
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
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
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
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
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