|
19 | 19 | #include "qgsprocessingprovider.h"
|
20 | 20 | #include "qgsprocessingutils.h"
|
21 | 21 | #include "qgsprocessingalgorithm.h"
|
| 22 | +#include "qgsprocessingcontext.h" |
22 | 23 | #include <QObject>
|
23 | 24 | #include <QtTest/QSignalSpy>
|
24 | 25 | #include "qgstest.h"
|
25 | 26 | #include "qgsrasterlayer.h"
|
26 | 27 | #include "qgsproject.h"
|
| 28 | +#include "qgspointv2.h" |
| 29 | +#include "qgsgeometry.h" |
27 | 30 |
|
28 | 31 | class DummyAlgorithm : public QgsProcessingAlgorithm
|
29 | 32 | {
|
@@ -102,6 +105,7 @@ class TestQgsProcessing: public QObject
|
102 | 105 | void normalizeLayerSource();
|
103 | 106 | void mapLayerFromString();
|
104 | 107 | void algorithm();
|
| 108 | + void features(); |
105 | 109 |
|
106 | 110 | private:
|
107 | 111 |
|
@@ -430,5 +434,80 @@ void TestQgsProcessing::algorithm()
|
430 | 434 | QCOMPARE( p3->algorithms().size(), 2 );
|
431 | 435 | }
|
432 | 436 |
|
| 437 | +void TestQgsProcessing::features() |
| 438 | +{ |
| 439 | + QgsVectorLayer *layer = new QgsVectorLayer( "Point", "v1", "memory" ); |
| 440 | + for ( int i = 1; i < 6; ++i ) |
| 441 | + { |
| 442 | + QgsFeature f( i ); |
| 443 | + f.setGeometry( QgsGeometry( new QgsPointV2( 1, 2 ) ) ); |
| 444 | + layer->dataProvider()->addFeatures( QgsFeatureList() << f ); |
| 445 | + } |
| 446 | + |
| 447 | + QgsProcessingContext context; |
| 448 | + // disable check for geometry validity |
| 449 | + context.setFlags( QgsProcessingContext::Flags( 0 ) ); |
| 450 | + |
| 451 | + std::function< QgsFeatureIds( QgsFeatureIterator it ) > getIds = []( QgsFeatureIterator it ) |
| 452 | + { |
| 453 | + QgsFeature f; |
| 454 | + QgsFeatureIds ids; |
| 455 | + while ( it.nextFeature( f ) ) |
| 456 | + { |
| 457 | + ids << f.id(); |
| 458 | + } |
| 459 | + return ids; |
| 460 | + }; |
| 461 | + |
| 462 | + // test with all features |
| 463 | + QgsFeatureIds ids = getIds( QgsProcessingUtils::getFeatures( layer, context ) ); |
| 464 | + QCOMPARE( ids, QgsFeatureIds() << 1 << 2 << 3 << 4 << 5 ); |
| 465 | + QCOMPARE( QgsProcessingUtils::featureCount( layer, context ), 5L ); |
| 466 | + |
| 467 | + // test with selected features |
| 468 | + context.setFlags( QgsProcessingContext::UseSelection ); |
| 469 | + layer->selectByIds( QgsFeatureIds() << 2 << 4 ); |
| 470 | + ids = getIds( QgsProcessingUtils::getFeatures( layer, context ) ); |
| 471 | + QCOMPARE( ids, QgsFeatureIds() << 2 << 4 ); |
| 472 | + QCOMPARE( QgsProcessingUtils::featureCount( layer, context ), 2L ); |
| 473 | + |
| 474 | + // selection, but not using selected features |
| 475 | + context.setFlags( QgsProcessingContext::Flags( 0 ) ); |
| 476 | + layer->selectByIds( QgsFeatureIds() << 2 << 4 ); |
| 477 | + ids = getIds( QgsProcessingUtils::getFeatures( layer, context ) ); |
| 478 | + QCOMPARE( ids, QgsFeatureIds() << 1 << 2 << 3 << 4 << 5 ); |
| 479 | + QCOMPARE( QgsProcessingUtils::featureCount( layer, context ), 5L ); |
| 480 | + |
| 481 | + // using selected features, but no selection |
| 482 | + context.setFlags( QgsProcessingContext::UseSelection ); |
| 483 | + layer->removeSelection(); |
| 484 | + ids = getIds( QgsProcessingUtils::getFeatures( layer, context ) ); |
| 485 | + QCOMPARE( ids, QgsFeatureIds() << 1 << 2 << 3 << 4 << 5 ); |
| 486 | + QCOMPARE( QgsProcessingUtils::featureCount( layer, context ), 5L ); |
| 487 | + |
| 488 | + |
| 489 | + // test that feature request is honored |
| 490 | + context.setFlags( QgsProcessingContext::Flags( 0 ) ); |
| 491 | + ids = getIds( QgsProcessingUtils::getFeatures( layer, context, QgsFeatureRequest().setFilterFids( QgsFeatureIds() << 1 << 3 << 5 ) ) ); |
| 492 | + QCOMPARE( ids, QgsFeatureIds() << 1 << 3 << 5 ); |
| 493 | + |
| 494 | + // count is only rough - but we expect (for now) to see full layer count |
| 495 | + QCOMPARE( QgsProcessingUtils::featureCount( layer, context ), 5L ); |
| 496 | + |
| 497 | + |
| 498 | + //test that feature request is honored when using selections |
| 499 | + context.setFlags( QgsProcessingContext::UseSelection ); |
| 500 | + layer->selectByIds( QgsFeatureIds() << 2 << 4 ); |
| 501 | + ids = getIds( QgsProcessingUtils::getFeatures( layer, context, QgsFeatureRequest().setFlags( QgsFeatureRequest::NoGeometry ) ) ); |
| 502 | + QCOMPARE( ids, QgsFeatureIds() << 2 << 4 ); |
| 503 | + |
| 504 | +#if 0 |
| 505 | + // test exception is raised when filtering invalid geoms |
| 506 | + context.setInvalidGeometryCheck( QgsFeatureRequest.GeometryAbortOnInvalid ) |
| 507 | +#endif |
| 508 | + |
| 509 | + delete layer; |
| 510 | +} |
| 511 | + |
433 | 512 | QGSTEST_MAIN( TestQgsProcessing )
|
434 | 513 | #include "testqgsprocessing.moc"
|
0 commit comments