def test_serializer_mapping(): """Tests that ``SerializerMapping`` correctly passes its inputs to the underlying serializers. Also checks that state is retrieved / loaded correctly.""" serializer1 = Serializer() serializer1.serialize = Mock(return_value='test1') class Serializer1State(ProcessState): pass serializer2 = Serializer() serializer2.serialize = Mock(return_value='test2') class Serializer2State(ProcessState): pass serializer_mapping = SerializerMapping({ 'key1': serializer1, 'key2': serializer2 }) assert serializer_mapping({ 'key1': 'serializer1', 'key2': 'serializer2' }) == { 'key1': 'test1', 'key2': 'test2' } serializer1.serialize.assert_called_once_with('serializer1') serializer2.serialize.assert_called_once_with('serializer2') with pytest.raises(ValueError, match='output must be a mapping'): serializer_mapping('not a mapping') serializer1_state = Serializer1State() serializer2_state = Serializer2State() serializer1.set_state(serializer1_state) serializer2.set_state(serializer2_state) data_pipeline_state = DataPipelineState() serializer_mapping.attach_data_pipeline_state(data_pipeline_state) assert serializer1._data_pipeline_state is data_pipeline_state assert serializer2._data_pipeline_state is data_pipeline_state assert data_pipeline_state.get_state(Serializer1State) is serializer1_state assert data_pipeline_state.get_state(Serializer2State) is serializer2_state
def __init__( self, preprocess: Optional[Preprocess] = None, postprocess: Optional[Postprocess] = None, serializer: Optional[Serializer] = None, ) -> None: self._preprocess_pipeline = preprocess or DefaultPreprocess() self._postprocess_pipeline = postprocess or Postprocess() self._serializer = serializer or Serializer() self._running_stage = None
def test_serializer(): """Tests that ``Serializer`` can be enabled and disabled correctly.""" my_serializer = Serializer() assert my_serializer.serialize('test') == 'test' my_serializer.serialize = Mock() my_serializer.disable() assert my_serializer('test') == 'test' my_serializer.serialize.assert_not_called() my_serializer.enable() my_serializer('test') my_serializer.serialize.assert_called_once()