Exemplo n.º 1
0
def test_apply_contextmanager():
    pipeline1 = processor1 >> processor2
    pipeline2 = define(key='value')(pipeline1) >> processor3

    # pipeline1 is used to build pipeline2 but `define` context must be applied
    # to processors in pipeline2 only
    assert all(len(p.context_managers) == 0 for p in pipeline1)
    assert all(len(p.context_managers) for p in pipeline2[:2])
Exemplo n.º 2
0
def test_apply_contextmanager_later():
    pipeline1 = processor1 >> processor2
    pipeline2 = pipeline1 >> processor3

    # apply context to pipeline1 but pipeline2 must be not changed
    pipeline1 = define(key='value')(pipeline1)

    # pipeline1 is used to build pipeline2 but `define` context must be applied
    # to processors in pipeline2 only
    assert all(len(p.context_managers) for p in pipeline1)
    assert all(len(p.context_managers) == 0 for p in pipeline2)
Exemplo n.º 3
0
def test_program_run(program):
    service_mock = Mock()
    response_mock = Mock()
    injections = {
        'response': response_mock,
        'service': service_mock,
        'a': 2,
        'b': 3
    }
    program.run_processor('pipeline2.processor3', injections)
    service_mock.call.assert_called_once_with(5)
    service_mock.call.reset_mock()

    program.add_contextmanager(define(a=3))
    program.run_processor('pipeline2.processor3', injections)
    service_mock.call.assert_called_once_with(6)
    service_mock.call.reset_mock()

    program.add_contextmanager(define(a=5, b=5))
    program.run_processor('pipeline2.processor3', injections)
    service_mock.call.assert_called_once_with(10)
Exemplo n.º 4
0
def test_pipe_processor(processor):
    response_mock = Mock()
    injections = {'response': response_mock, 'a': 2, 'b': 3}
    processor.process(injections)
    response_mock.emit_message.assert_called_once_with({'result': 5})
    response_mock.emit_message.reset_mock()

    # apply contextmanager
    define_contextmanager1 = define(a=3)
    processor = define_contextmanager1(processor)
    assert len(processor.context_managers) == 1
    processor.process(injections)
    response_mock.emit_message.assert_called_once_with({'result': 6})
    response_mock.emit_message.reset_mock()

    define_contextmanager2 = define(
        a=5, b=5)  # this should override another context values
    processor = define_contextmanager2(processor)
    assert len(processor.context_managers) == 2
    processor.process(injections)
    response_mock.emit_message.assert_called_once_with({'result': 10})
Exemplo n.º 5
0

@pagination
@pipe_processor
def log_page(logger, page=0, updated=False):
    # pagination contextmanager creates a page context for the processor, page=0 by default
    # also this processor expects an `updated` context but no one generates the context for now.
    logger.info('Current page: %s, updated: %s', page, updated)
    return {}


# And here we create another processor that is based on previous one
# but redefine the `page` context generated by `pagination` and add a new context `updated`
# Also here we redefine an infrastructure context `logger` with new custom logger factory
new_logger_factory = logger_context('custom')
log_fixed_page = define(page=10, updated=True,
                        logger=new_logger_factory)(log_page)


# In same way you may redefine default config but it's useful to use
# a special context manager `define_config` for this
@pipe_processor
def log_config(logger, config):
    logger.info('Config: key1=%s, key2=%s', config.key1, config.key2)
    return {}


log_replaced_config = define_config({'key1': 'UPDATED'})(log_config)
log_merged_config = define_config({'key2': {
    'key2_1': 'MERGED'
}}, merge=True)(log_config)