Skip to content

Commit

Permalink
added a test for vector joins in layer definition files
Browse files Browse the repository at this point in the history
  • Loading branch information
SebDieBln committed Jan 1, 2016
1 parent b2b44cc commit c136f9f
Showing 1 changed file with 55 additions and 0 deletions.
55 changes: 55 additions & 0 deletions tests/src/core/testqgsvectorlayerjoinbuffer.cpp
Expand Up @@ -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
Expand Down Expand Up @@ -53,6 +55,7 @@ class TestVectorLayerJoinBuffer : public QObject
void testJoinSubset_data();
void testJoinSubset();
void testJoinTwoTimes();
void testJoinLayerDefinitionFile();

private:
QgsVectorLayer* mLayerA;
Expand Down Expand Up @@ -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"
Expand Down

0 comments on commit c136f9f

Please sign in to comment.