test_orderedrelationmodel.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. #include "qfield_testbase.h"
  2. #include "orderedrelationmodel.h"
  3. #include <QtTest>
  4. #include <qgsapplication.h>
  5. #include <qgsproject.h>
  6. #include <qgsrelationmanager.h>
  7. #include <qgsvectorlayer.h>
  8. class TestOrderedRelationModel : public QObject
  9. {
  10. Q_OBJECT
  11. private slots:
  12. void init()
  13. {
  14. //create layers
  15. mReferencingLayer.reset( new QgsVectorLayer( QStringLiteral( "NoGeometry?field=id:integer&field=foreignkey:integer&field=rank:integer" ), QStringLiteral( "king" ), QStringLiteral( "memory" ) ) );
  16. mReferencingLayer->setDisplayExpression( "name" );
  17. QVERIFY( mReferencingLayer->isValid() );
  18. QgsProject::instance()->addMapLayer( mReferencingLayer.get(), false, false );
  19. mReferencedLayer.reset( new QgsVectorLayer( QStringLiteral( "NoGeometry?field=id:integer" ), QStringLiteral( "land" ), QStringLiteral( "memory" ) ) );
  20. mReferencedLayer->setDisplayExpression( "name" );
  21. QVERIFY( mReferencedLayer->isValid() );
  22. QgsProject::instance()->addMapLayer( mReferencedLayer.get(), false, false );
  23. //create relations
  24. mRelation.setId( QStringLiteral( "referenced.referencing" ) );
  25. mRelation.setName( QStringLiteral( "referenced.referencing" ) );
  26. mRelation.setReferencingLayer( mReferencingLayer->id() );
  27. mRelation.setReferencedLayer( mReferencedLayer->id() );
  28. mRelation.addFieldPair( QStringLiteral( "foreignkey" ), QStringLiteral( "id" ) );
  29. QVERIFY( mRelation.isValid() );
  30. QgsProject::instance()->relationManager()->addRelation( mRelation );
  31. // add features to the referencing layer
  32. QgsFeature referencing_f1( mReferencingLayer->fields() );
  33. referencing_f1.setAttribute( QStringLiteral( "id" ), 1 );
  34. referencing_f1.setAttribute( QStringLiteral( "foreignkey" ), 1 );
  35. referencing_f1.setAttribute( QStringLiteral( "rank" ), 1 );
  36. QgsFeature referencing_f2( mReferencingLayer->fields() );
  37. referencing_f2.setAttribute( QStringLiteral( "id" ), 2 );
  38. referencing_f2.setAttribute( QStringLiteral( "foreignkey" ), 1 );
  39. referencing_f2.setAttribute( QStringLiteral( "rank" ), 2 );
  40. QgsFeature referencing_f3( mReferencingLayer->fields() );
  41. referencing_f3.setAttribute( QStringLiteral( "id" ), 3 );
  42. referencing_f3.setAttribute( QStringLiteral( "foreignkey" ), 1 );
  43. referencing_f3.setAttribute( QStringLiteral( "rank" ), 3 );
  44. QgsFeature referencing_f4( mReferencingLayer->fields() );
  45. referencing_f4.setAttribute( QStringLiteral( "id" ), 4 );
  46. referencing_f4.setAttribute( QStringLiteral( "foreignkey" ), 1 );
  47. referencing_f4.setAttribute( QStringLiteral( "rank" ), 4 );
  48. mReferencingLayer->startEditing();
  49. mReferencingLayer->addFeature( referencing_f1 );
  50. mReferencingLayer->addFeature( referencing_f2 );
  51. mReferencingLayer->addFeature( referencing_f3 );
  52. mReferencingLayer->addFeature( referencing_f4 );
  53. mReferencingLayer->commitChanges();
  54. QCOMPARE( mReferencingLayer->featureCount(), 4L );
  55. // add features to the referenced layer
  56. QgsFeature referenced_ft1( mReferencedLayer->fields() );
  57. referenced_ft1.setAttribute( QStringLiteral( "id" ), 1 );
  58. mReferencedLayer->startEditing();
  59. mReferencedLayer->addFeature( referenced_ft1 );
  60. mReferencedLayer->commitChanges();
  61. QCOMPARE( mReferencedLayer->featureCount(), 1L );
  62. mModel = new OrderedRelationModel();
  63. }
  64. void cleanup()
  65. {
  66. delete mModel;
  67. QgsProject::instance()->removeMapLayer( mReferencedLayer.get() );
  68. QgsProject::instance()->removeMapLayer( mReferencingLayer.get() );
  69. }
  70. void testDeleteFeature()
  71. {
  72. QCOMPARE( mModel->rowCount(), 0 );
  73. mModel->setRelation( mRelation );
  74. mModel->setOrderingField( QStringLiteral( "rank" ) );
  75. mModel->setFeature( mReferencedLayer->getFeature( 1 ) );
  76. QVERIFY( QSignalSpy( mModel, &ReferencingFeatureListModel::modelUpdated ).wait( 1000 ) );
  77. QCOMPARE( mModel->rowCount(), 4 );
  78. QVERIFY( mModel->deleteFeature( 2 ) );
  79. QVERIFY( QSignalSpy( mModel, &ReferencingFeatureListModel::modelUpdated ).wait( 1000 ) );
  80. QCOMPARE( mModel->rowCount(), 3 );
  81. QVERIFY( !mReferencingLayer->getFeature( 2 ).isValid() );
  82. QCOMPARE( mReferencingLayer->getFeature( 1 ).attribute( QStringLiteral( "rank" ) ).toInt(), 1 );
  83. QCOMPARE( mReferencingLayer->getFeature( 3 ).attribute( QStringLiteral( "rank" ) ).toInt(), 2 );
  84. QCOMPARE( mReferencingLayer->getFeature( 4 ).attribute( QStringLiteral( "rank" ) ).toInt(), 3 );
  85. }
  86. void testMoveFeature()
  87. {
  88. QCOMPARE( mModel->rowCount(), 0 );
  89. mModel->setRelation( mRelation );
  90. mModel->setOrderingField( QStringLiteral( "rank" ) );
  91. mModel->setFeature( mReferencedLayer->getFeature( 1 ) );
  92. QVERIFY( QSignalSpy( mModel, &ReferencingFeatureListModel::modelUpdated ).wait( 1000 ) );
  93. QCOMPARE( mModel->rowCount(), 4 );
  94. // try to move items out of range
  95. QVERIFY( !mModel->moveItems( 2, 40 ) );
  96. QCOMPARE( mReferencingLayer->getFeature( 1 ).attribute( QStringLiteral( "rank" ) ).toInt(), 1 );
  97. QCOMPARE( mReferencingLayer->getFeature( 2 ).attribute( QStringLiteral( "rank" ) ).toInt(), 2 );
  98. QCOMPARE( mReferencingLayer->getFeature( 3 ).attribute( QStringLiteral( "rank" ) ).toInt(), 3 );
  99. QCOMPARE( mReferencingLayer->getFeature( 4 ).attribute( QStringLiteral( "rank" ) ).toInt(), 4 );
  100. // try to move items out of range
  101. QVERIFY( !mModel->moveItems( -2, 2 ) );
  102. QCOMPARE( mReferencingLayer->getFeature( 1 ).attribute( QStringLiteral( "rank" ) ).toInt(), 1 );
  103. QCOMPARE( mReferencingLayer->getFeature( 2 ).attribute( QStringLiteral( "rank" ) ).toInt(), 2 );
  104. QCOMPARE( mReferencingLayer->getFeature( 3 ).attribute( QStringLiteral( "rank" ) ).toInt(), 3 );
  105. QCOMPARE( mReferencingLayer->getFeature( 4 ).attribute( QStringLiteral( "rank" ) ).toInt(), 4 );
  106. QVERIFY( mModel->moveItems( 1, 3 ) );
  107. QVERIFY( QSignalSpy( mModel, &ReferencingFeatureListModel::modelUpdated ).wait( 1000 ) );
  108. QCOMPARE( mModel->rowCount(), 4 );
  109. QCOMPARE( mReferencingLayer->getFeature( 1 ).attribute( QStringLiteral( "rank" ) ).toInt(), 1 );
  110. QCOMPARE( mReferencingLayer->getFeature( 2 ).attribute( QStringLiteral( "rank" ) ).toInt(), 4 );
  111. QCOMPARE( mReferencingLayer->getFeature( 3 ).attribute( QStringLiteral( "rank" ) ).toInt(), 2 );
  112. QCOMPARE( mReferencingLayer->getFeature( 4 ).attribute( QStringLiteral( "rank" ) ).toInt(), 3 );
  113. // check feature ordering
  114. QCOMPARE( mModel->data( mModel->index( 0, 0 ), OrderedRelationModel::FeatureIdRole ), 1 );
  115. QCOMPARE( mModel->data( mModel->index( 1, 0 ), OrderedRelationModel::FeatureIdRole ), 3 );
  116. QCOMPARE( mModel->data( mModel->index( 2, 0 ), OrderedRelationModel::FeatureIdRole ), 4 );
  117. QCOMPARE( mModel->data( mModel->index( 3, 0 ), OrderedRelationModel::FeatureIdRole ), 2 );
  118. }
  119. private:
  120. std::unique_ptr<QgsVectorLayer> mReferencedLayer;
  121. std::unique_ptr<QgsVectorLayer> mReferencingLayer;
  122. QgsRelation mRelation;
  123. OrderedRelationModel *mModel;
  124. };
  125. QFIELDTEST_MAIN( TestOrderedRelationModel )
  126. #include "test_orderedrelationmodel.moc"