Skip to content

Commit

Permalink
Install test
Browse files Browse the repository at this point in the history
  • Loading branch information
dalthviz committed Feb 20, 2023
1 parent f4d691f commit 46aad9f
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 18 deletions.
1 change: 1 addition & 0 deletions .github/workflows/linux-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ jobs:
matrix:
PYTHON_VERSION: ['3.8', '3.9', '3.10']
USE_CONDA: ['True', 'False']
timeout-minutes: 15
steps:
- name: Checkout branch
uses: actions/checkout@v2
Expand Down
2 changes: 1 addition & 1 deletion spyder_env_manager/spyder/widgets/helper_widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def __init__(self, parent, title, messages, types, contents):
super().__init__(parent, Qt.WindowTitleHint | Qt.WindowCloseButtonHint)

self.resize(450, 130)
self.setWindowTitle(_(title))
self.setWindowTitle(title)
self.setModal(True)
self.lineedits = {}

Expand Down
4 changes: 2 additions & 2 deletions spyder_env_manager/spyder/widgets/main_widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -681,9 +681,9 @@ def _run_env_manager_action(
*manager_action_args,
**manager_action_kwargs,
)
self.manager_worker.moveToThread(self.env_manager_action_thread)
self.manager_worker.sig_ready.connect(on_ready)
self.manager_worker.sig_ready.connect(self.env_manager_action_thread.quit)
self.manager_worker.moveToThread(self.env_manager_action_thread)
self.env_manager_action_thread.started.connect(self.manager_worker.start)
self.start_spinner()
self.env_manager_action_thread.start()
Expand Down Expand Up @@ -962,7 +962,7 @@ def _message_new_environment(self):
contents = [
{"conda-like"},
{},
["3.7", "3.8", "3.9", "3.10"],
["3.7.12", "3.8.16", "3.9.16", "3.10.9"],
]
self._message_box_editable(
title,
Expand Down
6 changes: 3 additions & 3 deletions spyder_env_manager/tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@

# Local imports
from spyder_env_manager.spyder.confpage import SpyderEnvManagerConfigPage
from spyder_env_manager.tests.test_plugin import spyder_env_manager
from spyder_env_manager.tests.test_plugin import spyder_env_manager_conf


def test_config(spyder_env_manager, qtbot):
def test_config(spyder_env_manager_conf, qtbot):
"""Test that config page can be created and shown."""
dlg = ConfigDialog()
page = SpyderEnvManagerConfigPage(
spyder_env_manager, parent=spyder_env_manager.main
spyder_env_manager_conf, parent=spyder_env_manager_conf.main
)
page.initialize()
dlg.add_page(page)
Expand Down
111 changes: 99 additions & 12 deletions spyder_env_manager/tests/test_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
)

# Constants
LONG_OPERATION_TIMEOUT = 100000
LONG_OPERATION_TIMEOUT = 600000
OPERATION_TIMEOUT = 50000
IMPORT_FILE_PATH = str(Path(__file__).parent / "data" / "import_env.yml")

Expand All @@ -49,6 +49,36 @@ def get_plugin(self, plugin_name, error=True):
return Mock()


@pytest.fixture
def spyder_env_manager_conf(tmp_path, qtbot, monkeypatch):
# Mocking mainwindow and get_config
window = MainMock()
backends_root_path = tmp_path / "backends"
backends_root_path.mkdir(parents=True)

def get_conf(self, option, default=None, section=None):
if option == "environments_path":
return str(backends_root_path)
else:
return default

monkeypatch.setattr(SpyderEnvManagerWidget, "get_conf", get_conf)

# Setup plugin
plugin = SpyderEnvManager(parent=window, configuration=CONF)
window.setCentralWidget(plugin.get_widget())
window.show()

yield plugin

# Wait for pending operations and close
qtbot.waitUntil(
lambda: plugin.get_widget().actions_enabled,
timeout=LONG_OPERATION_TIMEOUT,
)
plugin.get_widget().close()


@pytest.fixture
def spyder_env_manager(tmp_path, qtbot, monkeypatch):
# Mocking mainwindow and config
Expand All @@ -65,24 +95,23 @@ def get_conf(self, option, default=None, section=None):
monkeypatch.setattr(SpyderEnvManagerWidget, "get_conf", get_conf)

# Setup plugin
plugin = SpyderEnvManager(parent=window, configuration=CONF)
widget = plugin.get_widget()
window.setCentralWidget(widget)
qtbot.addWidget(window)
plugin = SpyderEnvManager(parent=window, configuration=Mock())
window.setCentralWidget(plugin.get_widget())
window.show()

yield plugin

# Wait for pending operations and close
qtbot.waitUntil(lambda: widget.actions_enabled, timeout=LONG_OPERATION_TIMEOUT)
widget.close()
del plugin
del window
qtbot.waitUntil(
lambda: plugin.get_widget().actions_enabled,
timeout=LONG_OPERATION_TIMEOUT,
)
plugin.get_widget().close()


# ---- Tests
# ------------------------------------------------------------------------
def test_plugin_initial_state(spyder_env_manager, qtbot):
def test_plugin_initial_state(spyder_env_manager):
"""Check plugin initialization and that actions and widgets have the correct state when initializing."""
widget = spyder_env_manager.get_widget()

Expand All @@ -105,14 +134,15 @@ def test_plugin_initial_state(spyder_env_manager, qtbot):


def test_environment_creation(spyder_env_manager, qtbot, caplog):
"""Test creating an environment."""
"""Test creating an environment and installing a package in it."""
caplog.set_level(logging.DEBUG)
widget = spyder_env_manager.get_widget()

# Create environment
def handle_environment_creation_dialog():
dialog = widget.findChild(CustomParametersDialog)
dialog.lineedit_string.setText("test_env")
dialog.combobox_edit.setCurrentText("3.8")
dialog.combobox_edit.setCurrentText("3.8.16")
dialog.accept()

QTimer.singleShot(100, handle_environment_creation_dialog)
Expand Down Expand Up @@ -152,3 +182,60 @@ def handle_environment_import_dialog():
lambda: widget.packages_table.source_model.rowCount() == 3,
timeout=OPERATION_TIMEOUT,
)


def test_environment_package_installation(spyder_env_manager, qtbot, caplog):
"""Test creating an environment and installing a package in it."""
caplog.set_level(logging.DEBUG)
widget = spyder_env_manager.get_widget()

# Create environment
create_dialog = Mock()
create_dialog.combobox = combobox_mock = Mock()
combobox_mock.currentText = Mock(return_value="conda-like")
create_dialog.lineedit_string = lineedit_string_mock = Mock()
lineedit_string_mock.text = Mock(return_value="test_env")
create_dialog.combobox_edit = combobox_edit_mock = Mock()
combobox_edit_mock.currentText = Mock(return_value="3.9.16")

assert create_dialog.combobox.currentText() == "conda-like"
assert create_dialog.lineedit_string.text() == "test_env"
assert create_dialog.combobox_edit.currentText() == "3.9.16"

widget._run_action_for_env(
dialog=create_dialog, action=SpyderEnvManagerWidgetActions.NewEnvironment
)

qtbot.waitUntil(
lambda: widget.stack_layout.currentWidget() == widget.packages_table,
timeout=LONG_OPERATION_TIMEOUT,
)
assert widget.select_environment.currentText() == "test_env"
qtbot.waitUntil(
lambda: widget.packages_table.source_model.rowCount() == 2,
timeout=OPERATION_TIMEOUT,
)

# Install package in environment
install_dialog = Mock()
install_dialog.lineedit_string = lineedit_string_mock = Mock()
lineedit_string_mock.text = Mock(return_value="packaging")
install_dialog.combobox = combobox_mock = Mock()
combobox_mock.currentText = Mock(return_value="==")
install_dialog.lineedit_version = lineedit_version_mock = Mock()
lineedit_version_mock.text = Mock(return_value="22.0")

assert install_dialog.lineedit_string.text() == "packaging"
assert install_dialog.combobox.currentText() == "=="
assert install_dialog.lineedit_version.text() == "22.0"

widget._run_action_for_env(
dialog=install_dialog, action=SpyderEnvManagerWidgetActions.InstallPackage
)

qtbot.waitUntil(
lambda: widget.packages_table.source_model.rowCount() == 3,
timeout=LONG_OPERATION_TIMEOUT,
)
assert widget.packages_table.get_package_info(0)["name"] == "packaging"
assert widget.packages_table.get_package_info(0)["version"] == "22.0"

0 comments on commit 46aad9f

Please sign in to comment.