|
| 1 | +/*************************************************************************** |
| 2 | +
|
| 3 | + testqgssqliteexpressioncompiler.cpp |
| 4 | +
|
| 5 | + --------------------- |
| 6 | + begin : 14.8.2018 |
| 7 | + copyright : (C) 2018 by Alessandro Pasotti |
| 8 | + email : elpaso at itopen dot it |
| 9 | + *************************************************************************** |
| 10 | + * * |
| 11 | + * This program is free software; you can redistribute it and/or modify * |
| 12 | + * it under the terms of the GNU General Public License as published by * |
| 13 | + * the Free Software Foundation; either version 2 of the License, or * |
| 14 | + * (at your option) any later version. * |
| 15 | + * * |
| 16 | + ***************************************************************************/ |
| 17 | +#include "qgstest.h" |
| 18 | + |
| 19 | + |
| 20 | +#include <qgsapplication.h> |
| 21 | +//header for class being tested |
| 22 | +#include "qgsexpression.h" |
| 23 | +#include "qgsvectorlayer.h" |
| 24 | +#include "qgsproject.h" |
| 25 | +#include "qgssqliteexpressioncompiler.h" |
| 26 | + |
| 27 | +class TestQgsSQLiteExpressionCompiler: public QObject |
| 28 | +{ |
| 29 | + Q_OBJECT |
| 30 | + |
| 31 | + public: |
| 32 | + |
| 33 | + TestQgsSQLiteExpressionCompiler() = default; |
| 34 | + |
| 35 | + QgsExpression makeExpression( const int length ); |
| 36 | + |
| 37 | + |
| 38 | + private slots: |
| 39 | + |
| 40 | + void initTestCase(); |
| 41 | + void cleanupTestCase(); |
| 42 | + void testMakeExpression(); |
| 43 | + void testCompiler(); |
| 44 | + |
| 45 | + private: |
| 46 | + |
| 47 | + QgsVectorLayer *mPointsLayer = nullptr; |
| 48 | +}; |
| 49 | + |
| 50 | + |
| 51 | + |
| 52 | +QgsExpression TestQgsSQLiteExpressionCompiler::makeExpression( const int length ) |
| 53 | +{ |
| 54 | + QStringList expString; |
| 55 | + for ( int i = 0; i < length; ++i ) |
| 56 | + { |
| 57 | + expString.append( QStringLiteral( "(\"Z\" >= %1) AND (\"Bottom\" <= %2)" ).arg( i ).arg( i + 1 ) ); |
| 58 | + } |
| 59 | + QgsExpression exp( expString.join( QStringLiteral( ") OR (" ) ).prepend( '(' ).append( ')' ) ); |
| 60 | + return exp; |
| 61 | +} |
| 62 | + |
| 63 | +void TestQgsSQLiteExpressionCompiler::initTestCase() |
| 64 | +{ |
| 65 | + // |
| 66 | + // Runs once before any tests are run |
| 67 | + // |
| 68 | + // init QGIS's paths - true means that all path will be inited from prefix |
| 69 | + QgsApplication::init(); |
| 70 | + QgsApplication::initQgis(); |
| 71 | + // Will make sure the settings dir with the style file for color ramp is created |
| 72 | + QgsApplication::createDatabase(); |
| 73 | + QgsApplication::showSettings(); |
| 74 | + //create a point layer that will be used in all tests... |
| 75 | + mPointsLayer = new QgsVectorLayer( QStringLiteral( "Point?crs=epsg:4326&field=Z:integer&field=Bottom:integer" ), QStringLiteral( "test mem layer" ), QStringLiteral( "memory" ) ); |
| 76 | + QgsProject::instance()->addMapLayer( mPointsLayer ); |
| 77 | +} |
| 78 | + |
| 79 | +void TestQgsSQLiteExpressionCompiler::cleanupTestCase() |
| 80 | +{ |
| 81 | + QgsApplication::exitQgis(); |
| 82 | +} |
| 83 | + |
| 84 | +void TestQgsSQLiteExpressionCompiler::testMakeExpression() |
| 85 | +{ |
| 86 | + QgsExpression exp( makeExpression( 1 ) ); |
| 87 | + QVERIFY( exp.isValid() ); |
| 88 | + QCOMPARE( QString( exp ), QString( "((\"Z\" >= 0) AND (\"Bottom\" <= 1))" ) ); |
| 89 | + QgsExpression exp2( makeExpression( 2 ) ); |
| 90 | + QVERIFY( exp2.isValid() ); |
| 91 | + QCOMPARE( QString( exp2 ), QString( "((\"Z\" >= 0) AND (\"Bottom\" <= 1)) OR ((\"Z\" >= 1) AND (\"Bottom\" <= 2))" ) ); |
| 92 | +} |
| 93 | + |
| 94 | +void TestQgsSQLiteExpressionCompiler::testCompiler() |
| 95 | +{ |
| 96 | + QgsSQLiteExpressionCompiler compiler = QgsSQLiteExpressionCompiler( mPointsLayer->fields() ); |
| 97 | + QgsExpression exp( makeExpression( 1 ) ); |
| 98 | + QCOMPARE( compiler.compile( &exp ), QgsSqlExpressionCompiler::Result::Complete ); |
| 99 | + QCOMPARE( compiler.result(), QString( exp ) ); |
| 100 | + exp = makeExpression( 3 ); |
| 101 | + QCOMPARE( compiler.compile( &exp ), QgsSqlExpressionCompiler::Result::Complete ); |
| 102 | + // Check that parenthesis matches |
| 103 | + QCOMPARE( compiler.result().count( '(' ), compiler.result().count( ')' ) ); |
| 104 | + QCOMPARE( compiler.result(), QString( "((((\"Z\" >= 0) AND (\"Bottom\" <= 1)) OR ((\"Z\" >= 1) AND (\"Bottom\" <= 2))) OR ((\"Z\" >= 2) AND (\"Bottom\" <= 3)))" ) ); |
| 105 | +} |
| 106 | + |
| 107 | + |
| 108 | + |
| 109 | +QGSTEST_MAIN( TestQgsSQLiteExpressionCompiler ) |
| 110 | + |
| 111 | +#include "testqgssqliteexpressioncompiler.moc" |
0 commit comments