-
Notifications
You must be signed in to change notification settings - Fork 85
Fix literal wildcards in text filters #4609
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,6 +13,8 @@ | |
|
|
||
| #include <QtTest/QtTest> | ||
| #include <QDateTime> | ||
| #include <QFile> | ||
| #include <QTemporaryDir> | ||
|
|
||
| #include <qgsproject.h> | ||
| #include <qgsvectorlayer.h> | ||
|
|
@@ -554,11 +556,73 @@ void TestFilterController::testTextFilter() | |
| filterValues[filterId] = QVariantList{ QStringLiteral( "great" ) }; | ||
| mController->processFilters( filterValues ); | ||
|
|
||
| const QString expected = QStringLiteral( "(\"Condition\" LIKE '%great%')" ); | ||
| const QString expected = QStringLiteral( "(\"Condition\" LIKE '%great%' ESCAPE '!')" ); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we are not escaping anything here |
||
| QCOMPARE( layer->subsetString(), expected ); | ||
| QCOMPARE( layer->featureCount(), ( long long ) 14 ); | ||
| } | ||
|
|
||
| void TestFilterController::testTextFilterWildcards_data() | ||
| { | ||
| QTest::addColumn<QString>( "searchValue" ); | ||
| QTest::addColumn<QString>( "expectedPattern" ); | ||
| QTest::addColumn<qlonglong>( "expectedCount" ); | ||
|
|
||
| QTest::newRow( "percent" ) << QStringLiteral( "%" ) << QStringLiteral( "%!%%" ) << 2LL; | ||
| QTest::newRow( "underscore" ) << QStringLiteral( "_" ) << QStringLiteral( "%!_%" ) << 2LL; | ||
| QTest::newRow( "escape-character" ) << QStringLiteral( "!" ) << QStringLiteral( "%!!%" ) << 1LL; | ||
| QTest::newRow( "combined-wildcards" ) << QStringLiteral( "%_" ) << QStringLiteral( "%!%!_%" ) << 1LL; | ||
| } | ||
|
Comment on lines
+564
to
+574
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. don't use this, just use the expected values inline |
||
|
|
||
| void TestFilterController::testTextFilterWildcards() | ||
| { | ||
| QFETCH( QString, searchValue ); | ||
| QFETCH( QString, expectedPattern ); | ||
| QFETCH( qlonglong, expectedCount ); | ||
|
|
||
| QTemporaryDir tempDir; | ||
| QVERIFY( tempDir.isValid() ); | ||
| const QString layerPath = tempDir.filePath( QStringLiteral( "roads.gpkg" ) ); | ||
| QVERIFY( QFile::copy( TestUtils::testDataDir() + QStringLiteral( "/filtering/roads.gpkg" ), layerPath ) ); | ||
|
|
||
| const QString fieldName = QStringLiteral( "Condition" ); | ||
| QgsVectorLayer *layer = new QgsVectorLayer( | ||
| layerPath + QStringLiteral( "|layername=roads" ), | ||
| QStringLiteral( "text-filter-wildcards" ), | ||
| QStringLiteral( "ogr" ) | ||
| ); | ||
| QVERIFY( layer ); | ||
| QVERIFY( layer->isValid() ); | ||
| QgsProject::instance()->addMapLayer( layer ); | ||
|
Comment on lines
+582
to
+595
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What's wrong with the approach in other tests? |
||
|
|
||
| const QStringList values = | ||
| { | ||
| QStringLiteral( "plain" ), | ||
| QStringLiteral( "percent%only" ), | ||
| QStringLiteral( "under_score" ), | ||
| QStringLiteral( "bang!mark" ), | ||
| QStringLiteral( "both%_chars" ) | ||
| }; | ||
|
Comment on lines
+597
to
+604
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. use values that could match values in condition |
||
| for ( const QString &value : values ) | ||
| { | ||
| QVERIFY( TestUtils::addFeatureToLayer( layer, fieldName, value ) ); | ||
| } | ||
|
Comment on lines
+605
to
+608
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. don't add more features to the layer there already are some, use them |
||
|
|
||
| const QString sql = QStringLiteral( "\"Condition\" LIKE '%@@value@@%'" ); | ||
| const QString filterId = TestUtils::setupControllerWithFilter( | ||
| mController.get(), FieldFilter::TextFilter, layer->id(), fieldName, sql ); | ||
| QVERIFY( !filterId.isEmpty() ); | ||
|
|
||
| QVariantMap filterValues; | ||
| filterValues[filterId] = QVariantList{ searchValue }; | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Apply the filter values one by one and check the results after each one |
||
| mController->processFilters( filterValues ); | ||
|
|
||
| const QString expected = QStringLiteral( "(\"Condition\" LIKE '%1' ESCAPE '!')" ).arg( expectedPattern ); | ||
| QCOMPARE( layer->subsetString(), expected ); | ||
| QCOMPARE( layer->featureCount(), expectedCount ); | ||
|
|
||
| QgsProject::instance()->removeMapLayer( layer ); | ||
| } | ||
|
|
||
| // Checkbox filter | ||
| void TestFilterController::testCheckboxFilter() | ||
| { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Create new function in
InputUtilswhich will do these steps and just call it from here. Use backslash as the escape character instead. Also are these all the wildcards you can get?