示例#1
0
def test_simple_registry_with_minimal_signature(mocker):
    cause = mocker.Mock(event=None, diff=None)

    registry = SimpleRegistry()
    registry.register(some_fn)
    handlers = registry.get_cause_handlers(cause)

    assert len(handlers) == 1
    assert handlers[0].fn is some_fn
示例#2
0
def test_simple_registry_via_list(mocker):
    cause = mocker.Mock(event=None, diff=None)

    registry = SimpleRegistry()
    handlers = registry.get_cause_handlers(cause)

    assert isinstance(handlers, collections.abc.Iterable)
    assert isinstance(handlers, collections.abc.Container)
    assert isinstance(handlers, collections.abc.Collection)
    assert not handlers
def test_simple_registry_via_list(mocker):
    cause = mocker.Mock(event=None, diff=None)

    registry = SimpleRegistry()
    with pytest.deprecated_call(match=r"use ResourceChangingRegistry.get_handlers\(\)"):
        handlers = registry.get_cause_handlers(cause)

    assert isinstance(handlers, collections.abc.Iterable)
    assert isinstance(handlers, collections.abc.Container)
    assert isinstance(handlers, collections.abc.Collection)
    assert not handlers
示例#4
0
def test_simple_registry_with_minimal_signature(cause_factory):

    cause = cause_factory()
    registry = SimpleRegistry()
    with pytest.deprecated_call(match=r"use @kopf.on"):
        registry.register(some_fn)
    with pytest.deprecated_call(match=r"cease using the internal registries"):
        handlers = registry.get_cause_handlers(cause)

    assert len(handlers) == 1
    assert handlers[0].fn is some_fn
示例#5
0
def test_simple_registry_via_list(cause_factory):

    cause = cause_factory()
    registry = SimpleRegistry()
    with pytest.deprecated_call(match=r"cease using the internal registries"):
        handlers = registry.get_cause_handlers(cause)

    assert isinstance(handlers, collections.abc.Iterable)
    assert isinstance(handlers, collections.abc.Container)
    assert isinstance(handlers, collections.abc.Collection)
    assert not handlers
示例#6
0
def test_simple_registry_with_minimal_signature(mocker):
    cause = mocker.Mock(event=None, diff=None)

    registry = SimpleRegistry()
    registry.register(some_fn)
    with pytest.deprecated_call(
            match=r"use ResourceChangingRegistry.get_handlers\(\)"):
        handlers = registry.get_cause_handlers(cause)

    assert len(handlers) == 1
    assert handlers[0].fn is some_fn
示例#7
0
def test_simple_registry_with_minimal_signature(cause_factory):

    cause = cause_factory()
    registry = SimpleRegistry()
    with pytest.deprecated_call(match=r"registry.register\(\) is deprecated"):
        registry.register(some_fn)
    with pytest.deprecated_call(
            match=r"use ResourceChangingRegistry.get_handlers\(\)"):
        handlers = registry.get_cause_handlers(cause)

    assert len(handlers) == 1
    assert handlers[0].fn is some_fn
示例#8
0
def test_with_prefix(mocker):
    get_fn_id = mocker.patch('kopf.reactor.registries.get_callable_id',
                             return_value='some-id')

    registry = SimpleRegistry(prefix='some-prefix')
    registry.register(some_fn)
    handlers = registry.get_state_changing_handlers(mocker.MagicMock())

    assert get_fn_id.called

    assert len(handlers) == 1
    assert handlers[0].fn is some_fn
    assert handlers[0].id == 'some-prefix/some-id'
def test_subhandler_declaratively(mocker):
    cause = mocker.MagicMock(reason=Reason.UPDATE, diff=None)

    registry = SimpleRegistry()
    subregistry_var.set(registry)

    @kopf.on.this()
    def fn(**_):
        pass

    handlers = registry.get_cause_handlers(cause)
    assert len(handlers) == 1
    assert handlers[0].fn is fn
示例#10
0
def test_simple_registry_via_iter(mocker):
    cause = mocker.Mock(event=None, diff=None)

    registry = SimpleRegistry()
    iterator = registry.iter_cause_handlers(cause)

    assert isinstance(iterator, collections.abc.Iterator)
    assert not isinstance(iterator, collections.abc.Collection)
    assert not isinstance(iterator, collections.abc.Container)
    assert not isinstance(iterator, (list, tuple))

    handlers = list(iterator)
    assert not handlers
示例#11
0
def test_with_explicit_id_and_prefix_and_suffix(mocker, field):
    get_fn_id = mocker.patch('kopf.reactor.registry.get_callable_id',
                             return_value='some-id')

    registry = SimpleRegistry(prefix='some-prefix')
    registry.register(some_fn, id='explicit-id', field=field)
    handlers = registry.get_handlers(mocker.MagicMock())

    assert not get_fn_id.called

    assert len(handlers) == 1
    assert handlers[0].fn is some_fn
    assert handlers[0].id == 'some-prefix/explicit-id/some-field.sub-field'
示例#12
0
def test_with_no_hints(mocker):
    get_fn_id = mocker.patch('kopf.reactor.registries.get_callable_id',
                             return_value='some-id')

    registry = SimpleRegistry()
    registry.register(some_fn)
    handlers = registry.get_cause_handlers(mocker.MagicMock())

    assert get_fn_id.called

    assert len(handlers) == 1
    assert handlers[0].fn is some_fn
    assert handlers[0].id == 'some-id'
示例#13
0
def test_with_prefix(mocker):
    get_fn_id = mocker.patch('kopf.reactor.registries.get_callable_id', return_value='some-id')

    registry = SimpleRegistry(prefix='some-prefix')
    registry.register(some_fn)
    with pytest.deprecated_call(match=r"get_cause_handlers\(\) is deprecated"):
        handlers = registry.get_cause_handlers(mocker.MagicMock())

    assert get_fn_id.called

    assert len(handlers) == 1
    assert handlers[0].fn is some_fn
    assert handlers[0].id == 'some-prefix/some-id'
示例#14
0
def test_simple_registry_via_iter(cause_factory):

    cause = cause_factory()
    registry = SimpleRegistry()
    iterator = registry.iter_cause_handlers(cause)

    assert isinstance(iterator, collections.abc.Iterator)
    assert not isinstance(iterator, collections.abc.Collection)
    assert not isinstance(iterator, collections.abc.Container)
    assert not isinstance(iterator, (list, tuple))

    with pytest.deprecated_call(match=r"cease using the internal registries"):
        handlers = list(iterator)
    assert not handlers
def test_simple_registry_via_iter(mocker):
    cause = mocker.Mock(event=None, diff=None)

    registry = SimpleRegistry()
    iterator = registry.iter_cause_handlers(cause)

    assert isinstance(iterator, collections.abc.Iterator)
    assert not isinstance(iterator, collections.abc.Collection)
    assert not isinstance(iterator, collections.abc.Container)
    assert not isinstance(iterator, (list, tuple))

    with pytest.deprecated_call(match=r"use ResourceChangingRegistry.iter_handlers\(\)"):
        handlers = list(iterator)
    assert not handlers
示例#16
0
def test_with_prefix_and_suffix(mocker, field):
    get_fn_id = mocker.patch('kopf.reactor.registries.get_callable_id',
                             return_value='some-id')
    diff = [('add', ('some-field', 'sub-field'), 'old', 'new')]

    registry = SimpleRegistry(prefix='some-prefix')
    registry.register(some_fn, field=field)
    handlers = registry.get_handlers(mocker.MagicMock(diff=diff))

    assert get_fn_id.called

    assert len(handlers) == 1
    assert handlers[0].fn is some_fn
    assert handlers[0].id == 'some-prefix/some-id/some-field.sub-field'
示例#17
0
def test_with_explicit_id_and_prefix_and_suffix(mocker, field):
    get_fn_id = mocker.patch('kopf.reactor.registries.get_callable_id', return_value='some-id')
    diff = [('add', ('some-field', 'sub-field'), 'old', 'new')]

    registry = SimpleRegistry(prefix='some-prefix')
    registry.register(some_fn, id='explicit-id', field=field)
    with pytest.deprecated_call(match=r"get_cause_handlers\(\) is deprecated"):
        handlers = registry.get_cause_handlers(mocker.MagicMock(diff=diff))

    assert not get_fn_id.called

    assert len(handlers) == 1
    assert handlers[0].fn is some_fn
    assert handlers[0].id == 'some-prefix/explicit-id/some-field.sub-field'
示例#18
0
def test_subhandler_declaratively(mocker):
    cause = mocker.MagicMock(reason=Reason.UPDATE, diff=None)

    registry = SimpleRegistry()
    subregistry_var.set(registry)

    @kopf.on.this()
    def fn(**_):
        pass

    with pytest.deprecated_call(
            match=r"use ResourceChangingRegistry.get_handlers\(\)"):
        handlers = registry.get_cause_handlers(cause)

    assert len(handlers) == 1
    assert handlers[0].fn is fn
示例#19
0
def test_subhandler_declaratively(parent_handler, cause_factory):
    cause = cause_factory(reason=Reason.UPDATE)

    registry = SimpleRegistry()
    subregistry_var.set(registry)

    with context([(handler_var, parent_handler)]):
        @kopf.on.this()
        def fn(**_):
            pass

    with pytest.deprecated_call(match=r"cease using the internal registries"):
        handlers = registry.get_cause_handlers(cause)

    assert len(handlers) == 1
    assert handlers[0].fn is fn
示例#20
0
def test_with_prefix(mocker, cause_factory):
    get_fn_id = mocker.patch('kopf.reactor.registries.get_callable_id',
                             return_value='some-id')
    cause = cause_factory()

    registry = SimpleRegistry()
    with pytest.deprecated_call(match=r"use @kopf.on"):
        registry.register(some_fn)
    with pytest.deprecated_call(match=r"cease using the internal registries"):
        handlers = registry.get_cause_handlers(cause)

    assert get_fn_id.called

    assert len(handlers) == 1
    assert handlers[0].fn is some_fn
    assert handlers[0].id == 'some-prefix/some-id'
示例#21
0
def test_with_no_hints(mocker, cause_factory):
    get_fn_id = mocker.patch('kopf.reactor.registries.get_callable_id',
                             return_value='some-id')
    cause = cause_factory()

    registry = SimpleRegistry()
    with pytest.deprecated_call(match=r"registry.register\(\) is deprecated"):
        registry.register(some_fn)
    with pytest.deprecated_call(match=r"get_cause_handlers\(\) is deprecated"):
        handlers = registry.get_cause_handlers(cause)

    assert get_fn_id.called

    assert len(handlers) == 1
    assert handlers[0].fn is some_fn
    assert handlers[0].id == 'some-id'
示例#22
0
def test_with_explicit_id_and_prefix_and_suffix(mocker, field, cause_factory):
    get_fn_id = mocker.patch('kopf.reactor.registries.get_callable_id',
                             return_value='some-id')
    diff = [('add', ('some-field', 'sub-field'), 'old', 'new')]
    cause = cause_factory(diff=diff)

    registry = SimpleRegistry(prefix='some-prefix')
    with pytest.deprecated_call(match=r"use @kopf.on"):
        registry.register(some_fn, id='explicit-id', field=field)
    with pytest.deprecated_call(match=r"cease using the internal registries"):
        handlers = registry.get_cause_handlers(cause)

    assert not get_fn_id.called

    assert len(handlers) == 1
    assert handlers[0].fn is some_fn
    assert handlers[0].id == 'some-prefix/explicit-id/some-field.sub-field'
示例#23
0
def test_with_suffix(mocker, field, cause_factory):
    get_fn_id = mocker.patch('kopf.reactor.registries.get_callable_id',
                             return_value='some-id')
    diff = [('add', ('some-field', 'sub-field'), 'old', 'new')]
    cause = cause_factory(diff=diff)

    registry = SimpleRegistry()
    with pytest.deprecated_call(match=r"registry.register\(\) is deprecated"):
        registry.register(some_fn, field=field)
    with pytest.deprecated_call(match=r"get_cause_handlers\(\) is deprecated"):
        handlers = registry.get_cause_handlers(cause)

    assert get_fn_id.called

    assert len(handlers) == 1
    assert handlers[0].fn is some_fn
    assert handlers[0].id == 'some-id/some-field.sub-field'
示例#24
0
def test_with_suffix(mocker, field, cause_factory):
    get_fn_id = mocker.patch('kopf.reactor.registries.get_callable_id',
                             return_value='some-id')
    old = {'some-field': {'sub-field': 'old'}}
    new = {'some-field': {'sub-field': 'new'}}
    cause = cause_factory(old=old, new=new, body=new)

    registry = SimpleRegistry()
    with pytest.deprecated_call(match=r"use @kopf.on"):
        registry.register(some_fn, field=field)
    with pytest.deprecated_call(match=r"cease using the internal registries"):
        handlers = registry.get_cause_handlers(cause)

    assert get_fn_id.called

    assert len(handlers) == 1
    assert handlers[0].fn is some_fn
    assert handlers[0].id == 'some-id/some-field.sub-field'
示例#25
0
def test_subhandler_imperatively(parent_handler, cause_factory):
    cause = cause_factory(reason=Reason.UPDATE)

    registry = SimpleRegistry()
    subregistry_var.set(registry)

    def fn(**_):
        pass

    with context([(handler_var, parent_handler)]):
        kopf.register(fn)

    with pytest.deprecated_call(
            match=r"use ResourceChangingRegistry.get_handlers\(\)"):
        handlers = registry.get_cause_handlers(cause)

    assert len(handlers) == 1
    assert handlers[0].fn is fn
示例#26
0
def test_subhandler_fails_with_no_parent_handler():

    registry = SimpleRegistry()
    subregistry_var.set(registry)

    # Check if the contextvar is indeed not set (as a prerequisite).
    with pytest.raises(LookupError):
        handler_var.get()

    # Check the actual behaviour of the decorator.
    with pytest.raises(LookupError):
        @kopf.on.this()
        def fn(**_):
            pass
示例#27
0
def test_creation_of_simple_with_prefix_keyword():
    registry = SimpleRegistry(prefix='hello')
    assert registry.prefix == 'hello'
示例#28
0
def registry(request):
    if request.param == 'simple':
        return SimpleRegistry()
    if request.param == 'global':
        return GlobalRegistry()
    raise Exception(f"Unsupported registry type: {request.param}")
示例#29
0
def test_creation_of_simple():
    registry = SimpleRegistry()
    assert isinstance(registry, BaseRegistry)
    assert isinstance(registry, SimpleRegistry)
示例#30
0
def test_creation_of_simple_no_prefix():
    registry = SimpleRegistry()
    assert isinstance(registry, BaseRegistry)
    assert isinstance(registry, SimpleRegistry)
    assert registry.prefix is None