def test_computed_field_function_not_a_function(): """Tests when provided function is missing, ValueError is raised.""" obj = construct_mock(get_cats_name='tabby', ) with raises(ValueError): dict_utils.computed_field_function('get_cats_name', dict_utils.id_name_dict)(obj)
def test_computed_field_function(): """Tests if provided function is being called and dictionary created.""" obj = construct_mock( get_cats_name=lambda: construct_mock(id='cat-01', name='Mittens'), ) result = dict_utils.computed_field_function('get_cats_name', dict_utils.id_name_dict)(obj) assert result == {'id': 'cat-01', 'name': 'Mittens'}
class Company(BaseESModel): """Elasticsearch representation of Company model.""" id = Keyword() archived = Boolean() archived_by = fields.contact_or_adviser_field() archived_on = Date() archived_reason = Text() business_type = fields.id_name_field() company_number = fields.NormalizedKeyword() created_on = Date() description = fields.EnglishText() employee_range = fields.id_name_field() export_experience_category = fields.id_name_field() export_to_countries = fields.id_name_field() future_interest_countries = fields.id_name_field() global_headquarters = fields.id_name_field() headquarter_type = fields.id_name_field() modified_on = Date() name = Text(fields={ 'keyword': fields.NormalizedKeyword(), 'trigram': fields.TrigramText(), }, ) reference_code = fields.NormalizedKeyword() sector = fields.sector_field() address = fields.address_field() registered_address = fields.address_field() one_list_group_global_account_manager = _adviser_field_with_indexed_id() trading_names = fields.TextWithTrigram() turnover_range = fields.id_name_field() uk_region = fields.id_name_field() uk_based = Boolean() uk_address_postcode = fields.PostcodeKeyword() uk_registered_address_postcode = fields.PostcodeKeyword() vat_number = Keyword(index=False) duns_number = Keyword() website = Text() suggest = Completion(contexts=[ { 'name': 'country', 'type': 'category', }, ], ) latest_interaction_date = Date() COMPUTED_MAPPINGS = { 'suggest': get_suggestions, 'address': partial(dict_utils.address_dict, prefix='address'), 'registered_address': partial(dict_utils.address_dict, prefix='registered_address'), 'one_list_group_global_account_manager': dict_utils.computed_field_function( 'get_one_list_group_global_account_manager', dict_utils.contact_or_adviser_dict, ), 'latest_interaction_date': lambda obj: obj.latest_interaction_date, 'uk_address_postcode': lambda obj: obj.address_postcode if obj.uk_based else '', 'uk_registered_address_postcode': lambda obj: obj.registered_address_postcode if obj.uk_based else '', } MAPPINGS = { 'archived_by': dict_utils.contact_or_adviser_dict, 'business_type': dict_utils.id_name_dict, 'employee_range': dict_utils.id_name_dict, 'export_experience_category': dict_utils.id_name_dict, 'export_to_countries': lambda col: [dict_utils.id_name_dict(c) for c in col.all()], 'future_interest_countries': lambda col: [dict_utils.id_name_dict(c) for c in col.all()], 'global_headquarters': dict_utils.id_name_dict, 'headquarter_type': dict_utils.id_name_dict, 'sector': dict_utils.sector_dict, 'turnover_range': dict_utils.id_name_dict, 'uk_based': bool, 'uk_region': dict_utils.id_name_dict, } SEARCH_FIELDS = ( 'id', 'name', # to find 2-letter words 'name.trigram', 'company_number', 'trading_names', # to find 2-letter words 'trading_names.trigram', 'reference_code', 'address.country.name.trigram', 'address.postcode.trigram', 'registered_address.country.name.trigram', 'registered_address.postcode.trigram', ) class Meta: """Default document meta data.""" doc_type = DOC_TYPE class Index: doc_type = DOC_TYPE
class Company(BaseSearchModel): """ OpenSearch representation of Company model. """ id = Keyword() archived = Boolean() archived_by = fields.contact_or_adviser_field() archived_on = Date() archived_reason = Text() business_type = fields.id_name_field() company_number = fields.NormalizedKeyword() created_on = Date() description = fields.EnglishText() employee_range = fields.id_name_field() export_experience_category = fields.id_name_field() export_to_countries = fields.id_name_field() future_interest_countries = fields.id_name_field() global_headquarters = fields.id_name_field() headquarter_type = fields.id_name_field() modified_on = Date() name = Text( fields={ 'keyword': fields.NormalizedKeyword(), 'trigram': fields.TrigramText(), }, ) reference_code = fields.NormalizedKeyword() sector = fields.sector_field() address = fields.address_field() registered_address = fields.address_field() one_list_group_global_account_manager = _adviser_field_with_indexed_id() trading_names = fields.TextWithTrigram() turnover_range = fields.id_name_field() uk_region = fields.id_name_field() uk_based = Boolean() uk_address_postcode = fields.PostcodeKeyword() uk_registered_address_postcode = fields.PostcodeKeyword() vat_number = Keyword(index=False) duns_number = Keyword() website = Text() latest_interaction_date = Date() export_segment = Text() export_sub_segment = Text() COMPUTED_MAPPINGS = { 'address': partial(dict_utils.address_dict, prefix='address'), 'registered_address': partial(dict_utils.address_dict, prefix='registered_address'), 'one_list_group_global_account_manager': dict_utils.computed_field_function( 'get_one_list_group_global_account_manager', dict_utils.contact_or_adviser_dict, ), 'export_to_countries': lambda obj: [ dict_utils.id_name_dict(o.country) for o in obj.export_countries.all() if o.status == CompanyExportCountry.Status.CURRENTLY_EXPORTING ], 'future_interest_countries': lambda obj: [ dict_utils.id_name_dict(o.country) for o in obj.export_countries.all() if o.status == CompanyExportCountry.Status.FUTURE_INTEREST ], 'latest_interaction_date': lambda obj: obj.latest_interaction_date, 'uk_address_postcode': lambda obj: obj.address_postcode if obj.uk_based else '', 'uk_registered_address_postcode': lambda obj: obj.registered_address_postcode if obj.uk_based else '', } MAPPINGS = { 'archived_by': dict_utils.contact_or_adviser_dict, 'business_type': dict_utils.id_name_dict, 'employee_range': dict_utils.id_name_dict, 'export_experience_category': dict_utils.id_name_dict, 'global_headquarters': dict_utils.id_name_dict, 'headquarter_type': dict_utils.id_name_dict, 'sector': dict_utils.sector_dict, 'turnover_range': dict_utils.id_name_dict, 'uk_based': bool, 'uk_region': dict_utils.id_name_dict, } SEARCH_FIELDS = ( 'id', 'name', # to find 2-letter words 'name.trigram', 'company_number', 'trading_names', # to find 2-letter words 'trading_names.trigram', 'reference_code', 'sector.name', 'address.line_1.trigram', 'address.line_2.trigram', 'address.town.trigram', 'address.county.trigram', 'address.area.name.trigram', 'address.postcode', 'address.country.name.trigram', 'registered_address.line_1.trigram', 'registered_address.line_2.trigram', 'registered_address.town.trigram', 'registered_address.county.trigram', 'registered_address.area.name.trigram', 'registered_address.postcode', 'registered_address.country.name.trigram', )