Skip to content

Commit 600518d

Browse files
committed
Add unit tests for QgsProcessingUtils::mapLayerFromString
1 parent 11fb72e commit 600518d

File tree

3 files changed

+76
-6
lines changed

3 files changed

+76
-6
lines changed

python/core/processing/qgsprocessingutils.sip

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,11 @@ class QgsProcessingUtils
6868

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

src/core/processing/qgsprocessingutils.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,11 @@ class CORE_EXPORT QgsProcessingUtils
8080
static QList< QgsMapLayer * > compatibleLayers( QgsProject *project, bool sort = true );
8181

8282
/**
83-
* Interprets a string as a map layer within the supplied \a context. The method will attempt to
83+
* Interprets a string as a map layer within the supplied \a context.
84+
*
85+
* The method will attempt to
8486
* load a layer matching the passed \a string. E.g. if the string matches a layer ID or name
85-
* within the current project this layer will be returned.
87+
* within the context's project or temporary layer store then this layer will be returned.
8688
* If the string is a file path and \a allowLoadingNewLayers is true, then the layer at this
8789
* file path will be loaded and added to the context's temporary layer store.
8890
* Ownership of the layer remains with the \a context or the context's current project.

tests/src/core/testqgsprocessing.cpp

Lines changed: 68 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ class TestQgsProcessing: public QObject
104104
void removeProvider();
105105
void compatibleLayers();
106106
void normalizeLayerSource();
107+
void mapLayers();
107108
void mapLayerFromString();
108109
void algorithm();
109110
void features();
@@ -331,7 +332,7 @@ void TestQgsProcessing::normalizeLayerSource()
331332
QCOMPARE( QgsProcessingUtils::normalizeLayerSource( "data\\layers \"new\"\\test.shp" ), QString( "data/layers 'new'/test.shp" ) );
332333
}
333334

334-
void TestQgsProcessing::mapLayerFromString()
335+
void TestQgsProcessing::mapLayers()
335336
{
336337
// test mapLayerFromProject
337338

@@ -364,7 +365,7 @@ void TestQgsProcessing::mapLayerFromString()
364365
QCOMPARE( QgsProcessingUtils::mapLayerFromProject( r1->id(), &p ), r1 );
365366
QCOMPARE( QgsProcessingUtils::mapLayerFromProject( v1->id(), &p ), v1 );
366367

367-
// test mapLayerFromString
368+
// test loadMapLayerFromString
368369
QgsMapLayer *l = QgsProcessingUtils::loadMapLayerFromString( raster2 );
369370
QVERIFY( l->isValid() );
370371
QCOMPARE( l->type(), QgsMapLayer::RasterLayer );
@@ -379,6 +380,71 @@ void TestQgsProcessing::mapLayerFromString()
379380
delete l;
380381
}
381382

383+
void TestQgsProcessing::mapLayerFromString()
384+
{
385+
// test mapLayerFromString
386+
387+
QgsProcessingContext c;
388+
QgsProject p;
389+
390+
// add a bunch of layers to a project
391+
QString testDataDir = QStringLiteral( TEST_DATA_DIR ) + '/'; //defined in CmakeLists.txt
392+
QString raster1 = testDataDir + "tenbytenraster.asc";
393+
QString raster2 = testDataDir + "landsat.tif";
394+
QFileInfo fi1( raster1 );
395+
QgsRasterLayer *r1 = new QgsRasterLayer( fi1.filePath(), "R1" );
396+
QVERIFY( r1->isValid() );
397+
QFileInfo fi2( raster2 );
398+
QgsRasterLayer *r2 = new QgsRasterLayer( fi2.filePath(), "ar2" );
399+
QVERIFY( r2->isValid() );
400+
401+
QgsVectorLayer *v1 = new QgsVectorLayer( "Polygon", "V4", "memory" );
402+
QgsVectorLayer *v2 = new QgsVectorLayer( "Point", "v1", "memory" );
403+
p.addMapLayers( QList<QgsMapLayer *>() << r1 << r2 << v1 << v2 );
404+
405+
// no project set yet
406+
QVERIFY( ! QgsProcessingUtils::mapLayerFromString( QString(), c ) );
407+
QVERIFY( ! QgsProcessingUtils::mapLayerFromString( QStringLiteral( "v1" ), c ) );
408+
409+
c.setProject( &p );
410+
411+
// layers from current project
412+
QVERIFY( ! QgsProcessingUtils::mapLayerFromString( QString(), c ) );
413+
QCOMPARE( QgsProcessingUtils::mapLayerFromString( raster1, c ), r1 );
414+
QCOMPARE( QgsProcessingUtils::mapLayerFromString( raster2, c ), r2 );
415+
QCOMPARE( QgsProcessingUtils::mapLayerFromString( "R1", c ), r1 );
416+
QCOMPARE( QgsProcessingUtils::mapLayerFromString( "ar2", c ), r2 );
417+
QCOMPARE( QgsProcessingUtils::mapLayerFromString( "V4", c ), v1 );
418+
QCOMPARE( QgsProcessingUtils::mapLayerFromString( "v1", c ), v2 );
419+
QCOMPARE( QgsProcessingUtils::mapLayerFromString( r1->id(), c ), r1 );
420+
QCOMPARE( QgsProcessingUtils::mapLayerFromString( v1->id(), c ), v1 );
421+
422+
// check that layers in context temporary store are used
423+
QgsVectorLayer *v5 = new QgsVectorLayer( "Polygon", "V5", "memory" );
424+
QgsVectorLayer *v6 = new QgsVectorLayer( "Point", "v6", "memory" );
425+
c.temporaryLayerStore().addMapLayers( QList<QgsMapLayer *>() << v5 << v6 );
426+
QCOMPARE( QgsProcessingUtils::mapLayerFromString( "V5", c ), v5 );
427+
QCOMPARE( QgsProcessingUtils::mapLayerFromString( "v6", c ), v6 );
428+
QCOMPARE( QgsProcessingUtils::mapLayerFromString( v5->id(), c ), v5 );
429+
QCOMPARE( QgsProcessingUtils::mapLayerFromString( v6->id(), c ), v6 );
430+
QVERIFY( ! QgsProcessingUtils::mapLayerFromString( "aaaaa", c ) );
431+
432+
// if specified, check that layers can be loaded
433+
QVERIFY( ! QgsProcessingUtils::mapLayerFromString( "aaaaa", c ) );
434+
QString newRaster = testDataDir + "requires_warped_vrt.tif";
435+
// don't allow loading
436+
QVERIFY( ! QgsProcessingUtils::mapLayerFromString( newRaster, c, false ) );
437+
// allow loading
438+
QgsMapLayer *loadedLayer = QgsProcessingUtils::mapLayerFromString( newRaster, c, true );
439+
QVERIFY( loadedLayer->isValid() );
440+
QCOMPARE( loadedLayer->type(), QgsMapLayer::RasterLayer );
441+
// should now be in temporary store
442+
QCOMPARE( c.temporaryLayerStore().mapLayer( loadedLayer->id() ), loadedLayer );
443+
444+
// since it's now in temporary store, should be accessible even if we deny loading new layers
445+
QCOMPARE( QgsProcessingUtils::mapLayerFromString( newRaster, c, false ), loadedLayer );
446+
}
447+
382448
void TestQgsProcessing::algorithm()
383449
{
384450
DummyAlgorithm alg( "test" );

0 commit comments

Comments
 (0)