-
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 #5709 from uktrade/TET-830-add-omis-to-activity-st…
…ream Add omis order to company activity
- Loading branch information
Showing
26 changed files
with
491 additions
and
20 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
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 |
---|---|---|
|
@@ -110,6 +110,7 @@ | |
CompanyReferralFactory, | ||
CompanyInteractionFactory, | ||
InvestmentProjectFactory, | ||
OrderFactory, | ||
] | ||
|
||
|
||
|
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
25 changes: 25 additions & 0 deletions
25
datahub/company_activity/migrations/0007_companyactivity_order_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-10-14 08:22 | ||
|
||
from django.db import migrations, models | ||
import django.db.models.deletion | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('order', '0014_alter_order_company_alter_order_contact_and_more'), | ||
('company_activity', '0006_alter_great_data_comment_and_more'), | ||
] | ||
|
||
operations = [ | ||
migrations.AddField( | ||
model_name='companyactivity', | ||
name='order', | ||
field=models.ForeignKey(blank=True, help_text='If related to an omis order, must not have relations to any other activity (referral, event etc)', null=True, on_delete=django.db.models.deletion.CASCADE, related_name='activity', to='order.order', unique=True), | ||
), | ||
migrations.AlterField( | ||
model_name='companyactivity', | ||
name='activity_source', | ||
field=models.CharField(choices=[('interaction', 'interaction'), ('referral', 'referral'), ('event', 'event'), ('investment', 'investment'), ('order', 'order')], 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_order_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,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_orders, | ||
schedule_sync_order_to_company_activity, | ||
) | ||
from datahub.omis.order.test.factories import OrderFactory | ||
|
||
|
||
@pytest.mark.django_db | ||
class TestCompanyActivityOrderTasks: | ||
""" | ||
Tests for the schedule_sync_investments_to_company_activity task. | ||
""" | ||
|
||
def test_orders_are_copied_to_company_activity(self): | ||
""" | ||
Test that investments are added to the CompanyActivity model. | ||
""" | ||
orders = OrderFactory.create_batch(5) | ||
|
||
# Remove the created CompanyActivities added by the omis order `save` method | ||
# to mimic already existing data in staging and prod database. | ||
CompanyActivity.objects.all().delete() | ||
assert CompanyActivity.objects.count() == 0 | ||
|
||
# Check the "existing" orders are added to the company activity model | ||
schedule_sync_order_to_company_activity() | ||
assert CompanyActivity.objects.count() == len(orders) | ||
|
||
company_activity = CompanyActivity.objects.get(order_id=orders[0].id) | ||
assert company_activity.date == orders[0].created_on | ||
assert company_activity.activity_source == CompanyActivity.ActivitySource.order | ||
assert company_activity.company_id == orders[0].company.id | ||
|
||
@mock.patch('datahub.company_activity.models.CompanyActivity.objects.bulk_create') | ||
def test_order_are_bulk_created_in_batches(self, mocked_bulk_create, caplog): | ||
""" | ||
Test that omis orders are bulk created in batches. | ||
""" | ||
caplog.set_level('INFO') | ||
batch_size = 5 | ||
|
||
OrderFactory.create_batch(10) | ||
|
||
# Delete any activity created through the investments save method. | ||
CompanyActivity.objects.all().delete() | ||
assert CompanyActivity.objects.count() == 0 | ||
|
||
# Ensure omis orders are bulk_created | ||
relate_company_activity_to_orders(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_order_with_a_company_activity_are_not_added_again(self): | ||
""" | ||
Test that investment projects which are already part of the `CompanyActivity` model | ||
are not added again. | ||
""" | ||
OrderFactory.create_batch(4) | ||
|
||
assert CompanyActivity.objects.count() == 4 | ||
|
||
# Check count remains unchanged. | ||
schedule_sync_order_to_company_activity() | ||
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
Oops, something went wrong.