File: test_value.py

package info (click to toggle)
textual 2.1.2-1.1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 55,080 kB
  • sloc: python: 85,423; lisp: 1,669; makefile: 101
file content (127 lines) | stat: -rw-r--r-- 3,899 bytes parent folder | download | duplicates (2)
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
import pytest

from textual.app import App
from textual.widgets import Select
from textual.widgets.select import InvalidSelectValueError

SELECT_OPTIONS = [(str(n), n) for n in range(3)]
MORE_OPTIONS = [(str(n), n) for n in range(5, 8)]


class SelectApp(App[None]):
    def __init__(self, initial_value=Select.BLANK):
        self.initial_value = initial_value
        super().__init__()

    def compose(self):
        yield Select[int](SELECT_OPTIONS, value=self.initial_value)


async def test_initial_value_is_validated():
    """The initial value should be respected if it is a legal value.

    Regression test for https://github.com/Textualize/textual/discussions/3037.
    """
    app = SelectApp(1)
    async with app.run_test():
        assert app.query_one(Select).value == 1


async def test_value_unknown_option_raises_error():
    """Setting the value to an unknown value raises an error."""
    app = SelectApp()
    async with app.run_test():
        with pytest.raises(InvalidSelectValueError):
            app.query_one(Select).value = "french fries"


async def test_initial_value_inside_compose_is_validated():
    """Setting the value to an unknown value inside compose should raise an error."""

    class SelectApp(App[None]):
        def compose(self):
            s = Select[int](SELECT_OPTIONS)
            s.value = 73
            yield s

    app = SelectApp()
    with pytest.raises(InvalidSelectValueError):
        async with app.run_test():
            pass


async def test_value_assign_to_blank():
    """Setting the value to BLANK should work with default `allow_blank` value."""
    app = SelectApp(1)
    async with app.run_test():
        select = app.query_one(Select)
        assert select.value == 1
        select.value = Select.BLANK
        assert select.is_blank()


async def test_initial_value_is_picked_if_allow_blank_is_false():
    """The initial value should be picked by default if allow_blank=False."""

    class SelectApp(App[None]):
        def compose(self):
            yield Select[int](SELECT_OPTIONS, allow_blank=False)

    app = SelectApp()
    async with app.run_test():
        assert app.query_one(Select).value == 0


async def test_initial_value_is_picked_if_allow_blank_is_false():
    """The initial value should be respected even if allow_blank=False."""

    class SelectApp(App[None]):
        def compose(self):
            yield Select[int](SELECT_OPTIONS, value=2, allow_blank=False)

    app = SelectApp()
    async with app.run_test():
        assert app.query_one(Select).value == 2


async def test_set_value_to_blank_with_allow_blank_false():
    """Setting the value to BLANK with allow_blank=False should raise an error."""

    class SelectApp(App[None]):
        def compose(self):
            yield Select[int](SELECT_OPTIONS, allow_blank=False)

    app = SelectApp()
    async with app.run_test():
        with pytest.raises(InvalidSelectValueError):
            app.query_one(Select).value = Select.BLANK


async def test_set_options_resets_value_to_blank():
    """Resetting the options should reset the value to BLANK."""

    class SelectApp(App[None]):
        def compose(self):
            yield Select[int](SELECT_OPTIONS, value=2)

    app = SelectApp()
    async with app.run_test():
        select = app.query_one(Select)
        assert select.value == 2
        select.set_options(MORE_OPTIONS)
        assert select.is_blank()


async def test_set_options_resets_value_if_allow_blank_is_false():
    """Resetting the options should reset the value if allow_blank=False."""

    class SelectApp(App[None]):
        def compose(self):
            yield Select[int](SELECT_OPTIONS, allow_blank=False)

    app = SelectApp()
    async with app.run_test():
        select = app.query_one(Select)
        assert select.value == 0
        select.set_options(MORE_OPTIONS)
        assert select.value > 2