@@ -1334,7 +1334,6 @@ void TestQgsProcessing::createFeatureSink()
1334
1334
QVERIFY ( sink->addFeature ( f ) );
1335
1335
QCOMPARE ( layer->featureCount (), 1L );
1336
1336
context.temporaryLayerStore ()->removeAllMapLayers ();
1337
- layer = nullptr ;
1338
1337
1339
1338
// non memory layer output
1340
1339
destination = QDir::tempPath () + " /create_feature_sink.tab" ;
@@ -1354,8 +1353,6 @@ void TestQgsProcessing::createFeatureSink()
1354
1353
QCOMPARE ( layer->fields ().at ( 0 ).name (), QStringLiteral ( " my_field" ) );
1355
1354
QCOMPARE ( layer->fields ().at ( 0 ).type (), QVariant::String );
1356
1355
QCOMPARE ( layer->featureCount (), 1L );
1357
- delete layer;
1358
- layer = nullptr ;
1359
1356
1360
1357
// no extension, should default to shp
1361
1358
destination = QDir::tempPath () + " /create_feature_sink2" ;
@@ -1364,7 +1361,6 @@ void TestQgsProcessing::createFeatureSink()
1364
1361
QVERIFY ( sink.get () );
1365
1362
f.setGeometry ( QgsGeometry::fromWkt ( QStringLiteral ( " PointZ(1 2 3)" ) ) );
1366
1363
QVERIFY ( sink->addFeature ( f ) );
1367
- QVERIFY ( !layer );
1368
1364
QCOMPARE ( destination, prevDest );
1369
1365
sink.reset ( nullptr );
1370
1366
layer = qobject_cast< QgsVectorLayer *>( QgsProcessingUtils::mapLayerFromString ( destination, context, true ) );
@@ -1375,13 +1371,51 @@ void TestQgsProcessing::createFeatureSink()
1375
1371
QCOMPARE ( layer->fields ().at ( 1 ).name (), QStringLiteral ( " my_field" ) );
1376
1372
QCOMPARE ( layer->fields ().at ( 1 ).type (), QVariant::String );
1377
1373
QCOMPARE ( layer->featureCount (), 1L );
1378
- delete layer;
1379
- layer = nullptr ;
1380
1374
1381
1375
// windows style path
1382
1376
destination = " d:\\ temp\\ create_feature_sink.tab" ;
1383
1377
sink.reset ( QgsProcessingUtils::createFeatureSink ( destination, context, fields, QgsWkbTypes::Polygon, QgsCoordinateReferenceSystem::fromEpsgId ( 3111 ) ) );
1384
1378
QVERIFY ( sink.get () );
1379
+
1380
+ // save to geopackage
1381
+ QString geopackagePath = QDir::tempPath () + " /packaged.gpkg" ;
1382
+ if ( QFileInfo::exists ( geopackagePath ) )
1383
+ QFile::remove ( geopackagePath );
1384
+ destination = QStringLiteral ( " ogr:dbname='%1' table=\" polygons\" (geom) sql=" ).arg ( geopackagePath );
1385
+ sink.reset ( QgsProcessingUtils::createFeatureSink ( destination, context, fields, QgsWkbTypes::Polygon, QgsCoordinateReferenceSystem::fromEpsgId ( 3111 ) ) );
1386
+ QVERIFY ( sink.get () );
1387
+ f = QgsFeature ( fields );
1388
+ f.setGeometry ( QgsGeometry::fromWkt ( QStringLiteral ( " Polygon((0 0, 0 1, 1 1, 1 0, 0 0 ))" ) ) );
1389
+ f.setAttributes ( QgsAttributes () << " val" );
1390
+ QVERIFY ( sink->addFeature ( f ) );
1391
+ sink.reset ( nullptr );
1392
+ layer = qobject_cast< QgsVectorLayer *>( QgsProcessingUtils::mapLayerFromString ( destination, context, true ) );
1393
+ QVERIFY ( layer->isValid () );
1394
+ QCOMPARE ( layer->wkbType (), QgsWkbTypes::Polygon );
1395
+ QVERIFY ( layer->getFeatures ().nextFeature ( f ) );
1396
+ QCOMPARE ( f.attribute ( " my_field" ).toString (), QStringLiteral ( " val" ) );
1397
+
1398
+ // add another output to the same geopackage
1399
+ QString destination2 = QStringLiteral ( " ogr:dbname='%1' table=\" points\" (geom) sql=" ).arg ( geopackagePath );
1400
+ sink.reset ( QgsProcessingUtils::createFeatureSink ( destination2, context, fields, QgsWkbTypes::Point, QgsCoordinateReferenceSystem::fromEpsgId ( 3111 ) ) );
1401
+ QVERIFY ( sink.get () );
1402
+ f = QgsFeature ( fields );
1403
+ f.setGeometry ( QgsGeometry::fromWkt ( QStringLiteral ( " Point(0 0)" ) ) );
1404
+ f.setAttributes ( QgsAttributes () << " val2" );
1405
+ QVERIFY ( sink->addFeature ( f ) );
1406
+ sink.reset ( nullptr );
1407
+ layer = qobject_cast< QgsVectorLayer *>( QgsProcessingUtils::mapLayerFromString ( destination2, context, true ) );
1408
+ QVERIFY ( layer->isValid () );
1409
+ QCOMPARE ( layer->wkbType (), QgsWkbTypes::Point );
1410
+ QVERIFY ( layer->getFeatures ().nextFeature ( f ) );
1411
+ QCOMPARE ( f.attribute ( " my_field" ).toString (), QStringLiteral ( " val2" ) );
1412
+
1413
+ // original polygon layer should remain
1414
+ layer = qobject_cast< QgsVectorLayer *>( QgsProcessingUtils::mapLayerFromString ( destination, context, true ) );
1415
+ QVERIFY ( layer->isValid () );
1416
+ QCOMPARE ( layer->wkbType (), QgsWkbTypes::Polygon );
1417
+ QVERIFY ( layer->getFeatures ().nextFeature ( f ) );
1418
+ QCOMPARE ( f.attribute ( " my_field" ).toString (), QStringLiteral ( " val" ) );
1385
1419
}
1386
1420
1387
1421
void TestQgsProcessing::parameters ()
0 commit comments