Add unit tests for QgsProcessingUtils::mapLayerFromString

This commit is contained in:
Nyall Dawson 2017-05-02 12:28:05 +10:00
parent 11fb72eef7
commit 600518de72
3 changed files with 76 additions and 6 deletions

View File

@ -68,9 +68,11 @@ class QgsProcessingUtils
static QgsMapLayer *mapLayerFromString( const QString &string, QgsProcessingContext &context, bool allowLoadingNewLayers = true );
%Docstring
Interprets a string as a map layer within the supplied ``context``. The method will attempt to
Interprets a string as a map layer within the supplied ``context``.
The method will attempt to
load a layer matching the passed ``string``. E.g. if the string matches a layer ID or name
within the current project this layer will be returned.
within the context's project or temporary layer store then this layer will be returned.
If the string is a file path and ``allowLoadingNewLayers`` is true, then the layer at this
file path will be loaded and added to the context's temporary layer store.
Ownership of the layer remains with the ``context`` or the context's current project.

View File

@ -80,9 +80,11 @@ class CORE_EXPORT QgsProcessingUtils
static QList< QgsMapLayer * > compatibleLayers( QgsProject *project, bool sort = true );
/**
* Interprets a string as a map layer within the supplied \a context. The method will attempt to
* Interprets a string as a map layer within the supplied \a context.
*
* The method will attempt to
* load a layer matching the passed \a string. E.g. if the string matches a layer ID or name
* within the current project this layer will be returned.
* within the context's project or temporary layer store then this layer will be returned.
* If the string is a file path and \a allowLoadingNewLayers is true, then the layer at this
* file path will be loaded and added to the context's temporary layer store.
* Ownership of the layer remains with the \a context or the context's current project.

View File

@ -104,6 +104,7 @@ class TestQgsProcessing: public QObject
void removeProvider();
void compatibleLayers();
void normalizeLayerSource();
void mapLayers();
void mapLayerFromString();
void algorithm();
void features();
@ -331,7 +332,7 @@ void TestQgsProcessing::normalizeLayerSource()
QCOMPARE( QgsProcessingUtils::normalizeLayerSource( "data\\layers \"new\"\\test.shp" ), QString( "data/layers 'new'/test.shp" ) );
}
void TestQgsProcessing::mapLayerFromString()
void TestQgsProcessing::mapLayers()
{
// test mapLayerFromProject
@ -364,7 +365,7 @@ void TestQgsProcessing::mapLayerFromString()
QCOMPARE( QgsProcessingUtils::mapLayerFromProject( r1->id(), &p ), r1 );
QCOMPARE( QgsProcessingUtils::mapLayerFromProject( v1->id(), &p ), v1 );
// test mapLayerFromString
// test loadMapLayerFromString
QgsMapLayer *l = QgsProcessingUtils::loadMapLayerFromString( raster2 );
QVERIFY( l->isValid() );
QCOMPARE( l->type(), QgsMapLayer::RasterLayer );
@ -379,6 +380,71 @@ void TestQgsProcessing::mapLayerFromString()
delete l;
}
void TestQgsProcessing::mapLayerFromString()
{
// test mapLayerFromString
QgsProcessingContext c;
QgsProject p;
// add a bunch of layers to a project
QString testDataDir = QStringLiteral( TEST_DATA_DIR ) + '/'; //defined in CmakeLists.txt
QString raster1 = testDataDir + "tenbytenraster.asc";
QString raster2 = testDataDir + "landsat.tif";
QFileInfo fi1( raster1 );
QgsRasterLayer *r1 = new QgsRasterLayer( fi1.filePath(), "R1" );
QVERIFY( r1->isValid() );
QFileInfo fi2( raster2 );
QgsRasterLayer *r2 = new QgsRasterLayer( fi2.filePath(), "ar2" );
QVERIFY( r2->isValid() );
QgsVectorLayer *v1 = new QgsVectorLayer( "Polygon", "V4", "memory" );
QgsVectorLayer *v2 = new QgsVectorLayer( "Point", "v1", "memory" );
p.addMapLayers( QList<QgsMapLayer *>() << r1 << r2 << v1 << v2 );
// no project set yet
QVERIFY( ! QgsProcessingUtils::mapLayerFromString( QString(), c ) );
QVERIFY( ! QgsProcessingUtils::mapLayerFromString( QStringLiteral( "v1" ), c ) );
c.setProject( &p );
// layers from current project
QVERIFY( ! QgsProcessingUtils::mapLayerFromString( QString(), c ) );
QCOMPARE( QgsProcessingUtils::mapLayerFromString( raster1, c ), r1 );
QCOMPARE( QgsProcessingUtils::mapLayerFromString( raster2, c ), r2 );
QCOMPARE( QgsProcessingUtils::mapLayerFromString( "R1", c ), r1 );
QCOMPARE( QgsProcessingUtils::mapLayerFromString( "ar2", c ), r2 );
QCOMPARE( QgsProcessingUtils::mapLayerFromString( "V4", c ), v1 );
QCOMPARE( QgsProcessingUtils::mapLayerFromString( "v1", c ), v2 );
QCOMPARE( QgsProcessingUtils::mapLayerFromString( r1->id(), c ), r1 );
QCOMPARE( QgsProcessingUtils::mapLayerFromString( v1->id(), c ), v1 );
// check that layers in context temporary store are used
QgsVectorLayer *v5 = new QgsVectorLayer( "Polygon", "V5", "memory" );
QgsVectorLayer *v6 = new QgsVectorLayer( "Point", "v6", "memory" );
c.temporaryLayerStore().addMapLayers( QList<QgsMapLayer *>() << v5 << v6 );
QCOMPARE( QgsProcessingUtils::mapLayerFromString( "V5", c ), v5 );
QCOMPARE( QgsProcessingUtils::mapLayerFromString( "v6", c ), v6 );
QCOMPARE( QgsProcessingUtils::mapLayerFromString( v5->id(), c ), v5 );
QCOMPARE( QgsProcessingUtils::mapLayerFromString( v6->id(), c ), v6 );
QVERIFY( ! QgsProcessingUtils::mapLayerFromString( "aaaaa", c ) );
// if specified, check that layers can be loaded
QVERIFY( ! QgsProcessingUtils::mapLayerFromString( "aaaaa", c ) );
QString newRaster = testDataDir + "requires_warped_vrt.tif";
// don't allow loading
QVERIFY( ! QgsProcessingUtils::mapLayerFromString( newRaster, c, false ) );
// allow loading
QgsMapLayer *loadedLayer = QgsProcessingUtils::mapLayerFromString( newRaster, c, true );
QVERIFY( loadedLayer->isValid() );
QCOMPARE( loadedLayer->type(), QgsMapLayer::RasterLayer );
// should now be in temporary store
QCOMPARE( c.temporaryLayerStore().mapLayer( loadedLayer->id() ), loadedLayer );
// since it's now in temporary store, should be accessible even if we deny loading new layers
QCOMPARE( QgsProcessingUtils::mapLayerFromString( newRaster, c, false ), loadedLayer );
}
void TestQgsProcessing::algorithm()
{
DummyAlgorithm alg( "test" );