-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
CLS2-1031 Add EYB Lead to Company Activity and Opensearch (#5828)
* Add EYB Lead to Company Activity * Add EYB Lead / Company Activity to Opensearch
- Loading branch information
1 parent
8ea5471
commit 91c9bf7
Showing
18 changed files
with
335 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -112,6 +112,7 @@ | |
GreatExportEnquiryFactory, | ||
InvestmentProjectFactory, | ||
OrderFactory, | ||
EYBLeadFactory, | ||
] | ||
|
||
|
||
|
25 changes: 25 additions & 0 deletions
25
datahub/company_activity/migrations/0018_companyactivity_eyb_lead_and_more.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
# Generated by Django 4.2.16 on 2024-11-27 15:47 | ||
|
||
from django.db import migrations, models | ||
import django.db.models.deletion | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('investment_lead', '0009_alter_eyblead_marketing_hashed_uuid'), | ||
('company_activity', '0017_ingestedfile_file_created_and_more'), | ||
] | ||
|
||
operations = [ | ||
migrations.AddField( | ||
model_name='companyactivity', | ||
name='eyb_lead', | ||
field=models.ForeignKey(blank=True, help_text='If related to an EYB lead, must not have relations to any other activity (referral, event etc)', null=True, on_delete=django.db.models.deletion.CASCADE, related_name='activity', to='investment_lead.eyblead', unique=True), | ||
), | ||
migrations.AlterField( | ||
model_name='companyactivity', | ||
name='activity_source', | ||
field=models.CharField(choices=[('interaction', 'interaction'), ('referral', 'referral'), ('event', 'event'), ('investment', 'investment'), ('order', 'order'), ('great_export_enquiry', 'great_export_enquiry'), ('eyb_lead', 'eyb_lead')], help_text='The type of company activity, such as an interaction, event, referral etc.', max_length=255), | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 76 additions & 0 deletions
76
datahub/company_activity/tests/test_tasks/test_eyb_lead_tasks.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
from unittest import mock | ||
|
||
import pytest | ||
|
||
from datahub.company_activity.models import CompanyActivity | ||
from datahub.company_activity.tasks.sync import ( | ||
relate_company_activity_to_eyb_lead, | ||
schedule_sync_data_to_company_activity, | ||
) | ||
from datahub.investment_lead.test.factories import EYBLeadFactory | ||
|
||
|
||
@pytest.mark.django_db | ||
class TestCompanyActivityEYBLeadTasks: | ||
""" | ||
Tests for the schedule_sync_data_to_company_activity task. | ||
""" | ||
|
||
def test_eyb_leads_are_copied_to_company_activity(self): | ||
""" | ||
Test that eyb leads are added to the CompanyActivity model. | ||
""" | ||
eyb_leads = EYBLeadFactory.create_batch(5) | ||
|
||
# Remove the created CompanyActivities added by the eyb lead `save` method | ||
# to mimic already existing data in staging and prod database. | ||
CompanyActivity.objects.all().delete() | ||
assert CompanyActivity.objects.count() == 0 | ||
|
||
# Check the "existing" eyb leads are added to the company activity model | ||
schedule_sync_data_to_company_activity(relate_company_activity_to_eyb_lead) | ||
assert CompanyActivity.objects.count() == len(eyb_leads) | ||
|
||
company_activity = CompanyActivity.objects.get(eyb_lead=eyb_leads[0]) | ||
assert company_activity.date == eyb_leads[0].created_on | ||
assert company_activity.activity_source == CompanyActivity.ActivitySource.eyb_lead | ||
assert company_activity.eyb_lead_id == eyb_leads[0].id | ||
|
||
@mock.patch('datahub.company_activity.models.CompanyActivity.objects.bulk_create') | ||
def test_eyb_leads_are_bulk_created_in_batches(self, mocked_bulk_create, caplog): | ||
""" | ||
Test that eyb leads are bulk created in batches. | ||
""" | ||
caplog.set_level('INFO') | ||
batch_size = 5 | ||
|
||
EYBLeadFactory.create_batch(10) | ||
|
||
# Delete any activity created through the investments save method. | ||
CompanyActivity.objects.all().delete() | ||
assert CompanyActivity.objects.count() == 0 | ||
|
||
# Ensure eyb leads are bulk_created | ||
relate_company_activity_to_eyb_lead(batch_size) | ||
assert mocked_bulk_create.call_count == 2 | ||
|
||
assert ( | ||
f'Creating in batches of: {batch_size} CompanyActivities. 10 remaining.' in caplog.text | ||
) | ||
assert ( | ||
f'Creating in batches of: {batch_size} CompanyActivities. 5 remaining.' in caplog.text | ||
) | ||
assert 'Finished bulk creating CompanyActivities.' in caplog.text | ||
|
||
def test_eyb_leads_with_a_company_activity_are_not_added_again(self): | ||
""" | ||
Test that eyb leads which are already part of the `CompanyActivity` model | ||
are not added again. | ||
""" | ||
EYBLeadFactory.create_batch(4) | ||
|
||
assert CompanyActivity.objects.count() == 4 | ||
|
||
# Check count remains unchanged. | ||
schedule_sync_data_to_company_activity(relate_company_activity_to_eyb_lead) | ||
assert CompanyActivity.objects.count() == 4 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,30 @@ | ||
import pytest | ||
|
||
from datahub.company_activity.models import CompanyActivity | ||
from datahub.investment_lead.test.factories import EYBLeadFactory | ||
|
||
|
||
@pytest.mark.django_db | ||
class TestEYBLead: | ||
"""Tests EYB Lead model""" | ||
|
||
def test_save_without_company_no_company_activity(self): | ||
assert not CompanyActivity.objects.all().exists() | ||
EYBLeadFactory(company=None) | ||
assert not CompanyActivity.objects.all().exists() | ||
|
||
def test_save_with_company_creates_company_activity(self): | ||
assert not CompanyActivity.objects.all().exists() | ||
|
||
eyb_lead = EYBLeadFactory() | ||
|
||
assert CompanyActivity.objects.all().count() == 1 | ||
|
||
company_activity = CompanyActivity.objects.get(eyb_lead=eyb_lead.id) | ||
assert company_activity.company_id == eyb_lead.company.id | ||
assert company_activity.date == eyb_lead.created_on | ||
assert company_activity.activity_source == CompanyActivity.ActivitySource.eyb_lead | ||
|
||
def test_str(self, eyb_lead_instance_from_db): | ||
"""Test the human friendly string representation of the object""" | ||
assert str(eyb_lead_instance_from_db) == eyb_lead_instance_from_db.name |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.