|
| 1 | +/*************************************************************************** |
| 2 | + testqgsfieldexpressionwidget.cpp |
| 3 | + -------------------------------------- |
| 4 | + Date : January 2016 |
| 5 | + Copyright : (C) 2016 Denis Rouzaud |
| 6 | + Email : denis.rouzaud@gmail.com |
| 7 | + *************************************************************************** |
| 8 | + * * |
| 9 | + * This program is free software; you can redistribute it and/or modify * |
| 10 | + * it under the terms of the GNU General Public License as published by * |
| 11 | + * the Free Software Foundation; either version 2 of the License, or * |
| 12 | + * (at your option) any later version. * |
| 13 | + * * |
| 14 | + ***************************************************************************/ |
| 15 | + |
| 16 | + |
| 17 | +#include <QtTest/QtTest> |
| 18 | +#include <QObject> |
| 19 | + |
| 20 | +//qgis includes... |
| 21 | +#include <qgsvectorlayer.h> |
| 22 | +#include <qgsapplication.h> |
| 23 | +#include <qgsvectorlayerjoinbuffer.h> |
| 24 | +#include <qgsmaplayerregistry.h> |
| 25 | +#include <qgsfieldexpressionwidget.h> |
| 26 | +#include <qgsproject.h> |
| 27 | + |
| 28 | +/** @ingroup UnitTests |
| 29 | + * This is a unit test for the field expression widget |
| 30 | + * |
| 31 | + * @see QgsFieldExpressionWidget |
| 32 | + */ |
| 33 | +class TestQgsFieldExpressionWidget : public QObject |
| 34 | +{ |
| 35 | + Q_OBJECT |
| 36 | + |
| 37 | + public: |
| 38 | + TestQgsFieldExpressionWidget() |
| 39 | + : mLayerA( nullptr ) |
| 40 | + , mLayerB( nullptr ) |
| 41 | + {} |
| 42 | + |
| 43 | + private slots: |
| 44 | + void initTestCase(); // will be called before the first testfunction is executed. |
| 45 | + void cleanupTestCase(); // will be called after the last testfunction was executed. |
| 46 | + void init(); // will be called before each testfunction is executed. |
| 47 | + void cleanup(); // will be called after every testfunction. |
| 48 | + |
| 49 | + void testRemoveJoin(); |
| 50 | + |
| 51 | + private: |
| 52 | + QgsFieldExpressionWidget* mWidget; |
| 53 | + QgsVectorLayer* mLayerA; |
| 54 | + QgsVectorLayer* mLayerB; |
| 55 | +}; |
| 56 | + |
| 57 | +// runs before all tests |
| 58 | +void TestQgsFieldExpressionWidget::initTestCase() |
| 59 | +{ |
| 60 | + QgsApplication::init(); |
| 61 | + QgsApplication::initQgis(); |
| 62 | + |
| 63 | + // Set up the QSettings environment |
| 64 | + QCoreApplication::setOrganizationName( "QGIS" ); |
| 65 | + QCoreApplication::setOrganizationDomain( "qgis.org" ); |
| 66 | + QCoreApplication::setApplicationName( "QGIS-TEST" ); |
| 67 | + |
| 68 | + // Create memory layers |
| 69 | + // LAYER A // |
| 70 | + mLayerA = new QgsVectorLayer( "Point?field=id_a:integer", "A", "memory" ); |
| 71 | + QVERIFY( mLayerA->isValid() ); |
| 72 | + QVERIFY( mLayerA->fields().count() == 1 ); |
| 73 | + QgsMapLayerRegistry::instance()->addMapLayer( mLayerA ); |
| 74 | + // LAYER B // |
| 75 | + mLayerB = new QgsVectorLayer( "Point?field=id_b:integer&field=value_b", "B", "memory" ); |
| 76 | + QVERIFY( mLayerB->isValid() ); |
| 77 | + QVERIFY( mLayerB->fields().count() == 2 ); |
| 78 | + QgsMapLayerRegistry::instance()->addMapLayer( mLayerB ); |
| 79 | + |
| 80 | + // init widget |
| 81 | + mWidget = new QgsFieldExpressionWidget(); |
| 82 | + mWidget->setLayer( mLayerA ); |
| 83 | + |
| 84 | + |
| 85 | +} |
| 86 | + |
| 87 | +void TestQgsFieldExpressionWidget::init() |
| 88 | +{ |
| 89 | +} |
| 90 | + |
| 91 | +void TestQgsFieldExpressionWidget::cleanup() |
| 92 | +{ |
| 93 | +} |
| 94 | + |
| 95 | +void TestQgsFieldExpressionWidget::cleanupTestCase() |
| 96 | +{ |
| 97 | + QgsApplication::exitQgis(); |
| 98 | +} |
| 99 | + |
| 100 | +void TestQgsFieldExpressionWidget::testRemoveJoin() |
| 101 | +{ |
| 102 | + |
| 103 | + QVERIFY( mLayerA->fields().count() == 1 ); |
| 104 | + |
| 105 | + QgsVectorJoinInfo joinInfo; |
| 106 | + joinInfo.targetFieldName = "id_a"; |
| 107 | + joinInfo.joinLayerId = mLayerB->id(); |
| 108 | + joinInfo.joinFieldName = "id_b"; |
| 109 | + joinInfo.memoryCache = false; |
| 110 | + joinInfo.prefix = "B_"; |
| 111 | + mLayerA->addJoin( joinInfo ); |
| 112 | + |
| 113 | + QVERIFY( mLayerA->fields().count() == 2 ); |
| 114 | + |
| 115 | + const QString expr = "'hello '|| B_value_b"; |
| 116 | + mWidget->setField( expr ); |
| 117 | + |
| 118 | + bool isExpression, isValid; |
| 119 | + QVERIFY( mWidget->isValidExpression() ); |
| 120 | + QCOMPARE( mWidget->currentField( &isExpression, &isValid ), expr ); |
| 121 | + QVERIFY( isExpression ); |
| 122 | + QVERIFY( isValid ); |
| 123 | + |
| 124 | + QVERIFY( mLayerA->removeJoin( mLayerB->id() ) ); |
| 125 | + |
| 126 | + QVERIFY( mLayerA->fields().count() == 1 ); |
| 127 | + |
| 128 | + QCOMPARE( mWidget->mCombo->currentText(), expr ); |
| 129 | + |
| 130 | + QCOMPARE( mWidget->currentField( &isExpression, &isValid ), expr ); |
| 131 | + QVERIFY( isExpression ); |
| 132 | + // 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. |
| 133 | +} |
| 134 | + |
| 135 | + |
| 136 | +QTEST_MAIN( TestQgsFieldExpressionWidget ) |
| 137 | +#include "testqgsfieldexpressionwidget.moc" |
| 138 | + |
| 139 | + |
0 commit comments