Skip to content

Commit

Permalink
Add some unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Jan 5, 2018
1 parent 171f402 commit 60a28e3
Show file tree
Hide file tree
Showing 4 changed files with 172 additions and 13 deletions.
13 changes: 12 additions & 1 deletion python/core/layout/qgslayoutatlas.sip
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ atlas page.

.. seealso:: :py:func:`setFilenameExpression()`

.. seealso:: :py:func:`filenameExpressionErrorString()`
.. seealso:: :py:func:`currentFilename()`
%End

bool setFilenameExpression( const QString &expression, QString &errorString /Out/ );
Expand All @@ -89,6 +89,17 @@ If an invalid expression is passed, false will be returned and ``errorString``
will be set to the expression error.

.. seealso:: :py:func:`filenameExpression()`

.. seealso:: :py:func:`currentFilename()`
%End

QString currentFilename() const;
%Docstring
Returns the current feature filename.

.. seealso:: :py:func:`filenameExpression()`

.. seealso:: :py:func:`setFilenameExpression()`
%End

QgsVectorLayer *coverageLayer() const;
Expand Down
18 changes: 8 additions & 10 deletions src/core/layout/qgslayoutatlas.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,11 @@ bool QgsLayoutAtlas::setFilenameExpression( const QString &pattern, QString &err
return updateFilenameExpression( errorString );
}

QString QgsLayoutAtlas::currentFilename() const
{
return mCurrentFilename;
}

QgsExpressionContext QgsLayoutAtlas::createExpressionContext()
{
QgsExpressionContext expressionContext;
Expand All @@ -408,10 +413,9 @@ QgsExpressionContext QgsLayoutAtlas::createExpressionContext()

if ( mCoverageLayer )
expressionContext.lastScope()->setFields( mCoverageLayer->fields() );
#if 0 //TODO

if ( mLayout && mEnabled )
expressionContext.lastScope()->setFeature( mCurrentFeature );
#endif

return expressionContext;
}
Expand Down Expand Up @@ -440,13 +444,8 @@ bool QgsLayoutAtlas::updateFilenameExpression( QString &error )
mFilenameExpression.prepare( &expressionContext );
}

#if 0 //TODO
//if atlas preview is currently enabled, regenerate filename for current feature
if ( mComposition->atlasMode() == QgsComposition::PreviewAtlas )
{
evalFeatureFilename( expressionContext );
}
#endif
// regenerate current filename
evalFeatureFilename( expressionContext );
return true;
}

Expand Down Expand Up @@ -496,7 +495,6 @@ bool QgsLayoutAtlas::prepareForFeature( const int featureI )
// generate filename for current feature
if ( !evalFeatureFilename( expressionContext ) )
{

//error evaluating filename
return false;
}
Expand Down
10 changes: 9 additions & 1 deletion src/core/layout/qgslayoutatlas.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ class CORE_EXPORT QgsLayoutAtlas : public QObject, public QgsAbstractLayoutItera
* Returns the filename expression used for generating output filenames for each
* atlas page.
* \see setFilenameExpression()
* \see filenameExpressionErrorString()
* \see currentFilename()
*/
QString filenameExpression() const { return mFilenameExpressionString; }

Expand All @@ -87,9 +87,17 @@ class CORE_EXPORT QgsLayoutAtlas : public QObject, public QgsAbstractLayoutItera
* If an invalid expression is passed, false will be returned and \a errorString
* will be set to the expression error.
* \see filenameExpression()
* \see currentFilename()
*/
bool setFilenameExpression( const QString &expression, QString &errorString SIP_OUT );

/**
* Returns the current feature filename.
* \see filenameExpression()
* \see setFilenameExpression()
*/
QString currentFilename() const;

/**
* Returns the coverage layer used for the atlas features.
* \see setCoverageLayer()
Expand Down
144 changes: 143 additions & 1 deletion tests/src/python/test_qgslayoutatlas.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
from qgis.PyQt.QtXml import QDomDocument
from utilities import unitTestDataPath
from qgis.testing import start_app, unittest
from qgis.PyQt.QtTest import QSignalSpy

start_app()

Expand Down Expand Up @@ -86,7 +87,148 @@ def testReadWriteXml(self):
self.assertTrue(atlas2.filterFeatures())
self.assertEqual(atlas2.filterExpression(), 'filter exp')

def test
def testIteration(self):
p = QgsProject()
vectorFileInfo = QFileInfo(unitTestDataPath() + "/france_parts.shp")
vector_layer = QgsVectorLayer(vectorFileInfo.filePath(), vectorFileInfo.completeBaseName(), "ogr")
self.assertTrue(vector_layer.isValid())
p.addMapLayer(vector_layer)

l = QgsPrintLayout(p)
atlas = l.atlas()
atlas.setEnabled(True)
atlas.setCoverageLayer(vector_layer)

atlas_feature_changed_spy = QSignalSpy(atlas.featureChanged)
context_changed_spy = QSignalSpy(l.context().changed)

self.assertTrue(atlas.beginRender())
self.assertTrue(atlas.first())
self.assertEqual(len(atlas_feature_changed_spy), 1)
self.assertEqual(len(context_changed_spy), 1)
self.assertEqual(atlas.currentFeatureNumber(), 0)
self.assertEqual(l.context().feature()[4], 'Basse-Normandie')
self.assertEqual(l.context().layer(), vector_layer)

self.assertTrue(atlas.next())
self.assertEqual(len(atlas_feature_changed_spy), 2)
self.assertEqual(len(context_changed_spy), 2)
self.assertEqual(atlas.currentFeatureNumber(), 1)
self.assertEqual(l.context().feature()[4], 'Bretagne')

self.assertTrue(atlas.next())
self.assertEqual(len(atlas_feature_changed_spy), 3)
self.assertEqual(len(context_changed_spy), 3)
self.assertEqual(atlas.currentFeatureNumber(), 2)
self.assertEqual(l.context().feature()[4], 'Pays de la Loire')

self.assertTrue(atlas.next())
self.assertEqual(len(atlas_feature_changed_spy), 4)
self.assertEqual(len(context_changed_spy), 4)
self.assertEqual(atlas.currentFeatureNumber(), 3)
self.assertEqual(l.context().feature()[4], 'Centre')

self.assertFalse(atlas.next())
self.assertTrue(atlas.seekTo(2))
self.assertEqual(len(atlas_feature_changed_spy), 5)
self.assertEqual(len(context_changed_spy), 5)
self.assertEqual(atlas.currentFeatureNumber(), 2)
self.assertEqual(l.context().feature()[4], 'Pays de la Loire')

self.assertTrue(atlas.last())
self.assertEqual(len(atlas_feature_changed_spy), 6)
self.assertEqual(len(context_changed_spy), 6)
self.assertEqual(atlas.currentFeatureNumber(), 3)
self.assertEqual(l.context().feature()[4], 'Centre')

self.assertTrue(atlas.previous())
self.assertEqual(len(atlas_feature_changed_spy), 7)
self.assertEqual(len(context_changed_spy), 7)
self.assertEqual(atlas.currentFeatureNumber(), 2)
self.assertEqual(l.context().feature()[4], 'Pays de la Loire')

self.assertTrue(atlas.previous())
self.assertTrue(atlas.previous())
self.assertEqual(len(atlas_feature_changed_spy), 9)
self.assertFalse(atlas.previous())
self.assertEqual(len(atlas_feature_changed_spy), 9)

self.assertTrue(atlas.endRender())
self.assertEqual(len(atlas_feature_changed_spy), 10)

def testUpdateFeature(self):
p = QgsProject()
vectorFileInfo = QFileInfo(unitTestDataPath() + "/france_parts.shp")
vector_layer = QgsVectorLayer(vectorFileInfo.filePath(), vectorFileInfo.completeBaseName(), "ogr")
self.assertTrue(vector_layer.isValid())
p.addMapLayer(vector_layer)

l = QgsPrintLayout(p)
atlas = l.atlas()
atlas.setEnabled(True)
atlas.setCoverageLayer(vector_layer)

self.assertTrue(atlas.beginRender())
self.assertTrue(atlas.first())
self.assertEqual(atlas.currentFeatureNumber(), 0)
self.assertEqual(l.context().feature()[4], 'Basse-Normandie')
self.assertEqual(l.context().layer(), vector_layer)

vector_layer.startEditing()
self.assertTrue(vector_layer.changeAttributeValue(l.context().feature().id(), 4, 'Nah, Canberra mate!'))
self.assertEqual(l.context().feature()[4], 'Basse-Normandie')
l.atlas().refreshCurrentFeature()
self.assertEqual(l.context().feature()[4], 'Nah, Canberra mate!')
vector_layer.rollBack()

def testFileName(self):
p = QgsProject()
vectorFileInfo = QFileInfo(unitTestDataPath() + "/france_parts.shp")
vector_layer = QgsVectorLayer(vectorFileInfo.filePath(), vectorFileInfo.completeBaseName(), "ogr")
self.assertTrue(vector_layer.isValid())
p.addMapLayer(vector_layer)

l = QgsPrintLayout(p)
atlas = l.atlas()
atlas.setEnabled(True)
atlas.setCoverageLayer(vector_layer)
atlas.setFilenameExpression("'output_' || \"NAME_1\"")

self.assertTrue(atlas.beginRender())
self.assertEqual(atlas.count(), 4)
atlas.first()
self.assertEqual(atlas.currentFilename(), 'output_Basse-Normandie')
atlas.next()
self.assertEqual(atlas.currentFilename(), 'output_Bretagne')
atlas.next()
self.assertEqual(atlas.currentFilename(), 'output_Pays de la Loire')
atlas.next()
self.assertEqual(atlas.currentFilename(), 'output_Centre')

# try changing expression, filename should be updated instantly
atlas.setFilenameExpression("'export_' || \"NAME_1\"")
self.assertEqual(atlas.currentFilename(), 'export_Centre')

atlas.endRender()

def testNameForPage(self):
p = QgsProject()
vectorFileInfo = QFileInfo(unitTestDataPath() + "/france_parts.shp")
vector_layer = QgsVectorLayer(vectorFileInfo.filePath(), vectorFileInfo.completeBaseName(), "ogr")
self.assertTrue(vector_layer.isValid())
p.addMapLayer(vector_layer)

l = QgsPrintLayout(p)
atlas = l.atlas()
atlas.setEnabled(True)
atlas.setCoverageLayer(vector_layer)
atlas.setPageNameExpression("\"NAME_1\"")

self.assertTrue(atlas.beginRender())
self.assertEqual(atlas.nameForPage(0), 'Basse-Normandie')
self.assertEqual(atlas.nameForPage(1), 'Bretagne')
self.assertEqual(atlas.nameForPage(2), 'Pays de la Loire')
self.assertEqual(atlas.nameForPage(3), 'Centre')


if __name__ == '__main__':
Expand Down

0 comments on commit 60a28e3

Please sign in to comment.