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

add logging for company merge command #5981

Merged
merged 6 commits into from
Mar 3, 2025
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
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
from logging import getLogger

from datahub.company.merge_company import merge_companies
from datahub.company.models import Company
from datahub.dbmaintenance.management.base import CSVBaseCommand
from datahub.dbmaintenance.utils import parse_uuid

logger = getLogger(__name__)


class Command(CSVBaseCommand):
"""
Expand All @@ -15,6 +19,16 @@ class Command(CSVBaseCommand):
its related models, and find the company with Duns number and merge.
"""

additional_logging: dict

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.additional_logging = {
'companies_with_subsidiaries': [],
'target_companies_archived': [],
'source_company_global_headquarters': [],
}

def _process_row(self, row, simulate=False, **options):
"""Process one single row."""
source_pk = parse_uuid(row['id'])
Expand All @@ -23,4 +37,28 @@ def _process_row(self, row, simulate=False, **options):
source_company = Company.objects.get(pk=source_pk)
target_company = Company.objects.get(duns_number=target_duns)

if source_company.subsidiaries.all().exists():
self.additional_logging['companies_with_subsidiaries'].append(str(source_company.id))
if target_company.archived:
self.additional_logging['target_companies_archived'].append(str(target_company.id))
if source_company.global_headquarters:
self.additional_logging['source_company_global_headquarters'].append(
str(source_company.id))
merge_companies(source_company, target_company, None)

def handle(self, *args, **options):
"""
Process the CSV file and logs some additional logging to help with companies merging
"""
super().handle(*args, **options)
logger.info(
'List of Source Companies with Subsidiaries: '
f'{self.additional_logging["companies_with_subsidiaries"]}')
logger.info(
'List of Target Companies Archived: '
f'{self.additional_logging["target_companies_archived"]}')
logger.info(
'List of Source Compnies with Global Headqaurters: '
f'{self.additional_logging["source_company_global_headquarters"]}')

self.additional_logging.clear()
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from django.core.management import call_command

from datahub.company.test.factories import CompanyFactory

from datahub.company.test.factories import SubsidiaryFactory
pytestmark = pytest.mark.django_db


Expand Down Expand Up @@ -49,3 +49,94 @@ def test_merge_id_duns_number(s3_stubber):
company_4.refresh_from_db()
assert company_3.transferred_to == company_1
assert company_4.transferred_to == company_2


def test_logs_contain_errors(s3_stubber, caplog):
"""Tests errors are captured in the logs"""
caplog.set_level('INFO')
global_company = CompanyFactory()
company_source = CompanyFactory(
global_headquarters=global_company,
)
company_with_duns = CompanyFactory(duns_number='12345678', archived=True)

bucket = 'test_bucket'
object_key = 'test_key'
csv_content = f"""id,duns
{company_source.id},{company_with_duns.duns_number}
"""

s3_stubber.add_response(
'get_object',
{
'Body': BytesIO(csv_content.encode(encoding='utf-8')),
},
expected_params={
'Bucket': bucket,
'Key': object_key,
},
)

call_command('company_merge_duns_number', bucket, object_key)

assert 'List of Target Companies Archived: ' in caplog.text
assert str(company_with_duns.id) in caplog.text
assert 'List of Source Compnies with Global Headqaurters: ' in caplog.text
assert str(company_source.id)


def test_subsidiary_logs(s3_stubber, caplog):
"""Tests subsidiary errors are captured in the logs"""
caplog.set_level('INFO')
company = CompanyFactory()
SubsidiaryFactory(global_headquarters=company)
company_with_duns = CompanyFactory(duns_number='12345678')
bucket = 'test_bucket'
object_key = 'test_key'
csv_content = f"""id,duns
{company.id},{company_with_duns.duns_number}
"""

s3_stubber.add_response(
'get_object',
{
'Body': BytesIO(csv_content.encode(encoding='utf-8')),
},
expected_params={
'Bucket': bucket,
'Key': object_key,
},
)

call_command('company_merge_duns_number', bucket, object_key)

assert 'List of Source Companies with Subsidiaries: ' in caplog.text
assert f'{str(company.id)}' in caplog.text


def test_non_subsidiary_logs(s3_stubber, caplog):
"""Tests subsidiary list in log is empty"""
caplog.set_level('INFO')
non_subsidiary_company = CompanyFactory()
company_with_duns = CompanyFactory(duns_number='12345678')

bucket = 'test_bucket'
object_key = 'test_key'
csv_content = f"""id,duns
{non_subsidiary_company.id},{company_with_duns.duns_number}
"""

s3_stubber.add_response(
'get_object',
{
'Body': BytesIO(csv_content.encode(encoding='utf-8')),
},
expected_params={
'Bucket': bucket,
'Key': object_key,
},
)

call_command('company_merge_duns_number', bucket, object_key)

assert 'List of Source Companies with Subsidiaries: []' in caplog.text