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

19942 Fix broken unit tests (report folder) #2522

Merged
merged 2 commits into from
Mar 15, 2024
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
80 changes: 40 additions & 40 deletions legal-api/src/legal_api/reports/report.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
from legal_api.models import ConsentContinuationOut, CorpType, Document, EntityRole, Filing, LegalEntity
from legal_api.models.legal_entity import ASSOCIATION_TYPE_DESC
from legal_api.reports.registrar_meta import RegistrarInfo
from legal_api.services import MinioService, VersionedBusinessDetailsService
from legal_api.services import BusinessService, MinioService, VersionedBusinessDetailsService
from legal_api.utils.auth import jwt
from legal_api.utils.formatting import float_to_str
from legal_api.utils.legislation_datetime import LegislationDatetime
Expand All @@ -43,7 +43,7 @@ class Report: # pylint: disable=too-few-public-methods, too-many-lines
def __init__(self, filing):
"""Create the Report instance."""
self._filing = filing
self._legal_entity = None
self._business = None
self._report_key = None
self._report_date_time = LegislationDatetime.now()

Expand All @@ -62,8 +62,8 @@ def _get_static_report(self):

def _get_report(self):
if self._filing.legal_entity_id:
self._legal_entity = LegalEntity.find_by_internal_id(self._filing.legal_entity_id)
Report._populate_business_info_to_filing(self._filing, self._legal_entity)
self._business = BusinessService.fetch_business_by_filing(self._filing)
Report._populate_business_info_to_filing(self._filing, self._business)
if self._report_key == "alteration":
self._report_key = "alterationNotice"
headers = {"Authorization": "Bearer {}".format(jwt.get_token_auth_header()), "Content-Type": "application/json"}
Expand All @@ -84,8 +84,8 @@ def _get_report(self):
def _get_report_filename(self):
filing_date = str(self._filing.filing_date)[:19]
legal_entity_number = (
self._legal_entity.identifier
if self._legal_entity
self._business.identifier
if self._business
else self._filing.filing_json["filing"].get("business", {}).get("identifier", "")
)
description = ReportMeta.reports[self._report_key]["filingDescription"]
Expand Down Expand Up @@ -206,7 +206,7 @@ def _get_template_filename(self):
# Get template specific to legal type
file_name = None
specific_template = ReportMeta.reports[self._report_key].get(
self._business.legal_type, None # pylint: disable=no-member
self._business.entity_type, None # pylint: disable=no-member
)
if file_name is None:
# Fallback to default if specific template not found
Expand All @@ -221,9 +221,7 @@ def _get_template_filename(self):

def _get_template_data(self):
if self._report_key in ["noticeOfArticles", "amendedRegistrationStatement", "correctedRegistrationStatement"]:
filing = VersionedBusinessDetailsService.get_company_details_revision(
self._filing.id, self._legal_entity.id
)
filing = VersionedBusinessDetailsService.get_company_details_revision(self._filing.id, self._business.id)
self._format_noa_data(filing)
else:
filing = copy.deepcopy(self._filing.filing_json["filing"])
Expand Down Expand Up @@ -322,8 +320,8 @@ def _set_registrar_info(self, filing):
filing["registrarInfo"] = {**RegistrarInfo.get_registrar_info(self._filing.effective_date)}

def _set_tax_id(self, filing):
if self._legal_entity and self._legal_entity.tax_id:
filing["taxId"] = self._legal_entity.tax_id
if self._business and self._business.is_legal_entity and self._business.tax_id:
filing["taxId"] = self._business.tax_id

def _set_description(self, filing):
legal_type = self._filing.filing_json["filing"].get("business", {}).get("legalType", "NA")
Expand Down Expand Up @@ -356,12 +354,14 @@ def _set_dates(self, filing):
filing["effective_date_time"] = LegislationDatetime.format_as_report_string(effective_date)
filing["effective_date"] = effective_date.strftime(OUTPUT_DATE_FORMAT)
# Recognition Date
if self._legal_entity:
recognition_datetime = LegislationDatetime.as_legislation_timezone(self._legal_entity.founding_date)
if self._business:
recognition_datetime = LegislationDatetime.as_legislation_timezone(
self._business.founding_date if self._business.is_legal_entity else self._business.start_date
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are we okay with making recognition_date for alternate name entities be the start_date, since there is no founding_date? Or do we just not want to include the recognition date in the output for alternate names?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, we can use start_date for the recognition date

)
filing["recognition_date_time"] = LegislationDatetime.format_as_report_string(recognition_datetime)
filing["recognition_date_utc"] = recognition_datetime.strftime(OUTPUT_DATE_FORMAT)
if self._legal_entity.start_date:
filing["start_date_utc"] = self._legal_entity.start_date.strftime(OUTPUT_DATE_FORMAT)
if self._business.start_date:
filing["start_date_utc"] = self._business.start_date.strftime(OUTPUT_DATE_FORMAT)
# For Annual Report - Set AGM date as the effective date
if self._filing.filing_type == "annualReport":
agm_date_str = filing.get("annualReport", {}).get("annualGeneralMeetingDate", None)
Expand Down Expand Up @@ -527,7 +527,7 @@ def _format_certificate_of_restoration_data(self, filing):
if self._filing.filing_sub_type == "limitedRestorationToFull":
business_previous_restoration_expiry = (
VersionedBusinessDetailsService.find_last_value_from_business_revision(
self._filing, self._legal_entity, is_restoration_expiry_date=True
self._filing, self._business, is_restoration_expiry_date=True
)
)
restoration_expiry_datetime = LegislationDatetime.as_legislation_timezone(
Expand All @@ -536,15 +536,15 @@ def _format_certificate_of_restoration_data(self, filing):
filing["previous_restoration_expiry_date"] = restoration_expiry_datetime.strftime(OUTPUT_DATE_FORMAT)

business_dissolution = VersionedBusinessDetailsService.find_last_value_from_business_revision(
self._filing, self._legal_entity, is_dissolution_date=True
self._filing, self._business, is_dissolution_date=True
)
filing["formatted_dissolution_date"] = LegislationDatetime.format_as_report_string(
business_dissolution.dissolution_date
)

# pylint: disable=no-member
def _format_dissolution_data(self, filing):
if self._business.legal_type in ["SP", "GP"] and filing["dissolution"]["dissolutionType"] == "voluntary":
if self._business.entity_type in ["SP", "GP"] and filing["dissolution"]["dissolutionType"] == "voluntary":
filing["dissolution"]["dissolution_date_str"] = LegislationDatetime.as_legislation_timezone_from_date_str(
filing["dissolution"]["dissolutionDate"]
).strftime(OUTPUT_DATE_FORMAT)
Expand All @@ -557,7 +557,7 @@ def _format_restoration_data(self, filing):
filing["offices"] = filing["restoration"]["offices"]
meta_data = self._filing.meta_data or {}
filing["fromBusinessName"] = meta_data.get("restoration", {}).get("fromBusinessName")
filing["numberedBusinessNameSuffix"] = LegalEntity.BUSINESSES[self._legal_entity.entity_type][
filing["numberedBusinessNameSuffix"] = LegalEntity.BUSINESSES[self._business.entity_type][
"numberedBusinessNameSuffix"
]

Expand All @@ -573,7 +573,7 @@ def _format_restoration_data(self, filing):
filing["noticeDate"] = filing["restoration"].get("noticeDate", "Not Applicable")

business_dissolution = VersionedBusinessDetailsService.find_last_value_from_business_revision(
self._filing, self._legal_entity, is_dissolution_date=True
self._filing, self._business, is_dissolution_date=True
)
filing["dissolutionBusinessName"] = business_dissolution.business_name

Expand All @@ -594,7 +594,7 @@ def _format_consent_continuation_out_data(self, filing):
expiry_date = LegislationDatetime.as_legislation_timezone(cco.expiry_date)
filing["cco_expiry_date"] = expiry_date.strftime(OUTPUT_DATE_FORMAT)

filing["offices"] = VersionedBusinessDetailsService.get_office_revision(self._filing, self._legal_entity.id)
filing["offices"] = VersionedBusinessDetailsService.get_office_revision(self._filing, self._business.id)

with suppress(KeyError):
self._format_address(filing["offices"]["registeredOffice"]["deliveryAddress"])
Expand Down Expand Up @@ -653,7 +653,7 @@ def _format_alteration_data(self, filing):
filing["listOfTranslations"] = filing["alteration"].get("nameTranslations", [])
# Get previous translations for deleted translations. No record created in aliases version for deletions
filing["previousNameTranslations"] = VersionedBusinessDetailsService.get_name_translations_revision(
self._filing.transaction_id, self._legal_entity.id
self._filing.transaction_id, self._business.id
)
if filing["alteration"].get("shareStructure", None):
filing["shareClasses"] = filing["alteration"]["shareStructure"].get("shareClasses", [])
Expand Down Expand Up @@ -700,12 +700,12 @@ def _format_change_of_registration_data(
self, filing, filing_type
): # noqa: E501 # pylint: disable=too-many-locals, too-many-branches, too-many-statements
prev_completed_filing = Filing.get_previous_completed_filing(self._filing)
versioned_legal_entity = VersionedBusinessDetailsService.get_business_revision_obj(
prev_completed_filing, self._legal_entity.id
versioned_business = VersionedBusinessDetailsService.get_business_revision_obj(
prev_completed_filing, self._business.id
)

# Change of Name
prev_business_name = versioned_legal_entity.legal_name
prev_business_name = versioned_business.legal_name
name_request_json = filing.get(filing_type).get("nameRequest")
filing["nameRequest"] = name_request_json
if name_request_json:
Expand All @@ -715,7 +715,7 @@ def _format_change_of_registration_data(
filing["newBusinessName"] = to_business_name

# Change of Nature of Business
prev_naics_description = versioned_legal_entity.naics_description
prev_naics_description = versioned_business.naics_description
naics_json = filing.get(filing_type).get("business", {}).get("naics")
if naics_json:
to_naics_description = naics_json.get("naicsDescription")
Expand All @@ -724,7 +724,7 @@ def _format_change_of_registration_data(

# Change of start date
if filing_type == "correction":
prev_start_date = versioned_legal_entity.start_date
prev_start_date = versioned_business.start_date
new_start_date_str = filing.get(filing_type).get("startDate")
if new_start_date_str != LegislationDatetime.format_as_legislation_date(prev_start_date):
filing["newStartDate"] = new_start_date_str
Expand Down Expand Up @@ -782,7 +782,7 @@ def _format_change_of_registration_data(
filing["newParties"].append(party)

existing_party_json = VersionedBusinessDetailsService.get_party_role_revision(
prev_completed_filing, self._legal_entity.id, True
prev_completed_filing, self._business.id, True
)
parties_deleted = [p for p in existing_party_json if p["officer"]["id"] not in parties_to_edit]
filing["ceasedParties"] = parties_deleted
Expand Down Expand Up @@ -865,34 +865,34 @@ def _has_change(self, old_value, new_value):
return has_change

def _format_correction_data(self, filing):
if self._legal_entity.legal_type in ["SP", "GP"]:
if self._business.entity_type in ["SP", "GP"]:
self._format_change_of_registration_data(filing, "correction")
else:
prev_completed_filing = Filing.get_previous_completed_filing(self._filing)
versioned_legal_entity = VersionedBusinessDetailsService.get_business_revision_obj(
prev_completed_filing, self._legal_entity.id
versioned_business = VersionedBusinessDetailsService.get_business_revision_obj(
prev_completed_filing, self._business.id
)

self._format_name_request_data(filing, versioned_legal_entity)
self._format_name_request_data(filing, versioned_business)
self._format_name_translations_data(filing, prev_completed_filing)
self._format_office_data(filing, prev_completed_filing)
self._format_party_data(filing, prev_completed_filing)
self._format_share_class_data(filing, prev_completed_filing)
self._format_resolution_data(filing)

def _format_name_request_data(self, filing, versioned_legal_entity: LegalEntity):
def _format_name_request_data(self, filing, versioned_business: any):
name_request_json = filing.get("correction").get("nameRequest", {})
filing["nameRequest"] = name_request_json
prev_business_name = versioned_legal_entity.legal_name
legal_entity = VersionedBusinessDetailsService.get_business_revision_obj(self._filing, self._legal_entity.id)
prev_business_name = versioned_business.legal_name
legal_entity = VersionedBusinessDetailsService.get_business_revision_obj(self._filing, self._business.id)
if prev_business_name != legal_entity.legal_name:
filing["previousBusinessName"] = prev_business_name
filing["newBusinessName"] = legal_entity.legal_name

def _format_name_translations_data(self, filing, prev_completed_filing: Filing):
filing["listOfTranslations"] = filing["correction"].get("nameTranslations", [])
versioned_name_translations = VersionedBusinessDetailsService.get_name_translations_revision(
prev_completed_filing, self._legal_entity.id
prev_completed_filing, self._business.id
)
filing["previousNameTranslations"] = versioned_name_translations
filing["nameTranslationsChange"] = sorted(
Expand Down Expand Up @@ -968,7 +968,7 @@ def _format_party_data(self, filing, prev_completed_filing: Filing):
filing["newParties"].append(party)

existing_party_json = VersionedBusinessDetailsService.get_party_role_revision(
prev_completed_filing, self._legal_entity.id, True
prev_completed_filing, self._business.id, True
)
parties_deleted = [p for p in existing_party_json if p["officer"]["id"] not in parties_to_edit]
filing["ceasedParties"] = parties_deleted
Expand Down Expand Up @@ -1076,7 +1076,7 @@ def _format_special_resolution(self, filing):

display_name = FILINGS.get(self._filing.filing_type, {}).get("displayName")
if isinstance(display_name, dict):
display_name = display_name.get(self._legal_entity.entity_type)
display_name = display_name.get(self._business.entity_type)
filing_source = "specialResolution" if self._filing.filing_type == "specialResolution" else "correction"
filing["header"]["displayName"] = display_name
resolution_date_str = filing.get(filing_source, {}).get("resolutionDate", None)
Expand Down Expand Up @@ -1130,7 +1130,7 @@ def _set_meta_info(self, filing):

# Appears in the Description section of the PDF Document Properties as Subject.
if self._report_key == "noticeOfArticles":
filing["meta_subject"] = "{} ({})".format(self._legal_entity.legal_name, self._legal_entity.identifier)
filing["meta_subject"] = "{} ({})".format(self._business.legal_name, self._business.identifier)
else:
legal_name = self._filing.filing_json["filing"].get("business", {}).get("businessName", "NA")
filing["meta_subject"] = "{} ({})".format(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def get_parties(identifier, party_id=None):
HTTPStatus.UNAUTHORIZED,
)

if business.entity_type == BusinessCommon.EntityTypes.SOLE_PROP.value:
if business.is_alternate_name_entity and (business.entity_type == BusinessCommon.EntityTypes.SOLE_PROP.value):
return jsonify(parties=[business.owner_data_json])

if party_id:
Expand Down
19 changes: 9 additions & 10 deletions legal-api/tests/unit/reports/test_report.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ def create_report(identifier, entity_type, report_type, filing_type, template):
filing = factory_completed_filing(legal_entity, filing_json)

report = Report(filing)
report._legal_entity = legal_entity
report._business = legal_entity
report._report_key = report_type
if report._report_key == "correction":
report._report_key = report._filing.filing_json["filing"]["correction"]["correctedFilingType"]
Expand All @@ -76,7 +76,7 @@ def create_report(identifier, entity_type, report_type, filing_type, template):

def populate_business_info_to_filing(report):
"""Assert _populate_business_info_to_filing works as expected."""
report._populate_business_info_to_filing(report._filing, report._legal_entity)
report._populate_business_info_to_filing(report._filing, report._business)
filing_json = report._filing.filing_json
assert filing_json["filing"]["business"]["formatted_founding_date_time"]
assert filing_json["filing"]["business"]["formatted_founding_date"]
Expand All @@ -90,11 +90,10 @@ def set_dates(report):
assert filing_json["effective_date_time"]
assert filing_json["effective_date"]
assert filing_json["recognition_date_time"]
# TODO: figure out why this fails and improve test/code
# if report._report_key == 'annualReport':
# assert filing_json['agm_date']
# if report_type == 'correction':
# assert filing_json['original_filing_date_time']
if report._filing._filing_type == "annualReport":
assert filing_json["agm_date"]
if report._report_key == "correction":
assert filing_json["original_filing_date_time"]


def substitute_template_parts(report):
Expand Down Expand Up @@ -136,7 +135,7 @@ def set_addresses(report):
report._set_addresses(filing_json["filing"])

assert filing_json["filing"].get("registeredOfficeAddress")
if report._legal_entity.entity_type == "BEN" and report._report_key == "changeOfAddress":
if report._business.entity_type == "BEN" and report._report_key == "changeOfAddress":
assert filing_json["filing"].get("recordsOfficeAddress")


Expand Down Expand Up @@ -205,7 +204,7 @@ def test_get_pdf(session, test_name, identifier, entity_type, report_type, filin
if report_type in ["annualReport", "changeOfAddress"]:
set_addresses(report)

if report._legal_entity.entity_type != "CP":
if report._business.entity_type != "CP":
set_tax_id(report)

filename = report._get_report_filename()
Expand Down Expand Up @@ -285,7 +284,7 @@ def filing_numbered_company(legal_entity, template, legal_name):
def create_alteration_report(filing, business, report_type):
"""Create a report for alteration."""
report = Report(filing)
report._legal_entity = business
report._business = business
report._report_key = report_type
populate_business_info_to_filing(report)
set_dates(report)
Expand Down
Loading