Skip to content

Commit

Permalink
QtCore/QtGui: Add testing for aliased methods
Browse files Browse the repository at this point in the history
  • Loading branch information
dalthviz committed Jan 25, 2022
1 parent 74dd309 commit 13718ad
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 11 deletions.
7 changes: 6 additions & 1 deletion .github/workflows/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,12 @@ eval "$(conda shell.bash hook)"
conda remove -q -n test-env --all || true

# Create and activate conda environment for this test
conda create -q -n test-env python=${PYTHON_VERSION} pytest 'pytest-cov>=3.0.0'
if [ "$PYQT5_QT_VERSION" = "5.9" ] || [ "$PYSIDE2_QT_VERSION" = "5.9" ] ; then
conda create -q -n test-env python=${PYTHON_VERSION} pytest 'pytest-cov>=3.0.0' 'pytest-qt==3.3.0'
else
conda create -q -n test-env python=${PYTHON_VERSION} pytest 'pytest-cov>=3.0.0' 'pytest-qt'
fi

conda activate test-env

if [ "$USE_CONDA" = "Yes" ]; then
Expand Down
10 changes: 7 additions & 3 deletions qtpy/QtCore.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
from PyQt6.QtCore import pyqtProperty as Property
from PyQt6.QtCore import QT_VERSION_STR as __version__

# For issue #153
# For issue #153 and updated for issue #305
from PyQt6.QtCore import QDateTime
QDateTime.toPython = lambda self, *args, **kwargs: self.toPyDateTime(*args, **kwargs)

Expand All @@ -37,6 +37,7 @@
QCoreApplication.exec_ = QCoreApplication.exec
QEventLoop.exec_ = lambda self, *args, **kwargs: self.exec(*args, **kwargs)
QThread.exec_ = lambda self, *args, **kwargs: self.exec(*args, **kwargs)
QTextStreamManipulator.exec_ = lambda self, *args, **kwargs: self.exec(*args, **kwargs)

QLibraryInfo.location = QLibraryInfo.path

Expand Down Expand Up @@ -64,9 +65,12 @@
from PyQt5.QtCore import pyqtProperty as Property
from PyQt5.QtCore import QT_VERSION_STR as __version__

# For issue #153
# For issue #153 and updated for issue #305
from PyQt5.QtCore import QDateTime
QDateTime.toPython = QDateTime.toPyDateTime
QDateTime.toPython = lambda self, *args, **kwargs: self.toPyDateTime(*args, **kwargs)

# Map missing methods on PyQt5 5.12
QTextStreamManipulator.exec_ = lambda self, *args, **kwargs: self.exec(*args, **kwargs)

# Those are imported from `import *`
del pyqtSignal, pyqtBoundSignal, pyqtSlot, pyqtProperty, QT_VERSION_STR
Expand Down
4 changes: 4 additions & 0 deletions qtpy/QtGui.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@
from PyQt5.QtGui import *
elif PYSIDE2:
from PySide2.QtGui import *
if hasattr(QFontMetrics, 'horizontalAdvance'):
# Needed to prevent raising a DeprecationWarning when using QFontMetrics.width
QFontMetrics.width = lambda self, *args, **kwargs: self.horizontalAdvance(*args, **kwargs)

elif PYSIDE6:
from PySide6.QtGui import *
from PySide6.QtOpenGL import *
Expand Down
35 changes: 34 additions & 1 deletion qtpy/tests/test_qtcore.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
"""Test QtCore."""

from datetime import datetime
import os
import sys

import pytest

from qtpy import PYQT5, PYQT6, PYSIDE2, PYSIDE6, PYQT_VERSION, PYSIDE_VERSION, QtCore
Expand All @@ -10,9 +14,38 @@ def test_qtmsghandler():
assert QtCore.qInstallMessageHandler is not None


def test_DateTime_toPython():
def test_qdatetime_toPython():
"""Test QDateTime.toPython"""
q_date = QtCore.QDateTime.currentDateTime()
assert QtCore.QDateTime.toPython is not None
py_date = q_date.toPython()
assert isinstance(py_date, datetime)


@pytest.mark.skipif(sys.platform.startswith('linux') and os.environ.get('USE_CONDA', 'No') == 'No',
reason="Fatal Python error: Aborted on Linux CI when not using conda")
def test_qeventloop_exec_(qtbot):
"""Test QEventLoop.exec_"""
assert QtCore.QEventLoop.exec_ is not None
event_loop = QtCore.QEventLoop(None)
QtCore.QTimer.singleShot(1000, event_loop.quit)
event_loop.exec_()


def test_qthread_exec_():
"""Test QThread.exec_"""
assert QtCore.QThread.exec_ is not None


def test_qlibraryinfo_location():
"""Test QLibraryInfo.location"""
assert QtCore.QLibraryInfo.location is not None
assert QtCore.QLibraryInfo.location(QtCore.QLibraryInfo.PrefixPath) is not None


def test_qtextstreammanipulator_exec_():
"""Test QTextStreamManipulator.exec_"""
QtCore.QTextStreamManipulator.exec_ is not None


@pytest.mark.skipif(PYSIDE2 or PYQT6,
Expand Down
33 changes: 29 additions & 4 deletions qtpy/tests/test_qtgui.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,47 @@
"""Test QtGui."""
import os
import sys

import pytest

from qtpy import PYQT5, PYQT_VERSION, QtGui


def test_qdrag_functions():
@pytest.mark.skipif(sys.platform.startswith('linux') and os.environ.get('USE_CONDA', 'No') == 'No',
reason="Fatal Python error: Aborted on Linux CI when not using conda")
def test_qfontmetrics_width(qtbot):
"""Test QFontMetrics width"""
assert QtGui.QFontMetrics.width is not None
font = QtGui.QFont("times", 24)
font_metrics = QtGui.QFontMetrics(font)
width = font_metrics.width("Test")
assert width in range(40, 62)


@pytest.mark.skipif(sys.platform.startswith('linux') and os.environ.get('USE_CONDA', 'No') == 'No',
reason="Fatal Python error: Aborted on Linux CI when not using conda")
def test_qdrag_functions(qtbot):
"""Test functions mapping for QtGui.QDrag."""
assert QtGui.QDrag.exec_
assert QtGui.QDrag.exec_ is not None
drag = QtGui.QDrag(None)
drag.exec_()


def test_qguiapplication_functions():
"""Test functions mapping for QtGui.QGuiApplication."""
assert QtGui.QGuiApplication.exec_
assert QtGui.QGuiApplication.exec_ is not None


@pytest.mark.skipif(sys.platform.startswith('linux') and os.environ.get('USE_CONDA', 'No') == 'No',
reason="Segmentation fault/Aborted on Linux CI when not using conda")
def test_qtextdocument_functions():
"""Test functions mapping for QtGui.QTextDocument."""
assert QtGui.QTextDocument.print_
assert QtGui.QTextDocument.print_ is not None
text_document = QtGui.QTextDocument("Test")
print_device = QtGui.QPdfWriter('test.pdf')
text_document.print_(print_device)
assert os.path.exists('test.pdf')
os.remove('test.pdf')


@pytest.mark.skipif(PYQT5 and PYQT_VERSION.startswith('5.9'),
Expand Down
2 changes: 0 additions & 2 deletions qtpy/tests/test_qtprintsupport.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,3 @@ def test_qtprintsupport():
assert QtPrintSupport.QPrinter is not None
assert QtPrintSupport.QPrinterInfo is not None
assert QtPrintSupport.QPrintPreviewWidget is not None


1 change: 1 addition & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,4 @@ exclude =
test =
pytest>=6.0.0,<7.0
pytest-cov>=3.0.0
pytest-qt

0 comments on commit 13718ad

Please sign in to comment.