def test_wizard_get_steps_label(): questions = [ { 'name': 'question1', 'type': 'input', 'message': 'message', 'description': 'description', 'validators': [{ 'name': 'required' }], }, { 'name': 'question2', 'type': 'input', 'message': 'message', 'description': 'description', 'validators': [{ 'name': 'required' }], }, ] handlers = [get_instance(q) for q in questions] wz = WizardDialog('title', handlers) steps_lables = wz.get_steps_labels() class_current = steps_lables.children[0].content.text[0][0] assert 'class:dialog.step.current ' == class_current assert '1. Question1' == steps_lables.children[0].content.text[0][1] assert 'class:dialog.step ' == steps_lables.children[1].content.text[0][0] assert '2. Question2' == steps_lables.children[1].content.text[0][1]
def show_dialog( questions, title, intro=None, summary=False, next_text='Next', previous_text='Previous', cancel_text='Cancel', finish_text='Finish', ): handlers = [get_instance(q) for q in questions] app = Application( layout=Layout( WizardDialog( title, handlers, intro=intro, summary=summary, next_text=next_text, previous_text=previous_text, cancel_text=cancel_text, finish_text=finish_text, ), ), mouse_support=True, style=for_dialog(), full_screen=True, ) if not app.run(): return answers = {} for handler in handlers: answers.update(handler.get_answer()) return answers
def test_wizard_previous_first_step(mocker): mocked_app = mocker.MagicMock() mocker.patch('interrogatio.widgets.wizard.get_app', return_value=mocked_app) questions = [ { 'name': 'question1', 'type': 'input', 'message': 'message', 'description': 'description', 'validators': [{ 'name': 'required' }], }, { 'name': 'question2', 'type': 'input', 'message': 'message', 'description': 'description', 'validators': [{ 'name': 'required' }], }, ] handlers = [get_instance(q) for q in questions] mocker.patch.object(WizardDialog, 'validate') wz = WizardDialog('title', handlers) wz.next() # noqa: B305 wz.previous() wz.previous() assert wz.current_step_idx == 0 assert wz.current_step == wz.steps[0] assert len(wz.buttons) == 2 assert wz.previous_btn not in wz.buttons
def test_wizard_next_latest_step(mocker): mocked_app = mocker.MagicMock() mocker.patch('interrogatio.widgets.wizard.get_app', return_value=mocked_app) questions = [ { 'name': 'question1', 'type': 'input', 'message': 'message', 'description': 'description', 'validators': [{ 'name': 'required' }], }, { 'name': 'question2', 'type': 'input', 'message': 'message', 'description': 'description', 'validators': [{ 'name': 'required' }], }, ] handlers = [get_instance(q) for q in questions] mocked_validate = mocker.patch.object(WizardDialog, 'validate') wz = WizardDialog('title', handlers) wz.next() # noqa: B305 wz.next() # noqa: B305 assert wz.current_step_idx == 1 assert wz.current_step == wz.steps[1] assert len(wz.buttons) == 3 assert wz.previous_btn in wz.buttons assert mocked_validate.call_count == 2 mocked_app.exit.assert_called_once_with(result=True)
def test_wizard_cancel(mocker): mocked_app = mocker.MagicMock() mocker.patch('interrogatio.widgets.wizard.get_app', return_value=mocked_app) questions = [ { 'name': 'question1', 'type': 'input', 'message': 'message', 'description': 'description', 'validators': [{ 'name': 'required' }], }, { 'name': 'question2', 'type': 'input', 'message': 'message', 'description': 'description', 'validators': [{ 'name': 'required' }], }, ] handlers = [get_instance(q) for q in questions] wz = WizardDialog('title', handlers) wz.cancel() mocked_app.exit.assert_called_once_with(result=False)
def test_wizard_get_status(): questions = [ { 'name': 'question1', 'type': 'input', 'message': 'message', 'description': 'description', 'validators': [{ 'name': 'required' }], }, { 'name': 'question2', 'type': 'input', 'message': 'message', 'description': 'description', 'validators': [{ 'name': 'required' }], }, ] handlers = [get_instance(q) for q in questions] wz = WizardDialog('title', handlers) assert wz.get_status().text == '' wz.error_messages = 'An error' assert wz.get_status().content.text[0][0] == 'class:error' assert wz.get_status().content.text[0][1] == 'An error'
def test_wizard_get_buttons_container(mocker): mocked_app = mocker.MagicMock() mocker.patch('interrogatio.widgets.wizard.get_app', return_value=mocked_app) questions = [ { 'name': 'question1', 'type': 'input', 'message': 'message', 'description': 'description', }, ] handlers = [get_instance(q) for q in questions] wz = WizardDialog('title', handlers) assert wz.get_buttons_container() is not None
def test_wizard_next_validate_invalid(mocker): mocked_app = mocker.MagicMock() mocker.patch('interrogatio.widgets.wizard.get_app', return_value=mocked_app) questions = [ { 'name': 'question1', 'type': 'input', 'message': 'message', 'description': 'description', 'validators': [RequiredValidator('This field is required')], }, ] handlers = [get_instance(q) for q in questions] wz = WizardDialog('title', handlers) wz.next() # noqa: B305 assert wz.current_step_idx == 0 assert wz.current_step == wz.steps[0] assert len(wz.buttons) == 2 assert wz.error_messages == 'This field is required'
def test_single_step(mocker): mocked_app = mocker.MagicMock() mocker.patch('interrogatio.widgets.wizard.get_app', return_value=mocked_app) questions = [ { 'name': 'question1', 'type': 'input', 'message': 'message', 'description': 'description', }, ] handlers = [get_instance(q) for q in questions] wz = WizardDialog('title', handlers) wz.next() # noqa: B305 assert wz.current_step_idx == 0 assert wz.current_step == wz.steps[0] assert len(wz.buttons) == 2 assert wz.previous_btn not in wz.buttons mocked_app.exit.assert_called_once_with(result=True)
def test_get_summary(mocker): mocked_app = mocker.MagicMock() mocker.patch('interrogatio.widgets.wizard.get_app', return_value=mocked_app) questions = [ { 'name': 'question1', 'type': 'input', 'message': 'message', 'description': 'description', 'validators': [{ 'name': 'required' }], }, { 'name': 'question2', 'type': 'input', 'message': 'message', 'description': 'description', 'validators': [{ 'name': 'required' }], }, ] handlers = [get_instance(q) for q in questions] mocker.patch.object( QHandler, 'get_formatted_value', side_effect=['value1', 'value2'], ) wz = WizardDialog('title', handlers, summary=True) summary = wz.get_summary() expected_text1 = summary.content.text[0][1] + summary.content.text[1][1] expected_text2 = summary.content.text[2][1] + summary.content.text[3][1] assert 'Question1: value1' in expected_text1 assert 'Question2: value2' in expected_text2
def test_wizard_get_title(): questions = [ { 'name': 'question1', 'type': 'input', 'message': 'message', 'description': 'description', 'validators': [{ 'name': 'required' }], }, { 'name': 'question2', 'type': 'input', 'message': 'message', 'description': 'description', 'validators': [{ 'name': 'required' }], }, ] handlers = [get_instance(q) for q in questions] wz = WizardDialog('title', handlers) assert wz.get_title() == 'title - 1 of 2'
def test_wizard_init_default(): questions = [ { 'name': 'question1', 'type': 'input', 'message': 'message', 'description': 'description', 'validators': [{ 'name': 'required' }], }, { 'name': 'question2', 'type': 'input', 'message': 'message', 'description': 'description', 'validators': [{ 'name': 'required' }], }, ] handlers = [get_instance(q) for q in questions] wz = WizardDialog('title', handlers) assert len(wz.steps) == 2
def interrogatio(questions, theme='default'): """ Prompts user for inputs as defined in the questions parameter and returns a dictionary with the answers. :param questions: a list of questions. :type questions: list :param theme: the name of the theme to use. :type theme: string :return: a dictionary with the answers. :rtype: dict :raise InvalidQuestionError: if there is an error in the question definition. :raise ThemeNotFoundError: if the specified theme does not exists. Usage: .. code-block:: python from interrogatio import interrogatio questions = [ { 'name': 'name', 'type': 'input', 'message': 'What is your name' }, { 'name': 'favorite_pet', 'type': 'input', 'message': 'What is your favorite pet' } ] answers = interrogatio(questions, theme='purple') """ set_theme(theme) answers = {} validate_questions(questions) for q in questions: handler = get_instance(q) handler.set_context(answers) layout = handler.get_layout() layout.align = HorizontalAlign.LEFT bindings = [load_key_bindings()] handler_bindings = handler.get_keybindings() if handler_bindings: # pragma: no branch bindings.append(handler_bindings) app = Application( layout=Layout(layout), key_bindings=merge_key_bindings(bindings), style=for_prompt(), ) while True: result = app.run() if not result: return if handler.is_valid(answers): answers.update(handler.get_answer()) break else: print_formatted_text( FormattedText([('class:error', handler.errors[0])]), style=for_prompt(), ) return answers