Skip to content
Permalink
Browse files

Add tests for compiling string operators

  • Loading branch information
m-kuhn committed May 31, 2018
1 parent 6a88bfc commit dbe4186a9c6d9820b26dbe63ada5445d2c38374b
@@ -65,6 +65,14 @@ after a timeout to give the system some time to stay responsive).
If you want to check if the iterator successfully completed, better use :py:func:`QgsFeatureIterator.isClosed()`

.. versionadded:: 3.0
%End

bool compileFailed() const;
%Docstring
Indicator if there was an error when sending the compiled query to the server.
This indicates that there is something wrong with the expression compiler.

.. versionadded:: 3.2
%End

protected:
@@ -144,6 +152,7 @@ Remove reference, delete if refs == 0




virtual bool prepareSimplification( const QgsSimplifyMethod &simplifyMethod );
%Docstring
Setup the simplification of geometries to fetch using the specified simplify method
@@ -251,6 +260,14 @@ Returns the status of expression compilation for filter expression requests.
.. versionadded:: 2.16
%End

bool compileFailed() const;
%Docstring
Indicator if there was an error when sending the compiled query to the server.
This indicates that there is something wrong with the expression compiler.

.. versionadded:: 3.2
%End


protected:

@@ -147,6 +147,11 @@ void QgsAbstractFeatureIterator::deref()
delete this;
}

bool QgsAbstractFeatureIterator::compileFailed() const
{
return mCompileFailed;
}

bool QgsAbstractFeatureIterator::prepareSimplification( const QgsSimplifyMethod &simplifyMethod )
{
Q_UNUSED( simplifyMethod );
@@ -83,6 +83,14 @@ class CORE_EXPORT QgsAbstractFeatureIterator
return mValid;
}

/**
* Indicator if there was an error when sending the compiled query to the server.
* This indicates that there is something wrong with the expression compiler.
*
* \since QGIS 3.2
*/
bool compileFailed() const;

protected:

/**
@@ -173,6 +181,8 @@ class CORE_EXPORT QgsAbstractFeatureIterator
//! Status of compilation of filter expression
CompileStatus mCompileStatus = NoCompilation;

bool mCompileFailed = false;

//! Setup the simplification of geometries to fetch using the specified simplify method
virtual bool prepareSimplification( const QgsSimplifyMethod &simplifyMethod );

@@ -323,6 +333,14 @@ class CORE_EXPORT QgsFeatureIterator
*/
QgsAbstractFeatureIterator::CompileStatus compileStatus() const { return mIter->compileStatus(); }

/**
* Indicator if there was an error when sending the compiled query to the server.
* This indicates that there is something wrong with the expression compiler.
*
* \since QGIS 3.2
*/
bool compileFailed() const { return mIter->compileFailed(); }

friend bool operator== ( const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2 ) SIP_SKIP;
friend bool operator!= ( const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2 ) SIP_SKIP;

@@ -210,7 +210,10 @@ QgsPostgresFeatureIterator::QgsPostgresFeatureIterator( QgsPostgresFeatureSource
//try with the fallback where clause, e.g., for cases when using compiled expression failed to prepare
success = declareCursor( fallbackWhereClause, -1, false, orderByParts.join( QStringLiteral( "," ) ) );
if ( success )
{
mExpressionCompiled = false;
mCompileFailed = true;
}
}

if ( !success && !orderByParts.isEmpty() )
@@ -213,6 +213,7 @@ QgsSpatiaLiteFeatureIterator::QgsSpatiaLiteFeatureIterator( QgsSpatiaLiteFeature
//try with the fallback where clause, e.g., for cases when using compiled expression failed to prepare
mExpressionCompiled = false;
success = prepareStatement( fallbackWhereClause, -1, orderByParts.join( QStringLiteral( "," ) ) );
mCompileFailed = true;
}

if ( !success )
@@ -817,3 +817,25 @@ def testMinMaxAfterChanges(self):
self.assertTrue(vl.dataProvider().deleteAttributes([0]))
self.assertEqual(vl.dataProvider().minimumValue(0), -200)
self.assertEqual(vl.dataProvider().maximumValue(0), 400)

def testStringComparison(self):
"""
Test if string comparisons with numbers are cast by the expression
compiler (or work fine without doing anything :P)
"""
vl = self.getEditableLayer()
for expression in (
'5 LIKE \'5\'',
'5 ILIKE \'5\'',
'15 NOT LIKE \'5\'',
'15 NOT ILIKE \'5\'',
'5 ~ \'5\''):
iterator = vl.dataProvider().getFeatures(QgsFeatureRequest().setFilterExpression('5 LIKE \'5\''))
count = len([f for f in iterator])
self.assertEqual(count, 5)
self.assertFalse(iterator.compileFailed())
self.enableCompiler()
iterator = vl.dataProvider().getFeatures(QgsFeatureRequest().setFilterExpression('5 LIKE \'5\''))
self.assertEqual(count, 5)
self.assertFalse(iterator.compileFailed())
self.disableCompiler()

0 comments on commit dbe4186

Please sign in to comment.
You can’t perform that action at this time.