Example #1
0
def test_add_duplicate_middleware(middleware_factory):
    mw = middleware_factory()
    with pytest.raises(ValueError):
        manager = RequestManager(None, BaseProvider(), middlewares=[mw, mw])

    manager = RequestManager(None, BaseProvider(), middlewares=[])
    manager.middleware_stack.add(mw)

    with pytest.raises(ValueError):
        manager.middleware_stack.add(mw)
    assert tuple(manager.middleware_stack) == (mw, )
Example #2
0
def test_provider_property_setter_and_getter():
    provider_a = BaseProvider()
    provider_b = BaseProvider()

    assert provider_a is not provider_b

    manager = RequestManager(None, provider_a)
    assert manager.provider is provider_a

    manager.provider = provider_b

    assert manager.provider is provider_b
Example #3
0
def test_provider_property_setter_and_getter(middleware_factory):
    provider = BaseProvider()

    middleware_a = middleware_factory()
    middleware_b = middleware_factory()
    middleware_c = middleware_factory()
    assert middleware_a is not middleware_b
    assert middleware_a is not middleware_c

    manager = RequestManager(None, provider, middlewares=[])

    assert tuple(manager.middleware_stack) == tuple()

    manager.middleware_stack.add(middleware_a)
    manager.middleware_stack.add(middleware_b)

    manager.middleware_stack.clear()

    assert tuple(manager.middleware_stack) == tuple()

    manager.middleware_stack.add(middleware_c)
    manager.middleware_stack.add(middleware_b)
    manager.middleware_stack.add(middleware_a)

    with pytest.raises(ValueError):
        manager.middleware_stack.add(middleware_b)

    assert tuple(manager.middleware_stack) == (
        middleware_a,
        middleware_b,
        middleware_c,
    )
def test_provider_property_setter_and_getter():
    provider = BaseProvider()

    middleware_a = middleware_factory()
    middleware_b = middleware_factory()
    assert middleware_a is not middleware_b

    manager = RequestManager(None, provider, middlewares=[])

    assert manager.middlewares == tuple()

    manager.add_middleware(middleware_a)
    manager.add_middleware(middleware_b)

    manager.clear_middlewares()

    assert manager.middlewares == tuple()

    manager.add_middleware(middleware_b)
    manager.add_middleware(middleware_a)
    manager.add_middleware(middleware_b)
    manager.add_middleware(middleware_b)
    manager.add_middleware(middleware_a)

    assert manager.middlewares == (
        middleware_a,
        middleware_b,
        middleware_b,
        middleware_a,
        middleware_b,
    )
def test_add_named_middleware(middleware_factory):
    mw = middleware_factory()
    manager = RequestManager(None,
                             BaseProvider(),
                             middlewares=[(mw, 'the-name')])
    assert len(manager.middleware_onion) == 1

    assert tuple(manager.middleware_onion) == (mw, )
Example #6
0
def test_bury_middleware(middleware_factory):
    mw1 = middleware_factory()
    mw2 = middleware_factory()
    mw3 = middleware_factory()

    manager = RequestManager(None, BaseProvider(), middlewares=[mw1, mw2])

    manager.middleware_stack.inject(mw3, layer=0)

    assert tuple(manager.middleware_stack) == (mw1, mw2, mw3)
Example #7
0
def test_add_middleware(middleware_factory):
    mw1 = middleware_factory()
    mw2 = middleware_factory()
    mw3 = middleware_factory()

    manager = RequestManager(None, BaseProvider(), middlewares=[mw1, mw2])

    manager.middleware_stack.add(mw3)

    assert tuple(manager.middleware_stack) == (mw3, mw1, mw2)
def test_export_middlewares(middleware_factory):
    mw1 = middleware_factory()
    mw2 = middleware_factory()
    manager = RequestManager(None,
                             BaseProvider(),
                             middlewares=[(mw1, 'name1'), (mw2, 'name2')])
    assert tuple(manager.middleware_onion) == (mw1, mw2)

    middlewares = manager.middleware_onion.middlewares
    assert middlewares == [(mw1, 'name1'), (mw2, 'name2')]
Example #9
0
def test_add_named_duplicate_middleware(middleware_factory):
    mw = middleware_factory()
    manager = RequestManager(None, BaseProvider(), middlewares=[(mw, 'the-name'), (mw, 'name2')])
    assert tuple(manager.middleware_stack) == (mw, mw)

    manager.middleware_stack.clear()
    assert len(manager.middleware_stack) == 0

    manager.middleware_stack.add(mw, 'name1')
    manager.middleware_stack.add(mw, 'name2')
    assert tuple(manager.middleware_stack) == (mw, mw)
Example #10
0
def test_middleware_holds_state_across_requests():
    provider = BaseProvider()

    manager = RequestManager(None, provider, middlewares=[stateful_middleware])
    state_a = manager.request_blocking('test_statefulness', [])
    assert len(state_a) == 1

    state_b = manager.request_blocking('test_statefulness', [])

    assert id(state_a) == id(state_b)
    assert len(state_b) == 2
Example #11
0
def test_remove_middleware(middleware_factory):
    mw1 = middleware_factory()
    mw2 = middleware_factory()
    mw3 = middleware_factory()

    manager = RequestManager(None, BaseProvider(), middlewares=[mw1, mw2, mw3])

    assert tuple(manager.middleware_stack) == (mw1, mw2, mw3)

    manager.middleware_stack.remove(mw2)

    assert tuple(manager.middleware_stack) == (mw1, mw3)
Example #12
0
def test_provider_property_triggers_regeneration_of_wrapped_middleware():
    provider = BaseProvider()

    manager = RequestManager(None, provider)

    prev_id = id(manager._wrapped_provider_request_functions)

    manager.providers = provider

    after_id = id(manager._wrapped_provider_request_functions)

    assert prev_id != after_id
Example #13
0
def test_bury_named_middleware(middleware_factory):
    mw1 = middleware_factory()
    mw2 = middleware_factory()
    mw3 = middleware_factory()

    manager = RequestManager(None, BaseProvider(), middlewares=[mw1, mw2])

    manager.middleware_stack.inject(mw3, name='middleware3', layer=0)

    assert tuple(manager.middleware_stack) == (mw1, mw2, mw3)

    # make sure middleware was injected with correct name, by trying to remove
    # it by name.
    manager.middleware_stack.remove('middleware3')

    assert tuple(manager.middleware_stack) == (mw1, mw2)
Example #14
0
def test_replace_middleware(middleware_factory):
    mw1 = middleware_factory()
    mw2 = middleware_factory()
    mw3 = middleware_factory()

    manager = RequestManager(None, BaseProvider(), middlewares=[mw1, (mw2, '2nd'), mw3])

    assert tuple(manager.middleware_onion) == (mw1, mw2, mw3)

    mw_replacement = middleware_factory()
    manager.middleware_onion.replace('2nd', mw_replacement)

    assert tuple(manager.middleware_onion) == (mw1, mw_replacement, mw3)

    manager.middleware_onion.remove('2nd')

    assert tuple(manager.middleware_onion) == (mw1, mw3)
Example #15
0
def test_replace_middleware_without_name(middleware_factory):
    mw1 = middleware_factory()
    mw2 = middleware_factory()
    mw3 = middleware_factory()

    manager = RequestManager(None, BaseProvider(), middlewares=[mw1, mw2, mw3])

    assert tuple(manager.middleware_stack) == (mw1, mw2, mw3)

    mw_replacement = middleware_factory()
    manager.middleware_stack.replace(mw2, mw_replacement)

    assert tuple(manager.middleware_stack) == (mw1, mw_replacement, mw3)

    manager.middleware_stack.remove(mw_replacement)

    assert tuple(manager.middleware_stack) == (mw1, mw3)
def test_modifying_middleware_regenerates_request_functions():
    provider = BaseProvider()

    manager = RequestManager(None, provider, middlewares=[])

    id_a = id(manager._wrapped_provider_request_functions)

    manager.add_middleware(middleware_factory())

    id_b = id(manager._wrapped_provider_request_functions)

    assert id_b != id_a

    manager.clear_middlewares()

    id_c = id(manager._wrapped_provider_request_functions)

    assert id_b != id_c
def test_setProvider_api_deprecation():
    manager = RequestManager(None, BaseProvider())

    with pytest.warns(DeprecationWarning):
        manager.setProvider(BaseProvider())