diff --git a/tests/src/core/CMakeLists.txt b/tests/src/core/CMakeLists.txt index 23bcf61bf52..09ec8ab8c09 100644 --- a/tests/src/core/CMakeLists.txt +++ b/tests/src/core/CMakeLists.txt @@ -232,13 +232,13 @@ SET(TESTS testqgstranslateproject.cpp testqobjectuniqueptr.cpp testqgspostgresstringutils.cpp + estqgsstoredexpressionmanager.cpp ) IF(WITH_QTWEBKIT) SET(TESTS ${TESTS}) ENDIF(WITH_QTWEBKIT) - IF(HAVE_OPENCL) SET(TESTS ${TESTS} testqgsopenclutils.cpp diff --git a/tests/src/core/testqgsstoredexpressionmanager.cpp b/tests/src/core/testqgsstoredexpressionmanager.cpp new file mode 100644 index 00000000000..ec8f3964fa5 --- /dev/null +++ b/tests/src/core/testqgsstoredexpressionmanager.cpp @@ -0,0 +1,129 @@ +/*************************************************************************** + testqgsstoredexpressionmanager.cpp + -------------------------------------- + Date : August 2019 + Copyright : (C) 2019 David Signer + email : david at opengis dot ch + *************************************************************************** + * * + * 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 +#include "qgstest.h" +#include "qgsstoredexpressionmanager.h" + +class TestQgsStoredExpressionManager : public QObject +{ + Q_OBJECT + private: + QgsStoredExpressionManager *mManager = nullptr; + + private slots: + void init(); + void cleanup(); + + void storeSingleExpression(); + void storeListOfExpressions(); + void editExpressionsByExpression(); + void deleteExpressionByExpression(); +}; + +void TestQgsStoredExpressionManager::init() +{ + mManager = new QgsStoredExpressionManager( QgsStoredExpressionManager::FilterExpression ); + + QList newStoredExpressions; + + //fill up + for ( int i = 0; i < 10; i++ ) + { + QgsStoredExpression storedExpression; + storedExpression.id = QUuid::createUuid(); + storedExpression.name = QStringLiteral( "test%1" ).arg( i ); + storedExpression.expression = QStringLiteral( "\"age\"=%1" ).arg( i ); + newStoredExpressions.append( storedExpression ); + } + mManager->addStoredExpressions( newStoredExpressions ); +} + +void TestQgsStoredExpressionManager::cleanup() +{ + delete mManager; +} + +void TestQgsStoredExpressionManager::storeSingleExpression() +{ + QString name = QStringLiteral( "test0" ); + QString expression = QStringLiteral( "\"age\"=0" ); + QUuid id = mManager->addStoredExpression( name, expression ); + + //get stored expression by id + QgsStoredExpression storedExpression = mManager->storedExpression( id ); + QCOMPARE( storedExpression.id, id ); + QCOMPARE( storedExpression.name, name ); + QCOMPARE( storedExpression.expression, expression ); + + //get all expressions + QList allStoredExpressions = mManager->storedExpressions(); + QCOMPARE( allStoredExpressions.count(), 11 ); + QCOMPARE( allStoredExpressions.at( 10 ).id, id ); + QCOMPARE( allStoredExpressions.at( 10 ).name, name ); + QCOMPARE( allStoredExpressions.at( 10 ).expression, expression ); +} + +void TestQgsStoredExpressionManager::storeListOfExpressions() +{ + QList newStoredExpressions; + + //fill up + for ( int i = 10; i < 20; i++ ) + { + QgsStoredExpression storedExpression; + storedExpression.id = QUuid::createUuid(); + storedExpression.name = QStringLiteral( "test%1" ).arg( i ); + storedExpression.expression = QStringLiteral( "\"age\"=%1" ).arg( i ); + newStoredExpressions.append( storedExpression ); + } + mManager->addStoredExpressions( newStoredExpressions ); + + //get all expressions + QList allStoredExpressions = mManager->storedExpressions(); + QCOMPARE( allStoredExpressions.count(), 20 ); + QCOMPARE( allStoredExpressions.at( 0 ).name, QStringLiteral( "test0" ) ); + QCOMPARE( allStoredExpressions.at( 0 ).expression, QStringLiteral( "\"age\"=0" ) ); + QCOMPARE( allStoredExpressions.at( 14 ).name, QStringLiteral( "test14" ) ); + QCOMPARE( allStoredExpressions.at( 14 ).expression, QStringLiteral( "\"age\"=14" ) ); + QCOMPARE( allStoredExpressions.at( 19 ).name, QStringLiteral( "test19" ) ); + QCOMPARE( allStoredExpressions.at( 19 ).expression, QStringLiteral( "\"age\"=19" ) ); +} + +void TestQgsStoredExpressionManager::editExpressionsByExpression() +{ + QgsStoredExpression storedExpression = mManager->findStoredExpressionByExpression( QStringLiteral( "\"age\"=4" ) ); + QCOMPARE( storedExpression.name, QStringLiteral( "test4" ) ); + + mManager->updateStoredExpression( storedExpression.id, QStringLiteral( "Much older" ), QStringLiteral( "\"age\">99" ) ); + + QCOMPARE( mManager->storedExpression( storedExpression.id ).name, QStringLiteral( "Much older" ) ); + QCOMPARE( mManager->storedExpression( storedExpression.id ).expression, QStringLiteral( "\"age\">99" ) ); +} + +void TestQgsStoredExpressionManager::deleteExpressionByExpression() +{ + QgsStoredExpression storedExpression = mManager->findStoredExpressionByExpression( QStringLiteral( "\"age\"=4" ) ); + QCOMPARE( storedExpression.name, QStringLiteral( "test4" ) ); + + mManager->removeStoredExpression( storedExpression.id ); + + storedExpression = mManager->findStoredExpressionByExpression( QStringLiteral( "\"age\"=4" ) ); + + QVERIFY( storedExpression.id.isNull() ); +} + +QGSTEST_MAIN( TestQgsStoredExpressionManager ) +#include "testqgsstoredexpressionmanager.moc"