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

Make stub QgisInterface and MockMessageBar inherit real interfaces #54

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,4 @@ target/
/tests/data/db.gpkg-shm
/tests/data/db.gpkg-wal
/tests/data/*.tif.aux.xml
/Makefile
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Unreleased

## Fixes

* Make stub QgisInterface and MockMessageBar inherit real interfaces

# Version 2.0.0 (29-11-2023)

## New Features
Expand Down
3 changes: 2 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,8 @@ ban-relative-imports = "all"

[tool.ruff.per-file-ignores]
"src/pytest_qgis/pytest_qgis.py"=["PLR2004"] # TODO: Fix magic values. Remove this after.
"src/pytest_qgis/qgis_interface.py" = ["N802", "N803"]
"src/pytest_qgis/qgis_interface.py" = ["N802", "N803", "N815", "ARG002"]
"src/pytest_qgis/mock_qgis_classes.py" = ["N802", "N803"]
"tests/*" = [
"ANN001",
"ANN201",
Expand Down
95 changes: 77 additions & 18 deletions src/pytest_qgis/mock_qgis_classes.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,37 +15,96 @@
#
# You should have received a copy of the GNU General Public License
# along with pytest-qgis. If not, see <https://www.gnu.org/licenses/>.


from typing import Dict, List
import typing

from qgis.core import Qgis
from qgis.PyQt.QtCore import QObject
from qgis.gui import QgsMessageBar as QgsMessageBarOriginal


class MockMessageBar(QObject):
class MockMessageBar(QgsMessageBarOriginal):
"""Mocked message bar to hold the messages."""

def __init__(self) -> None:
super().__init__()
self.messages: Dict[int, List[str]] = {
Qgis.Info: [],
Qgis.Warning: [],
Qgis.Critical: [],
Qgis.Success: [],
super().__init__(None)
self.messages: dict[int, list[str]] = {}
self._init_messages()

def _init_messages(self) -> None:
self.messages = {
Qgis.MessageLevel.Info: [],
Qgis.MessageLevel.Warning: [],
Qgis.MessageLevel.Critical: [],
Qgis.MessageLevel.Success: [],
}

def get_messages(self, level: int) -> List[str]:
def get_messages(self, level: int) -> list[str]:
"""Used to test which messages have been logged."""
return self.messages[level]

def pushMessage( # noqa: N802
def clear_messages(self) -> None:
"""Clear logged messages."""
self._init_messages()

@typing.overload
def pushMessage(
self,
title: str,
text: str,
level: int,
duration: int, # noqa: ARG002
text: typing.Optional[str] = None,
level: Qgis.MessageLevel = Qgis.MessageLevel.Info,
duration: int = -1,
) -> None:
...

@typing.overload
def pushMessage(
self,
title: typing.Optional[str] = None,
text: typing.Optional[str] = None,
level: Qgis.MessageLevel = Qgis.MessageLevel.Info,
duration: int = -1,
) -> None:
...

@typing.overload
def pushMessage( # noqa: PLR0913
self,
title: typing.Optional[str] = None,
text: typing.Optional[str] = None,
showMore: typing.Optional[str] = None,
level: Qgis.MessageLevel = Qgis.MessageLevel.Info,
duration: int = -1,
) -> None:
...

@typing.no_type_check
def pushMessage(
self,
*args: typing.Union[str, int],
**kwargs: dict[str, typing.Union[str, int]],
) -> None:
"""A mocked method for pushing a message to the bar."""
msg = f"{title}:{text}"
title = kwargs.get("title")
text = kwargs.get("text")
level = kwargs.get("level", Qgis.MessageLevel.Info)

length = len(args)
if length == 1 and not text:
text = args[0]
elif length == 1 and not title:
title = args[0]
elif length > 1 and isinstance(args[1], str):
# title, text, level, ...
title = args[0]
text = args[1]
if length > 2 and isinstance(args[2], int): # noqa: PLR2004
level = args[2]
elif length > 3 and isinstance(args[3], int): # noqa: PLR2004
level = args[3]
elif length > 1 and isinstance(args[1], int):
# text, level, ...
text = args[0]
level = args[1]
elif args and not text:
text = ", ".join(map(str, args))

msg = f"{title or 'no-title'}:{text}"
self.messages[level].append(msg)
3 changes: 1 addition & 2 deletions src/pytest_qgis/pytest_qgis.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@

import pytest
from qgis.core import Qgis, QgsApplication, QgsProject, QgsRectangle, QgsVectorLayer
from qgis.gui import QgisInterface as QgisInterfaceOrig
from qgis.gui import QgsGui, QgsLayerTreeMapCanvasBridge, QgsMapCanvas
from qgis.PyQt import QtCore, QtWidgets, sip
from qgis.PyQt.QtCore import QCoreApplication
Expand Down Expand Up @@ -185,7 +184,7 @@ def qgis_version() -> int:


@pytest.fixture(scope="session")
def qgis_iface() -> QgisInterfaceOrig:
def qgis_iface() -> QgisInterface:
assert _IFACE
return _IFACE

Expand Down
Loading
Loading