Skip to content

Commit

Permalink
Add test for QgsFieldExpressionWidget
Browse files Browse the repository at this point in the history
the test checks that fields are updated properly on join removal, that expression is not lost and that validity is correct
  • Loading branch information
3nids committed Feb 1, 2016
1 parent 6854902 commit cae49d7
Show file tree
Hide file tree
Showing 3 changed files with 142 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/gui/qgsfieldexpressionwidget.h
Expand Up @@ -156,6 +156,8 @@ class GUI_EXPORT QgsFieldExpressionWidget : public QWidget
QScopedPointer< QgsExpressionContext > mExpressionContext;
ExpressionContextCallback mExpressionContextCallback;
const void* mExpressionContextCallbackContext;

friend class TestQgsFieldExpressionWidget;
};

#endif // QGSFIELDEXPRESSIONWIDGET_H
1 change: 1 addition & 0 deletions tests/src/gui/CMakeLists.txt
Expand Up @@ -123,6 +123,7 @@ ADD_QGIS_TEST(zoomtest testqgsmaptoolzoom.cpp)
#ADD_QGIS_TEST(histogramtest testqgsrasterhistogram.cpp)
ADD_QGIS_TEST(doublespinbox testqgsdoublespinbox.cpp)
ADD_QGIS_TEST(dualviewtest testqgsdualview.cpp )
ADD_QGIS_TEST(fieldexpressionwidget testqgsfieldexpressionwidget.cpp )
ADD_QGIS_TEST(filewidget testqgsfilewidget.cpp )
ADD_QGIS_TEST(mapcanvastest testqgsmapcanvas.cpp )
ADD_QGIS_TEST(projectionissues testprojectionissues.cpp)
Expand Down
139 changes: 139 additions & 0 deletions tests/src/gui/testqgsfieldexpressionwidget.cpp
@@ -0,0 +1,139 @@
/***************************************************************************
testqgsfieldexpressionwidget.cpp
--------------------------------------
Date : January 2016
Copyright : (C) 2016 Denis Rouzaud
Email : denis.rouzaud@gmail.com
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/


#include <QtTest/QtTest>
#include <QObject>

//qgis includes...
#include <qgsvectorlayer.h>
#include <qgsapplication.h>
#include <qgsvectorlayerjoinbuffer.h>
#include <qgsmaplayerregistry.h>
#include <qgsfieldexpressionwidget.h>
#include <qgsproject.h>

/** @ingroup UnitTests
* This is a unit test for the field expression widget
*
* @see QgsFieldExpressionWidget
*/
class TestQgsFieldExpressionWidget : public QObject
{
Q_OBJECT

public:
TestQgsFieldExpressionWidget()
: mLayerA( nullptr )
, mLayerB( nullptr )
{}

private slots:
void initTestCase(); // will be called before the first testfunction is executed.
void cleanupTestCase(); // will be called after the last testfunction was executed.
void init(); // will be called before each testfunction is executed.
void cleanup(); // will be called after every testfunction.

void testRemoveJoin();

private:
QgsFieldExpressionWidget* mWidget;
QgsVectorLayer* mLayerA;
QgsVectorLayer* mLayerB;
};

// runs before all tests
void TestQgsFieldExpressionWidget::initTestCase()
{
QgsApplication::init();
QgsApplication::initQgis();

// Set up the QSettings environment
QCoreApplication::setOrganizationName( "QGIS" );
QCoreApplication::setOrganizationDomain( "qgis.org" );
QCoreApplication::setApplicationName( "QGIS-TEST" );

// Create memory layers
// LAYER A //
mLayerA = new QgsVectorLayer( "Point?field=id_a:integer", "A", "memory" );
QVERIFY( mLayerA->isValid() );
QVERIFY( mLayerA->fields().count() == 1 );
QgsMapLayerRegistry::instance()->addMapLayer( mLayerA );
// LAYER B //
mLayerB = new QgsVectorLayer( "Point?field=id_b:integer&field=value_b", "B", "memory" );
QVERIFY( mLayerB->isValid() );
QVERIFY( mLayerB->fields().count() == 2 );
QgsMapLayerRegistry::instance()->addMapLayer( mLayerB );

// init widget
mWidget = new QgsFieldExpressionWidget();
mWidget->setLayer( mLayerA );


}

void TestQgsFieldExpressionWidget::init()
{
}

void TestQgsFieldExpressionWidget::cleanup()
{
}

void TestQgsFieldExpressionWidget::cleanupTestCase()
{
QgsApplication::exitQgis();
}

void TestQgsFieldExpressionWidget::testRemoveJoin()
{

QVERIFY( mLayerA->fields().count() == 1 );

QgsVectorJoinInfo joinInfo;
joinInfo.targetFieldName = "id_a";
joinInfo.joinLayerId = mLayerB->id();
joinInfo.joinFieldName = "id_b";
joinInfo.memoryCache = false;
joinInfo.prefix = "B_";
mLayerA->addJoin( joinInfo );

QVERIFY( mLayerA->fields().count() == 2 );

const QString expr = "'hello '|| B_value_b";
mWidget->setField( expr );

bool isExpression, isValid;
QVERIFY( mWidget->isValidExpression() );
QCOMPARE( mWidget->currentField( &isExpression, &isValid ), expr );
QVERIFY( isExpression );
QVERIFY( isValid );

QVERIFY( mLayerA->removeJoin( mLayerB->id() ) );

QVERIFY( mLayerA->fields().count() == 1 );

QCOMPARE( mWidget->mCombo->currentText(), expr );

QCOMPARE( mWidget->currentField( &isExpression, &isValid ), expr );
QVERIFY( isExpression );
// QVERIFY( !isValid ); TODO: the expression should not be valid anymore since the field doesn't exist anymore. Maybe we need a new expression method to get more details.
}


QTEST_MAIN( TestQgsFieldExpressionWidget )
#include "testqgsfieldexpressionwidget.moc"


0 comments on commit cae49d7

Please sign in to comment.