def test_all_aliases(app, session): """Assert that aliases are altered correctly.""" # setup identifier = 'BC1234567' alias_1 = 'A1 LTD.' alias_2 = 'A2 LTD.' alias_3 = 'A3 LTD.' alias_4 = 'A4 LTD.' business = create_business(identifier) business.aliases.append(Alias(alias=alias_1, type=Alias.AliasType.TRANSLATION.value)) business.aliases.append(Alias(alias=alias_2, type=Alias.AliasType.TRANSLATION.value)) business.save() business_aliases = business.aliases.all() assert len(business_aliases) == 2 component = {'nameTranslations': [ { 'id': str(business_aliases[0].id), 'name': alias_3 }, { 'name': alias_4 } ]} # test aliases.update_aliases(business, component.get('nameTranslations')) # validate business_aliases = business.aliases.all() assert 2 == len(business_aliases) assert next((x for x in business_aliases if str(x.id) == component['nameTranslations'][0]['id']), None).alias == alias_3.upper() assert next((x for x in business_aliases if x.alias == component['nameTranslations'][1]['name'].upper()), None) assert not next((x for x in business_aliases if x.alias == alias_1.upper()), None) assert not next((x for x in business_aliases if x.alias == alias_2.upper()), None)
def test_modified_aliases(app, session): """Assert that aliases are altered.""" # setup identifier = 'BC1234567' old_value_1 = 'A1 LTD.' new_value_1 = 'SOCIÉTÉ GÉNÉRALE' old_value_2 = 'B1 LTD.' new_value_2 = 'B2 LTD.' business = create_business(identifier) business.aliases.append(Alias(alias=old_value_1, type=Alias.AliasType.TRANSLATION.value)) business.aliases.append(Alias(alias=old_value_2, type=Alias.AliasType.TRANSLATION.value)) business.save() business_aliases = business.aliases.all() assert len(business_aliases) == 2 component = {'nameTranslations': [ { 'id': str(business_aliases[0].id), 'name': new_value_1 }, { 'id': str(business_aliases[1].id), 'name': new_value_2 } ]} # test aliases.update_aliases(business, component.get('nameTranslations')) # validate business_aliases = business.aliases.all() assert len(business_aliases) == 2 for alias in component['nameTranslations']: business_alias = next((x for x in business_aliases if str(x.id) == alias['id']), None) assert business_alias.alias == alias['name'].upper()
def test_valid_alias_save(session): """Assert that a valid alias can be saved.""" identifier = 'CP1234567' business = factory_business(identifier) alias = Alias( alias='ABC Ltd.', type='TRANSLATION', business_id=business.id ) alias.save() assert alias.id
def test_find_alias_by_id(session): """Assert that the method returns correct value.""" identifier = 'CP1234567' business = factory_business(identifier) alias = Alias( alias='ABC Ltd.', type='TRANSLATION', business_id=business.id ) alias.save() res = Alias.find_by_id(alias.id) assert res assert res.json == alias.json
def get(identifier, alias_id=None): """Return a JSON of the aliases.""" business = Business.find_by_identifier(identifier) if not business: return jsonify({'message': f'{identifier} not found'}), HTTPStatus.NOT_FOUND # return the matching alias if alias_id: alias, msg, code = AliasResource._get_alias(business, alias_id) return jsonify(alias or msg), code aliases_list = [] alias_type = request.args.get('type') if alias_type: aliases = Alias.find_by_type(business.id, alias_type.upper()) else: aliases = business.aliases.all() for alias in aliases: alias_json = alias.json aliases_list.append(alias_json) return jsonify(aliases=aliases_list)
def test_alias_json(session): """Assert the json format of alias.""" identifier = 'CP1234567' business = factory_business(identifier) alias = Alias( alias='ABC Ltd.', type='TRANSLATION', business_id=business.id ) alias.save() alias_json = { 'id': str(alias.id), 'name': alias.alias, 'type': alias.type } assert alias_json == alias.json
def _get_alias(business, alias_id=None): # find by ID alias = None if alias_id: rv = Alias.find_by_id(alias_id=alias_id) if rv: alias = {'alias': rv.json} if not alias: return None, { 'message': f'{business.identifier} alias not found' }, HTTPStatus.NOT_FOUND return alias, None, HTTPStatus.OK
def test_cease_aliases(app, session): """Assert that aliases are removed.""" # setup identifier = 'BC1234567' alias_1 = 'A1 LTD.' alias_2 = 'A2 LTD.' alias_3 = 'A3 LTD.' business = create_business(identifier) business.aliases.append(Alias(alias=alias_1, type=Alias.AliasType.TRANSLATION.value)) business.aliases.append(Alias(alias=alias_2, type=Alias.AliasType.TRANSLATION.value)) business.save() assert len(business.aliases.all()) == 2 component = {'nameTranslations': [ {'name': alias_3} ]} # test aliases.update_aliases(business, component.get('nameTranslations')) # validate business_aliases = business.aliases.all() assert 1 == len(business_aliases) assert business_aliases[0].alias == alias_3.upper()
def test_find_alias_by_business_and_type(session): """Assert that the method returns correct value.""" identifier = 'CP1234567' business = factory_business(identifier) alias1 = Alias( alias='ABC Ltd.', type='TRANSLATION', business_id=business.id ) alias2 = Alias( alias='DEF Ltd.', type='DBA', business_id=business.id ) alias1.save() alias2.save() res = Alias.find_by_type(business.id, 'TRANSLATION') assert res assert len(res) == 1 assert res[0].alias == 'ABC Ltd.'
def update_aliases(business: Business, aliases) -> Dict: """Update the aliases of the business.""" if not business: return { 'error': babel('Business required before alternate names can be set.') } for alias in aliases: if alias_id := alias.get('id'): existing_alias = next( (x for x in business.aliases.all() if str(x.id) == alias_id), None) existing_alias.alias = alias['name'].upper() else: new_alias = Alias(alias=alias['name'].upper(), type=Alias.AliasType.TRANSLATION.value) business.aliases.append(new_alias)
def add_business_aliases(business: Business, aliases_json: dict): """Create name translations and add them to business.""" for name_obj in aliases_json['names']: alias = Alias(alias=name_obj['legalName'], type=Alias.AliasType.TRANSLATION.value) business.aliases.append(alias)
"""Manages the names of a Business.""" from typing import Dict from flask_babel import _ as babel # noqa: N813 from legal_api.models import Alias, Business def update_aliases(business: Business, aliases) -> Dict: """Update the aliases of the business.""" if not business: return { 'error': babel('Business required before alternate names can be set.') } for alias in aliases: if (alias_id := alias.get('id')) and \ (existing_alias := next((x for x in business.aliases.all() if str(x.id) == alias_id), None)): existing_alias.alias = alias['name'].upper() else: new_alias = Alias(alias=alias['name'].upper(), type=Alias.AliasType.TRANSLATION.value) business.aliases.append(new_alias) for current_alias in business.aliases.all(): if not next( (x for x in aliases if x['name'].upper() == current_alias.alias.upper()), None): business.aliases.remove(current_alias) return None