예제 #1
0
def test_merge_overwrite():
    """Test that in the case of a type mismatch, the values are overwritten."""
    tmp = NavigableDict({'data': {'a': 1}})
    tmp.merge({'data': 'None', 'meta': 'Available'})
    assert len(tmp) == 2
    assert tmp.data == 'None'
    assert tmp.meta == 'Available'
예제 #2
0
def test_merge_list():
    """Test that merging two lists works correctly."""
    tmp = NavigableDict({'data': {'a': [1, 2]}})
    tmp.merge({'data': {'a': [3, 4, 5]}})
    assert len(tmp) == 1
    assert len(tmp.data) == 1
    assert len(tmp.data.a) == 5
예제 #3
0
def test_merge_navigable_dict():
    """Test that merging in a NavigableDict works."""
    tmp = NavigableDict({'data': {'a': 1}})
    tmp.merge(NavigableDict({'data': {'b': 2}, 'meta': 'Available'}))
    assert len(tmp) == 2
    assert len(tmp.data) == 2
    assert tmp.data.a == 1
    assert tmp.data.b == 2
    assert tmp.meta == 'Available'
예제 #4
0
def test_merge_dict():
    """Test that merging in a dict is coerced correctly."""
    tmp = NavigableDict({'data': {'a': 1}})
    tmp.merge({'data': {'b': 2}, 'meta': 'Available'})
    assert len(tmp) == 2
    assert len(tmp.data) == 2
    assert tmp.data.a == 1
    assert tmp.data.b == 2
    assert tmp.meta == 'Available'
예제 #5
0
    def __call__(self, record):
        """Transform the ``record`` according to the mappings of this :class:`~polymatheia.transform.Transform`.

        :param record: The record to transform
        :type record: :class:`~polymatheia.data.NavigableDict`
        :return: The transformed record
        :rtype: :class:`~polymatheia.data.NavigableDict`
        """
        result = NavigableDict({})
        if self._mapping[0] == 'copy':
            result.set(self._mapping[1], record.get(self._mapping[2]))
        elif self._mapping[0] == 'static':
            result.set(self._mapping[1], self._mapping[2])
        elif self._mapping[0] == 'fill':
            if record.get(self._mapping[1]) is None:
                result.set(self._mapping[1], self._mapping[2])
            else:
                result.set(self._mapping[1], record.get(self._mapping[1]))
        elif self._mapping[0] == 'split':
            value = record.get(self._mapping[3])
            if value:
                if isinstance(value, str):
                    for idx, part in enumerate(value.split(self._mapping[2])):
                        result.set(self._mapping[1].format(idx + 1), part)
                elif isinstance(value, list):
                    for idx, part in enumerate(value):
                        result.set(self._mapping[1].format(idx + 1), part)
        elif self._mapping[0] == 'combine':
            result.set(self._mapping[1], [record.get(path) for path in self._mapping[2:]])
        elif self._mapping[0] == 'join':
            if len(self._mapping) == 4:
                value = record.get(self._mapping[3])
                if value:
                    result.set(self._mapping[1], self._mapping[2].join(value))
            else:
                result.set(self._mapping[1], self._mapping[2].join([record.get(path) for path in self._mapping[3:]]))
        elif self._mapping[0] == 'sequence':
            tmp = record
            for part in self._mapping[1:]:
                result = part(tmp)
                tmp = result
        elif self._mapping[0] == 'parallel':
            for part in self._mapping[1:]:
                result.merge(part(record))
        elif self._mapping[0] == 'custom':
            result.set(self._mapping[1], self._mapping[2](record))
        return result