Skip to content
Permalink
Browse files

Add tests and utility methods

  • Loading branch information
elpaso committed Nov 29, 2019
1 parent e8d76e2 commit 2ab794bcc5f9e343f8b1af40311c282e00ee0c9f
@@ -359,6 +359,18 @@ the selected expression must be a stored expression.
.. versionadded:: 3.12
%End

const QList<QgsExpressionItem *> findExpressions( const QString &label );
%Docstring
Returns the list of expression items matching a ``label``.

.. note::

this function is exposed for testing purposes only

.. versionadded:: 3.12
%End


signals:

void expressionParsed( bool isValid );
@@ -581,11 +581,10 @@ void QgsExpressionBuilderWidget::loadRecent( const QString &collection )
}

QgsSettings settings;
QString location = QStringLiteral( "/expressions/recent/%1" ).arg( collection );
QStringList expressions = settings.value( location ).toStringList();
const QString location = QStringLiteral( "/expressions/recent/%1" ).arg( collection );
const QStringList expressions = settings.value( location ).toStringList();
int i = 0;
const auto constExpressions = expressions;
for ( const QString &expression : constExpressions )
for ( const QString &expression : expressions )
{
this->registerItem( name, expression, expression, expression, QgsExpressionItem::ExpressionNode, false, i );
i++;
@@ -595,7 +594,7 @@ void QgsExpressionBuilderWidget::loadRecent( const QString &collection )
void QgsExpressionBuilderWidget::loadStored( const QString &collection )
{
mRecentKey = collection;
QString groupName = tr( "Stored (%1)" ).arg( collection );
const QString groupName = tr( "Stored (%1)" ).arg( collection );

// Cleanup
if ( mExpressionGroups.contains( groupName ) )
@@ -604,26 +603,16 @@ void QgsExpressionBuilderWidget::loadStored( const QString &collection )
node->removeRows( 0, node->rowCount() );
}

const QList<QStandardItem *> groupItems { mModel->findItems( groupName ) };
if ( ! groupItems.isEmpty() )
{
const QModelIndex idx { mModel->indexFromItem( groupItems.first() ) };
if ( idx.isValid() )
{
mModel->removeRow( idx.row(), idx.parent() );
}
}

QgsSettings settings;
QString location = QStringLiteral( "/expressions/stored/%1" ).arg( collection );
const QString location = QStringLiteral( "/expressions/stored/%1" ).arg( collection );
settings.beginGroup( location, QgsSettings::Section::Gui );
QString label;
QString helpText;
QString expression;
int i = 0;
for ( const auto &label : settings.childKeys() )
for ( const auto &label : settings.childGroups() )
{
settings.beginGroup( groupName );
settings.beginGroup( label );
expression = settings.value( QStringLiteral( "expression" ) ).toString();
helpText = settings.value( QStringLiteral( "helpText" ) ).toString();
this->registerItem( groupName, label, expression, helpText, QgsExpressionItem::ExpressionNode, false, i++ );
@@ -634,7 +623,7 @@ void QgsExpressionBuilderWidget::loadStored( const QString &collection )
void QgsExpressionBuilderWidget::saveToStored( const QString &label, const QString expression, const QString &helpText, const QString &collection )
{
QgsSettings settings;
QString location = QStringLiteral( "/expressions/stored/%1" ).arg( collection );
const QString location = QStringLiteral( "/expressions/stored/%1" ).arg( collection );
settings.beginGroup( location, QgsSettings::Section::Gui );
settings.beginGroup( label );
settings.setValue( QStringLiteral( "expression" ), expression );
@@ -1230,6 +1219,17 @@ void QgsExpressionBuilderWidget::removeSelectedExpression()

}

const QList<QgsExpressionItem *> QgsExpressionBuilderWidget::findExpressions( const QString &label )
{
QList<QgsExpressionItem *> result;
const QList<QStandardItem *> found { mModel->findItems( label, Qt::MatchFlag::MatchRecursive ) };
for ( const auto &item : qgis::as_const( found ) )
{
result.push_back( dynamic_cast<QgsExpressionItem *>( item ) );
}
return result;
}

void QgsExpressionBuilderWidget::indicatorClicked( int line, int index, Qt::KeyboardModifiers state )
{
if ( state & Qt::ControlModifier )
@@ -357,6 +357,14 @@ class GUI_EXPORT QgsExpressionBuilderWidget : public QWidget, private Ui::QgsExp
*/
void removeSelectedExpression( );

/**
* Returns the list of expression items matching a \a label.
* \note this function is exposed for testing purposes only
* \since QGIS 3.12
*/
const QList<QgsExpressionItem *> findExpressions( const QString &label );


private slots:
void indicatorClicked( int line, int index, Qt::KeyboardModifiers state );
void showContextMenu( QPoint );
@@ -164,6 +164,33 @@ def testRelations(self):
items = m.findItems('Relation Number Two', Qt.MatchRecursive)
self.assertEqual(len(items), 1)

def testStoredExpressions(self):
"""Check that expressions can be stored and retrieved"""

w = QgsExpressionBuilderWidget()

w.saveToStored('Stored Expression Number One', '"field_one" = 123', "An humble expression", "my_custom_collection")
items = w.findExpressions('Stored Expression Number One')
self.assertEqual(len(items), 1)
exp = items[0]
self.assertEqual(exp.getExpressionText(), '"field_one" = 123')

# Add another one with the same name (overwrite)
w.saveToStored('Stored Expression Number One', '"field_two" = 456', "An even more humble expression", "my_custom_collection")
items = w.findExpressions('Stored Expression Number One')
self.assertEqual(len(items), 1)
exp = items[0]
self.assertEqual(exp.getExpressionText(), '"field_two" = 456')

# Reload by creating a new widget
w = QgsExpressionBuilderWidget()
self.assertEqual(exp.getExpressionText(), '"field_two" = 456')

# Test removal
w.removeFromStored('Stored Expression Number One', "my_custom_collection")
items = w.findExpressions('Stored Expression Number One')
self.assertEqual(len(items), 0)


if __name__ == '__main__':
unittest.main()

0 comments on commit 2ab794b

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