Navigation Menu

Skip to content

Commit

Permalink
Add test for search priority
Browse files Browse the repository at this point in the history
  • Loading branch information
m-kuhn authored and nyalldawson committed Mar 30, 2020
1 parent 8c2a301 commit a6113f0
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions tests/src/app/testqgsapplocatorfilters.cpp
Expand Up @@ -38,6 +38,7 @@ class TestQgsAppLocatorFilters : public QObject
void testLayouts();
void testSearchActiveLayer();
void testSearchAllLayers();
void testSearchAllLayersPrioritizeExactMatch();

private:
QgisApp *mQgisApp = nullptr;
Expand Down Expand Up @@ -221,6 +222,39 @@ void TestQgsAppLocatorFilters::testSearchAllLayers()
QgsProject::instance()->removeAllMapLayers();
}

void TestQgsAppLocatorFilters::testSearchAllLayersPrioritizeExactMatch()
{
QString layerDef = QStringLiteral( "Point?crs=epsg:4326&field=pk:integer&field=my_text:string&field=my_number:integer&key=pk" );
QgsVectorLayer *l1 = new QgsVectorLayer( layerDef, QStringLiteral( "Layer 1" ), QStringLiteral( "memory" ) );

QgsProject::instance()->addMapLayers( QList< QgsMapLayer *>() << l1 );

QgsFeature f1;
f1.setAttributes( QVector<QVariant>() << 100 << "A nice feature" << 100 );
f1.setGeometry( QgsGeometry::fromWkt( "Point (-71.123 78.23)" ) );
QgsFeature f2;
f2.setAttributes( QVector<QVariant>() << 101 << "Something crazy" << 3 );
f2.setGeometry( QgsGeometry::fromWkt( "Point (-72.123 78.23)" ) );
QgsFeature f3;
f3.setAttributes( QVector<QVariant>() << 102 << "Another feature" << 1 );
f3.setGeometry( QgsGeometry::fromWkt( "Point (-73.123 78.23)" ) );

l1->dataProvider()->addFeatures( QgsFeatureList() << f1 << f2 << f3 );

QgsAllLayersFeaturesLocatorFilter filter;
QgsLocatorContext context;
context.usingPrefix = true; // Searching for short strings is only available with prefix

l1->setDisplayExpression( QStringLiteral( "\"my_number\"" ) );

QList< QgsLocatorResult > results = gatherResults( &filter, QStringLiteral( "1" ), context );
QCOMPARE( results.count(), 2 );
QCOMPARE( results.first().displayString, QStringLiteral( "1" ) );
QCOMPARE( results.last().displayString, QStringLiteral( "100" ) );

QgsProject::instance()->removeAllMapLayers();
}

QList<QgsLocatorResult> TestQgsAppLocatorFilters::gatherResults( QgsLocatorFilter *filter, const QString &string, const QgsLocatorContext &context )
{
QSignalSpy spy( filter, &QgsLocatorFilter::resultFetched );
Expand Down

0 comments on commit a6113f0

Please sign in to comment.