Skip to content
Permalink
Browse files

added a test for vector joins in layer definition files

  • Loading branch information
SebDieBln committed Jan 1, 2016
1 parent b2b44cc commit c136f9f550e24f3ad33cda8a814a10197a7adbd3
Showing with 55 additions and 0 deletions.
  1. +55 −0 tests/src/core/testqgsvectorlayerjoinbuffer.cpp
@@ -23,6 +23,8 @@
#include <qgsapplication.h>
#include <qgsvectorlayerjoinbuffer.h>
#include <qgsmaplayerregistry.h>
#include <qgslayerdefinition.h>
#include <qgsproject.h>

/** @ingroup UnitTests
* This is a unit test for the vector layer join buffer
@@ -53,6 +55,7 @@ class TestVectorLayerJoinBuffer : public QObject
void testJoinSubset_data();
void testJoinSubset();
void testJoinTwoTimes();
void testJoinLayerDefinitionFile();

private:
QgsVectorLayer* mLayerA;
@@ -339,6 +342,58 @@ void TestVectorLayerJoinBuffer::testJoinTwoTimes()
QCOMPARE( mLayerA->vectorJoins().count(), 0 );
}

void TestVectorLayerJoinBuffer::testJoinLayerDefinitionFile()
{
bool r;

QgsMapLayerRegistry::instance()->removeAllMapLayers();

// Create two layers
QgsVectorLayer* layerA = new QgsVectorLayer( "Point?crs=epsg:4326&field=key:integer&field=value:double&index=yes", "layerA", "memory" );
QVERIFY( layerA );
QgsMapLayerRegistry::instance()->addMapLayer( layerA );

QgsVectorLayer* layerB = new QgsVectorLayer( "Point?crs=epsg:4326&field=id:integer&index=yes", "layerB", "memory" );
QVERIFY( layerB );
QgsMapLayerRegistry::instance()->addMapLayer( layerB );

// Create vector join
QgsVectorJoinInfo joinInfo;
joinInfo.targetFieldName = "id";
joinInfo.joinLayerId = layerA->id();
joinInfo.joinFieldName = "key";
joinInfo.memoryCache = true;
joinInfo.prefix = "joined_";
r = layerB->addJoin( joinInfo );
QVERIFY( r );

// Generate QLR
QDomDocument qlrDoc( "qgis-layer-definition" );
QString errorMessage;
r = QgsLayerDefinition::exportLayerDefinition( qlrDoc, QgsProject::instance()->layerTreeRoot()->children(), errorMessage );
QVERIFY2( r, errorMessage.toUtf8().constData() );

// Clear
QgsMapLayerRegistry::instance()->removeAllMapLayers();

// Load QLR
r = QgsLayerDefinition::loadLayerDefinition( qlrDoc, QgsProject::instance()->layerTreeRoot(), errorMessage );
QVERIFY2( r, errorMessage.toUtf8().constData() );

// Get layer
QList<QgsMapLayer*> mapLayers = QgsMapLayerRegistry::instance()->mapLayersByName( "layerB" );
QCOMPARE( mapLayers.count(), 1 );

QgsVectorLayer* vLayer = static_cast<QgsVectorLayer*>( mapLayers.value( 0 ) );
QVERIFY( vLayer );

// Check for vector join
QCOMPARE( vLayer->vectorJoins().count(), 1 );

// Check for joined field
QVERIFY( vLayer->fieldNameIndex( joinInfo.prefix + "value" ) >= 0 );
}


QTEST_MAIN( TestVectorLayerJoinBuffer )
#include "testqgsvectorlayerjoinbuffer.moc"

0 comments on commit c136f9f

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