Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sync UK company and project site address fields on applicable projects #5929

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions datahub/investment/project/signals.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import reversion

from django.db.models import Q
from django.db.models.signals import m2m_changed, post_save, pre_save
from django.dispatch import receiver
Expand Down Expand Up @@ -121,3 +123,30 @@ def update_country_investment_originates_from(sender, **kwargs):
investment_project.save(
update_fields=('country_investment_originates_from',),
)


@receiver(
post_save,
sender=Company,
dispatch_uid='update_project_site_address_fields_when_company_address_changes_from_post_save',
)
def update_project_site_address_fields_when_company_address_changes(sender, **kwargs):
"""Updates site address in applicable projects when the company's address changes."""
instance = kwargs['instance']
created = kwargs['created']
if not created:
investment_projects = InvestmentProject.objects.filter(
uk_company_id=instance.pk,
site_address_is_company_address=True,
)
for investment_project in investment_projects:
with reversion.create_revision():
investment_project.address_1 = instance.address_1
investment_project.address_2 = instance.address_2
investment_project.address_town = instance.address_town
investment_project.address_postcode = instance.address_postcode
investment_project.save(
update_fields=[
'address_1', 'address_2', 'address_town', 'address_postcode',
],
)
65 changes: 65 additions & 0 deletions datahub/investment/project/test/test_signals.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
from unittest.mock import Mock

import pytest
import reversion

from reversion.models import Version

from datahub.company.test.factories import CompanyFactory
from datahub.core.constants import (
Country as CountryConstant,
InvestmentProjectStage as InvestmentProjectStageConstant,
)
# from core.models import Country
from datahub.core.test_utils import random_obj_for_model
from datahub.investment.project.test.factories import InvestmentProjectFactory
from datahub.metadata.models import InvestmentBusinessActivity
Expand Down Expand Up @@ -136,3 +140,64 @@ def test_update_investor_company_doesnt_update_country_of_origin_for_won_project
str(project.country_investment_originates_from_id)
== CountryConstant.japan.value.id
)


@pytest.mark.django_db
class TestUpdateProjectSiteAddressFieldsWhenCompanyAddressChanges:

def test_update_project_site_address_fields_when_company_address_changes(self):
"""Test post save signal updates site address in applicable investment projects."""
old_address_fields = {
'address_1': 'Old Admiralty Building',
'address_2': 'Whitehall',
'address_town': 'London',
'address_postcode': 'SW1A 2AA',
}
# setup models
with reversion.create_revision():
uk_based_company = CompanyFactory(
address_country_id=CountryConstant.united_kingdom.value.id,
**old_address_fields,
)
project_to_update = InvestmentProjectFactory(
uk_company=uk_based_company,
site_address_is_company_address=True,
**old_address_fields,
)
project_to_not_update = InvestmentProjectFactory(
uk_company=uk_based_company,
site_address_is_company_address=False,
**old_address_fields,
)

# initial assertions
assert Version.objects.get_for_object(project_to_update).count() == 1
assert Version.objects.get_for_object(project_to_not_update).count() == 1

# update company address
new_address_fields = {
'address_1': '10 Downing Street',
'address_2': 'Whitehall',
'address_town': 'Manchester',
'address_postcode': 'M1 1AA',
}
for attribute, value in new_address_fields.items():
setattr(uk_based_company, attribute, value)
uk_based_company.save()

# final assertions
project_to_update.refresh_from_db()
assert project_to_update.site_address_is_company_address is True
assert project_to_update.address_1 == new_address_fields['address_1']
assert project_to_update.address_2 == new_address_fields['address_2']
assert project_to_update.address_town == new_address_fields['address_town']
assert project_to_update.address_postcode == new_address_fields['address_postcode']
assert Version.objects.get_for_object(project_to_update).count() == 2

project_to_not_update.refresh_from_db()
assert project_to_not_update.site_address_is_company_address is False
assert project_to_not_update.address_1 == old_address_fields['address_1']
assert project_to_not_update.address_2 == old_address_fields['address_2']
assert project_to_not_update.address_town == old_address_fields['address_town']
assert project_to_not_update.address_postcode == old_address_fields['address_postcode']
assert Version.objects.get_for_object(project_to_not_update).count() == 1