Skip to content

Commit

Permalink
Add tests for sorting feature list by display expression
Browse files Browse the repository at this point in the history
  • Loading branch information
m-kuhn committed May 17, 2018
1 parent 44bb21d commit ce403b1
Showing 1 changed file with 34 additions and 2 deletions.
36 changes: 34 additions & 2 deletions tests/src/app/testqgsattributetable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include "qgsunittypes.h"
#include "qgssettings.h"
#include "qgsvectorfilewriter.h"
#include "qgsfeaturelistmodel.h"

#include "qgstest.h"

Expand All @@ -49,6 +50,7 @@ class TestQgsAttributeTable : public QObject
void testFieldCalculationArea();
void testNoGeom();
void testSelected();
void testSortByDisplayExpression();

private:
QgisApp *mQgisApp = nullptr;
Expand Down Expand Up @@ -208,8 +210,6 @@ void TestQgsAttributeTable::testNoGeom()
void TestQgsAttributeTable::testSelected()
{
// test attribute table opening in show selected mode
QgsSettings s;

std::unique_ptr< QgsVectorLayer> tempLayer( new QgsVectorLayer( QStringLiteral( "LineString?crs=epsg:3111&field=pk:int&field=col1:double" ), QStringLiteral( "vl" ), QStringLiteral( "memory" ) ) );
QVERIFY( tempLayer->isValid() );

Expand Down Expand Up @@ -241,6 +241,38 @@ void TestQgsAttributeTable::testSelected()
QVERIFY( dlg->mMainView->masterModel()->request().filterFids().isEmpty() );
}

void TestQgsAttributeTable::testSortByDisplayExpression()
{
std::unique_ptr< QgsVectorLayer> tempLayer( new QgsVectorLayer( QStringLiteral( "LineString?crs=epsg:3111&field=pk:int&field=col1:double" ), QStringLiteral( "vl" ), QStringLiteral( "memory" ) ) );
QVERIFY( tempLayer->isValid() );

QgsFeature f1( tempLayer->dataProvider()->fields(), 1 );
f1.setAttribute( 0, 1 );
f1.setAttribute( 1, 3.2 );
QgsFeature f2( tempLayer->dataProvider()->fields(), 2 );
f2.setAttribute( 0, 2 );
f2.setAttribute( 1, 1.8 );
QgsFeature f3( tempLayer->dataProvider()->fields(), 3 );
f3.setAttribute( 0, 3 );
f3.setAttribute( 1, 5.0 );
QVERIFY( tempLayer->dataProvider()->addFeatures( QgsFeatureList() << f1 << f2 << f3 ) );

std::unique_ptr< QgsAttributeTableDialog > dlg( new QgsAttributeTableDialog( tempLayer.get() ) );

dlg->mMainView->mFeatureList->setDisplayExpression( "pk" );
QgsFeatureListModel *listModel = dlg->mMainView->mFeatureListModel;
QCOMPARE( listModel->rowCount(), 3 );

QCOMPARE( listModel->index( 0, 0 ).data( Qt::DisplayRole ), QVariant( 1 ) );
QCOMPARE( listModel->index( 1, 0 ).data( Qt::DisplayRole ), QVariant( 2 ) );
QCOMPARE( listModel->index( 2, 0 ).data( Qt::DisplayRole ), QVariant( 3 ) );

dlg->mMainView->mFeatureList->setDisplayExpression( "col1" );
QCOMPARE( listModel->index( 0, 0 ).data( Qt::DisplayRole ), QVariant( 1.8 ) );
QCOMPARE( listModel->index( 1, 0 ).data( Qt::DisplayRole ), QVariant( 3.2 ) );
QCOMPARE( listModel->index( 2, 0 ).data( Qt::DisplayRole ), QVariant( 5.0 ) );
}

void TestQgsAttributeTable::testRegression15974()
{
// Test duplicated rows in attribute table + two crashes.
Expand Down

0 comments on commit ce403b1

Please sign in to comment.