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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
|
from selectui import SelectItem, SelectUI
class TestPydanticIntegration:
"""Integration tests for Pydantic SelectItem with SelectUI."""
def test_selectui_with_selectitems(self):
"""Test SelectUI works with SelectItem instances."""
items = [
SelectItem(title="Python", subtitle="Language", info="1991"),
SelectItem(title="Rust", subtitle="Systems", info="2010"),
]
app = SelectUI(items=items, oneshot=True)
assert len(app.items) == 2
assert app.items[0]["title"] == "Python"
assert app.items[1]["title"] == "Rust"
def test_selectui_preserves_selectitem_data(self):
"""Test that SelectUI preserves SelectItem data field."""
items = [
SelectItem(
title="Python",
subtitle="Language",
data={"website": "python.org", "typing": "dynamic"}
),
]
app = SelectUI(items=items)
assert app.items[0]["website"] == "python.org"
assert app.items[0]["typing"] == "dynamic"
def test_mixed_items_not_allowed(self):
"""Test that items list should be homogeneous."""
# Note: Current implementation assumes all items are same type
# This test documents the behavior
items = [SelectItem(title="A")]
app = SelectUI(items=items)
# Should convert to dicts
assert isinstance(app.items[0], dict)
class TestDictToPydanticConversion:
"""Test converting dict items to Pydantic and back."""
def test_from_dict_to_selectui(self, custom_key_items):
"""Test converting custom dict items to SelectItem then to SelectUI."""
items = [
SelectItem.from_dict(
item,
title_key="name",
subtitle_key="role",
info_key="team"
)
for item in custom_key_items
]
app = SelectUI(items=items, oneshot=True)
assert len(app.items) == 3
assert app.items[0]["title"] == "Alice"
def test_json_to_selectitem_to_selectui(self):
"""Test full pipeline: JSON -> SelectItem -> SelectUI."""
json_data = [
{"product": "Laptop", "price": "$1299", "stock": 45},
{"product": "Mouse", "price": "$29", "stock": 200},
]
items = [
SelectItem.from_dict(
item,
title_key="product",
subtitle_key=None,
info_key="price"
)
for item in json_data
]
app = SelectUI(items=items)
assert len(app.items) == 2
assert app.items[0]["title"] == "Laptop"
assert app.items[0]["info"] == "$1299"
assert app.items[0]["stock"] == 45
class TestCustomKeyMappings:
"""Integration tests for custom key mappings."""
def test_selectui_with_custom_keys_dict(self, custom_key_items):
"""Test SelectUI with dict items and custom keys."""
app = SelectUI(
items=custom_key_items,
title_key="name",
subtitle_key="role",
info_key="team"
)
assert app.title_key == "name"
assert app.subtitle_key == "role"
assert app.info_key == "team"
def test_selectui_with_custom_keys_pydantic(self):
"""Test SelectUI with SelectItem and custom keys."""
items = [
SelectItem(title="Alice", subtitle="Engineer", info="Backend"),
]
app = SelectUI(
items=items,
title_key="name",
subtitle_key="role",
info_key="team"
)
# Should have both standard and custom keys
assert app.items[0]["title"] == "Alice"
assert app.items[0]["name"] == "Alice"
class TestRealWorldScenarios:
"""Test real-world usage scenarios."""
def test_github_api_response_format(self):
"""Test handling GitHub-style API response."""
repos = [
{
"full_name": "python/cpython",
"description": "The Python programming language",
"stargazers_count": 50000,
"html_url": "https://github.com/python/cpython"
},
{
"full_name": "rust-lang/rust",
"description": "Empowering everyone to build reliable software",
"stargazers_count": 80000,
"html_url": "https://github.com/rust-lang/rust"
},
]
items = [
SelectItem.from_dict(
repo,
title_key="full_name",
subtitle_key="description",
info_key="stargazers_count"
)
for repo in repos
]
app = SelectUI(items=items, oneshot=True)
assert len(app.items) == 2
assert app.items[0]["html_url"] == "https://github.com/python/cpython"
def test_database_query_results(self):
"""Test handling database query results."""
users = [
{"id": 1, "username": "alice", "email": "alice@example.com", "role": "admin"},
{"id": 2, "username": "bob", "email": "bob@example.com", "role": "user"},
]
items = [
SelectItem.from_dict(
user,
title_key="username",
subtitle_key="email",
info_key="role"
)
for user in users
]
app = SelectUI(items=items)
assert len(app.items) == 2
assert app.items[0]["id"] == 1
assert app.items[0]["title"] == "alice"
def test_file_listing_scenario(self):
"""Test file listing scenario."""
files = [
{"filename": "config.json", "size": 1024, "modified": "2024-01-15"},
{"filename": "data.csv", "size": 4096, "modified": "2024-01-14"},
]
items = [
SelectItem.from_dict(
file,
title_key="filename",
subtitle_key=None,
info_key="size"
)
for file in files
]
app = SelectUI(items=items, oneshot=True)
assert len(app.items) == 2
assert app.items[0]["modified"] == "2024-01-15"
class TestEdgeCasesIntegration:
"""Integration tests for edge cases."""
def test_empty_items_to_selectui(self):
"""Test SelectUI with empty items list."""
items = []
app = SelectUI(items=items)
assert app.items == []
assert app.filtered_items == []
def test_single_item(self):
"""Test SelectUI with single item."""
items = [SelectItem(title="Only Item")]
app = SelectUI(items=items)
assert len(app.items) == 1
def test_large_number_of_items(self):
"""Test SelectUI with many items."""
items = [
SelectItem(title=f"Item {i}", subtitle=f"Subtitle {i}")
for i in range(1000)
]
app = SelectUI(items=items)
assert len(app.items) == 1000
def test_items_with_unicode(self):
"""Test SelectUI with Unicode characters."""
items = [
SelectItem(title="Python 🐍", subtitle="Programming"),
SelectItem(title="Rust 🦀", subtitle="Systems"),
SelectItem(title="日本語", subtitle="Japanese"),
]
app = SelectUI(items=items)
assert len(app.items) == 3
assert app.items[0]["title"] == "Python 🐍"
assert app.items[2]["title"] == "日本語"
def test_items_with_special_chars(self):
"""Test SelectUI with special characters."""
items = [
SelectItem(title="test@example.com", subtitle="Email"),
SelectItem(title="user-name_123", subtitle="Username"),
SelectItem(title="$100.00", subtitle="Price"),
]
app = SelectUI(items=items)
assert len(app.items) == 3
assert "@" in app.items[0]["title"]
assert "$" in app.items[2]["title"]
class TestBackwardCompatibility:
"""Test backward compatibility with plain dicts."""
def test_plain_dicts_still_work(self, sample_items):
"""Test that plain dict items still work."""
app = SelectUI(items=sample_items, oneshot=True)
assert len(app.items) == 4
assert isinstance(app.items[0], dict)
def test_mixed_usage_patterns(self):
"""Test that both dict and Pydantic patterns work."""
# Pattern 1: Plain dicts
app1 = SelectUI(items=[{"title": "A"}])
assert app1.items[0]["title"] == "A"
# Pattern 2: Pydantic models
app2 = SelectUI(items=[SelectItem(title="B")])
assert app2.items[0]["title"] == "B"
# Both should work identically
assert isinstance(app1.items[0], dict)
assert isinstance(app2.items[0], dict)
|