def test_basic_question_is_valid__invalid_validator__exception(): question = BasicQuestion(SOME_NAME, SOME_STRING, SOME_DEFAULT, validator=object()) with pytest.raises(ValueError): question.validate(SOME_STRING, SOME_ANSWERS)
def test_basic_question_is_valid__validator__result_of_validator( is_valid, validator_response): question = BasicQuestion(SOME_NAME, SOME_STRING, SOME_DEFAULT, validator=lambda v, a: validator_response) result = question.validate(SOME_STRING, SOME_ANSWERS) assert result.valid == is_valid
def test_value_if_not_asked__BasicQuestion(should_ask, value_if_not_asked, expected_answer): interactions = [ Confirm( "a", SOME_STRING, True, should_ask=lambda answers: should_ask, value_if_not_asked=False, ), Choice( "b", SOME_STRING, SOME_OPTIONS, SOME_DEFAULT, should_ask=lambda answers: should_ask, value_if_not_asked=value_if_not_asked, ), BasicQuestion( "c", SOME_STRING, SOME_DEFAULT, should_ask=lambda answers: should_ask, value_if_not_asked=value_if_not_asked, ), ] results = get_answers(interactions, no_user_input=True) assert results == expected_answer
def test_to_answer__basic_question_value_set__set_value(): questions = [BasicQuestion(SOME_NAME, SOME_STRING, SOME_DEFAULT)] answers = to_answers(questions, Namespace(**{SOME_NAME: SOME_STRING})) assert SOME_NAME in answers assert answers[SOME_NAME] == SOME_STRING
def test_create_parser__basic_question__value(): parser = create_parser( [BasicQuestion(SOME_NAME, SOME_STRING, SOME_DEFAULT)]) result = parser.parse_args([SOME_ARG_NAME, SOME_STRING]) assert vars(result)[SOME_NAME] is SOME_STRING
def test_basic_question__ask__validator__default_value(): result = BasicQuestion(SOME_NAME, SOME_STRING, SOME_DEFAULT, validator=lambda v, a: ValidationSuccess()).ask( SOME_ANSWERS, no_user_input=True) assert result == SOME_DEFAULT
def test_basic_question__static_message__ask_called(mocker): user_io = mocker.patch("columbo._interaction.user_io") BasicQuestion(SOME_NAME, SOME_STRING, SOME_DEFAULT).ask(SOME_ANSWERS, no_user_input=True) user_io.ask.assert_called_once_with(SOME_STRING, default=SOME_DEFAULT, no_user_input=True)
def test_basic_question_copy__diff_message__confirm_diff_message(mocker): user_io = mocker.patch("columbo._interaction.user_io") BasicQuestion(SOME_NAME, some_dynamic_string, some_dynamic_default).copy( message=SOME_STRING, default=SOME_DEFAULT).ask(SOME_ANSWERS, no_user_input=True) user_io.ask.assert_called_once_with(SOME_STRING, default=SOME_DEFAULT, no_user_input=True)
def test_basic_question__dynamic_message__ask_dynamic_message(mocker): user_io = mocker.patch("columbo._interaction.user_io") BasicQuestion(SOME_NAME, some_dynamic_string, some_dynamic_default).ask(SOME_ANSWERS, no_user_input=True) user_io.ask.assert_called_once_with( SOME_DYNAMIC_STRING_RESULT, default=SOME_DYNAMIC_DEFAULT_RESULT, no_user_input=True, )
def test_to_answer__basic_question_dont_ask__value_not_stored(): questions = [ BasicQuestion(SOME_NAME, SOME_STRING, SOME_DEFAULT, should_ask=lambda _: False) ] result = to_answers(questions, Namespace(**{SOME_NAME: SOME_STRING})) assert SOME_NAME not in result
def test_should_ask__false_without_value_if_not_asked__succeeds(): interactions = [ BasicQuestion( "a", SOME_STRING, SOME_DEFAULT, should_ask=lambda answers: False, ), ] results = get_answers(interactions, no_user_input=True) assert results == {}
def test_value_if_not_asked__raises_exception_without_should_ask(): """Declaring a Confirm, Choice, or BasicQuestion with a value_if_not_asked and without should_ask should raise an error.""" with pytest.raises( ValueError, match= "You should either remove value_if_not_asked or add should_ask.", ): BasicQuestion( SOME_NAME, SOME_STRING, SOME_DEFAULT, value_if_not_asked="a", )
def test_to_answer__basic_question_value_not_valid__exception(): questions = [ BasicQuestion( SOME_NAME, SOME_STRING, SOME_DEFAULT, validator=lambda v, a: ValidationFailure("some-error"), ) ] with pytest.raises(CliException): with pytest.deprecated_call(): to_answers(questions, Namespace(**{SOME_NAME: SOME_STRING}))
def test_basic_question_copy__no_args__confirm_same_message(mocker): user_io = mocker.patch("columbo._interaction.user_io") question = BasicQuestion(SOME_NAME, SOME_STRING, SOME_DEFAULT) question.ask(SOME_ANSWERS, no_user_input=True) question.copy().ask(SOME_ANSWERS, no_user_input=True) calls = user_io.ask.mock_calls assert len(calls) == 2 assert calls[0] == calls[1]
def test_get_answers__previous_answers__result_has_previous_answers_and_question_answers( ): existing_key = "question1" existing_answer = "some previous answer" existing_answers = {existing_key: existing_answer} expected_answers = {existing_key: existing_answer, SOME_NAME: SOME_DEFAULT} interactions = [ BasicQuestion(SOME_NAME, "What is the answer to life?", default=SOME_DEFAULT) ] new_answers = get_answers(interactions, existing_answers, no_user_input=True) assert new_answers == expected_answers
def test_basic_question__invalid_asked_multiple_times(mocker, validity_responses): """ A BasicQuestion will continue to be asked until a valid response is provided We force the validator to fail several times, and assert the user_io.ask() is invoked the correct amount of times. """ user_io_ask_mock = mocker.patch("columbo._interaction.user_io.ask") BasicQuestion( SOME_NAME, SOME_STRING, SOME_DEFAULT, validator=mocker.Mock(side_effect=validity_responses), ).ask(SOME_ANSWERS) assert user_io_ask_mock.call_count == len(validity_responses)
def test_basic_question__no_input__default_value(): result = BasicQuestion(SOME_NAME, SOME_STRING, SOME_DEFAULT).ask(SOME_ANSWERS, no_user_input=True) assert result == SOME_DEFAULT
def test_basic_question_copy__new_instance(): original = BasicQuestion(SOME_NAME, SOME_STRING, SOME_DEFAULT) copy = original.copy() assert copy is not original
result = to_answers(questions, Namespace(**{SOME_NAME: SOME_STRING})) assert SOME_NAME not in result def test_to_answer__unsupported_question__exception(): with pytest.raises(ValueError): to_answers([SampleQuestion(SOME_NAME, SOME_STRING)], SOME_NAMESPACE) @pytest.mark.parametrize( ["description", "questions", "partial_expected_result"], [ ( "basic question", [BasicQuestion(SOME_NAME, SOME_STRING, SOME_DEFAULT)], "--my-test-value MY-TEST-VALUE", ), ( "choice static options, options listed", [Choice(SOME_NAME, SOME_STRING, SOME_OPTIONS, SOME_DEFAULT)], "--my-test-value {x,y,z}", ), ( "choice dynamic options, options not listed", [ Choice(SOME_NAME, SOME_STRING, some_dynamic_options, SOME_DEFAULT) ], "--my-test-value MY-TEST-VALUE", ),
def test_basic_question_is_valid__no_validator__always_valid(value): question = BasicQuestion(SOME_NAME, SOME_STRING, SOME_DEFAULT) result = question.validate(value, SOME_ANSWERS) assert result.valid
def test_parse_args__duplicate_question_name_in_answers__exception(): with pytest.raises(DuplicateQuestionNameException): parse_args( [BasicQuestion(SOME_NAME, SOME_STRING, SOME_DEFAULT)], answers={SOME_NAME: "existing value"}, )