Skip to content

Commit

Permalink
Add tests for the new duration parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
nirvn committed Jul 26, 2021
1 parent 52bac40 commit b8dd5d1
Show file tree
Hide file tree
Showing 2 changed files with 234 additions and 0 deletions.
97 changes: 97 additions & 0 deletions tests/src/analysis/testqgsprocessing.cpp
Expand Up @@ -577,6 +577,7 @@ class TestQgsProcessing: public QObject
void parameterLayerList();
void parameterNumber();
void parameterDistance();
void parameterDuration();
void parameterScale();
void parameterRange();
void parameterRasterLayer();
Expand Down Expand Up @@ -4310,6 +4311,102 @@ void TestQgsProcessing::parameterDistance()
QVERIFY( !def->checkValueIsAcceptable( QVariant() ) ); // should NOT be acceptable, falls back to invalid default value
}

void TestQgsProcessing::parameterDuration()
{
QgsProcessingContext context;

// not optional!
std::unique_ptr< QgsProcessingParameterDuration > def( new QgsProcessingParameterDuration( "non_optional", QString(), 5, false ) );
QCOMPARE( def->defaultUnit(), QgsUnitTypes::TemporalMilliseconds );
def->setDefaultUnit( QgsUnitTypes::TemporalDays );
QCOMPARE( def->defaultUnit(), QgsUnitTypes::TemporalDays );
std::unique_ptr< QgsProcessingParameterDuration > clone( def->clone() );
QCOMPARE( clone->defaultUnit(), QgsUnitTypes::TemporalDays );

QVERIFY( def->checkValueIsAcceptable( 5 ) );
QVERIFY( def->checkValueIsAcceptable( "1.1" ) );
QVERIFY( !def->checkValueIsAcceptable( "1.1,2" ) );
QVERIFY( !def->checkValueIsAcceptable( "layer12312312" ) );
QVERIFY( !def->checkValueIsAcceptable( "" ) );
QVERIFY( def->checkValueIsAcceptable( QVariant() ) ); // should be acceptable, falls back to default value

// string representing a number
QVariantMap params;
params.insert( "non_optional", QString( "1.1" ) );
double number = QgsProcessingParameters::parameterAsDouble( def.get(), params, context );
QGSCOMPARENEAR( number, 1.1, 0.001 );

// double
params.insert( "non_optional", 1.1 );
number = QgsProcessingParameters::parameterAsDouble( def.get(), params, context );
QGSCOMPARENEAR( number, 1.1, 0.001 );
// int
params.insert( "non_optional", 1 );
number = QgsProcessingParameters::parameterAsDouble( def.get(), params, context );
QGSCOMPARENEAR( number, 1, 0.001 );

// nonsense string
params.insert( "non_optional", QString( "i'm not a number, and nothing you can do will make me one" ) );
number = QgsProcessingParameters::parameterAsDouble( def.get(), params, context );
QCOMPARE( number, 5.0 );

// with min value
def->setMinimum( 11 );
QVERIFY( !def->checkValueIsAcceptable( 5 ) );
QVERIFY( !def->checkValueIsAcceptable( "1.1" ) );
QVERIFY( def->checkValueIsAcceptable( 25 ) );
QVERIFY( def->checkValueIsAcceptable( "21.1" ) );
// with max value
def->setMaximum( 21 );
QVERIFY( !def->checkValueIsAcceptable( 35 ) );
QVERIFY( !def->checkValueIsAcceptable( "31.1" ) );
QVERIFY( def->checkValueIsAcceptable( 15 ) );
QVERIFY( def->checkValueIsAcceptable( "11.1" ) );

QCOMPARE( def->valueAsPythonString( QVariant(), context ), QStringLiteral( "None" ) );
QCOMPARE( def->valueAsPythonString( 5, context ), QStringLiteral( "5" ) );
QCOMPARE( def->valueAsPythonString( QStringLiteral( "1.1" ), context ), QStringLiteral( "1.1" ) );
QCOMPARE( def->valueAsPythonString( QVariant::fromValue( QgsProperty::fromExpression( "\"a\"=1" ) ), context ), QStringLiteral( "QgsProperty.fromExpression('\"a\"=1')" ) );

QVariantMap map = def->toVariantMap();
QgsProcessingParameterDuration fromMap( "x" );
QVERIFY( fromMap.fromVariantMap( map ) );
QCOMPARE( fromMap.name(), def->name() );
QCOMPARE( fromMap.description(), def->description() );
QCOMPARE( fromMap.flags(), def->flags() );
QCOMPARE( fromMap.defaultValue(), def->defaultValue() );
QCOMPARE( fromMap.minimum(), def->minimum() );
QCOMPARE( fromMap.maximum(), def->maximum() );
QCOMPARE( fromMap.dataType(), def->dataType() );
QCOMPARE( fromMap.defaultUnit(), QgsUnitTypes::TemporalDays );
def.reset( dynamic_cast< QgsProcessingParameterDuration *>( QgsProcessingParameters::parameterFromVariantMap( map ) ) );
QVERIFY( dynamic_cast< QgsProcessingParameterDuration *>( def.get() ) );

// optional
def.reset( new QgsProcessingParameterDuration( "optional", QString(), 5.4, true ) );
QVERIFY( def->checkValueIsAcceptable( 5 ) );
QVERIFY( def->checkValueIsAcceptable( "1.1" ) );
QVERIFY( def->checkValueIsAcceptable( "" ) );
QVERIFY( def->checkValueIsAcceptable( QVariant() ) );

params.insert( "optional", QVariant() );
number = QgsProcessingParameters::parameterAsDouble( def.get(), params, context );
QGSCOMPARENEAR( number, 5.4, 0.001 );
// unconvertible string
params.insert( "optional", QVariant( "aaaa" ) );
number = QgsProcessingParameters::parameterAsDouble( def.get(), params, context );
QGSCOMPARENEAR( number, 5.4, 0.001 );

// non-optional, invalid default
def.reset( new QgsProcessingParameterDuration( "non_optional", QString(), QVariant(), false ) );
QVERIFY( def->checkValueIsAcceptable( 5 ) );
QVERIFY( def->checkValueIsAcceptable( "1.1" ) );
QVERIFY( !def->checkValueIsAcceptable( "1.1,2" ) );
QVERIFY( !def->checkValueIsAcceptable( "layer12312312" ) );
QVERIFY( !def->checkValueIsAcceptable( "" ) );
QVERIFY( !def->checkValueIsAcceptable( QVariant() ) ); // should NOT be acceptable, falls back to invalid default value
}

void TestQgsProcessing::parameterScale()
{
QgsProcessingContext context;
Expand Down
137 changes: 137 additions & 0 deletions tests/src/gui/testprocessinggui.cpp
Expand Up @@ -206,6 +206,7 @@ class TestProcessingGui : public QObject
void testNumericWrapperDouble();
void testNumericWrapperInt();
void testDistanceWrapper();
void testDurationWrapper();
void testScaleWrapper();
void testRangeWrapper();
void testMatrixDialog();
Expand Down Expand Up @@ -2123,6 +2124,142 @@ void TestProcessingGui::testDistanceWrapper()
QVERIFY( static_cast< QgsProcessingParameterDistance * >( def.get() )->parentParameterName().isEmpty() );
}

void TestProcessingGui::testDurationWrapper()
{
QgsProcessingParameterDuration param( QStringLiteral( "duration" ), QStringLiteral( "duration" ) );

// standard wrapper
QgsProcessingDurationWidgetWrapper wrapper( &param );

QgsProcessingContext context;
QWidget *w = wrapper.createWrappedWidget( context );

QSignalSpy spy( &wrapper, &QgsProcessingDurationWidgetWrapper::widgetValueHasChanged );
wrapper.setWidgetValue( 55.5, context );
QCOMPARE( spy.count(), 1 );
QCOMPARE( wrapper.widgetValue().toDouble(), 55.5 );
QCOMPARE( wrapper.mDoubleSpinBox->value(), 55.5 );
wrapper.setWidgetValue( -34.0, context );
QCOMPARE( spy.count(), 2 );
QCOMPARE( wrapper.widgetValue().toDouble(), -34.0 );
QCOMPARE( wrapper.mDoubleSpinBox->value(), -34.0 );

QLabel *l = wrapper.createWrappedLabel();
QVERIFY( l );
QCOMPARE( l->text(), QStringLiteral( "duration" ) );
QCOMPARE( l->toolTip(), param.toolTip() );
delete l;

// check signal
wrapper.mDoubleSpinBox->setValue( 43.0 );
QCOMPARE( spy.count(), 3 );

// with default unit
QgsProcessingParameterDuration paramDefaultUnit( QStringLiteral( "dur" ), QStringLiteral( "dur" ) );
paramDefaultUnit.setDefaultUnit( QgsUnitTypes::TemporalDays );
QgsProcessingDurationWidgetWrapper wrapperDefaultUnit( &paramDefaultUnit, QgsProcessingGui::Standard );
w = wrapperDefaultUnit.createWrappedWidget( context );
w->show();
QCOMPARE( wrapperDefaultUnit.mUnitsCombo->currentText(), QgsUnitTypes::toString( QgsUnitTypes::TemporalDays ) );
delete w;

// with decimals
QgsProcessingParameterDuration paramDecimals( QStringLiteral( "num" ), QStringLiteral( "num" ), QVariant(), true, 1, 1.02 );
QVariantMap metadata;
QVariantMap wrapperMetadata;
wrapperMetadata.insert( QStringLiteral( "decimals" ), 2 );
metadata.insert( QStringLiteral( "widget_wrapper" ), wrapperMetadata );
paramDecimals.setMetadata( metadata );
QgsProcessingDurationWidgetWrapper wrapperDecimals( &paramDecimals, QgsProcessingGui::Standard );
w = wrapperDecimals.createWrappedWidget( context );
QCOMPARE( wrapperDecimals.mDoubleSpinBox->decimals(), 2 );
QCOMPARE( wrapperDecimals.mDoubleSpinBox->singleStep(), 0.01 ); // single step should never be less than set number of decimals
delete w;

// batch wrapper
QgsProcessingDurationWidgetWrapper wrapperB( &param, QgsProcessingGui::Batch );

w = wrapperB.createWrappedWidget( context );
QSignalSpy spy2( &wrapperB, &QgsProcessingDurationWidgetWrapper::widgetValueHasChanged );
wrapperB.setWidgetValue( 34, context );
QCOMPARE( spy2.count(), 1 );
QCOMPARE( wrapperB.widgetValue().toDouble(), 34.0 );
QCOMPARE( wrapperB.mDoubleSpinBox->value(), 34.0 );
wrapperB.setWidgetValue( -57, context );
QCOMPARE( spy2.count(), 2 );
QCOMPARE( wrapperB.widgetValue().toDouble(), -57.0 );
QCOMPARE( wrapperB.mDoubleSpinBox->value(), -57.0 );

// check signal
static_cast< QgsDoubleSpinBox * >( w )->setValue( 29 );
QCOMPARE( spy2.count(), 3 );

// should be no label in batch mode
QVERIFY( !wrapperB.createWrappedLabel() );
delete w;

// modeler wrapper
QgsProcessingDurationWidgetWrapper wrapperM( &param, QgsProcessingGui::Modeler );

w = wrapperM.createWrappedWidget( context );
QSignalSpy spy3( &wrapperM, &QgsProcessingDurationWidgetWrapper::widgetValueHasChanged );
wrapperM.setWidgetValue( 29, context );
QCOMPARE( wrapperM.widgetValue().toDouble(), 29.0 );
QCOMPARE( spy3.count(), 1 );
QCOMPARE( wrapperM.mDoubleSpinBox->value(), 29.0 );
wrapperM.setWidgetValue( -29, context );
QCOMPARE( wrapperM.widgetValue().toDouble(), -29.0 );
QCOMPARE( spy3.count(), 2 );
QCOMPARE( wrapperM.mDoubleSpinBox->value(), -29.0 );

// check signal
wrapperM.mDoubleSpinBox->setValue( 33 );
QCOMPARE( spy3.count(), 3 );

// should be a label in modeler mode
l = wrapperM.createWrappedLabel();
QVERIFY( l );
QCOMPARE( l->text(), QStringLiteral( "duration" ) );
QCOMPARE( l->toolTip(), param.toolTip() );
delete w;
delete l;

// config widget
QgsProcessingParameterWidgetContext widgetContext;
std::unique_ptr< QgsProcessingParameterDefinitionWidget > widget = std::make_unique< QgsProcessingParameterDefinitionWidget >( QStringLiteral( "duration" ), context, widgetContext );
std::unique_ptr< QgsProcessingParameterDefinition > def( widget->createParameter( QStringLiteral( "param_name" ) ) );
QCOMPARE( def->name(), QStringLiteral( "param_name" ) );
QVERIFY( !( def->flags() & QgsProcessingParameterDefinition::FlagOptional ) ); // should default to mandatory
QVERIFY( !( def->flags() & QgsProcessingParameterDefinition::FlagAdvanced ) );

// using a parameter definition as initial values
QgsProcessingParameterDuration durParam( QStringLiteral( "n" ), QStringLiteral( "test desc" ), 1 );
durParam.setMinimum( 1 );
durParam.setMaximum( 100 );
widget = std::make_unique< QgsProcessingParameterDefinitionWidget >( QStringLiteral( "duration" ), context, widgetContext, &durParam );
def.reset( widget->createParameter( QStringLiteral( "param_name" ) ) );
QCOMPARE( def->name(), QStringLiteral( "param_name" ) );
QCOMPARE( def->description(), QStringLiteral( "test desc" ) );
QVERIFY( !( def->flags() & QgsProcessingParameterDefinition::FlagOptional ) );
QVERIFY( !( def->flags() & QgsProcessingParameterDefinition::FlagAdvanced ) );
QCOMPARE( static_cast< QgsProcessingParameterDuration * >( def.get() )->defaultValue().toDouble(), 1.0 );
QCOMPARE( static_cast< QgsProcessingParameterDuration * >( def.get() )->minimum(), 1.0 );
QCOMPARE( static_cast< QgsProcessingParameterDuration * >( def.get() )->maximum(), 100.0 );
durParam.setFlags( QgsProcessingParameterDefinition::FlagAdvanced | QgsProcessingParameterDefinition::FlagOptional );
durParam.setMinimum( 10 );
durParam.setMaximum( 12 );
durParam.setDefaultValue( 11.5 );
widget = std::make_unique< QgsProcessingParameterDefinitionWidget >( QStringLiteral( "duration" ), context, widgetContext, &durParam );
def.reset( widget->createParameter( QStringLiteral( "param_name" ) ) );
QCOMPARE( def->name(), QStringLiteral( "param_name" ) );
QCOMPARE( def->description(), QStringLiteral( "test desc" ) );
QVERIFY( def->flags() & QgsProcessingParameterDefinition::FlagOptional );
QVERIFY( def->flags() & QgsProcessingParameterDefinition::FlagAdvanced );
QCOMPARE( static_cast< QgsProcessingParameterDuration * >( def.get() )->defaultValue().toDouble(), 11.5 );
QCOMPARE( static_cast< QgsProcessingParameterDuration * >( def.get() )->minimum(), 10.0 );
QCOMPARE( static_cast< QgsProcessingParameterDuration * >( def.get() )->maximum(), 12.0 );
}

void TestProcessingGui::testScaleWrapper()
{
auto testWrapper = []( QgsProcessingGui::WidgetType type )
Expand Down

0 comments on commit b8dd5d1

Please sign in to comment.