aboutsummaryrefslogtreecommitdiffstats
path: root/tests/test_models.py
blob: 0fa273559a09650cc24e7424fae6f9ce3a8deae9 (plain) (blame)
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
import pytest
from pydantic import ValidationError

from selectui import SelectItem


class TestSelectItemCreation:
    """Test SelectItem model creation."""

    def test_create_with_all_fields(self):
        """Test creating SelectItem with all fields."""
        item = SelectItem(
            title="Python",
            subtitle="Programming language",
            info="1991",
            data={"website": "python.org", "typing": "dynamic"}
        )

        assert item.title == "Python"
        assert item.subtitle == "Programming language"
        assert item.info == "1991"
        assert item.data["website"] == "python.org"
        assert item.data["typing"] == "dynamic"

    def test_create_with_required_only(self):
        """Test creating SelectItem with only required fields."""
        item = SelectItem(title="Minimal")

        assert item.title == "Minimal"
        assert item.subtitle is None
        assert item.info is None
        assert item.data == {}

    def test_create_with_optional_fields(self):
        """Test creating SelectItem with some optional fields."""
        item = SelectItem(title="Python", subtitle="Language")

        assert item.title == "Python"
        assert item.subtitle == "Language"
        assert item.info is None
        assert item.data == {}


class TestSelectItemValidation:
    """Test SelectItem validation."""

    def test_empty_title_fails(self):
        """Test that empty title raises validation error."""
        with pytest.raises(ValidationError) as exc_info:
            SelectItem(title="")

        error_msg = str(exc_info.value).lower()
        assert "string should have at least 1 character" in error_msg or "title cannot be empty" in error_msg

    def test_whitespace_title_fails(self):
        """Test that whitespace-only title raises validation error."""
        with pytest.raises(ValidationError) as exc_info:
            SelectItem(title="   ")

        assert "title cannot be empty" in str(exc_info.value).lower()

    def test_title_with_spaces_succeeds(self):
        """Test that title with spaces (but not only spaces) is valid."""
        item = SelectItem(title="Hello World")
        assert item.title == "Hello World"

    def test_missing_title_fails(self):
        """Test that missing title raises validation error."""
        with pytest.raises((ValidationError, TypeError)):
            SelectItem()  # type: ignore


class TestSelectItemToDict:
    """Test SelectItem.to_dict() method."""

    def test_to_dict_all_fields(self):
        """Test converting SelectItem with all fields to dict."""
        item = SelectItem(
            title="Python",
            subtitle="Language",
            info="1991",
            data={"extra": "field"}
        )

        result = item.to_dict()

        assert result["title"] == "Python"
        assert result["subtitle"] == "Language"
        assert result["info"] == "1991"
        assert result["extra"] == "field"

    def test_to_dict_minimal(self):
        """Test converting minimal SelectItem to dict."""
        item = SelectItem(title="Test")
        result = item.to_dict()

        assert result["title"] == "Test"
        assert "subtitle" not in result
        assert "info" not in result

    def test_to_dict_with_custom_data(self):
        """Test that custom data is preserved in to_dict."""
        item = SelectItem(
            title="Item",
            data={"custom1": "value1", "custom2": "value2"}
        )

        result = item.to_dict()

        assert result["custom1"] == "value1"
        assert result["custom2"] == "value2"


class TestSelectItemFromDict:
    """Test SelectItem.from_dict() method."""

    def test_from_dict_standard_keys(self):
        """Test from_dict with standard keys."""
        data = {
            "title": "Python",
            "subtitle": "Language",
            "info": "1991",
            "extra": "data"
        }

        item = SelectItem.from_dict(data)

        assert item.title == "Python"
        assert item.subtitle == "Language"
        assert item.info == "1991"
        assert item.data["extra"] == "data"

    def test_from_dict_custom_keys(self):
        """Test from_dict with custom key mappings."""
        data = {
            "name": "Alice",
            "role": "Engineer",
            "team": "Backend",
            "level": "Senior"
        }

        item = SelectItem.from_dict(
            data,
            title_key="name",
            subtitle_key="role",
            info_key="team"
        )

        assert item.title == "Alice"
        assert item.subtitle == "Engineer"
        assert item.info == "Backend"
        assert item.data["level"] == "Senior"

    def test_from_dict_missing_optional_keys(self):
        """Test from_dict when optional keys are missing."""
        data = {"title": "Only Title"}

        item = SelectItem.from_dict(data)

        assert item.title == "Only Title"
        assert item.subtitle is None
        assert item.info is None

    def test_from_dict_no_subtitle(self):
        """Test from_dict with subtitle_key=None."""
        data = {"filename": "test.py", "size": 1024}

        item = SelectItem.from_dict(
            data,
            title_key="filename",
            subtitle_key=None,
            info_key="size"
        )

        assert item.title == "test.py"
        assert item.subtitle is None
        assert item.info == "1024"

    def test_from_dict_preserves_all_data(self):
        """Test that from_dict preserves all original data."""
        data = {
            "name": "Alice",
            "role": "Engineer",
            "extra1": "value1",
            "extra2": "value2"
        }

        item = SelectItem.from_dict(data, title_key="name", subtitle_key="role")

        assert item.data["name"] == "Alice"
        assert item.data["role"] == "Engineer"
        assert item.data["extra1"] == "value1"
        assert item.data["extra2"] == "value2"

    def test_from_dict_type_conversion(self):
        """Test that from_dict converts values to strings."""
        data = {
            "title": 123,
            "subtitle": True,
            "info": 456.78
        }

        item = SelectItem.from_dict(data)

        assert item.title == "123"
        assert item.subtitle == "True"
        assert item.info == "456.78"


class TestSelectItemRoundTrip:
    """Test SelectItem conversion round trips."""

    def test_roundtrip_standard_keys(self):
        """Test SelectItem -> dict -> SelectItem with standard keys."""
        original = SelectItem(
            title="Test",
            subtitle="Subtitle",
            info="Info",
            data={"custom": "field"}
        )

        # Convert to dict and back
        data = original.to_dict()
        restored = SelectItem.from_dict(data)

        assert restored.title == original.title
        assert restored.subtitle == original.subtitle
        assert restored.info == original.info
        assert restored.data["custom"] == original.data["custom"]

    def test_roundtrip_minimal(self):
        """Test round trip with minimal SelectItem."""
        original = SelectItem(title="Minimal")

        data = original.to_dict()
        restored = SelectItem.from_dict(data)

        assert restored.title == original.title
        assert restored.subtitle == original.subtitle
        assert restored.info == original.info