Skip to content
Permalink
Browse files

Add a unit test for use of precalculated static node values in expres…

…sion compilation
  • Loading branch information
nyalldawson committed Feb 15, 2021
1 parent 75504c5 commit df30e64d1b52ed79e269ccc2b5cdbe3ebd1a9d76
Showing with 26 additions and 0 deletions.
  1. +26 −0 tests/src/core/testqgssqliteexpressioncompiler.cpp
@@ -41,6 +41,7 @@ class TestQgsSQLiteExpressionCompiler: public QObject
void cleanupTestCase();
void testMakeExpression();
void testCompiler();
void testPreparedCachedNodes();

private:

@@ -112,6 +113,31 @@ void TestQgsSQLiteExpressionCompiler::testCompiler()
QCOMPARE( compiler.result(), QStringLiteral( "lower('a') NOT LIKE lower('A') ESCAPE '\\'" ) );
}

void TestQgsSQLiteExpressionCompiler::testPreparedCachedNodes()
{
// test that expression compilation of an expression which has precalculated static values for nodes will take advantage of these values

QgsSQLiteExpressionCompiler compiler = QgsSQLiteExpressionCompiler( mPointsLayer->fields(), false );
QgsExpression exp( QStringLiteral( "\"Z\" = (1 + 2) OR \"z\" < (@static_var + 5)" ) );

QgsExpressionContext context;
std::unique_ptr< QgsExpressionContextScope > scope = qgis::make_unique< QgsExpressionContextScope >();
scope->setVariable( QStringLiteral( "static_var" ), 10, true );
context.appendScope( scope.release() );
// not possible to compile due to use of a variable
QCOMPARE( compiler.compile( &exp ), QgsSqlExpressionCompiler::Result::Fail );

// now prepare the expression, so that the static nodes will be precalculated
exp.prepare( &context );
// should now succeed -- the variable node was identified as a static value and replaced by a pre-computed value
QCOMPARE( compiler.compile( &exp ), QgsSqlExpressionCompiler::Result::Complete );
QCOMPARE( compiler.result(), QStringLiteral( "((\"Z\" = 3) OR (\"Z\" < 15))" ) );

// let's try again, denying the compiler the ability to use pre-computed values
QgsSQLiteExpressionCompiler compiler2 = QgsSQLiteExpressionCompiler( mPointsLayer->fields(), true );
// will fail, because it can't take advantage of the pre-computer variable value and a variable can't be compiled
QCOMPARE( compiler2.compile( &exp ), QgsSqlExpressionCompiler::Result::Fail );
}


QGSTEST_MAIN( TestQgsSQLiteExpressionCompiler )

0 comments on commit df30e64

Please sign in to comment.