Skip to content

Commit f6ef2d5

Browse files
Fix intermittently failing tests (#277)
1 parent cfcb4f6 commit f6ef2d5

File tree

4 files changed

+76
-71
lines changed

4 files changed

+76
-71
lines changed

.coveragerc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@ omit =
77
directory = coverage/html
88

99
[report]
10-
fail_under = 70
10+
fail_under = 74

buzz/gui.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -119,9 +119,9 @@ def __init__(self, default_language: Optional[str], parent: Optional[QWidget] =
119119
self.currentIndexChanged.connect(self.on_index_changed)
120120

121121
default_language_key = default_language if default_language != '' else None
122-
default_language_index = next((i for i, lang in enumerate(self.languages)
123-
if lang[0] == default_language_key), 0)
124-
self.setCurrentIndex(default_language_index)
122+
for i, lang in enumerate(self.languages):
123+
if lang[0] == default_language_key:
124+
self.setCurrentIndex(i)
125125

126126
def on_index_changed(self, index: int):
127127
self.languageChanged.emit(self.languages[index][0])
@@ -361,6 +361,11 @@ def reset_model_download(self):
361361
def on_word_level_timings_changed(self, value: int):
362362
self.transcription_options.word_level_timings = value == Qt.CheckState.Checked.value
363363

364+
def closeEvent(self, event: QtGui.QCloseEvent) -> None:
365+
if self.transcriber_thread is not None:
366+
self.transcriber_thread.wait()
367+
super().closeEvent(event)
368+
364369

365370
class TranscriptionViewerWidget(QWidget):
366371
transcription_task: FileTranscriptionTask

buzz/model_loader.py

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,8 @@ def run(self):
7070
file_path = os.path.join(root_dir, f'ggml-model-whisper-{model_name}.bin')
7171
expected_sha256 = WHISPER_CPP_MODELS_SHA256[model_name]
7272
self.download_model(url, file_path, expected_sha256)
73-
return
7473

75-
if self.model_type == ModelType.WHISPER:
74+
elif self.model_type == ModelType.WHISPER:
7675
root_dir = os.getenv(
7776
"XDG_CACHE_HOME",
7877
os.path.join(os.path.expanduser("~"), ".cache", "whisper")
@@ -82,9 +81,8 @@ def run(self):
8281
file_path = os.path.join(root_dir, os.path.basename(url))
8382
expected_sha256 = url.split('/')[-2]
8483
self.download_model(url, file_path, expected_sha256)
85-
return
8684

87-
if self.model_type == ModelType.HUGGING_FACE:
85+
else: # ModelType.HUGGING_FACE:
8886
self.progress.emit((0, 100))
8987

9088
try:
@@ -95,8 +93,9 @@ def run(self):
9593
return
9694

9795
self.progress.emit((100, 100))
98-
self.finished.emit(self.hugging_face_model_id)
99-
return
96+
file_path = self.hugging_face_model_id
97+
98+
self.finished.emit(file_path)
10099

101100
def download_model(self, url: str, file_path: str, expected_sha256: Optional[str]):
102101
try:
@@ -108,14 +107,12 @@ def download_model(self, url: str, file_path: str, expected_sha256: Optional[str
108107

109108
if os.path.isfile(file_path):
110109
if expected_sha256 is None:
111-
self.finished.emit(file_path)
112-
return
110+
return file_path
113111

114112
model_bytes = open(file_path, "rb").read()
115113
model_sha256 = hashlib.sha256(model_bytes).hexdigest()
116114
if model_sha256 == expected_sha256:
117-
self.finished.emit(file_path)
118-
return
115+
return file_path
119116
else:
120117
warnings.warn(
121118
f"{file_path} exists, but the SHA256 checksum does not match; re-downloading the file")
@@ -141,7 +138,7 @@ def download_model(self, url: str, file_path: str, expected_sha256: Optional[str
141138
"Model has been downloaded but the SHA256 checksum does not match. Please retry loading the "
142139
"model.")
143140

144-
self.finished.emit(file_path)
141+
return file_path
145142
except RuntimeError as exc:
146143
self.error.emit(str(exc))
147144
logging.exception('')

tests/gui_test.py

Lines changed: 59 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import logging
22
import os.path
33
import pathlib
4-
import platform
54
from unittest.mock import Mock, patch
65

76
import pytest
@@ -12,8 +11,7 @@
1211
from pytestqt.qtbot import QtBot
1312

1413
from buzz.cache import TasksCache
15-
from buzz.gui import (AboutDialog, AdvancedSettingsDialog, Application,
16-
AudioDevicesComboBox, DownloadModelProgressDialog,
14+
from buzz.gui import (AboutDialog, AdvancedSettingsDialog, AudioDevicesComboBox, DownloadModelProgressDialog,
1715
FileTranscriberWidget, LanguagesComboBox, MainWindow,
1816
RecordingTranscriberWidget,
1917
TemperatureValidator, TextDisplayBox,
@@ -25,30 +23,26 @@
2523
from tests.mock_sounddevice import MockInputStream
2624

2725

28-
class TestApplication:
29-
# FIXME: this seems to break the tests if not run??
30-
app = Application()
31-
32-
def test_should_open_application(self):
33-
assert self.app is not None
34-
35-
3626
class TestLanguagesComboBox:
37-
languagesComboxBox = LanguagesComboBox('en')
38-
39-
def test_should_show_sorted_whisper_languages(self):
40-
assert self.languagesComboxBox.itemText(0) == 'Detect Language'
41-
assert self.languagesComboxBox.itemText(10) == 'Belarusian'
42-
assert self.languagesComboxBox.itemText(20) == 'Dutch'
43-
assert self.languagesComboxBox.itemText(30) == 'Gujarati'
44-
assert self.languagesComboxBox.itemText(40) == 'Japanese'
45-
assert self.languagesComboxBox.itemText(50) == 'Lithuanian'
4627

47-
def test_should_select_en_as_default_language(self):
48-
assert self.languagesComboxBox.currentText() == 'English'
49-
50-
def test_should_select_detect_language_as_default(self):
28+
def test_should_show_sorted_whisper_languages(self, qtbot):
29+
languages_combox_box = LanguagesComboBox('en')
30+
qtbot.add_widget(languages_combox_box)
31+
assert languages_combox_box.itemText(0) == 'Detect Language'
32+
assert languages_combox_box.itemText(10) == 'Belarusian'
33+
assert languages_combox_box.itemText(20) == 'Dutch'
34+
assert languages_combox_box.itemText(30) == 'Gujarati'
35+
assert languages_combox_box.itemText(40) == 'Japanese'
36+
assert languages_combox_box.itemText(50) == 'Lithuanian'
37+
38+
def test_should_select_en_as_default_language(self, qtbot):
39+
languages_combox_box = LanguagesComboBox('en')
40+
qtbot.add_widget(languages_combox_box)
41+
assert languages_combox_box.currentText() == 'English'
42+
43+
def test_should_select_detect_language_as_default(self, qtbot):
5144
languages_combo_box = LanguagesComboBox(None)
45+
qtbot.add_widget(languages_combo_box)
5246
assert languages_combo_box.currentText() == 'Detect Language'
5347

5448

@@ -185,17 +179,16 @@ def check_task_completed():
185179

186180

187181
class TestFileTranscriberWidget:
188-
widget = FileTranscriberWidget(
189-
file_paths=['testdata/whisper-french.mp3'], parent=None)
190-
191182
def test_should_set_window_title(self, qtbot: QtBot):
192-
qtbot.addWidget(self.widget)
193-
assert self.widget.windowTitle() == 'whisper-french.mp3'
183+
widget = FileTranscriberWidget(
184+
file_paths=['testdata/whisper-french.mp3'], parent=None)
185+
qtbot.add_widget(widget)
186+
assert widget.windowTitle() == 'whisper-french.mp3'
194187

195188
def test_should_emit_triggered_event(self, qtbot: QtBot):
196189
widget = FileTranscriberWidget(
197190
file_paths=['testdata/whisper-french.mp3'], parent=None)
198-
qtbot.addWidget(widget)
191+
qtbot.add_widget(widget)
199192

200193
mock_triggered = Mock()
201194
widget.triggered.connect(mock_triggered)
@@ -254,31 +247,41 @@ def test_should_validate_temperature(self, text: str, state: QValidator.State):
254247

255248

256249
class TestTranscriptionViewerWidget:
257-
widget = TranscriptionViewerWidget(
258-
transcription_task=FileTranscriptionTask(
259-
id=0,
260-
file_path='testdata/whisper-french.mp3',
261-
file_transcription_options=FileTranscriptionOptions(
262-
file_paths=['testdata/whisper-french.mp3']),
263-
transcription_options=TranscriptionOptions(),
264-
segments=[Segment(40, 299, 'Bien'),
265-
Segment(299, 329, 'venue dans')],
266-
model_path=''))
267250

268251
def test_should_display_segments(self, qtbot: QtBot):
269-
qtbot.add_widget(self.widget)
252+
widget = TranscriptionViewerWidget(
253+
transcription_task=FileTranscriptionTask(
254+
id=0,
255+
file_path='testdata/whisper-french.mp3',
256+
file_transcription_options=FileTranscriptionOptions(
257+
file_paths=['testdata/whisper-french.mp3']),
258+
transcription_options=TranscriptionOptions(),
259+
segments=[Segment(40, 299, 'Bien'),
260+
Segment(299, 329, 'venue dans')],
261+
model_path=''))
262+
qtbot.add_widget(widget)
270263

271-
assert self.widget.windowTitle() == 'whisper-french.mp3'
264+
assert widget.windowTitle() == 'whisper-french.mp3'
272265

273-
text_display_box = self.widget.findChild(TextDisplayBox)
266+
text_display_box = widget.findChild(TextDisplayBox)
274267
assert isinstance(text_display_box, TextDisplayBox)
275268
assert text_display_box.toPlainText(
276269
) == '00:00:00.040 --> 00:00:00.299\nBien\n\n00:00:00.299 --> 00:00:00.329\nvenue dans'
277270

278271
def test_should_export_segments(self, tmp_path: pathlib.Path, qtbot: QtBot):
279-
qtbot.add_widget(self.widget)
272+
widget = TranscriptionViewerWidget(
273+
transcription_task=FileTranscriptionTask(
274+
id=0,
275+
file_path='testdata/whisper-french.mp3',
276+
file_transcription_options=FileTranscriptionOptions(
277+
file_paths=['testdata/whisper-french.mp3']),
278+
transcription_options=TranscriptionOptions(),
279+
segments=[Segment(40, 299, 'Bien'),
280+
Segment(299, 329, 'venue dans')],
281+
model_path=''))
282+
qtbot.add_widget(widget)
280283

281-
export_button = self.widget.findChild(QPushButton)
284+
export_button = widget.findChild(QPushButton)
282285
assert isinstance(export_button, QPushButton)
283286

284287
output_file_path = tmp_path / 'whisper.txt'
@@ -291,39 +294,39 @@ def test_should_export_segments(self, tmp_path: pathlib.Path, qtbot: QtBot):
291294

292295

293296
class TestTranscriptionTasksTableWidget:
294-
widget = TranscriptionTasksTableWidget()
295297

296298
def test_upsert_task(self, qtbot: QtBot):
297-
qtbot.add_widget(self.widget)
299+
widget = TranscriptionTasksTableWidget()
300+
qtbot.add_widget(widget)
298301

299302
task = FileTranscriptionTask(id=0, file_path='testdata/whisper-french.mp3',
300303
transcription_options=TranscriptionOptions(),
301304
file_transcription_options=FileTranscriptionOptions(
302305
file_paths=['testdata/whisper-french.mp3']), model_path='',
303306
status=FileTranscriptionTask.Status.QUEUED)
304307

305-
self.widget.upsert_task(task)
308+
widget.upsert_task(task)
306309

307-
assert self.widget.rowCount() == 1
308-
assert self.widget.item(0, 1).text() == 'whisper-french.mp3'
309-
assert self.widget.item(0, 2).text() == 'Queued'
310+
assert widget.rowCount() == 1
311+
assert widget.item(0, 1).text() == 'whisper-french.mp3'
312+
assert widget.item(0, 2).text() == 'Queued'
310313

311314
task.status = FileTranscriptionTask.Status.IN_PROGRESS
312315
task.fraction_completed = 0.3524
313-
self.widget.upsert_task(task)
316+
widget.upsert_task(task)
314317

315-
assert self.widget.rowCount() == 1
316-
assert self.widget.item(0, 1).text() == 'whisper-french.mp3'
317-
assert self.widget.item(0, 2).text() == 'In Progress (35%)'
318+
assert widget.rowCount() == 1
319+
assert widget.item(0, 1).text() == 'whisper-french.mp3'
320+
assert widget.item(0, 2).text() == 'In Progress (35%)'
318321

319322

320-
@pytest.mark.skip()
321323
class TestRecordingTranscriberWidget:
322324
def test_should_set_window_title(self, qtbot: QtBot):
323325
widget = RecordingTranscriberWidget()
324326
qtbot.add_widget(widget)
325327
assert widget.windowTitle() == 'Live Recording'
326328

329+
@pytest.mark.skip()
327330
def test_should_transcribe(self, qtbot):
328331
widget = RecordingTranscriberWidget()
329332
qtbot.add_widget(widget)

0 commit comments

Comments
 (0)