-
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.
Merge pull request #5771 from uktrade/TET-851-company-activity-great-…
…data Add great export enquiries to company activity
- Loading branch information
Showing
10 changed files
with
263 additions
and
6 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
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
24 changes: 24 additions & 0 deletions
24
datahub/company_activity/migrations/0012_companyactivity_great_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,24 @@ | ||
# Generated by Django 4.2.16 on 2024-11-04 16:59 | ||
|
||
from django.db import migrations, models | ||
import django.db.models.deletion | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('company_activity', '0011_revert_onetoone_to_foreignkey'), | ||
] | ||
|
||
operations = [ | ||
migrations.AddField( | ||
model_name='companyactivity', | ||
name='great', | ||
field=models.ForeignKey(blank=True, help_text='If related to an great export enquiry, must not have relations to any other activity (referral, event etc)', null=True, on_delete=django.db.models.deletion.CASCADE, related_name='activity', to='company_activity.greatexportenquiry', 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', 'great')], 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
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
48 changes: 48 additions & 0 deletions
48
datahub/company_activity/tests/models/test_great_export_enquiry.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,48 @@ | ||
import pytest | ||
|
||
from datahub.company.test.factories import CompanyFactory | ||
from datahub.company_activity.models import CompanyActivity | ||
from datahub.company_activity.tests.factories import GreatExportEnquiryFactory | ||
|
||
|
||
@pytest.mark.django_db | ||
class TestGreatExportEnquiry: | ||
"""Tests for the Great Export Enquiry model.""" | ||
|
||
def test_save(self): | ||
""" | ||
Test save also saves to the `CompanyActivity` model and does not save to the | ||
`CompanyActivity` model if it already exists. | ||
""" | ||
assert not CompanyActivity.objects.all().exists() | ||
great = GreatExportEnquiryFactory() | ||
assert CompanyActivity.objects.all().count() == 1 | ||
|
||
company_activity = CompanyActivity.objects.get(great_id=great.id) | ||
assert company_activity.company_id == great.company_id | ||
assert company_activity.date == great.created_on | ||
assert company_activity.activity_source == CompanyActivity.ActivitySource.great | ||
|
||
# Update and save the great export enquiry and ensure if doesn't create another | ||
# `CompanyActivity` and only updates it | ||
new_company = CompanyFactory() | ||
great.company_id = new_company.id | ||
great.save() | ||
|
||
assert CompanyActivity.objects.all().count() == 1 | ||
company_activity.refresh_from_db() | ||
assert company_activity.company_id == new_company.id | ||
|
||
great.delete() | ||
assert not CompanyActivity.objects.all().exists() | ||
|
||
def test_save_with_no_company(self): | ||
""" | ||
Test save does not save to the `CompanyActivity` model | ||
""" | ||
assert not CompanyActivity.objects.all().exists() | ||
|
||
# Try to save the great export enquiry with no company id which will not work | ||
GreatExportEnquiryFactory(company_id=None) | ||
|
||
assert not CompanyActivity.objects.all().exists() |
17 changes: 17 additions & 0 deletions
17
datahub/company_activity/tests/test_tasks/test_factories.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,17 @@ | ||
import pytest | ||
|
||
from datahub.company_activity.models import CompanyActivity | ||
from datahub.company_activity.tests.factories import CompanyActivityGreatExportEnquiryFactory | ||
|
||
|
||
@pytest.mark.django_db | ||
class TestCompanyActivityGreatExportEnquiryFactory: | ||
|
||
def test_factory_does_not_create_duplicates(self): | ||
""" | ||
As the GreatExportEnquiry models save method is overwritten to create a company | ||
activity. The _create method on the factory returns the created activity from the save | ||
rather than creating a new one causing duplicates. | ||
""" | ||
CompanyActivityGreatExportEnquiryFactory() | ||
assert CompanyActivity.objects.count() == 1 |
77 changes: 77 additions & 0 deletions
77
datahub/company_activity/tests/test_tasks/test_great_task.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,77 @@ | ||
from unittest import mock | ||
|
||
import pytest | ||
|
||
from datahub.company_activity.models import CompanyActivity | ||
from datahub.company_activity.tasks.sync import ( | ||
relate_company_activity_to_great, | ||
schedule_sync_data_to_company_activity, | ||
) | ||
from datahub.company_activity.tests.factories import GreatExportEnquiryFactory | ||
|
||
|
||
@pytest.mark.django_db | ||
class TestCompanyActivityGreatTasks: | ||
""" | ||
Tests for the schedule_sync_data_to_company_activity task for great export enquiry. | ||
""" | ||
|
||
def test_great_export_enquiry_are_copied_to_company_activity(self): | ||
""" | ||
Test that great export enquiry are added to the CompanyActivity model. | ||
""" | ||
great = GreatExportEnquiryFactory() | ||
GreatExportEnquiryFactory.create_batch(3) | ||
|
||
# Remove the created CompanyActivities added by the Great model `save` method | ||
# to mimic already existing data in staging and prod database. | ||
CompanyActivity.objects.all().delete() | ||
assert CompanyActivity.objects.count() == 0 | ||
|
||
# Check the "existing" great are added to the company activity model | ||
schedule_sync_data_to_company_activity(relate_company_activity_to_great) | ||
assert CompanyActivity.objects.count() == 4 | ||
|
||
company_activity = CompanyActivity.objects.get(great_id=great.id) | ||
assert company_activity.date == great.created_on | ||
assert company_activity.activity_source == CompanyActivity.ActivitySource.great | ||
assert company_activity.company_id == great.company.id | ||
|
||
@mock.patch('datahub.company_activity.models.CompanyActivity.objects.bulk_create') | ||
def test_great_export_enquiry_are_bulk_created_in_batches(self, mocked_bulk_create, caplog): | ||
""" | ||
Test that great export enquiries are bulk created in batches. | ||
""" | ||
caplog.set_level('INFO') | ||
batch_size = 5 | ||
|
||
GreatExportEnquiryFactory.create_batch(10) | ||
|
||
# Delete any activity created through the great export enquiry save method. | ||
CompanyActivity.objects.all().delete() | ||
assert CompanyActivity.objects.count() == 0 | ||
|
||
# Ensure great export enquiry are bulk_created | ||
relate_company_activity_to_great(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_great_export_enquiry_with_a_company_activity_are_not_added_again(self): | ||
""" | ||
Test that great export enquiries which are already part of the `CompanyActivity` model | ||
are not added again. | ||
""" | ||
GreatExportEnquiryFactory.create_batch(4) | ||
|
||
assert CompanyActivity.objects.count() == 4 | ||
|
||
# Check count remains unchanged. | ||
schedule_sync_data_to_company_activity(relate_company_activity_to_great) | ||
assert CompanyActivity.objects.count() == 4 |