From d204bffe4efbbb53dd7694e1df2c1cf1c992036e Mon Sep 17 00:00:00 2001 From: David Mendez Date: Tue, 18 Aug 2020 14:20:38 -0500 Subject: [PATCH] Implement parsing of a single property in source for mapping --- app/es_subset_generator/mappings.py | 14 ++++++++------ .../test/test_mappings_handling.py | 14 ++++++++++++-- 2 files changed, 20 insertions(+), 8 deletions(-) diff --git a/app/es_subset_generator/mappings.py b/app/es_subset_generator/mappings.py index 68e6cdd..f2585f0 100644 --- a/app/es_subset_generator/mappings.py +++ b/app/es_subset_generator/mappings.py @@ -24,13 +24,18 @@ def parse_mapping_from_es_response(index_mapping, required_source): """ Reads the response of the mapping from ES and produces the mapping to use for the subset index :param index_mapping: mapping dict obtained from es + :param required_source: source (list of properties) to get from the mapping :return: the mapping ready to be applied to the subset index """ + mappings_to_apply = { + 'properties': {} + } - for source_property in required_source: - print(f'locating: {source_property}') + for current_property in required_source: + mapping_to_apply = get_mapping_to_apply_in_subset_index(index_mapping, current_property) + mappings_to_apply['properties'][current_property] = mapping_to_apply - print(index_mapping) + return mappings_to_apply def get_mapping_to_apply_in_subset_index(index_mapping, property_path): @@ -44,14 +49,11 @@ def get_mapping_to_apply_in_subset_index(index_mapping, property_path): is_nested = source_property_config.get('properties') is not None if not is_nested: - mapping_to_apply = { 'type': source_property_config['type'] } return mapping_to_apply - - mapping_to_apply = { 'properties': {} } diff --git a/app/es_subset_generator/test/test_mappings_handling.py b/app/es_subset_generator/test/test_mappings_handling.py index e4b2a0d..e51aa96 100644 --- a/app/es_subset_generator/test/test_mappings_handling.py +++ b/app/es_subset_generator/test/test_mappings_handling.py @@ -22,8 +22,18 @@ class TestMappingsHandling(unittest.TestCase): with open(sample_mapping_path, 'rt') as sample_mapping_file: mapping_from_base_index = json.load(sample_mapping_file) source = ['pref_name'] - mappings.parse_mapping_from_es_response(mapping_from_base_index, source) - # FINISH THIS! + mappings_to_apply_got = mappings.parse_mapping_from_es_response(mapping_from_base_index, source) + + mappings_to_apply_must_be = { + "properties": { + 'pref_name': { + 'type': 'keyword' + }, + } + } + + self.assertEqual(mappings_to_apply_got, mappings_to_apply_must_be, + msg='The mappings were not produced correctly!') def test_locates_source_config_of_property(self): """ -- GitLab