def test_subhandler_declaratively(mocker): cause = mocker.MagicMock(reason=Reason.UPDATE, diff=None) registry = ResourceChangingRegistry() subregistry_var.set(registry) @kopf.on.this() def fn(**_): pass handlers = registry.get_handlers(cause) assert len(handlers) == 1 assert handlers[0].fn is fn
def test_subhandler_declaratively(parent_handler, cause_factory): cause = cause_factory(reason=Reason.UPDATE) registry = ResourceChangingRegistry() subregistry_var.set(registry) with context([(handler_var, parent_handler)]): @kopf.subhandler() def fn(**_): pass handlers = registry.get_handlers(cause) assert len(handlers) == 1 assert handlers[0].fn is fn
def test_subhandler_imperatively(mocker, parent_handler): cause = mocker.MagicMock(reason=Reason.UPDATE, diff=None) registry = ResourceChangingRegistry() subregistry_var.set(registry) def fn(**_): pass with context([(handler_var, parent_handler)]): kopf.register(fn) handlers = registry.get_handlers(cause) assert len(handlers) == 1 assert handlers[0].fn is fn
def test_invalid_oldnew_for_inappropriate_subhandlers(resource, decorator, registry): @decorator(resource.group, resource.version, resource.plural) def fn(**_): @kopf.on.this(field='f', old='x') def fn2(**_): pass subregistry = ResourceChangingRegistry() handler = registry.resource_changing_handlers.get_all_handlers()[0] with context([(handler_var, handler), (subregistry_var, subregistry)]): with pytest.raises(TypeError, match="can only be used in update handlers"): handler.fn()
def test_subhandler_fails_with_no_parent_handler(): registry = ResourceChangingRegistry() 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.subhandler() def fn(**_): pass