|
23 | 23 | #include <qgsapplication.h>
|
24 | 24 | #include <qgsvectorlayerjoinbuffer.h>
|
25 | 25 | #include <qgsmaplayerregistry.h>
|
| 26 | +#include <qgslayerdefinition.h> |
| 27 | +#include <qgsproject.h> |
26 | 28 |
|
27 | 29 | /** @ingroup UnitTests
|
28 | 30 | * This is a unit test for the vector layer join buffer
|
@@ -53,6 +55,7 @@ class TestVectorLayerJoinBuffer : public QObject
|
53 | 55 | void testJoinSubset_data();
|
54 | 56 | void testJoinSubset();
|
55 | 57 | void testJoinTwoTimes();
|
| 58 | + void testJoinLayerDefinitionFile(); |
56 | 59 |
|
57 | 60 | private:
|
58 | 61 | QgsVectorLayer* mLayerA;
|
@@ -339,6 +342,58 @@ void TestVectorLayerJoinBuffer::testJoinTwoTimes()
|
339 | 342 | QCOMPARE( mLayerA->vectorJoins().count(), 0 );
|
340 | 343 | }
|
341 | 344 |
|
| 345 | +void TestVectorLayerJoinBuffer::testJoinLayerDefinitionFile() |
| 346 | +{ |
| 347 | + bool r; |
| 348 | + |
| 349 | + QgsMapLayerRegistry::instance()->removeAllMapLayers(); |
| 350 | + |
| 351 | + // Create two layers |
| 352 | + QgsVectorLayer* layerA = new QgsVectorLayer( "Point?crs=epsg:4326&field=key:integer&field=value:double&index=yes", "layerA", "memory" ); |
| 353 | + QVERIFY( layerA ); |
| 354 | + QgsMapLayerRegistry::instance()->addMapLayer( layerA ); |
| 355 | + |
| 356 | + QgsVectorLayer* layerB = new QgsVectorLayer( "Point?crs=epsg:4326&field=id:integer&index=yes", "layerB", "memory" ); |
| 357 | + QVERIFY( layerB ); |
| 358 | + QgsMapLayerRegistry::instance()->addMapLayer( layerB ); |
| 359 | + |
| 360 | + // Create vector join |
| 361 | + QgsVectorJoinInfo joinInfo; |
| 362 | + joinInfo.targetFieldName = "id"; |
| 363 | + joinInfo.joinLayerId = layerA->id(); |
| 364 | + joinInfo.joinFieldName = "key"; |
| 365 | + joinInfo.memoryCache = true; |
| 366 | + joinInfo.prefix = "joined_"; |
| 367 | + r = layerB->addJoin( joinInfo ); |
| 368 | + QVERIFY( r ); |
| 369 | + |
| 370 | + // Generate QLR |
| 371 | + QDomDocument qlrDoc( "qgis-layer-definition" ); |
| 372 | + QString errorMessage; |
| 373 | + r = QgsLayerDefinition::exportLayerDefinition( qlrDoc, QgsProject::instance()->layerTreeRoot()->children(), errorMessage ); |
| 374 | + QVERIFY2( r, errorMessage.toUtf8().constData() ); |
| 375 | + |
| 376 | + // Clear |
| 377 | + QgsMapLayerRegistry::instance()->removeAllMapLayers(); |
| 378 | + |
| 379 | + // Load QLR |
| 380 | + r = QgsLayerDefinition::loadLayerDefinition( qlrDoc, QgsProject::instance()->layerTreeRoot(), errorMessage ); |
| 381 | + QVERIFY2( r, errorMessage.toUtf8().constData() ); |
| 382 | + |
| 383 | + // Get layer |
| 384 | + QList<QgsMapLayer*> mapLayers = QgsMapLayerRegistry::instance()->mapLayersByName( "layerB" ); |
| 385 | + QCOMPARE( mapLayers.count(), 1 ); |
| 386 | + |
| 387 | + QgsVectorLayer* vLayer = static_cast<QgsVectorLayer*>( mapLayers.value( 0 ) ); |
| 388 | + QVERIFY( vLayer ); |
| 389 | + |
| 390 | + // Check for vector join |
| 391 | + QCOMPARE( vLayer->vectorJoins().count(), 1 ); |
| 392 | + |
| 393 | + // Check for joined field |
| 394 | + QVERIFY( vLayer->fieldNameIndex( joinInfo.prefix + "value" ) >= 0 ); |
| 395 | +} |
| 396 | + |
342 | 397 |
|
343 | 398 | QTEST_MAIN( TestVectorLayerJoinBuffer )
|
344 | 399 | #include "testqgsvectorlayerjoinbuffer.moc"
|
|
0 commit comments