Skip to content
Permalink
Browse files

Add Python unit tests for atlas rendering

  • Loading branch information
Hugo Mercier
Hugo Mercier committed Oct 2, 2012
1 parent 2aa566d commit ce6ae6ecbc21a53a078a8b0d7f271562c41b1b71
@@ -13,3 +13,5 @@ ADD_PYTHON_TEST(PyQgsComposerHtml test_qgscomposerhtml.py)
ADD_PYTHON_TEST(PyQgsComposition test_qgscomposition.py)
ADD_PYTHON_TEST(PyQgsAnalysis test_qgsanalysis.py)
#ADD_PYTHON_TEST(PyQgsComposerMap test_qgscomposermap.py)
ADD_PYTHON_TEST(PyQgsComposerMapAtlas test_qgscomposermapatlas.py)
ADD_PYTHON_TEST(PyQgsComposerLabel test_qgscomposerlabel.py)
@@ -0,0 +1,113 @@
# -*- coding: utf-8 -*-
'''
test_qgscomposerlabel.py
--------------------------------------
Date : Oct 2012
Copyright : (C) 2012 by Dr. Hugo Mercier
email : hugo dot mercier at oslandia dot com
***************************************************************************
* *
* 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. *
* *
***************************************************************************/
'''
import unittest
from utilities import *
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyQt4.QtXml import *
from qgis.core import *

QGISAPP, CANVAS, IFACE, PARENT = getQgisTestApp()

class TestQgsComposerLabel(unittest.TestCase):

def testCase(self):
TEST_DATA_DIR = unitTestDataPath()
vectorFileInfo = QFileInfo( TEST_DATA_DIR + QDir().separator().toAscii() + "france_parts.shp")
mVectorLayer = QgsVectorLayer( vectorFileInfo.filePath(), vectorFileInfo.completeBaseName(), "ogr" )

QgsMapLayerRegistry.instance().addMapLayer( mVectorLayer )

# create composition with composer map
mMapRenderer = QgsMapRenderer()
layerStringList = QStringList()
layerStringList.append( mVectorLayer.id() )
mMapRenderer.setLayerSet( layerStringList )
mMapRenderer.setProjectionsEnabled( False )

mComposition = QgsComposition( mMapRenderer )
mComposition.setPaperSize( 297, 210 )

mLabel = QgsComposerLabel( mComposition )
mComposition.addComposerLabel( mLabel )

self.evaluation_test( mComposition, mLabel )
self.feature_evaluation_test( mComposition, mLabel, mVectorLayer )
self.page_evaluation_test( mComposition, mLabel, mVectorLayer )

def evaluation_test( self, mComposition, mLabel ):
# $CURRENT_DATE evaluation
mLabel.setText( "__$CURRENT_DATE__" )
assert mLabel.displayText() == ( "__" + QDate.currentDate().toString() + "__" )

# $CURRENT_DATE() evaluation
mLabel.setText( "__$CURRENT_DATE(dd)(ok)__" )
expected = "__" + QDateTime.currentDateTime().toString( "dd" ) + "(ok)__"
assert mLabel.displayText() == expected

# $CURRENT_DATE() evaluation (inside an expression)
mLabel.setText( "__[%$CURRENT_DATE(dd) + 1%](ok)__" )
dd = QDate.currentDate().day()
expected = "__" + QString( "%1" ).arg(dd+1) + "(ok)__"
assert mLabel.displayText() == expected

# expression evaluation (without associated feature)
mLabel.setText( "__[%\"NAME_1\"%][%21*2%]__" )
assert mLabel.displayText() == "__[NAME_1]42__"

def feature_evaluation_test( self, mComposition, mLabel, mVectorLayer ):
provider = mVectorLayer.dataProvider()

provider.select( provider.attributeIndexes() )
feat = QgsFeature()

provider.nextFeature( feat )
mLabel.setExpressionContext( feat, mVectorLayer )
mLabel.setText( "[%\"NAME_1\"||'_ok'%]")
assert mLabel.displayText() == "Basse-Normandie_ok"

provider.nextFeature( feat )
mLabel.setExpressionContext( feat, mVectorLayer )
assert mLabel.displayText() == "Bretagne_ok"

# evaluation with local variables
locs = { "$test" : "OK" }
mLabel.setExpressionContext( feat, mVectorLayer, locs )
mLabel.setText( "[%\"NAME_1\"||$test%]" )
assert mLabel.displayText() == "BretagneOK"

def page_evaluation_test( self, mComposition, mLabel, mVectorLayer ):
mComposition.setNumPages( 2 )
mLabel.setText( "[%$page||'/'||$numpages%]" )
assert mLabel.displayText() == "1/2"

# move the the second page and re-evaluate
mLabel.setItemPosition( 0, 320 )
assert mLabel.displayText() == "2/2"

# use setSpecialColumn
mLabel.setText( "[%$var1 + 1%]" )
QgsExpression.setSpecialColumn( "$var1", QVariant(41) )
assert mLabel.displayText() == "42"
QgsExpression.setSpecialColumn( "$var1", QVariant(99) )
assert mLabel.displayText() == "100"
QgsExpression.unsetSpecialColumn( "$var1" )
assert mLabel.displayText() == "[%$var1 + 1%]"

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

@@ -0,0 +1,170 @@
# -*- coding: utf-8 -*-
'''
test_qgscomposermapatlas.py
--------------------------------------
Date : Oct 2012
Copyright : (C) 2012 by Dr. Hugo Mercier
email : hugo dot mercier at oslandia dot com
***************************************************************************
* *
* 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. *
* *
***************************************************************************/
'''
import unittest
from utilities import *
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyQt4.QtXml import *
from qgis.core import *
from qgscompositionchecker import QgsCompositionChecker

QGISAPP, CANVAS, IFACE, PARENT = getQgisTestApp()

class TestQgsComposerMapAtlas(unittest.TestCase):

def testCase(self):
TEST_DATA_DIR = unitTestDataPath()
vectorFileInfo = QFileInfo( TEST_DATA_DIR + QDir().separator().toAscii() + "france_parts.shp")
mVectorLayer = QgsVectorLayer( vectorFileInfo.filePath(), vectorFileInfo.completeBaseName(), "ogr" )

QgsMapLayerRegistry.instance().addMapLayer( mVectorLayer )

# create composition with composer map
mMapRenderer = QgsMapRenderer()
layerStringList = QStringList()
layerStringList.append( mVectorLayer.id() )
mMapRenderer.setLayerSet( layerStringList )
mMapRenderer.setProjectionsEnabled( True )

# select epsg:2154
crs = QgsCoordinateReferenceSystem()
crs.createFromSrid( 2154 )
mMapRenderer.setDestinationCrs( crs )

mComposition = QgsComposition( mMapRenderer )
mComposition.setPaperSize( 297, 210 )

# fix the renderer, fill with green
props = { "color": "0,127,0" }
fillSymbol = QgsFillSymbolV2.createSimple( props )
renderer = QgsSingleSymbolRendererV2( fillSymbol )
mVectorLayer.setRendererV2( renderer )

# the atlas map
mAtlasMap = QgsComposerMap( mComposition, 20, 20, 130, 130 )
mAtlasMap.setFrameEnabled( True )
mAtlasMap.setAtlasCoverageLayer( mVectorLayer )
mComposition.addComposerMap( mAtlasMap )
mComposition.setAtlasMap( mAtlasMap )

# an overview
mOverview = QgsComposerMap( mComposition, 180, 20, 50, 50 )
mOverview.setFrameEnabled( True )
mOverview.setOverviewFrameMap( mAtlasMap.id() )
mComposition.addComposerMap( mOverview )
nextent = QgsRectangle( 49670.718, 6415139.086, 699672.519, 7065140.887 )
mOverview.setNewExtent( nextent )

# header label
mLabel1 = QgsComposerLabel( mComposition )
mComposition.addComposerLabel( mLabel1 )
mLabel1.setText( "[% \"NAME_1\" %] area" )
mLabel1.adjustSizeToText()
mLabel1.setItemPosition( 150, 5 )

# feature number label
mLabel2 = QgsComposerLabel( mComposition )
mComposition.addComposerLabel( mLabel2 )
mLabel2.setText( "# [%$feature || ' / ' || $numfeatures%]" )
mLabel2.adjustSizeToText()
mLabel2.setItemPosition( 150, 200 )

self.filename_test( mComposition )
self.autoscale_render_test( mComposition, mLabel1, TEST_DATA_DIR )
self.fixedscale_render_test( mComposition, mLabel1, TEST_DATA_DIR )
self.hidden_render_test( mComposition, mLabel1, TEST_DATA_DIR )

def filename_test( self, mComposition ):
atlasRender = QgsAtlasRendering( mComposition )
atlasRender.begin( "'output_' || $feature" )
for i in range(0, atlasRender.numFeatures()):
atlasRender.prepareForFeature( i )
expected = QString( "output_%1" ).arg(i+1)
print atlasRender.currentFilename()
assert atlasRender.currentFilename() == expected
atlasRender.end()

def autoscale_render_test( self, mComposition, mLabel1, TEST_DATA_DIR ):
atlasMap = mComposition.atlasMap()
atlasMap.setAtlasFixedScale( False )
atlasMap.setAtlasMargin( 0.10 )

atlasRender = QgsAtlasRendering( mComposition )

atlasRender.begin()

for i in range(0, 2):
atlasRender.prepareForFeature( i )
mLabel1.adjustSizeToText()

checker = QgsCompositionChecker()
res = checker.testComposition( "Atlas autoscale test", mComposition, \
QString( TEST_DATA_DIR ) + QDir.separator() + \
"control_images" + QDir.separator() + \
"expected_composermapatlas" + QDir.separator() + \
QString( "autoscale_%1.png" ).arg( i ) )
assert res[0] == True
atlasRender.end()

def fixedscale_render_test( self, mComposition, mLabel1, TEST_DATA_DIR ):
atlasMap = mComposition.atlasMap()
atlasMap.setNewExtent( QgsRectangle( 209838.166, 6528781.020, 610491.166, 6920530.620 ) );
atlasMap.setAtlasFixedScale( True )

atlasRender = QgsAtlasRendering( mComposition )

atlasRender.begin()

for i in range(0, 2):
atlasRender.prepareForFeature( i )
mLabel1.adjustSizeToText()

checker = QgsCompositionChecker()
res = checker.testComposition( "Atlas fixed scale test", mComposition, \
QString( TEST_DATA_DIR ) + QDir.separator() + \
"control_images" + QDir.separator() + \
"expected_composermapatlas" + QDir.separator() + \
QString( "fixedscale_%1.png" ).arg( i ) )
assert res[0] == True
atlasRender.end()

def hidden_render_test( self, mComposition, mLabel1, TEST_DATA_DIR ):
atlasMap = mComposition.atlasMap()
atlasMap.setNewExtent( QgsRectangle( 209838.166, 6528781.020, 610491.166, 6920530.620 ) );
atlasMap.setAtlasFixedScale( True )
atlasMap.setAtlasHideCoverage( True )

atlasRender = QgsAtlasRendering( mComposition )

atlasRender.begin()

for i in range(0, 2):
atlasRender.prepareForFeature( i )
mLabel1.adjustSizeToText()

checker = QgsCompositionChecker()
res = checker.testComposition( "Atlas hidden test", mComposition, \
QString( TEST_DATA_DIR ) + QDir.separator() + \
"control_images" + QDir.separator() + \
"expected_composermapatlas" + QDir.separator() + \
QString( "hiding_%1.png" ).arg( i ) )
assert res[0] == True
atlasRender.end()

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

0 comments on commit ce6ae6e

Please sign in to comment.
You can’t perform that action at this time.