def test_no_extra_space_on_command_built_through_command_for( handler_as_function): handler = Handler(body="/command", handler=handler_as_function) assert handler.command_for() == "/command" built_command_with_args = handler.command_for(None, 1, "some string", True) assert built_command_with_args == "/command 1 some string True"
def add_handler( # noqa: WPS211 self, handler: Callable, *, body: Optional[str] = None, name: Optional[str] = None, description: Optional[str] = None, full_description: Optional[str] = None, include_in_status: Union[bool, Callable] = True, dependencies: Optional[Sequence[deps.Depends]] = None, dependency_overrides_provider: Any = None, ) -> None: """Create new handler from passed arguments and store it inside. !!! info If `include_in_status` is a function, then `body` argument will be checked for matching public commands style, like `/command`. Arguments: handler: callable that will be used for executing handler. body: body template that will trigger this handler. name: optional name for handler that will be used in generating body. description: description for command that will be shown in bot's menu. full_description: full description that can be used for example in `/help` command. include_in_status: should this handler be shown in bot's menu, can be callable function with no arguments *(for now)*. dependencies: sequence of dependencies that should be executed before handler. dependency_overrides_provider: mock of callable for handler. """ if body is None: name = name or get_name_from_callable(handler) body = get_body_from_name(name) for registered_handler in self.handlers: _check_new_handler_restrictions(body, name, handler, registered_handler) dep_override = ( dependency_overrides_provider if dependency_overrides_provider is not None else self.dependency_overrides_provider ) command_handler = Handler( body=body, handler=handler, name=name, # type: ignore description=description, full_description=full_description, # type: ignore include_in_status=include_in_status, dependencies=_combine_dependencies(self.dependencies, dependencies), dependency_overrides_provider=dep_override, ) self.handlers.append(command_handler)
def _add_default_handler( self, default: Handler, dependencies: Optional[Sequence[Depends]] = None, ) -> None: default_dependencies = _combine_dependencies( self.dependencies, dependencies, default.dependencies, ) self.default_message_handler = Handler( body=default.body, handler=default.handler, name=default.name, description=default.description, full_description=default.full_description, include_in_status=default.include_in_status, dependencies=default_dependencies, dependency_overrides_provider=self.dependency_overrides_provider, )
def test_handler_docstring_stored_as_full_description(handler_as_function): handler = Handler(body="/command", handler=handler_as_function) assert handler.full_description == handler_as_function.__doc__
def test_any_body_for_hidden_handler(handler_as_function): Handler(handler=handler_as_function, body="any text!", include_in_status=False)
def test_name_when_name_was_passed_explicitly(handler_as_function): handler = Handler(handler=handler_as_function, body="/command", name="my_handler") assert handler.name == "my_handler"
def test_handler_from_function(handler_as_function): handler = Handler(body="/command", handler=handler_as_function) assert handler.name == "handler_function"
def default_handler(handler_as_function): return Handler(handler=handler_as_function, body="/default-handler")
def test_error_handler_from_class(handler_as_class): with pytest.raises(ValidationError): Handler(body="/command", handler=handler_as_class)
def test_that_menu_command_contain_only_single_word(handler_as_function): with pytest.raises(ValidationError): Handler(body="/many words handler", handler=handler_as_function)
def test_only_one_slash_in_public_command(handler_as_function): with pytest.raises(ValidationError): Handler(body="//command", handler=handler_as_function)
def test_error_from_callable(handler_as_callable_object): with pytest.raises(ValidationError): Handler(body="/command", handler=handler_as_callable_object)
def test_error_when_creating_executor_without_call(build_handler): handler = Handler(build_handler(...), body="/body") dependant = handler.dependant dependant.call = None with pytest.raises(AssertionError): get_executor(dependant)
def test_equality_is_false_if_not_handler_passed(handler_as_function): handler = Handler(body="/command", handler=handler_as_function) assert handler != ""
def test_equality_if_handlers_are_similar(handler_as_function): handler1 = Handler(body="/command", handler=handler_as_function) handler2 = Handler(body="/command", handler=handler_as_function) assert handler1 == handler2
def test_equality_is_false_if_handlers_are_different(handler_as_function): handler1 = Handler(body="/command1", handler=handler_as_function) handler2 = Handler(body="/command2", handler=handler_as_function) assert handler1 != handler2