Skip to content

Commit 4428005

Browse files
authored
Add settings wizard tests (#488)
* Small cleanup of settings_wizard.py * Add some tests for SettingsWizard class
1 parent 98bb3b2 commit 4428005

File tree

2 files changed

+69
-3
lines changed

2 files changed

+69
-3
lines changed

activity_browser/app/ui/wizards/settings_wizard.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# -*- coding: utf-8 -*-
22
import brightway2 as bw
3-
from PySide2 import QtWidgets, QtGui
3+
from PySide2 import QtWidgets
44
import os
55

66
from activity_browser.app.signals import signals
@@ -95,8 +95,8 @@ def restore_defaults(self):
9595
self.startup_project_combobox.setCurrentText(ab_settings.get_default_project_name())
9696

9797
def bwdir_browse(self):
98-
path = QtWidgets.QFileDialog().getExistingDirectory(
99-
None, "Select a brightway2 database folder"
98+
path = QtWidgets.QFileDialog.getExistingDirectory(
99+
self, "Select a brightway2 database folder"
100100
)
101101
if path:
102102
self.change_bw_dir(os.path.normpath(path))

tests/test_settings_wizard.py

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
# -*- coding: utf-8 -*-
2+
import brightway2 as bw
3+
from PySide2.QtCore import Qt
4+
from PySide2.QtWidgets import QMessageBox, QWizard
5+
import pytest
6+
7+
from activity_browser.app.ui.wizards.settings_wizard import SettingsWizard
8+
9+
10+
def test_settings_wizard_simple(qtbot, bw2test):
11+
"""Test some of the default values of the wizard."""
12+
wizard = SettingsWizard(None)
13+
qtbot.addWidget(wizard)
14+
wizard.show()
15+
16+
# Check that the default fields are default
17+
assert wizard.field("startup_project") == "default"
18+
assert wizard.field("custom_bw_dir") == ""
19+
assert wizard.last_bwdir == bw.projects._base_data_dir
20+
21+
# We can't click 'Save' from the start.
22+
assert not wizard.button(QWizard.FinishButton).isEnabled()
23+
24+
# cancel out of the wizard.
25+
qtbot.mouseClick(wizard.button(QWizard.CancelButton), Qt.LeftButton)
26+
27+
28+
def test_alter_startup_project(qtbot):
29+
"""Alter the default startup project"""
30+
wizard = SettingsWizard(None)
31+
qtbot.addWidget(wizard)
32+
wizard.show()
33+
34+
# Check we can't Save, alter the startup project and check again.
35+
assert not wizard.settings_page.isComplete()
36+
with qtbot.waitSignal(wizard.settings_page.completeChanged, timeout=100):
37+
index = wizard.settings_page.project_names.index("pytest_project")
38+
wizard.settings_page.startup_project_combobox.setCurrentIndex(index)
39+
assert wizard.field("startup_project") == "pytest_project"
40+
assert wizard.settings_page.isComplete()
41+
42+
with qtbot.waitSignal(wizard.finished, timeout=100):
43+
qtbot.mouseClick(wizard.button(QWizard.FinishButton), Qt.LeftButton)
44+
45+
46+
def test_restore_defaults(qtbot, monkeypatch):
47+
"""Restore the default startup project."""
48+
wizard = SettingsWizard(None)
49+
qtbot.addWidget(wizard)
50+
wizard.show()
51+
52+
# Follow-up from the last test, restore the startup_project to default
53+
assert wizard.field("startup_project") == "pytest_project"
54+
55+
with qtbot.waitSignal(wizard.settings_page.startup_project_combobox.currentIndexChanged, timeout=100):
56+
# No handle the popup about changing the brightway2 directory.
57+
monkeypatch.setattr(QMessageBox, "question", lambda *args: QMessageBox.No)
58+
qtbot.mouseClick(
59+
wizard.settings_page.restore_defaults_button,
60+
Qt.LeftButton
61+
)
62+
63+
assert wizard.field("startup_project") == "default"
64+
65+
with qtbot.waitSignal(wizard.finished, timeout=100):
66+
qtbot.mouseClick(wizard.button(QWizard.FinishButton), Qt.LeftButton)

0 commit comments

Comments
 (0)