Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 13718ad

Browse files
committed
QtCore/QtGui: Add testing for aliased methods
1 parent 74dd309 commit 13718ad

File tree

7 files changed

+81
-11
lines changed

7 files changed

+81
-11
lines changed

.github/workflows/test.sh

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,12 @@ eval "$(conda shell.bash hook)"
77
conda remove -q -n test-env --all || true
88

99
# Create and activate conda environment for this test
10-
conda create -q -n test-env python=${PYTHON_VERSION} pytest 'pytest-cov>=3.0.0'
10+
if [ "$PYQT5_QT_VERSION" = "5.9" ] || [ "$PYSIDE2_QT_VERSION" = "5.9" ] ; then
11+
conda create -q -n test-env python=${PYTHON_VERSION} pytest 'pytest-cov>=3.0.0' 'pytest-qt==3.3.0'
12+
else
13+
conda create -q -n test-env python=${PYTHON_VERSION} pytest 'pytest-cov>=3.0.0' 'pytest-qt'
14+
fi
15+
1116
conda activate test-env
1217

1318
if [ "$USE_CONDA" = "Yes" ]; then

qtpy/QtCore.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
from PyQt6.QtCore import pyqtProperty as Property
2121
from PyQt6.QtCore import QT_VERSION_STR as __version__
2222

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

@@ -37,6 +37,7 @@
3737
QCoreApplication.exec_ = QCoreApplication.exec
3838
QEventLoop.exec_ = lambda self, *args, **kwargs: self.exec(*args, **kwargs)
3939
QThread.exec_ = lambda self, *args, **kwargs: self.exec(*args, **kwargs)
40+
QTextStreamManipulator.exec_ = lambda self, *args, **kwargs: self.exec(*args, **kwargs)
4041

4142
QLibraryInfo.location = QLibraryInfo.path
4243

@@ -64,9 +65,12 @@
6465
from PyQt5.QtCore import pyqtProperty as Property
6566
from PyQt5.QtCore import QT_VERSION_STR as __version__
6667

67-
# For issue #153
68+
# For issue #153 and updated for issue #305
6869
from PyQt5.QtCore import QDateTime
69-
QDateTime.toPython = QDateTime.toPyDateTime
70+
QDateTime.toPython = lambda self, *args, **kwargs: self.toPyDateTime(*args, **kwargs)
71+
72+
# Map missing methods on PyQt5 5.12
73+
QTextStreamManipulator.exec_ = lambda self, *args, **kwargs: self.exec(*args, **kwargs)
7074

7175
# Those are imported from `import *`
7276
del pyqtSignal, pyqtBoundSignal, pyqtSlot, pyqtProperty, QT_VERSION_STR

qtpy/QtGui.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@
3030
from PyQt5.QtGui import *
3131
elif PYSIDE2:
3232
from PySide2.QtGui import *
33+
if hasattr(QFontMetrics, 'horizontalAdvance'):
34+
# Needed to prevent raising a DeprecationWarning when using QFontMetrics.width
35+
QFontMetrics.width = lambda self, *args, **kwargs: self.horizontalAdvance(*args, **kwargs)
36+
3337
elif PYSIDE6:
3438
from PySide6.QtGui import *
3539
from PySide6.QtOpenGL import *

qtpy/tests/test_qtcore.py

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
"""Test QtCore."""
22

3+
from datetime import datetime
4+
import os
5+
import sys
6+
37
import pytest
48

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

1216

13-
def test_DateTime_toPython():
17+
def test_qdatetime_toPython():
1418
"""Test QDateTime.toPython"""
19+
q_date = QtCore.QDateTime.currentDateTime()
1520
assert QtCore.QDateTime.toPython is not None
21+
py_date = q_date.toPython()
22+
assert isinstance(py_date, datetime)
23+
24+
25+
@pytest.mark.skipif(sys.platform.startswith('linux') and os.environ.get('USE_CONDA', 'No') == 'No',
26+
reason="Fatal Python error: Aborted on Linux CI when not using conda")
27+
def test_qeventloop_exec_(qtbot):
28+
"""Test QEventLoop.exec_"""
29+
assert QtCore.QEventLoop.exec_ is not None
30+
event_loop = QtCore.QEventLoop(None)
31+
QtCore.QTimer.singleShot(1000, event_loop.quit)
32+
event_loop.exec_()
33+
34+
35+
def test_qthread_exec_():
36+
"""Test QThread.exec_"""
37+
assert QtCore.QThread.exec_ is not None
38+
39+
40+
def test_qlibraryinfo_location():
41+
"""Test QLibraryInfo.location"""
42+
assert QtCore.QLibraryInfo.location is not None
43+
assert QtCore.QLibraryInfo.location(QtCore.QLibraryInfo.PrefixPath) is not None
44+
45+
46+
def test_qtextstreammanipulator_exec_():
47+
"""Test QTextStreamManipulator.exec_"""
48+
QtCore.QTextStreamManipulator.exec_ is not None
1649

1750

1851
@pytest.mark.skipif(PYSIDE2 or PYQT6,

qtpy/tests/test_qtgui.py

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,47 @@
11
"""Test QtGui."""
2+
import os
3+
import sys
4+
25
import pytest
36

47
from qtpy import PYQT5, PYQT_VERSION, QtGui
58

69

7-
def test_qdrag_functions():
10+
@pytest.mark.skipif(sys.platform.startswith('linux') and os.environ.get('USE_CONDA', 'No') == 'No',
11+
reason="Fatal Python error: Aborted on Linux CI when not using conda")
12+
def test_qfontmetrics_width(qtbot):
13+
"""Test QFontMetrics width"""
14+
assert QtGui.QFontMetrics.width is not None
15+
font = QtGui.QFont("times", 24)
16+
font_metrics = QtGui.QFontMetrics(font)
17+
width = font_metrics.width("Test")
18+
assert width in range(40, 62)
19+
20+
21+
@pytest.mark.skipif(sys.platform.startswith('linux') and os.environ.get('USE_CONDA', 'No') == 'No',
22+
reason="Fatal Python error: Aborted on Linux CI when not using conda")
23+
def test_qdrag_functions(qtbot):
824
"""Test functions mapping for QtGui.QDrag."""
9-
assert QtGui.QDrag.exec_
25+
assert QtGui.QDrag.exec_ is not None
26+
drag = QtGui.QDrag(None)
27+
drag.exec_()
1028

1129

1230
def test_qguiapplication_functions():
1331
"""Test functions mapping for QtGui.QGuiApplication."""
14-
assert QtGui.QGuiApplication.exec_
32+
assert QtGui.QGuiApplication.exec_ is not None
1533

1634

35+
@pytest.mark.skipif(sys.platform.startswith('linux') and os.environ.get('USE_CONDA', 'No') == 'No',
36+
reason="Segmentation fault/Aborted on Linux CI when not using conda")
1737
def test_qtextdocument_functions():
1838
"""Test functions mapping for QtGui.QTextDocument."""
19-
assert QtGui.QTextDocument.print_
39+
assert QtGui.QTextDocument.print_ is not None
40+
text_document = QtGui.QTextDocument("Test")
41+
print_device = QtGui.QPdfWriter('test.pdf')
42+
text_document.print_(print_device)
43+
assert os.path.exists('test.pdf')
44+
os.remove('test.pdf')
2045

2146

2247
@pytest.mark.skipif(PYQT5 and PYQT_VERSION.startswith('5.9'),

qtpy/tests/test_qtprintsupport.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,3 @@ def test_qtprintsupport():
1212
assert QtPrintSupport.QPrinter is not None
1313
assert QtPrintSupport.QPrinterInfo is not None
1414
assert QtPrintSupport.QPrintPreviewWidget is not None
15-
16-

setup.cfg

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,4 @@ exclude =
5656
test =
5757
pytest>=6.0.0,<7.0
5858
pytest-cov>=3.0.0
59+
pytest-qt

0 commit comments

Comments
 (0)