def test_get_field_mappings(self): expected = { self.procedure_concept_id: self.condition_concept_id, self.procedure_type_concept_id: self.condition_type_concept_id, self.procedure_source_concept_id: self.condition_source_concept_id } self.assertDictEqual( domain_mapping.get_field_mappings(self.condition_table, self.procedure_table), expected) self.assertDictEqual( domain_mapping.get_field_mappings(self.procedure_table, self.condition_table), {})
def resolve_specific_field_mappings(_src_table, _dest_table, _dest_specific_fields): """ This function generates a list of field mappings between _src_table and _dest_table for the domain specific fields. E.g. The fields value_as_number and value_as_concept_id can be mapped between observation and measurement. :param _src_table: the source CDM table :param _dest_table: the destination CDM table :param _dest_specific_fields: an array that contains the specific destination fields :return: """ specific_field_mappings = OrderedDict() # If the src_table and dest_table are the same, map all the fields onto themselves. if _src_table == _dest_table: for dest_specific_field in _dest_specific_fields: specific_field_mappings[dest_specific_field] = dest_specific_field else: # Retrieve the field mappings and put them into the dict specific_field_mappings.update( get_field_mappings(_src_table, _dest_table)) # For dest_specific_field that is not defined, map it to NULL for dest_specific_field in _dest_specific_fields: if dest_specific_field not in specific_field_mappings: specific_field_mappings[dest_specific_field] = NULL_VALUE return specific_field_mappings
def resolve_field_mappings(src_table, dest_table): """ This function generates the content of SQL select statement for the given src_table and dest_table. :param src_table: the source CDM table for rerouting :param dest_table: the destination CDM table for rerouting :return: the content of the SQL select statements """ select_statements = [] field_mappings = domain_mapping.get_field_mappings(src_table, dest_table) for dest_field, src_field in field_mappings.items(): if domain_mapping.value_requires_translation(src_table, dest_table, src_field, dest_field): value_mappings = domain_mapping.get_value_mappings( src_table, dest_table, src_field, dest_field) if len(value_mappings) == 0: if field_mapping.is_field_required(dest_table, dest_field): case_statements = ZERO_AS_DEST_FIELD.format( dest_field=dest_field) else: case_statements = NULL_AS_DEST_FIELD.format( dest_field=dest_field) else: case_statements = '\n\t\t'.join([ WHEN_STATEMENT.format(src_value=s, dest_value=d) for d, s in value_mappings.items() ]) dummy_value = fetch_dummy_value( dest_table, dest_field) if field_mapping.is_field_required( dest_table, dest_field) else NULL_VALUE case_statements = CASE_STATEMENT.format( src_field=src_field, dest_field=dest_field, dummy_value=dummy_value, statements=case_statements) select_statements.append(case_statements) else: select_statements.append( SRC_FIELD_AS_DEST_FIELD.format(src_field=src_field, dest_field=dest_field)) return ',\n\t'.join(select_statements)