From 7ab116cbc0a1ca53019dde354e40598078399169 Mon Sep 17 00:00:00 2001 From: "Benjamin Jakimow benjamin.jakimow@geo.hu-berlin.de" <benjamin.jakimow@geo.hu-berlin.de> Date: Thu, 23 Jan 2020 10:54:22 +0100 Subject: [PATCH] labeling.py - added type hint mapcanvas.py - hardened MapCanvas.contextMenu() modified tests Signed-off-by: Benjamin Jakimow benjamin.jakimow@geo.hu-berlin.de <benjamin.jakimow@geo.hu-berlin.de> --- eotimeseriesviewer/labeling.py | 2 +- eotimeseriesviewer/mapcanvas.py | 9 +++- runtests.bat | 37 +++++++------- runtests.sh | 30 +++++++++++ tests/test_dummy.py | 15 ++++++ tests/test_inmemorydata.py | 11 ++-- tests/test_labeling.py | 91 +++++++++++++++++++++++---------- 7 files changed, 140 insertions(+), 55 deletions(-) create mode 100644 runtests.sh create mode 100644 tests/test_dummy.py diff --git a/eotimeseriesviewer/labeling.py b/eotimeseriesviewer/labeling.py index ce2cd874..f89568d6 100644 --- a/eotimeseriesviewer/labeling.py +++ b/eotimeseriesviewer/labeling.py @@ -116,7 +116,7 @@ def labelShortcutLayerClassificationSchemes(layer:QgsVectorLayer): return classSchemes -def quickLabelLayers()->list: +def quickLabelLayers()->typing.List[QgsVectorLayer]: """ Returns a list of known QgsVectorLayers with at least one LabelShortcutEditWidget :return: [list-of-QgsVectorLayer] diff --git a/eotimeseriesviewer/mapcanvas.py b/eotimeseriesviewer/mapcanvas.py index ecdf440d..f50bb99d 100644 --- a/eotimeseriesviewer/mapcanvas.py +++ b/eotimeseriesviewer/mapcanvas.py @@ -944,8 +944,13 @@ class MapCanvas(QgsMapCanvas): a.triggered.connect(lambda *args, lyr=refRasterLayer: pasteStyleToClipboard(lyr)) a = menu.addAction('Paste Style') - a.setEnabled(b) - a.setEnabled('application/qgis.style' in QApplication.clipboard().mimeData().formats()) + + + a.setEnabled(False) + clipBoardMime = QApplication.clipboard().mimeData() + if isinstance(clipBoardMime, QMimeData) and 'application/qgis.style' in clipBoardMime.formats(): + a.setEnabled(True) + a.triggered.connect(lambda *args, lyr=refRasterLayer: self.onPasteStyleFromClipboard(lyr)) menu.addSeparator() diff --git a/runtests.bat b/runtests.bat index 505312f5..3d2c3ee3 100644 --- a/runtests.bat +++ b/runtests.bat @@ -5,21 +5,22 @@ set CI=True python3 make/setuprepository.py mkdir test-reports\today -python3 -m nose2 -s tests test_fileFormatLoading & move nose2-junit.xml test-reports/today/test_fileFormatLoading.xml -python3 -m nose2 -s tests test_init & move nose2-junit.xml test-reports/today/test_init.xml -python3 -m nose2 -s tests test_inmemorydata & move nose2-junit.xml test-reports/today/test_inmemorydata.xml -python3 -m nose2 -s tests test_labeling & move nose2-junit.xml test-reports/today/test_labeling.xml -python3 -m nose2 -s tests test_layerproperties & move nose2-junit.xml test-reports/today/test_layerproperties.xml -python3 -m nose2 -s tests test_main & move nose2-junit.xml test-reports/today/test_main.xml -python3 -m nose2 -s tests test_mapcanvas & move nose2-junit.xml test-reports/today/test_mapcanvas.xml -python3 -m nose2 -s tests test_maptools & move nose2-junit.xml test-reports/today/test_maptools.xml -python3 -m nose2 -s tests test_mapvisualization & move nose2-junit.xml test-reports/today/test_mapvisualization.xml -python3 -m nose2 -s tests test_qgis_environment & move nose2-junit.xml test-reports/today/test_qgis_environment.xml -python3 -m nose2 -s tests test_qgis_interaction & move nose2-junit.xml test-reports/today/test_qgis_interaction.xml -python3 -m nose2 -s tests test_resources & move nose2-junit.xml test-reports/today/test_resources.xml -python3 -m nose2 -s tests test_sensorvisualization & move nose2-junit.xml test-reports/today/test_sensorvisualization.xml -python3 -m nose2 -s tests test_settings & move nose2-junit.xml test-reports/today/test_settings.xml -python3 -m nose2 -s tests test_stackedbandinput & move nose2-junit.xml test-reports/today/test_stackedbandinput.xml -python3 -m nose2 -s tests test_temporalprofiles & move nose2-junit.xml test-reports/today/test_temporalprofiles.xml -python3 -m nose2 -s tests test_timeseries & move nose2-junit.xml test-reports/today/test_timeseries.xml -python3 -m nose2 -s tests test_utils & move nose2-junit.xml test-reports/today/test_utils.xml \ No newline at end of file +python -m nose2 -s tests test_dummy & move nose2-junit.xml test-reports/today/test_dummy.xml +python -m nose2 -s tests test_fileFormatLoading & move nose2-junit.xml test-reports/today/test_fileFormatLoading.xml +python -m nose2 -s tests test_init & move nose2-junit.xml test-reports/today/test_init.xml +python -m nose2 -s tests test_inmemorydata & move nose2-junit.xml test-reports/today/test_inmemorydata.xml +python -m nose2 -s tests test_labeling & move nose2-junit.xml test-reports/today/test_labeling.xml +python -m nose2 -s tests test_layerproperties & move nose2-junit.xml test-reports/today/test_layerproperties.xml +python -m nose2 -s tests test_main & move nose2-junit.xml test-reports/today/test_main.xml +python -m nose2 -s tests test_mapcanvas & move nose2-junit.xml test-reports/today/test_mapcanvas.xml +python -m nose2 -s tests test_maptools & move nose2-junit.xml test-reports/today/test_maptools.xml +python -m nose2 -s tests test_mapvisualization & move nose2-junit.xml test-reports/today/test_mapvisualization.xml +python -m nose2 -s tests test_qgis_environment & move nose2-junit.xml test-reports/today/test_qgis_environment.xml +python -m nose2 -s tests test_qgis_interaction & move nose2-junit.xml test-reports/today/test_qgis_interaction.xml +python -m nose2 -s tests test_resources & move nose2-junit.xml test-reports/today/test_resources.xml +python -m nose2 -s tests test_sensorvisualization & move nose2-junit.xml test-reports/today/test_sensorvisualization.xml +python -m nose2 -s tests test_settings & move nose2-junit.xml test-reports/today/test_settings.xml +python -m nose2 -s tests test_stackedbandinput & move nose2-junit.xml test-reports/today/test_stackedbandinput.xml +python -m nose2 -s tests test_temporalprofiles & move nose2-junit.xml test-reports/today/test_temporalprofiles.xml +python -m nose2 -s tests test_timeseries & move nose2-junit.xml test-reports/today/test_timeseries.xml +python -m nose2 -s tests test_utils & move nose2-junit.xml test-reports/today/test_utils.xml \ No newline at end of file diff --git a/runtests.sh b/runtests.sh new file mode 100644 index 00000000..9ab51e3d --- /dev/null +++ b/runtests.sh @@ -0,0 +1,30 @@ +#!/bin/bash +# use this script to run unit tests locally +# +QT_QPA_PLATFORM=offscreen +export QT_QPA_PLATFORM +CI=True +export CI + +python3 make/setuprepository.py + +mkdir test-reports/today +python3 -m nose2 -s tests test_dummy #| mv nose2-junit.xml test-reports/today/test_dummy.xml +python3 -m nose2 -s tests test_fileFormatLoading #| mv nose2-junit.xml test-reports/today/test_fileFormatLoading.xml +python3 -m nose2 -s tests test_init #| mv nose2-junit.xml test-reports/today/test_init.xml +python3 -m nose2 -s tests test_inmemorydata #| mv nose2-junit.xml test-reports/today/test_inmemorydata.xml +python3 -m nose2 -s tests test_labeling #| mv nose2-junit.xml test-reports/today/test_labeling.xml +python3 -m nose2 -s tests test_layerproperties #| mv nose2-junit.xml test-reports/today/test_layerproperties.xml +python3 -m nose2 -s tests test_main #| mv nose2-junit.xml test-reports/today/test_main.xml +python3 -m nose2 -s tests test_mapcanvas #| mv nose2-junit.xml test-reports/today/test_mapcanvas.xml +python3 -m nose2 -s tests test_maptools #| mv nose2-junit.xml test-reports/today/test_maptools.xml +python3 -m nose2 -s tests test_mapvisualization #| mv nose2-junit.xml test-reports/today/test_mapvisualization.xml +python3 -m nose2 -s tests test_qgis_environment #| mv nose2-junit.xml test-reports/today/test_qgis_environment.xml +python3 -m nose2 -s tests test_qgis_interaction #| mv nose2-junit.xml test-reports/today/test_qgis_interaction.xml +python3 -m nose2 -s tests test_resources #| mv nose2-junit.xml test-reports/today/test_resources.xml +python3 -m nose2 -s tests test_sensorvisualization #| mv nose2-junit.xml test-reports/today/test_sensorvisualization.xml +python3 -m nose2 -s tests test_settings #| mv nose2-junit.xml test-reports/today/test_settings.xml +python3 -m nose2 -s tests test_stackedbandinput #| mv nose2-junit.xml test-reports/today/test_stackedbandinput.xml +python3 -m nose2 -s tests test_temporalprofiles #| mv nose2-junit.xml test-reports/today/test_temporalprofiles.xml +python3 -m nose2 -s tests test_timeseries #| mv nose2-junit.xml test-reports/today/test_timeseries.xml +python3 -m nose2 -s tests test_utils #| mv nose2-junit.xml test-reports/today/test_utils.xml \ No newline at end of file diff --git a/tests/test_dummy.py b/tests/test_dummy.py new file mode 100644 index 00000000..b992b7c1 --- /dev/null +++ b/tests/test_dummy.py @@ -0,0 +1,15 @@ +import unittest + +from qgis.testing import start_app, stop_app, TestCase + +app = start_app(True) + +class MyTestCase(TestCase): + def test_something(self): + self.assertEqual(True, True) + + +if __name__ == '__main__': + unittest.main() + +#stop_app() \ No newline at end of file diff --git a/tests/test_inmemorydata.py b/tests/test_inmemorydata.py index 74fdeb5c..322a77b2 100644 --- a/tests/test_inmemorydata.py +++ b/tests/test_inmemorydata.py @@ -20,12 +20,13 @@ import os, sys, unittest, configparser -from eotimeseriesviewer.tests import initQgisApplication from PyQt5.QtGui import * from PyQt5.QtCore import * import unittest, tempfile - -QGIS_APP = initQgisApplication() +from qgis.testing import start_app, stop_app +print('START TEST {}'.format(__file__)) +QGIS_APP = start_app(True) +print('QGIS STARTED {}'.format(__file__)) class TestInMemoryData(unittest.TestCase): """ @@ -87,9 +88,7 @@ class TestInMemoryData(unittest.TestCase): if __name__ == '__main__': + print('start tests') unittest.main() print('all tests done in '.format(__file__)) - - -QGIS_APP.quit() \ No newline at end of file diff --git a/tests/test_labeling.py b/tests/test_labeling.py index f9af2454..c46ee9a0 100644 --- a/tests/test_labeling.py +++ b/tests/test_labeling.py @@ -18,22 +18,44 @@ """ from eotimeseriesviewer.tests import initQgisApplication, testRasterFiles -import unittest, tempfile +import unittest, tempfile, os +import qgis.testing + from eotimeseriesviewer.labeling import * from eotimeseriesviewer import DIR_REPO from eotimeseriesviewer.mapcanvas import MapCanvas from eotimeseriesviewer.tests import TestObjects from eotimeseriesviewer.mapvisualization import MapView -resourceDir = os.path.join(DIR_REPO, 'qgisresources') -QGIS_APP = initQgisApplication(qgisResourceDir=resourceDir) + +from osgeo import ogr +assert ogr.GetDriverCount() > 0 + SHOW_GUI = False and os.environ.get('CI') is None + reg = QgsGui.editorWidgetRegistry() if len(reg.factories()) == 0: reg.initEditors() -class testclassLabelingTest(unittest.TestCase): +class testclassLabelingTest(qgis.testing.TestCase): + + @classmethod + def setUpClass(cls): + print('## setUpClass') + app = qgis.testing.start_app(cleanup=True) + import eotimeseriesviewer.labeling + print('## setUpClass - cleanup') + for store in eotimeseriesviewer.MAP_LAYER_STORES: + store.removeAllMapLayers() + print('## setUpClass - done') + @classmethod + def tearDownClass(cls): + app = QgsApplication.instance() + if isinstance(app, QgsApplication): + pass + + def createVectorLayer(self)->QgsVectorLayer: @@ -55,7 +77,7 @@ class testclassLabelingTest(unittest.TestCase): return lyr def test_menu(self): - + print('## test_menu') ts = TestObjects.createTimeSeries() mv = MapView() @@ -78,7 +100,7 @@ class testclassLabelingTest(unittest.TestCase): canvas = MapCanvas() canvas.setTSD(tsd) canvas.setMapView(mv) - pos = QPoint(canvas.width()*0.5, canvas.height()*0.5) + pos = QPoint(int(canvas.width()*0.5), int(canvas.height()*0.5)) menu = canvas.contextMenu(pos) self.assertIsInstance(menu, QMenu) @@ -94,7 +116,7 @@ class testclassLabelingTest(unittest.TestCase): menu.exec_() def test_shortcuts(self): - + print('## test_shortcuts') vl = self.createVectorLayer() fields = vl.fields() @@ -120,7 +142,7 @@ class testclassLabelingTest(unittest.TestCase): def test_LabelShortcutEditorConfigWidget(self): - + print('## test_LabelShortcutEditorConfigWidget') vl = self.createVectorLayer() self.assertIsInstance(vl, QgsVectorLayer) @@ -136,7 +158,8 @@ class testclassLabelingTest(unittest.TestCase): self.assertIsInstance(w, LabelShortcutEditorConfigWidget) reg = QgsGui.editorWidgetRegistry() - reg.initEditors() + if len(reg.factories()) == 0: + reg.initEditors() registerLabelShortcutEditorWidget() classScheme1, classScheme2 = self.setupEditWidgets(vl) @@ -172,7 +195,7 @@ class testclassLabelingTest(unittest.TestCase): parent.layout().addWidget(dv) parent.layout().addWidget(panel) - parent.show() + #randomly click into table cells vl.startEditing() @@ -181,30 +204,36 @@ class testclassLabelingTest(unittest.TestCase): w = size.width() h = size.height() from random import randint - for i in range(500): + for i in range(5): + print('Test mouse press {}'.format(i+1)) x = randint(0, w-1) y = randint(0, h-1) - localPos = QPointF(x,y) + localPos = QPointF(x, y) event = QMouseEvent(QEvent.MouseButtonPress, localPos, Qt.LeftButton, Qt.LeftButton, Qt.NoModifier) dv.mousePressEvent(event) - s = "" + vl.selectByIds([1, 2, 3]) ts = TestObjects.createTimeSeries() tsd = ts[5] - - self.assertTrue(len(quickLabelLayers()) == 0) + if len(quickLabelLayers())> 0: + print('Found QuickLabelLayers:') + for l in quickLabelLayers(): + print('{}={}'.format(l.name(), l.source())) + assert vl not in quickLabelLayers() + n = len(quickLabelLayers()) QgsProject.instance().addMapLayer(vl) - self.assertTrue(len(quickLabelLayers()) == 1) + self.assertTrue(len(quickLabelLayers()) == n + 1) for lyr in quickLabelLayers(): assert isinstance(lyr, QgsVectorLayer) # applyShortcuts(lyr, tsd, [classScheme1[2], classScheme1[1]]) if SHOW_GUI: + parent.show() dv.show() - QGIS_APP.exec_() + QgsApplication.instance().exec_() self.assertTrue(vl.commitChanges()) pass @@ -248,11 +277,12 @@ class testclassLabelingTest(unittest.TestCase): return classScheme1, classScheme2 def test_LabelingDockActions(self): + print('## test_LabelingDockActions') registerLabelShortcutEditorWidget() self.assertTrue(EDITOR_WIDGET_REGISTRY_KEY in reg.factories().keys()) dock = LabelingDock() - dock.show() + self.assertIsInstance(dock, LabelingDock) lyr = self.createVectorLayer() self.assertIsInstance(lyr, QgsVectorLayer) @@ -270,7 +300,6 @@ class testclassLabelingTest(unittest.TestCase): lw.setCurrentVectorSource(lyr2) canvas = QgsMapCanvas() - canvas.show() def setLayers(): @@ -283,11 +312,14 @@ class testclassLabelingTest(unittest.TestCase): lw.sigMapExtentRequested.connect(setLayers) if SHOW_GUI: - QGIS_APP.exec_() + dock.show() + canvas.show() + QgsApplication.instance().exec_() - def test_canvasMenu(self): + def test_canvasMenu(self): + print('## test_canvasMenu') vl = self.createVectorLayer() c1, c2 = self.setupEditWidgets(vl) QgsProject.instance().addMapLayer(vl) @@ -298,20 +330,21 @@ class testclassLabelingTest(unittest.TestCase): ts = TestObjects.createTimeSeries() canvas = MapCanvas() canvas.setTSD(ts[0]) - canvas.show() + if SHOW_GUI: - QGIS_APP.exec_() + canvas.show() + QgsApplication.instance().exec_() def test_LabelingDock(self): - + print('## test_LabelingDock') registerLabelShortcutEditorWidget() self.assertTrue(EDITOR_WIDGET_REGISTRY_KEY in reg.factories().keys()) dock = LabelingDock() - dock.show() + lw = dock.labelingWidget() self.assertIsInstance(dock, LabelingDock) @@ -364,12 +397,14 @@ class testclassLabelingTest(unittest.TestCase): self.assertTrue(lyr.commitChanges()) - + print('FINISHED') if SHOW_GUI: - QGIS_APP.exec_() + dock.show() + QgsApplication.instance().exec_() + if __name__ == "__main__": SHOW_GUI = False and os.environ.get('CI') is None unittest.main() -QGIS_APP.quit() +# QgsApplication.instance().exitQgis() -- GitLab