Skip to content

Commit

Permalink
Add tests for compiling string operators
Browse files Browse the repository at this point in the history
  • Loading branch information
m-kuhn committed May 31, 2018
1 parent 6a88bfc commit dbe4186
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 0 deletions.
17 changes: 17 additions & 0 deletions python/core/auto_generated/qgsfeatureiterator.sip.in
Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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:

Expand Down
5 changes: 5 additions & 0 deletions src/core/qgsfeatureiterator.cpp
Expand Up @@ -147,6 +147,11 @@ void QgsAbstractFeatureIterator::deref()
delete this;
}

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

bool QgsAbstractFeatureIterator::prepareSimplification( const QgsSimplifyMethod &simplifyMethod )
{
Q_UNUSED( simplifyMethod );
Expand Down
18 changes: 18 additions & 0 deletions src/core/qgsfeatureiterator.h
Expand Up @@ -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:

/**
Expand Down Expand Up @@ -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 );

Expand Down Expand Up @@ -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;

Expand Down
3 changes: 3 additions & 0 deletions src/providers/postgres/qgspostgresfeatureiterator.cpp
Expand Up @@ -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() )
Expand Down
1 change: 1 addition & 0 deletions src/providers/spatialite/qgsspatialitefeatureiterator.cpp
Expand Up @@ -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 )
Expand Down
22 changes: 22 additions & 0 deletions tests/src/python/providertestbase.py
Expand Up @@ -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.