Пример #1
0
def test_get_answers__proper_funcs_called(
        mocker, basic_question_should_ask,
        expected_basic_question_ask_call_count):
    """
    Given a list of interactions of different types, validate the proper funcs should be invoked on each

    The parametrization asserts that Question interactions are only 'asked' when they should be asked
    """
    echo_interaction_mock = mocker.Mock(spec=Echo)
    basic_question_interaction_mock = mocker.Mock(
        spec=BasicQuestion,
        should_ask=mocker.Mock(return_value=basic_question_should_ask),
    )
    # mock names must be set after mock object is created
    echo_interaction_mock.name = "echo_interaction_mock"
    basic_question_interaction_mock.name = "basic_question_interaction_mock"

    interactions = [echo_interaction_mock, basic_question_interaction_mock]
    get_answers(interactions)

    # functions belonging to interactions should be invoked
    # in the case of a question, it either should or shouldn't be asked
    echo_interaction_mock.display.assert_called_once()
    basic_question_interaction_mock.should_ask.assert_called_once()
    assert (basic_question_interaction_mock.ask.call_count ==
            expected_basic_question_ask_call_count)
Пример #2
0
def test_get_answers__unknown_interaction_provided():
    """
    An unsupported interaction type should raise a ValueError
    """
    # `5` is not an interaction type that is supported
    interactions = [Echo("foo"), 5]
    with pytest.raises(ValueError):
        get_answers(interactions)
Пример #3
0
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
Пример #4
0
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 == {}
Пример #5
0
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
Пример #6
0
def test_parse_args__duplicate_question_name_in_answers__exception():
    with pytest.raises(DuplicateQuestionNameException):
        get_answers(
            [BasicQuestion(SOME_NAME, SOME_STRING, SOME_DEFAULT)],
            answers={SOME_NAME: "existing value"},
        )
Пример #7
0
def test_parse_args__duplicate_question_name__exception(questions):
    with pytest.raises(DuplicateQuestionNameException):
        get_answers(questions)