Daryl Lim Claude Opus 4.6 (1M context) commited on
Commit
83587df
·
1 Parent(s): 2ffda3e

test: add tests for new UI components and update language format

Browse files

Add tests for clear button, character count, output placeholder,
filterable dropdown, locale codes, and no-title check. Update
existing tests to use "French (fr)" display format.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>

Files changed (1) hide show
  1. tests/test_app.py +57 -14
tests/test_app.py CHANGED
@@ -95,9 +95,9 @@ def test_demo_has_dropdown(demo):
95
 
96
 
97
  def test_dropdown_default_is_french(demo):
98
- """Default dropdown value should be French."""
99
  dropdowns = [b for b in demo.blocks.values() if type(b).__name__ == "Dropdown"]
100
- assert dropdowns[0].value == "French"
101
 
102
 
103
  def test_demo_has_two_textboxes(demo):
@@ -119,12 +119,55 @@ def test_demo_has_translate_button(demo):
119
  assert any(b.value == "Translate" for b in buttons), "Expected a 'Translate' button"
120
 
121
 
122
- def test_translate_button_wires_only_text_and_language(demo):
123
- """Translate button should only wire input text and target language (no sliders)."""
124
- assert len(demo.fns) == 1, f"Expected 1 event handler, found {len(demo.fns)}"
125
- fn = list(demo.fns.values())[0]
126
- input_types = [type(i).__name__ for i in fn.inputs]
127
- assert input_types == ["Textbox", "Dropdown"], f"Expected [Textbox, Dropdown], got {input_types}"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
128
 
129
 
130
  # --- Slow tests (require CUDA + model download) ---
@@ -174,7 +217,7 @@ def test_translate_unsupported_language(loaded_app):
174
  @pytest.mark.slow
175
  @pytest.mark.skipif(not gpu_available, reason="Requires CUDA")
176
  def test_translate_returns_string(loaded_app):
177
- result = loaded_app.translate("Hello", "French")
178
  assert isinstance(result, str)
179
  assert len(result) > 0
180
 
@@ -183,7 +226,7 @@ def test_translate_returns_string(loaded_app):
183
  @pytest.mark.skipif(not gpu_available, reason="Requires CUDA")
184
  def test_translate_with_beam_search(loaded_app):
185
  """Translation with beam search (num_beams=4) should return a string."""
186
- result = loaded_app.translate("Hello", "French", num_beams=4)
187
  assert isinstance(result, str)
188
  assert len(result) > 0
189
 
@@ -192,7 +235,7 @@ def test_translate_with_beam_search(loaded_app):
192
  @pytest.mark.skipif(not gpu_available, reason="Requires CUDA")
193
  def test_translate_with_custom_temperature(loaded_app):
194
  """Translation with custom temperature should return a string."""
195
- result = loaded_app.translate("Hello", "French", temperature=0.5)
196
  assert isinstance(result, str)
197
  assert len(result) > 0
198
 
@@ -201,7 +244,7 @@ def test_translate_with_custom_temperature(loaded_app):
201
  @pytest.mark.skipif(not gpu_available, reason="Requires CUDA")
202
  def test_translate_with_custom_max_tokens(loaded_app):
203
  """Translation with low max_new_tokens should return a short string."""
204
- result = loaded_app.translate("Hello", "French", max_new_tokens=10)
205
  assert isinstance(result, str)
206
 
207
 
@@ -209,7 +252,7 @@ def test_translate_with_custom_max_tokens(loaded_app):
209
  @pytest.mark.skipif(not gpu_available, reason="Requires CUDA")
210
  def test_translate_empty_string(loaded_app):
211
  """Translating an empty string should not crash."""
212
- result = loaded_app.translate("", "French")
213
  assert isinstance(result, str)
214
 
215
 
@@ -218,6 +261,6 @@ def test_translate_empty_string(loaded_app):
218
  def test_translate_beam_search_ignores_temperature(loaded_app):
219
  """When beam search is active with non-default temperature, gr.Info should be called."""
220
  with patch("app.gr.Info") as mock_info:
221
- result = loaded_app.translate("Hello", "French", num_beams=4, temperature=0.5)
222
  mock_info.assert_called_once()
223
  assert isinstance(result, str)
 
95
 
96
 
97
  def test_dropdown_default_is_french(demo):
98
+ """Default dropdown value should be French (fr)."""
99
  dropdowns = [b for b in demo.blocks.values() if type(b).__name__ == "Dropdown"]
100
+ assert dropdowns[0].value == "French (fr)"
101
 
102
 
103
  def test_demo_has_two_textboxes(demo):
 
119
  assert any(b.value == "Translate" for b in buttons), "Expected a 'Translate' button"
120
 
121
 
122
+ def test_demo_has_clear_button(demo):
123
+ """UI should have a Clear button."""
124
+ buttons = [b for b in demo.blocks.values() if type(b).__name__ == "Button"]
125
+ assert any(b.value == "Clear" for b in buttons), "Expected a 'Clear' button"
126
+
127
+
128
+ def test_translate_handlers_wire_text_and_language(demo):
129
+ """Translate click and submit handlers should wire input text and target language."""
130
+ translate_fns = [fn for fn in demo.fns.values() if [type(i).__name__ for i in fn.inputs] == ["Textbox", "Dropdown"]]
131
+ assert len(translate_fns) == 2, f"Expected 2 translate handlers (click + submit), found {len(translate_fns)}"
132
+
133
+
134
+ def test_char_count_handler_exists(demo):
135
+ """Character count handler should wire input text to markdown output."""
136
+ char_fns = [
137
+ fn
138
+ for fn in demo.fns.values()
139
+ if [type(i).__name__ for i in fn.inputs] == ["Textbox"]
140
+ and [type(o).__name__ for o in fn.outputs] == ["Markdown"]
141
+ ]
142
+ assert len(char_fns) == 1, f"Expected 1 char count handler, found {len(char_fns)}"
143
+
144
+
145
+ def test_output_placeholder(demo):
146
+ """Output textbox should have 'Translation' as placeholder."""
147
+ textboxes = [b for b in demo.blocks.values() if type(b).__name__ == "Textbox"]
148
+ output = [t for t in textboxes if t.interactive is False]
149
+ assert output[0].placeholder == "Translation"
150
+
151
+
152
+ def test_no_title(demo):
153
+ """UI should not have an H1 title."""
154
+ markdowns = [b for b in demo.blocks.values() if type(b).__name__ == "Markdown"]
155
+ for md in markdowns:
156
+ assert not md.value.startswith("# "), f"Found unexpected title: {md.value}"
157
+
158
+
159
+ def test_dropdown_is_filterable(demo):
160
+ """Dropdown should be filterable/searchable."""
161
+ dropdowns = [b for b in demo.blocks.values() if type(b).__name__ == "Dropdown"]
162
+ assert dropdowns[0].filterable is True
163
+
164
+
165
+ def test_dropdown_choices_include_locale_codes(demo):
166
+ """Dropdown choices should include locale codes like 'French (fr)'."""
167
+ dropdowns = [b for b in demo.blocks.values() if type(b).__name__ == "Dropdown"]
168
+ # Gradio stores choices as (label, value) tuples
169
+ labels = [c[0] if isinstance(c, tuple) else c for c in dropdowns[0].choices]
170
+ assert all("(" in label and ")" in label for label in labels), f"Expected locale codes in choices: {labels}"
171
 
172
 
173
  # --- Slow tests (require CUDA + model download) ---
 
217
  @pytest.mark.slow
218
  @pytest.mark.skipif(not gpu_available, reason="Requires CUDA")
219
  def test_translate_returns_string(loaded_app):
220
+ result = loaded_app.translate("Hello", "French (fr)")
221
  assert isinstance(result, str)
222
  assert len(result) > 0
223
 
 
226
  @pytest.mark.skipif(not gpu_available, reason="Requires CUDA")
227
  def test_translate_with_beam_search(loaded_app):
228
  """Translation with beam search (num_beams=4) should return a string."""
229
+ result = loaded_app.translate("Hello", "French (fr)", num_beams=4)
230
  assert isinstance(result, str)
231
  assert len(result) > 0
232
 
 
235
  @pytest.mark.skipif(not gpu_available, reason="Requires CUDA")
236
  def test_translate_with_custom_temperature(loaded_app):
237
  """Translation with custom temperature should return a string."""
238
+ result = loaded_app.translate("Hello", "French (fr)", temperature=0.5)
239
  assert isinstance(result, str)
240
  assert len(result) > 0
241
 
 
244
  @pytest.mark.skipif(not gpu_available, reason="Requires CUDA")
245
  def test_translate_with_custom_max_tokens(loaded_app):
246
  """Translation with low max_new_tokens should return a short string."""
247
+ result = loaded_app.translate("Hello", "French (fr)", max_new_tokens=10)
248
  assert isinstance(result, str)
249
 
250
 
 
252
  @pytest.mark.skipif(not gpu_available, reason="Requires CUDA")
253
  def test_translate_empty_string(loaded_app):
254
  """Translating an empty string should not crash."""
255
+ result = loaded_app.translate("", "French (fr)")
256
  assert isinstance(result, str)
257
 
258
 
 
261
  def test_translate_beam_search_ignores_temperature(loaded_app):
262
  """When beam search is active with non-default temperature, gr.Info should be called."""
263
  with patch("app.gr.Info") as mock_info:
264
+ result = loaded_app.translate("Hello", "French (fr)", num_beams=4, temperature=0.5)
265
  mock_info.assert_called_once()
266
  assert isinstance(result, str)