File: test_selection_question.py

package info (click to toggle)
python-briefcase 0.3.25-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 7,596 kB
  • sloc: python: 62,519; makefile: 60
file content (169 lines) | stat: -rw-r--r-- 4,707 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
import pytest


def test_selection_question(console):
    "A question with a dictionary of options can be presented to the user."
    console.values = ["3"]

    options = {
        "first": "The first option",
        "second": "The second option",
        "third": "The third option",
        "fourth": "The fourth option",
    }
    result = console.selection_question(
        description="Test",
        intro="This is a test",
        options=options,
    )

    # Input is requested once
    assert console.prompts == ["Test: "]

    # "third" is the key of the third option.
    assert result == "third"


def test_selection_question_list(console):
    """If selection_question is given a list of values, they're presented as
    provided."""
    console.values = ["3"]

    options = [
        "The first option",
        "The second option",
        "The third option",
        "The fourth option",
    ]
    result = console.selection_question(
        description="Test",
        intro="This is a test",
        options=options,
    )

    # Input is requested once
    assert console.prompts == ["Test: "]

    # The third option is the third option :-)
    assert result == "The third option"


def test_selection_question_bad_input(console):
    # In order, return:
    #     blank
    #     'asdf'
    #     '10'
    #     '3'
    console.values = ["", "asdf", "10", "3"]
    options = {
        "first": "The first option",
        "second": "The second option",
        "third": "The third option",
        "fourth": "The fourth option",
    }
    result = console.selection_question(
        description="Test",
        intro="This is a test",
        options=options,
    )

    # Input is requested five times; first four cause errors.
    assert console.prompts == ["Test: "] * 4

    # The third option was eventually selected
    assert result == "third"


@pytest.mark.parametrize("index, default", [("1", "first"), ("3", "third")])
def test_selection_question_default(console, index, default):
    """If selection_question has a default, it is returned for no input."""
    # Return an empty response when prompted as though the user press entered
    console.values = [""]

    options = {
        "first": "The first option",
        "second": "The second option",
        "third": "The third option",
        "fourth": "The fourth option",
    }
    result = console.selection_question(
        description="Test",
        intro="This is a test",
        options=options,
        default=default,
    )

    # Input is requested once
    assert console.prompts == [f"Test [{index}]: "]

    # The default option is returned
    assert result == default


def test_override_used(console, capsys):
    """The override is used if valid."""
    override_value = "value"
    assert (
        console.selection_question(
            intro="intro",
            description="My variable",
            default=None,
            options=["value", "some_value", "other_value"],
            override_value="value",
        )
        == "value"
    )
    assert f"Using override value {override_value!r}" in capsys.readouterr().out


def test_override_validation(console, capsys):
    """The override is not used if it is not a valid option."""
    console.values = ["3"]

    result = console.selection_question(
        intro="intro",
        description="My variable",
        default="value",
        options=["value", "some_value", "other_value"],
        override_value="invalid_value",
    )

    output = capsys.readouterr().out

    assert "Invalid override value for My variable: 'invalid_value'" in output

    # "other value" was selected by the user after validation failed.
    assert result == "other_value"


def test_default_value_has_correct_index(console):
    """The default value is used and has the correct index if it is a valid option."""
    console.values = [""]

    result = console.selection_question(
        intro="intro",
        description="My variable",
        default="some_value",
        options=["value", "some_value", "other_value"],
        override_value=None,
    )

    # The result is the default value
    assert result == "some_value"


def test_exception_if_wrong_default(console):
    """An exception is raised if the default value is not a valid option."""
    console.values = [""]

    with pytest.raises(
        ValueError,
        match=r"'invalid_value' is not a valid default value",
    ):
        console.selection_question(
            intro="intro",
            description="My variable",
            default="invalid_value",
            options=["value", "some_value", "other_value"],
            override_value=None,
        )