Skip to content

Commit

Permalink
Merge pull request #5727 from uktrade/feature/TET-606-add-additional-…
Browse files Browse the repository at this point in the history
…merge-logs

Add logs to show source and target relation counts before merge
  • Loading branch information
DeanElliott96 authored Oct 28, 2024
2 parents 4730f16 + 91f7b28 commit d6478be
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
11 changes: 11 additions & 0 deletions datahub/company/merge_company.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import reversion

from datahub.company.merge import (
get_planned_changes,
is_model_a_valid_merge_source,
is_model_a_valid_merge_target,
MergeConfiguration,
Expand Down Expand Up @@ -150,6 +151,16 @@ def merge_companies(source_company: Company, target_company: Company, user):
with reversion.create_revision():
reversion.set_comment('Company merged')
try:
target_changes, _ = get_planned_changes(target_company, MERGE_CONFIGURATION)
source_changes, _ = get_planned_changes(source_company, MERGE_CONFIGURATION)
logger.info(
f'Target company with id: {target_company.id} relations before merge: \n'
f'{target_changes}',
)
logger.info(
f'Source company with id: {source_company.id} relations before merge: \n'
f'{source_changes}',
)
results = {
configuration.model: update_objects(configuration, source_company, target_company)
for configuration in MERGE_CONFIGURATION
Expand Down
38 changes: 38 additions & 0 deletions datahub/company/test/test_merge_company.py
Original file line number Diff line number Diff line change
Expand Up @@ -887,6 +887,44 @@ def test_company_legacy_export_wins_merges_successfully(self):
).count() == 2


@pytest.mark.django_db
class TestCompanyMerge:

def test_merge_logs_source_and_target_relations(self, caplog):
"""
Tests before we merge companies that the counts of each models relations are logged.
"""
adviser = AdviserFactory()
source_company = CompanyFactory()
target_company = CompanyFactory()

LegacyExportWinsToDataHubCompanyFactory(company=target_company)
TaskFactory(company=target_company)
CompanyInteractionFactory(company=target_company)
CompleteLargeCapitalInvestorProfileFactory(investor_company=target_company)

LegacyExportWinsToDataHubCompanyFactory(company=source_company)
TaskFactory(company=source_company)
CompanyInteractionFactory(company=source_company)
CompleteLargeCapitalInvestorProfileFactory(investor_company=source_company)

caplog.set_level('INFO')
target_planned_changes, _ = get_planned_changes(target_company, MERGE_CONFIGURATION)
source_planned_changes, _ = get_planned_changes(source_company, MERGE_CONFIGURATION)

merge_companies(source_company, target_company, adviser)

assert (
f'Target company with id: {target_company.id} relations before merge:' in caplog.text
)
assert f'{target_planned_changes}' in caplog.text

assert (
f'Source company with id: {source_company.id} relations before merge:' in caplog.text
)
assert f'{source_planned_changes}' in caplog.text


def _company_factory(
num_interactions=0,
num_contacts=0,
Expand Down

0 comments on commit d6478be

Please sign in to comment.