@@ -106,6 +106,7 @@ class TestQgsProcessing: public QObject
106
106
void mapLayerFromString ();
107
107
void algorithm ();
108
108
void features ();
109
+ void uniqueValues ();
109
110
110
111
private:
111
112
@@ -528,5 +529,45 @@ void TestQgsProcessing::features()
528
529
delete polyLayer;
529
530
}
530
531
532
+ void TestQgsProcessing::uniqueValues ()
533
+ {
534
+ QgsVectorLayer *layer = new QgsVectorLayer ( " Point?field=a:integer&field=b:string" , " v1" , " memory" );
535
+ for ( int i = 0 ; i < 6 ; ++i )
536
+ {
537
+ QgsFeature f ( i );
538
+ f.setAttributes ( QgsAttributes () << i % 3 + 1 << QString ( QChar ( ( i % 3 ) + 65 ) ) );
539
+ layer->dataProvider ()->addFeatures ( QgsFeatureList () << f );
540
+ }
541
+
542
+ QgsProcessingContext context;
543
+ context.setFlags ( QgsProcessingContext::Flags ( 0 ) );
544
+
545
+ // some bad checks
546
+ QVERIFY ( QgsProcessingUtils::uniqueValues ( nullptr , 0 , context ).isEmpty () );
547
+ QVERIFY ( QgsProcessingUtils::uniqueValues ( nullptr , -1 , context ).isEmpty () );
548
+ QVERIFY ( QgsProcessingUtils::uniqueValues ( nullptr , 10001 , context ).isEmpty () );
549
+ QVERIFY ( QgsProcessingUtils::uniqueValues ( layer, -1 , context ).isEmpty () );
550
+ QVERIFY ( QgsProcessingUtils::uniqueValues ( layer, 10001 , context ).isEmpty () );
551
+
552
+ // good checks
553
+ QCOMPARE ( QgsProcessingUtils::uniqueValues ( layer, 0 , context ), QList< QVariant >() << 1 << 2 << 3 );
554
+ QCOMPARE ( QgsProcessingUtils::uniqueValues ( layer, 1 , context ), QList< QVariant >() << QString ( " A" ) << QString ( " B" ) << QString ( " C" ) );
555
+
556
+ // using only selected features
557
+ layer->selectByIds ( QgsFeatureIds () << 1 << 2 << 4 );
558
+ // but not using selection yet...
559
+ QCOMPARE ( QgsProcessingUtils::uniqueValues ( layer, 0 , context ), QList< QVariant >() << 1 << 2 << 3 );
560
+ QCOMPARE ( QgsProcessingUtils::uniqueValues ( layer, 1 , context ), QList< QVariant >() << QString ( " A" ) << QString ( " B" ) << QString ( " C" ) );
561
+
562
+ // selection and using selection
563
+ context.setFlags ( QgsProcessingContext::UseSelectionIfPresent );
564
+ QVERIFY ( QgsProcessingUtils::uniqueValues ( layer, -1 , context ).isEmpty () );
565
+ QVERIFY ( QgsProcessingUtils::uniqueValues ( layer, 10001 , context ).isEmpty () );
566
+ QCOMPARE ( QgsProcessingUtils::uniqueValues ( layer, 0 , context ), QList< QVariant >() << 1 << 2 );
567
+ QCOMPARE ( QgsProcessingUtils::uniqueValues ( layer, 1 , context ), QList< QVariant >() << QString ( " A" ) << QString ( " B" ) );
568
+
569
+ delete layer;
570
+ }
571
+
531
572
QGSTEST_MAIN ( TestQgsProcessing )
532
573
#include " testqgsprocessing.moc"
0 commit comments