Skip to content
Permalink
Browse files

Add unit tests for joins - removal of joined layer, resolve refs

  • Loading branch information
wonder-sk committed Jan 31, 2017
1 parent 7683cfa commit f79fc2509b2ecf5ab32e0bd016cc36d39ac0575d
Showing with 60 additions and 2 deletions.
  1. +60 −2 tests/src/core/testqgsvectorlayerjoinbuffer.cpp
@@ -60,6 +60,8 @@ class TestVectorLayerJoinBuffer : public QObject
void testJoinLayerDefinitionFile();
void testCacheUpdate_data();
void testCacheUpdate();
void testRemoveJoinOnLayerDelete();
void testResolveReferences();

private:
QgsProject mProject;
@@ -599,9 +601,65 @@ void TestVectorLayerJoinBuffer::testCacheUpdate()
fi.nextFeature( fA2 );
QCOMPARE( fA2.attribute( "id_a" ).toInt(), 3 );
QCOMPARE( fA2.attribute( "B_value_b" ).toInt(), 12 );
}

void TestVectorLayerJoinBuffer::testRemoveJoinOnLayerDelete()
{
QgsVectorLayer* vlA = new QgsVectorLayer( QStringLiteral( "Point?field=id_a:integer" ), QStringLiteral( "cacheA" ), QStringLiteral( "memory" ) );
QVERIFY( vlA->isValid() );
QgsVectorLayer* vlB = new QgsVectorLayer( QStringLiteral( "Point?field=id_b:integer&field=value_b" ), QStringLiteral( "cacheB" ), QStringLiteral( "memory" ) );
QVERIFY( vlB->isValid() );

QgsVectorLayerJoinInfo joinInfo;
joinInfo.setTargetFieldName( QStringLiteral( "id_a" ) );
joinInfo.setJoinLayer( vlB );
joinInfo.setJoinFieldName( QStringLiteral( "id_b" ) );
joinInfo.setUsingMemoryCache( true );
joinInfo.setPrefix( QStringLiteral( "B_" ) );
vlA->addJoin( joinInfo );

QCOMPARE( vlA->vectorJoins().count(), 1 );
QCOMPARE( vlA->vectorJoins()[0].joinLayer(), vlB );
QCOMPARE( vlA->vectorJoins()[0].joinLayerId(), vlB->id() );
QCOMPARE( vlA->fields().count(), 2 );

delete vlB;

QCOMPARE( vlA->vectorJoins().count(), 0 );
QCOMPARE( vlA->fields().count(), 1 );

delete vlA;
}

void TestVectorLayerJoinBuffer::testResolveReferences()
{
QgsVectorLayer* vlA = new QgsVectorLayer( QStringLiteral( "Point?field=id_a:integer" ), QStringLiteral( "cacheA" ), QStringLiteral( "memory" ) );
QVERIFY( vlA->isValid() );
QgsVectorLayer* vlB = new QgsVectorLayer( QStringLiteral( "Point?field=id_b:integer&field=value_b" ), QStringLiteral( "cacheB" ), QStringLiteral( "memory" ) );
QVERIFY( vlB->isValid() );

QgsVectorLayerJoinInfo joinInfo;
joinInfo.setTargetFieldName( QStringLiteral( "id_a" ) );
joinInfo.setJoinLayerId( vlB->id() );
joinInfo.setJoinFieldName( QStringLiteral( "id_b" ) );
joinInfo.setUsingMemoryCache( true );
joinInfo.setPrefix( QStringLiteral( "B_" ) );
vlA->addJoin( joinInfo );

QCOMPARE( vlA->fields().count(), 1 );
QCOMPARE( vlA->vectorJoins()[0].joinLayer(), ( QgsVectorLayer* ) nullptr );
QCOMPARE( vlA->vectorJoins()[0].joinLayerId(), vlB->id() );

QgsProject project;
project.addMapLayer( vlB );

vlA->resolveReferences( &project );

QCOMPARE( vlA->fields().count(), 2 );
QCOMPARE( vlA->vectorJoins()[0].joinLayer(), vlB );
QCOMPARE( vlA->vectorJoins()[0].joinLayerId(), vlB->id() );

QgsProject::instance()->removeMapLayer( vlA );
QgsProject::instance()->removeMapLayer( vlB );
delete vlA;
}


0 comments on commit f79fc25

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