Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test improvements #755

Merged
merged 9 commits into from
May 25, 2024
5 changes: 3 additions & 2 deletions buzz/buzz.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,6 @@
format=log_format,
)

logging.debug("app_dir: %s", APP_BASE_DIR)

if getattr(sys, "frozen", False) is False:
stdout_handler = logging.StreamHandler(sys.stdout)
stdout_handler.setLevel(logging.DEBUG)
Expand All @@ -59,6 +57,9 @@
from buzz.cli import parse_command_line
from buzz.widgets.application import Application

logging.debug("app_dir: %s", APP_BASE_DIR)
logging.debug("log_dir: %s", log_dir)

Check warning on line 61 in buzz/buzz.py

View check run for this annotation

Codecov / codecov/patch

buzz/buzz.py#L60-L61

Added lines #L60 - L61 were not covered by tests

app = Application(sys.argv)
parse_command_line(app)
sys.exit(app.exec())
3 changes: 3 additions & 0 deletions buzz/locale.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import logging
import gettext

from PyQt6.QtCore import QLocale
Expand All @@ -8,6 +9,8 @@
locale_dir = get_path("locale")
gettext.bindtextdomain("buzz", locale_dir)

logging.debug(f"UI locales {QLocale().uiLanguages()}")

translate = gettext.translation(
APP_NAME.lower(), locale_dir, languages=QLocale().uiLanguages(), fallback=True
)
Expand Down
16 changes: 10 additions & 6 deletions buzz/locale/lv_LV/LC_MESSAGES/buzz.po
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ msgid ""
msgstr ""
"Project-Id-Version: \n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2024-05-18 17:24+0300\n"
"POT-Creation-Date: 2024-05-25 02:23+0300\n"
"PO-Revision-Date: 2024-05-18 16:28+0300\n"
"Last-Translator: \n"
"Language-Team: \n"
Expand Down Expand Up @@ -128,19 +128,19 @@ msgstr "Jūsu ievadītā URL adrese nav derīga."
msgid "Live Recording"
msgstr "Dzīvā ierakstīšana"

#: buzz/widgets/recording_transcriber_widget.py:104
#: buzz/widgets/recording_transcriber_widget.py:109
msgid "Click Record to begin..."
msgstr "Klikšķiniet Ierakstīt, lai sāktu..."

#: buzz/widgets/recording_transcriber_widget.py:116
#: buzz/widgets/recording_transcriber_widget.py:121
msgid "Microphone:"
msgstr "Mikrofons:"

#: buzz/widgets/recording_transcriber_widget.py:273
#: buzz/widgets/recording_transcriber_widget.py:278
msgid "An error occurred while starting a new recording:"
msgstr "Sākot jaunu ierakstu notikusi kļūda:"

#: buzz/widgets/recording_transcriber_widget.py:277
#: buzz/widgets/recording_transcriber_widget.py:282
msgid ""
"Please check your audio devices or check the application logs for more "
"information."
Expand Down Expand Up @@ -361,9 +361,13 @@ msgstr "Vai tiešām dzēst izvēlēto modeli?"
msgid "Reset to Defaults"
msgstr "Atjaunot noklusētos"

#: buzz/widgets/preferences_dialog/preferences_dialog.py:43
msgid "Preferences"
msgstr "Iestatījumi"

#: buzz/widgets/preferences_dialog/preferences_dialog.py:50
msgid "General"
msgstr "Vizpārīgi"
msgstr "Vispārīgi"

#: buzz/widgets/preferences_dialog/preferences_dialog.py:53
msgid "Models"
Expand Down
2 changes: 1 addition & 1 deletion buzz/widgets/preferences_dialog/preferences_dialog.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def __init__(

self.updated_preferences = copy.deepcopy(preferences)

self.setWindowTitle("Preferences")
self.setWindowTitle(_("Preferences"))

layout = QVBoxLayout(self)
tab_widget = QTabWidget(self)
Expand Down
21 changes: 21 additions & 0 deletions tests/app_main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import os
from unittest.mock import patch
from buzz.buzz import main

class TestMain:
def test_main(self):
with patch('buzz.widgets.application.Application') as mock_application, \
patch('buzz.cli.parse_command_line') as mock_parse_command_line, \
patch('buzz.buzz.sys') as mock_sys, \
patch('buzz.buzz.user_log_dir', return_value='/tmp/buzz') as mock_log_dir:

mock_application.return_value.exec.return_value = 0

mock_sys.argv = ['buzz.py']

main()

mock_application.assert_called_once_with(mock_sys.argv)
mock_parse_command_line.assert_called_once_with(mock_application.return_value)
mock_application.return_value.exec.assert_called_once()
assert os.path.isdir(mock_log_dir.return_value), "Log dir was not created"
9 changes: 5 additions & 4 deletions tests/gui_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
)
from pytestqt.qtbot import QtBot

from buzz.locale import _
from buzz.__version__ import VERSION
from buzz.widgets.audio_devices_combo_box import AudioDevicesComboBox
from buzz.widgets.transcriber.advanced_settings_dialog import AdvancedSettingsDialog
Expand Down Expand Up @@ -49,7 +50,7 @@ class TestLanguagesComboBox:
def test_should_show_sorted_whisper_languages(self, qtbot):
languages_combox_box = LanguagesComboBox("en")
qtbot.add_widget(languages_combox_box)
assert languages_combox_box.itemText(0) == "Detect Language"
assert languages_combox_box.itemText(0) == _("Detect Language")
assert languages_combox_box.itemText(10) == "Belarusian"

def test_should_select_en_as_default_language(self, qtbot):
Expand All @@ -60,7 +61,7 @@ def test_should_select_en_as_default_language(self, qtbot):
def test_should_select_detect_language_as_default(self, qtbot):
languages_combo_box = LanguagesComboBox(None)
qtbot.add_widget(languages_combo_box)
assert languages_combo_box.currentText() == "Detect Language"
assert languages_combo_box.currentText() == _("Detect Language")


class TestAudioDevicesComboBox:
Expand Down Expand Up @@ -102,7 +103,7 @@ def test_should_check_for_updates(self, qtbot: QtBot):
dialog.check_updates_button.click()

mock_message_box_information.assert_called_with(
dialog, "", "You're up to date!"
dialog, "", _("You're up to date!")
)


Expand All @@ -118,7 +119,7 @@ def test_should_update_advanced_settings(self, qtbot: QtBot):
transcription_options_mock = Mock()
dialog.transcription_options_changed.connect(transcription_options_mock)

assert dialog.windowTitle() == "Advanced Settings"
assert dialog.windowTitle() == _("Advanced Settings")
assert dialog.temperature_line_edit.text() == "0.0, 0.8"
assert dialog.initial_prompt_text_edit.toPlainText() == "prompt"

Expand Down
3 changes: 2 additions & 1 deletion tests/widgets/import_url_dialog_test.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from unittest.mock import patch

from buzz.locale import _
from buzz.widgets.import_url_dialog import ImportURLDialog


Expand All @@ -11,7 +12,7 @@ def test_should_show_error_with_invalid_url(self, qtbot):
with patch("PyQt6.QtWidgets.QMessageBox.critical") as mock_critical:
dialog.button_box.button(dialog.button_box.StandardButton.Ok).click()
mock_critical.assert_called_with(
dialog, "Invalid URL", "The URL you entered is invalid."
dialog, _("Invalid URL"), _("The URL you entered is invalid.")
)

def test_should_return_url_with_valid_url(self, qtbot):
Expand Down
7 changes: 4 additions & 3 deletions tests/widgets/main_window_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
)
from pytestqt.qtbot import QtBot

from buzz.locale import _
from buzz.db.entity.transcription import Transcription
from buzz.db.service.transcription_service import TranscriptionService
from buzz.widgets.main_window import MainWindow
Expand All @@ -25,7 +26,7 @@
mock_transcriptions: List[Transcription] = [
Transcription(status="completed"),
Transcription(status="canceled"),
Transcription(status="failed", error_message="Error"),
Transcription(status="failed", error_message=_("Error")),
]


Expand All @@ -48,7 +49,7 @@ def test_should_run_file_transcription_task(

self._import_file_and_start_transcription(window)

open_transcript_action = self._get_toolbar_action(window, "Open Transcript")
open_transcript_action = self._get_toolbar_action(window, _("Open Transcript"))
assert open_transcript_action.isEnabled() is False

table_widget = self._get_tasks_table(window)
Expand Down Expand Up @@ -251,7 +252,7 @@ def _import_file_and_start_transcription(
"",
)
new_transcription_action = TestMainWindow._get_toolbar_action(
window, "New Transcription"
window, _("New Transcription")
)
new_transcription_action.trigger()

Expand Down
7 changes: 4 additions & 3 deletions tests/widgets/model_download_progress_dialog.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from PyQt6.QtCore import Qt

from buzz.locale import _
from buzz.model_loader import ModelType
from buzz.widgets.model_download_progress_dialog import ModelDownloadProgressDialog

Expand All @@ -8,18 +9,18 @@ class TestModelDownloadProgressDialog:
def test_should_show_dialog(self, qtbot):
dialog = ModelDownloadProgressDialog(model_type=ModelType.WHISPER, parent=None)
qtbot.add_widget(dialog)
assert dialog.labelText() == "Downloading model (0%)"
assert dialog.labelText() == f"{_('Downloading model')} (0%)"

def test_should_update_label_on_progress(self, qtbot):
dialog = ModelDownloadProgressDialog(model_type=ModelType.WHISPER, parent=None)
qtbot.add_widget(dialog)
dialog.set_value(0.0)

dialog.set_value(0.01)
assert dialog.labelText().startswith("Downloading model (1%")
assert dialog.labelText().startswith(f"{_('Downloading model')} (1%")

dialog.set_value(0.1)
assert dialog.labelText().startswith("Downloading model (10%")
assert dialog.labelText().startswith(f"{_('Downloading model')} (10%")

# Other windows should not be processing while models are being downloaded
def test_should_be_an_application_modal(self, qtbot):
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from PyQt6.QtWidgets import QPushButton, QMessageBox, QLineEdit

from buzz.locale import _
from buzz.widgets.preferences_dialog.general_preferences_widget import (
GeneralPreferencesWidget,
)
Expand All @@ -18,7 +19,7 @@ def test_should_disable_test_button_if_no_api_key(self, qtbot, mocker):
test_button = widget.findChild(QPushButton)
assert isinstance(test_button, QPushButton)

assert test_button.text() == "Test"
assert test_button.text() == _("Test")
assert not test_button.isEnabled()

line_edit = widget.findChild(QLineEdit)
Expand Down Expand Up @@ -46,7 +47,7 @@ def test_should_test_openai_api_key(self, qtbot, mocker):

def mock_called():
message_box_warning_mock.assert_called()
assert message_box_warning_mock.call_args[0][1] == "OpenAI API Key Test"
assert message_box_warning_mock.call_args[0][1] == _("OpenAI API Key Test")
assert (
message_box_warning_mock.call_args[0][2]
== "Incorrect API key provided: wrong-ap*-key. You can find your "
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from PyQt6.QtWidgets import QComboBox, QPushButton
from pytestqt.qtbot import QtBot

from buzz.locale import _
from buzz.model_loader import (
TranscriptionModel,
ModelType,
Expand All @@ -29,10 +30,10 @@ def test_should_show_model_list(self, qtbot):
qtbot.add_widget(widget)

first_item = widget.model_list_widget.topLevelItem(0)
assert first_item.text(0) == "Downloaded"
assert first_item.text(0) == _("Downloaded")

second_item = widget.model_list_widget.topLevelItem(1)
assert second_item.text(0) == "Available for Download"
assert second_item.text(0) == _("Available for Download")

def test_should_change_model_type(self, qtbot):
widget = ModelsPreferencesWidget()
Expand All @@ -43,10 +44,10 @@ def test_should_change_model_type(self, qtbot):
combo_box.setCurrentText("Faster Whisper")

first_item = widget.model_list_widget.topLevelItem(0)
assert first_item.text(0) == "Downloaded"
assert first_item.text(0) == _("Downloaded")

second_item = widget.model_list_widget.topLevelItem(1)
assert second_item.text(0) == "Available for Download"
assert second_item.text(0) == _("Available for Download")

def test_should_download_model(self, qtbot: QtBot, clear_model_cache):
# make progress dialog non-modal to unblock qtbot.wait_until
Expand All @@ -58,7 +59,7 @@ def test_should_download_model(self, qtbot: QtBot, clear_model_cache):
assert widget.model.get_local_model_path() is None

available_item = widget.model_list_widget.topLevelItem(1)
assert available_item.text(0) == "Available for Download"
assert available_item.text(0) == _("Available for Download")

tiny_item = available_item.child(0)
assert tiny_item.text(0) == "Tiny"
Expand All @@ -67,7 +68,7 @@ def test_should_download_model(self, qtbot: QtBot, clear_model_cache):
download_button = widget.findChild(QPushButton, "DownloadButton")
assert isinstance(download_button, QPushButton)

assert download_button.text() == "Download"
assert download_button.text() == _("Download")
download_button.click()

def downloaded_model():
Expand Down Expand Up @@ -97,7 +98,7 @@ def test_should_show_downloaded_model(self, qtbot, default_model_path):
qtbot.add_widget(widget)

available_item = widget.model_list_widget.topLevelItem(0)
assert available_item.text(0) == "Downloaded"
assert available_item.text(0) == _("Downloaded")

tiny_item = available_item.child(0)
assert tiny_item.text(0) == "Tiny"
Expand Down
54 changes: 49 additions & 5 deletions tests/widgets/preferences_dialog/preferences_dialog_test.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,68 @@
import os

from PyQt6.QtCore import QSettings
from PyQt6.QtWidgets import QTabWidget
from pytestqt.qtbot import QtBot

from buzz.locale import _
from buzz.widgets.preferences_dialog.models.preferences import Preferences
from buzz.widgets.preferences_dialog.preferences_dialog import PreferencesDialog


class TestPreferencesDialog:
locale_file_path = os.path.abspath(
os.path.join(os.path.dirname(__file__), "../../../buzz/locale/lv_LV/LC_MESSAGES/buzz.mo")
)

def test_create(self, qtbot: QtBot, shortcuts):
dialog = PreferencesDialog(
shortcuts=shortcuts, preferences=Preferences.load(QSettings())
)
qtbot.add_widget(dialog)

assert dialog.windowTitle() == "Preferences"
assert dialog.windowTitle() == _("Preferences")

tab_widget = dialog.findChild(QTabWidget)
assert isinstance(tab_widget, QTabWidget)
assert tab_widget.count() == 4
assert tab_widget.tabText(0) == _("General")
assert tab_widget.tabText(1) == _("Models")
assert tab_widget.tabText(2) == _("Shortcuts")
assert tab_widget.tabText(3) == _("Folder Watch")

def test_create_localized(self, qtbot: QtBot, shortcuts, mocker):
mocker.patch(
"PyQt6.QtCore.QLocale.uiLanguages",
return_value=['lv_LV'],
)

# Reload the module after the patch
from importlib import reload
import buzz.locale
import buzz.widgets.preferences_dialog.models.preferences
import buzz.widgets.preferences_dialog.preferences_dialog

reload(buzz.locale)
reload(buzz.widgets.preferences_dialog.models.preferences)
reload(buzz.widgets.preferences_dialog.preferences_dialog)

from buzz.locale import _
from buzz.widgets.preferences_dialog.models.preferences import Preferences
from buzz.widgets.preferences_dialog.preferences_dialog import PreferencesDialog

dialog = PreferencesDialog(
shortcuts=shortcuts, preferences=Preferences.load(QSettings())
)
qtbot.add_widget(dialog)

assert os.path.isfile(self.locale_file_path), "File .mo file does not exist"
assert _("Preferences") == "Iestatījumi"
assert dialog.windowTitle() == "Iestatījumi"

tab_widget = dialog.findChild(QTabWidget)
assert isinstance(tab_widget, QTabWidget)
assert tab_widget.count() == 4
assert tab_widget.tabText(0) == "General"
assert tab_widget.tabText(1) == "Models"
assert tab_widget.tabText(2) == "Shortcuts"
assert tab_widget.tabText(3) == "Folder Watch"
assert tab_widget.tabText(0) == "Vispārīgi"
assert tab_widget.tabText(1) == "Modeļi"
assert tab_widget.tabText(2) == "Īsinājumi"
assert tab_widget.tabText(3) == "Mapes vērošana"
Loading
Loading