Skip to content

Commit b8a90ca

Browse files
committedAug 28, 2017
Add tests
1 parent 477775a commit b8a90ca

File tree

1 file changed

+120
-0
lines changed

1 file changed

+120
-0
lines changed
 

‎tests/src/gui/testqgsattributeform.cpp

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ class TestQgsAttributeForm : public QObject
4444
void testOKButtonStatus();
4545
void testDynamicForm();
4646
void testConstraintsOnJoinedFields();
47+
void testEditableJoin();
4748
};
4849

4950
void TestQgsAttributeForm::initTestCase()
@@ -511,5 +512,124 @@ void TestQgsAttributeForm::testConstraintsOnJoinedFields()
511512
QCOMPARE( label->text(), "layerB_" + warningLabel );
512513
}
513514

515+
void TestQgsAttributeForm::testEditableJoin()
516+
{
517+
// make temporary layers
518+
QString defA = QStringLiteral( "Point?field=id_a:integer" );
519+
QgsVectorLayer *layerA = new QgsVectorLayer( defA, QStringLiteral( "layerA" ), QStringLiteral( "memory" ) );
520+
521+
QString defB = QStringLiteral( "Point?field=id_b:integer&field=col0:integer" );
522+
QgsVectorLayer *layerB = new QgsVectorLayer( defB, QStringLiteral( "layerB" ), QStringLiteral( "memory" ) );
523+
524+
QString defC = QStringLiteral( "Point?field=id_c:integer&field=col0:integer" );
525+
QgsVectorLayer *layerC = new QgsVectorLayer( defC, QStringLiteral( "layerC" ), QStringLiteral( "memory" ) );
526+
527+
// join configuration
528+
QgsVectorLayerJoinInfo infoJoinAB;
529+
infoJoinAB.setTargetFieldName( "id_a" );
530+
infoJoinAB.setJoinLayer( layerB );
531+
infoJoinAB.setJoinFieldName( "id_b" );
532+
infoJoinAB.setDynamicFormEnabled( true );
533+
infoJoinAB.setEditable( true );
534+
535+
layerA->addJoin( infoJoinAB );
536+
537+
QgsVectorLayerJoinInfo infoJoinAC;
538+
infoJoinAC.setTargetFieldName( "id_a" );
539+
infoJoinAC.setJoinLayer( layerC );
540+
infoJoinAC.setJoinFieldName( "id_c" );
541+
infoJoinAC.setDynamicFormEnabled( true );
542+
infoJoinAC.setEditable( false );
543+
544+
layerA->addJoin( infoJoinAC );
545+
546+
// add features for main layer
547+
QgsFeature ftA( layerA->fields() );
548+
ftA.setAttribute( QStringLiteral( "id_a" ), 31 );
549+
layerA->startEditing();
550+
layerA->addFeature( ftA );
551+
layerA->commitChanges();
552+
553+
// add features for joined layers
554+
QgsFeature ft0B( layerB->fields() );
555+
ft0B.setAttribute( QStringLiteral( "id_b" ), 31 );
556+
ft0B.setAttribute( QStringLiteral( "col0" ), 11 );
557+
layerB->startEditing();
558+
layerB->addFeature( ft0B );
559+
layerB->commitChanges();
560+
561+
QgsFeature ft0C( layerC->fields() );
562+
ft0C.setAttribute( QStringLiteral( "id_c" ), 31 );
563+
ft0C.setAttribute( QStringLiteral( "col0" ), 13 );
564+
layerC->startEditing();
565+
layerC->addFeature( ft0C );
566+
layerC->commitChanges();
567+
568+
// start editing layers
569+
layerA->startEditing();
570+
layerB->startEditing();
571+
layerC->startEditing();
572+
573+
// build a form with feature A
574+
ftA = layerA->getFeature( 1 );
575+
576+
QgsAttributeForm form( layerA );
577+
form.setMode( QgsAttributeForm::SingleEditMode );
578+
form.setFeature( ftA );
579+
580+
for ( int i = 0; i < ftA.fields().count(); i++ )
581+
std::cout << " - " << ftA.fields().field( i ).name().toStdString() << " : " << ftA.attribute( i ).toString().toStdString() << std::endl;
582+
583+
// change layerA join id field to join with layerB and layerC
584+
QgsEditorWidgetWrapper *ww = nullptr;
585+
586+
ww = qobject_cast<QgsEditorWidgetWrapper *>( form.mWidgets[0] );
587+
QCOMPARE( ww->field().name(), QString( "id_a" ) );
588+
QCOMPARE( ww->value(), QVariant( 31 ) );
589+
590+
ww = qobject_cast<QgsEditorWidgetWrapper *>( form.mWidgets[1] );
591+
QCOMPARE( ww->field().name(), QString( "layerB_col0" ) );
592+
QCOMPARE( ww->value(), QVariant( 11 ) );
593+
594+
ww = qobject_cast<QgsEditorWidgetWrapper *>( form.mWidgets[2] );
595+
QCOMPARE( ww->field().name(), QString( "layerC_col0" ) );
596+
QCOMPARE( ww->value(), QVariant( 13 ) );
597+
598+
// test if widget is enabled for layerA
599+
ww = qobject_cast<QgsEditorWidgetWrapper *>( form.mWidgets[0] );
600+
QCOMPARE( ww->widget()->isEnabled(), true );
601+
602+
// test if widget is enabled for layerB
603+
ww = qobject_cast<QgsEditorWidgetWrapper *>( form.mWidgets[1] );
604+
QCOMPARE( ww->widget()->isEnabled(), true );
605+
606+
// test if widget is disabled for layerC
607+
ww = qobject_cast<QgsEditorWidgetWrapper *>( form.mWidgets[2] );
608+
QCOMPARE( ww->widget()->isEnabled(), false );
609+
610+
// change attributes
611+
form.changeAttribute( "layerB_col0", QVariant( 333 ) );
612+
form.changeAttribute( "layerC_col0", QVariant( 444 ) );
613+
form.save();
614+
615+
// commit changes
616+
layerA->commitChanges();
617+
layerB->commitChanges();
618+
layerC->commitChanges();
619+
620+
// check attributes
621+
ft0B = layerB->getFeature( 1 );
622+
QCOMPARE( ft0B.attribute( "col0" ), QVariant( 333 ) );
623+
624+
ft0C = layerC->getFeature( 1 );
625+
QCOMPARE( ft0C.attribute( "col0" ), QVariant( 13 ) );
626+
627+
// clean
628+
delete layerA;
629+
delete layerB;
630+
delete layerC;
631+
}
632+
633+
514634
QGSTEST_MAIN( TestQgsAttributeForm )
515635
#include "testqgsattributeform.moc"

0 commit comments

Comments
 (0)
Please sign in to comment.