def determine_converter(self):
     if not self.multivalue:
         converter = CONVERTER_MAP.get(self.data_type,
                                       data_converter.DEFAULT)
     else:
         converter = ListConverter(self.data_type)
     return converter
    def test_apply_multiple_value_field(self):
        # given:
        converter = ListConverter(base_converter=_create_mock_string_converter())
        cell_conversion = FieldOfSingleElementListCellConversion('library.books.authors', converter)

        # and:
        metadata = MetadataEntity(content={
            'books': [
                {'title': 'Once Upon a Time'}
            ]
        })

        # when:
        cell_conversion.apply(metadata, 'J dela Cruz||P Smith')

        # then:
        books = metadata.get_content('books')
        self.assertEqual(1, len(books))

        # and:
        book = books[0]
        self.assertEqual(['J dela Cruz - converted', 'P Smith - converted'], book.get('authors'))
 def test_convert_to_int_list_single_float(self):
     converter = ListConverter(data_type=DataType.INTEGER)
     float_input = 9606.0
     list_int_output = [9606]
     self.assertEqual(list_int_output, converter.convert(float_input))
 def test_convert_to_int_list_single(self):
     converter = ListConverter(data_type=DataType.INTEGER)
     self.assertEqual([9606], converter.convert(9606))
 def test_convert_to_int_list_multiple(self):
     converter = ListConverter(data_type=DataType.INTEGER)
     self.assertEqual([1, 2, 3], converter.convert('1||2||3'))
 def test_convert_to_string_list(self):
     converter = ListConverter()
     self.assertEqual(['apple', 'banana', 'carrot'],
                      converter.convert('apple||banana||carrot'))
     self.assertEqual(['durian', 'eggfruit', 'fig'],
                      converter.convert('durian||eggfruit||fig'))
 def __init__(self, field: str, converter: Converter):
     list_converter = ListConverter(base_converter=converter)
     super(ListElementCellConversion, self).__init__(field, list_converter)
import re
from abc import abstractmethod

from ingest.importer.conversion import data_converter
from ingest.importer.conversion.column_specification import \
    ColumnSpecification, ConversionType
from ingest.importer.conversion.data_converter import Converter, ListConverter
from ingest.importer.conversion.exceptions import UnknownMainCategory
from ingest.importer.conversion.metadata_entity import MetadataEntity
from ingest.importer.conversion.utils import split_field_chain

_LIST_CONVERTER = ListConverter()


class CellConversion(object):

    def __init__(self, field, converter: Converter):
        self.field = field
        self.applied_field = self._process_applied_field(field)
        self.converter = converter

    @staticmethod
    def _process_applied_field(field):
        pattern = r'(\w*\.){0,1}(?P<insert_field>.*)'
        match = re.match(pattern, field)
        return match.group('insert_field')

    @abstractmethod
    def apply(self, metadata: MetadataEntity, cell_data):
        # TODO Implement this method
        pass