Skip to content

Commit

Permalink
Support QToolBar in iface.addToolBar (#23)
Browse files Browse the repository at this point in the history
* Fix pre-commit

* Support QToolBar as an arg in iface.addToolBar

* Use pytest-qt only with tests that really need it
  • Loading branch information
Joonalai authored Jun 17, 2022
1 parent 7f17044 commit 70a3000
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 10 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ jobs:
- name: Run tests
run: >
docker run --rm --net=host -e QGIS_IN_CI=1 --volume `pwd`:/app -w=/app qgis/qgis:${{ matrix.docker_tags }} sh -c
"pip3 install -qr requirements.txt pytest-cov pytest-qt==3.3.0 && pip3 install -e . && xvfb-run -s '+extension GLX -screen 0 1024x768x24'
pytest -v --cov src/pytest_qgis --cov-report=xml"
"pip3 install -qr requirements.txt pytest-cov && pip3 install -e . && xvfb-run -s '+extension GLX -screen 0 1024x768x24'
pytest -v --cov src/pytest_qgis --cov-report=xml -m 'not with_pytest_qt' -p no:pytest-qt"
# Upload coverage report. Will not work if the repo is private
- name: Upload coverage to Codecov
Expand Down
2 changes: 2 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ repos:
rev: 21.5b1
hooks:
- id: black
additional_dependencies:
- click==8.0.4
- repo: https://github.com/PyCQA/flake8
rev: 3.9.2
hooks:
Expand Down
18 changes: 11 additions & 7 deletions src/pytest_qgis/qgis_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
)

import logging
from typing import Dict, List, Optional
from typing import Dict, List, Optional, Union

import sip
from qgis.core import (
Expand Down Expand Up @@ -220,15 +220,19 @@ def removeToolBarIcon(self, action: QAction) -> None:
"""
pass

def addToolBar(self, name: str) -> QToolBar:
def addToolBar(self, toolbar: Union[str, QToolBar]) -> QToolBar:
"""Add toolbar with specified name.
:param name: Name for the toolbar.
:type name: str
:param toolbar: Name for the toolbar or QToolBar object.
"""
toolbar = QToolBar(name, parent=self._mainWindow)
self._toolbars[name] = toolbar
return toolbar
if isinstance(toolbar, str):
name = toolbar
_toolbar = QToolBar(name, parent=self._mainWindow)
else:
name = toolbar.windowTitle()
_toolbar = toolbar
self._toolbars[name] = _toolbar
return _toolbar

def mapCanvas(self) -> QgsMapCanvas:
"""Return a pointer to the map canvas."""
Expand Down
10 changes: 9 additions & 1 deletion tests/test_pytest_qgis.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,16 @@ def test_iface_active_layer(qgis_iface, layer_polygon, layer_points):
assert qgis_iface.activeLayer() == layer_points


def test_iface_toolbar(qgis_iface):
def test_iface_toolbar_str(qgis_iface):
name = "test_bar"
toolbar: QToolBar = qgis_iface.addToolBar(name)
assert toolbar.windowTitle() == name
assert qgis_iface._toolbars == {name: toolbar}


def test_iface_toolbar_qtoolbar(qgis_iface):
name = "test_bar"
toolbar: QToolBar = QToolBar(name)
qgis_iface.addToolBar(toolbar)
assert toolbar.windowTitle() == name
assert qgis_iface._toolbars == {name: toolbar}
2 changes: 2 additions & 0 deletions tests/visual/test_qgis_ui.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
# along with pytest-qgis. If not, see <https://www.gnu.org/licenses/>.
import time

import pytest
from qgis.gui import QgsAttributeDialog
from qgis.PyQt import QtCore
from qgis.PyQt.QtCore import QCoreApplication
Expand All @@ -26,6 +27,7 @@
TIMEOUT = 0.01 if IN_CI else 1


@pytest.mark.with_pytest_qt()
def test_attribute_dialog_change(
qgis_iface, qgis_canvas, layer_points, qgis_bot, qtbot
):
Expand Down
5 changes: 5 additions & 0 deletions tests/visual/test_show_map.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@

"""
These tests are meant to be tested visually by the developer.
NOTE: if you have pytest-qt installed, you might encounter some
problems with tests in this module.
In that case, run these tests with pytest-qt disabled: "pytest -p no:pytest-qt"
"""

DEFAULT_TIMEOUT = 0.01 if IN_CI else 1
Expand Down

0 comments on commit 70a3000

Please sign in to comment.