Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Add Python unit test
  • Loading branch information
pblottiere committed May 13, 2020
1 parent 86503e5 commit 3cfe500
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
1 change: 1 addition & 0 deletions tests/src/python/CMakeLists.txt
Expand Up @@ -138,6 +138,7 @@ ADD_PYTHON_TEST(PyQgsLayoutUnitsComboBox test_qgslayoutunitscombobox.py)
ADD_PYTHON_TEST(PyQgsLegendPatchShape test_qgslegendpatchshape.py)
ADD_PYTHON_TEST(PyQgsLegendPatchShapeButton test_qgslegendpatchshapebutton.py)
ADD_PYTHON_TEST(PyQgsLegendPatchShapeWidget test_qgslegendpatchshapewidget.py)
ADD_PYTHON_TEST(PyQgsLegendRenderer test_qgslegendrenderer.py)
ADD_PYTHON_TEST(PyQgsLineSegment test_qgslinesegment.py)
ADD_PYTHON_TEST(PyQgsLineSymbolLayers test_qgslinesymbollayers.py)
ADD_PYTHON_TEST(PyQgsLocalDefaultSettings test_qgslocaldefaultsettings.py)
Expand Down
50 changes: 50 additions & 0 deletions tests/src/python/test_qgslegendrenderer.py
@@ -0,0 +1,50 @@
# coding=utf-8
""""Test QgsLegendRenderer JSON export
.. note:: This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
Run with ctest -V -R PyQgsLegendRenderer
"""

__author__ = 'elpaso@itopen.it'
__date__ = '2020-04-29'
__copyright__ = 'Copyright 2020, ItOpen'

import os

from qgis.core import (
QgsProject,
QgsLegendModel,
QgsLegendSettings,
QgsLegendRenderer,
QgsRenderContext,
)
from qgis.testing import start_app, unittest
from utilities import unitTestDataPath

QGISAPP = start_app()
TEST_DATA_DIR = unitTestDataPath()


class TestPyQgsLegendRenderer(unittest.TestCase):

def test_json_export(self):

project = QgsProject()
self.assertTrue(project.read(os.path.join(unitTestDataPath('qgis_server'), 'test_project.qgs')))
model = QgsLegendModel(project.layerTreeRoot())
ctx = QgsRenderContext()
settings = QgsLegendSettings()
renderer = QgsLegendRenderer(model, settings)
nodes = renderer.exportLegendToJson(ctx)['nodes'].toVariant()
self.assertEqual(len(nodes), 7)
self.assertEqual(nodes[0]['type'], 'layer')
self.assertEqual(nodes[0]['title'], 'testlayer')


if __name__ == '__main__':
unittest.main()

0 comments on commit 3cfe500

Please sign in to comment.