mirror of
https://github.com/qgis/QGIS.git
synced 2025-04-17 00:04:02 -04:00
test vector join: add PostgreSQL provider
This commit is contained in:
parent
c288311553
commit
13ccf70e1a
@ -1,4 +1,5 @@
|
||||
printf "[qgis_test]\nhost=localhost\ndbname=qgis_test\nuser=postgres" > ~/.pg_service.conf
|
||||
psql -c 'CREATE DATABASE qgis_test;' -U postgres
|
||||
psql -f $TRAVIS_BUILD_DIR/tests/testdata/provider/testdata.sql -U postgres -d qgis_test
|
||||
psql -f $TRAVIS_BUILD_DIR/tests/testdata/provider/reltests.sql -U postgres -d qgis_test
|
||||
psql -f $TRAVIS_BUILD_DIR/tests/testdata/provider/testdata_pg.sql -U postgres -d qgis_test
|
||||
psql -f $TRAVIS_BUILD_DIR/tests/testdata/provider/testdata_pg_reltests.sql -U postgres -d qgis_test
|
||||
psql -f $TRAVIS_BUILD_DIR/tests/testdata/provider/testdata_pg_vectorjoin.sql -U postgres -d qgis_test
|
||||
|
@ -85,7 +85,9 @@ bool QgsVectorLayerJoinBuffer::addJoin( const QgsVectorJoinInfo& joinInfo )
|
||||
// but then QgsProject makes sure to call createJoinCaches() which will do the connection.
|
||||
// Unique connection makes sure we do not respond to one layer's update more times (in case of multiple join)
|
||||
if ( QgsVectorLayer* vl = qobject_cast<QgsVectorLayer*>( QgsMapLayerRegistry::instance()->mapLayer( joinInfo.joinLayerId ) ) )
|
||||
{
|
||||
connect( vl, SIGNAL( updatedFields() ), this, SLOT( joinedLayerUpdatedFields() ), Qt::UniqueConnection );
|
||||
}
|
||||
|
||||
emit joinedFieldsChanged();
|
||||
return true;
|
||||
|
@ -33,6 +33,13 @@ INCLUDE_DIRECTORIES(SYSTEM
|
||||
ADD_DEFINITIONS(-DTEST_DATA_DIR="\\"${TEST_DATA_DIR}\\"")
|
||||
|
||||
ADD_DEFINITIONS(-DINSTALL_PREFIX="\\"${CMAKE_INSTALL_PREFIX}\\"")
|
||||
|
||||
# enable postgresql tests
|
||||
SET (ENABLE_PGTEST FALSE CACHE BOOL "Enable PostgreSQL provider tests")
|
||||
IF ( ENABLE_PGTEST )
|
||||
ADD_DEFINITIONS( "-DENABLE_PGTEST" )
|
||||
ENDIF ()
|
||||
|
||||
#############################################################
|
||||
# libraries
|
||||
|
||||
|
@ -37,9 +37,7 @@ class TestVectorLayerJoinBuffer : public QObject
|
||||
|
||||
public:
|
||||
TestVectorLayerJoinBuffer()
|
||||
: mLayerA( 0 )
|
||||
, mLayerB( 0 )
|
||||
, mLayerC( 0 )
|
||||
: mLayers( QMap<QPair<QString, QString>, QgsVectorLayer*>() )
|
||||
{}
|
||||
|
||||
private slots:
|
||||
@ -50,17 +48,20 @@ class TestVectorLayerJoinBuffer : public QObject
|
||||
|
||||
void testJoinBasic_data();
|
||||
void testJoinBasic();
|
||||
void testJoinTransitive_data();
|
||||
void testJoinTransitive();
|
||||
void testJoinDetectCycle_data();
|
||||
void testJoinDetectCycle();
|
||||
void testJoinSubset_data();
|
||||
void testJoinSubset();
|
||||
void testJoinTwoTimes_data();
|
||||
void testJoinTwoTimes();
|
||||
void testJoinLayerDefinitionFile();
|
||||
|
||||
private:
|
||||
QgsVectorLayer* mLayerA;
|
||||
QgsVectorLayer* mLayerB;
|
||||
QgsVectorLayer* mLayerC;
|
||||
QList<QString> mProviders;
|
||||
// map of layers. First key is the name of the layer A, B or C and second key is the provider memory or PG.
|
||||
QMap<QPair<QString, QString>, QgsVectorLayer*> mLayers;
|
||||
};
|
||||
|
||||
// runs before all tests
|
||||
@ -69,49 +70,116 @@ void TestVectorLayerJoinBuffer::initTestCase()
|
||||
QgsApplication::init();
|
||||
QgsApplication::initQgis();
|
||||
|
||||
// Set up the QSettings environment
|
||||
QCoreApplication::setOrganizationName( "QGIS" );
|
||||
QCoreApplication::setOrganizationDomain( "qgis.org" );
|
||||
QCoreApplication::setApplicationName( "QGIS-TEST" );
|
||||
|
||||
mProviders = QList<QString>() << "memory";
|
||||
|
||||
// Create memory layers
|
||||
// LAYER A //
|
||||
|
||||
mLayerA = new QgsVectorLayer( "Point?field=id_a:integer", "A", "memory" );
|
||||
QVERIFY( mLayerA->isValid() );
|
||||
QVERIFY( mLayerA->fields().count() == 1 );
|
||||
|
||||
QgsFeature fA1( mLayerA->dataProvider()->fields(), 1 );
|
||||
fA1.setAttribute( "id_a", 1 );
|
||||
QgsFeature fA2( mLayerA->dataProvider()->fields(), 2 );
|
||||
fA2.setAttribute( "id_a", 2 );
|
||||
mLayerA->dataProvider()->addFeatures( QgsFeatureList() << fA1 << fA2 );
|
||||
QVERIFY( mLayerA->featureCount() == 2 );
|
||||
|
||||
QgsVectorLayer* vlA = new QgsVectorLayer( "Point?field=id_a:integer", "A", "memory" );
|
||||
QVERIFY( vlA->isValid() );
|
||||
QVERIFY( vlA->fields().count() == 1 );
|
||||
// LAYER B //
|
||||
QgsVectorLayer* vlB = new QgsVectorLayer( "Point?field=id_b:integer&field=value_b", "B", "memory" );
|
||||
QVERIFY( vlB->isValid() );
|
||||
QVERIFY( vlB->fields().count() == 2 );
|
||||
// LAYER C //
|
||||
QgsVectorLayer* vlC = new QgsVectorLayer( "Point?field=id_c:integer&field=value_c", "C", "memory" );
|
||||
QVERIFY( vlC->isValid() );
|
||||
QVERIFY( vlC->fields().count() == 2 );
|
||||
// LAYER X //
|
||||
QgsVectorLayer* vlX = new QgsVectorLayer( "Point?field=id_x:integer&field=value_x1:integer&field=value_x2", "X", "memory" );
|
||||
QVERIFY( vlX->isValid() );
|
||||
QVERIFY( vlX->fields().count() == 3 );
|
||||
|
||||
mLayerB = new QgsVectorLayer( "Point?field=id_b:integer&field=value_b", "B", "memory" );
|
||||
QVERIFY( mLayerB->isValid() );
|
||||
QVERIFY( mLayerB->fields().count() == 2 );
|
||||
mLayers = QMap<QPair<QString, QString>, QgsVectorLayer*>();
|
||||
mLayers.insert( QPair<QString, QString>( "A", "memory" ), vlA );
|
||||
mLayers.insert( QPair<QString, QString>( "B", "memory" ), vlB );
|
||||
mLayers.insert( QPair<QString, QString>( "C", "memory" ), vlC );
|
||||
mLayers.insert( QPair<QString, QString>( "X", "memory" ), vlX );
|
||||
|
||||
QgsFeature fB1( mLayerB->dataProvider()->fields(), 1 );
|
||||
// Add PG layers
|
||||
#ifdef ENABLE_PGTEST
|
||||
QString dbConn = getenv( "QGIS_PGTEST_DB" );
|
||||
if ( dbConn.isEmpty() )
|
||||
{
|
||||
dbConn = "dbname='qgis_test' host=localhost port=5432 user='postgres' password='postgres'";
|
||||
}
|
||||
QgsVectorLayer* vlA_PG = new QgsVectorLayer( QString( "%1 sslmode=disable key='id_a' table=\"qgis_test\".\"table_a\" sql=" ).arg( dbConn ), "A_PG", "postgres" );
|
||||
QgsVectorLayer* vlB_PG = new QgsVectorLayer( QString( "%1 sslmode=disable key='id_b' table=\"qgis_test\".\"table_b\" sql=" ).arg( dbConn ), "B_PG", "postgres" );
|
||||
QgsVectorLayer* vlC_PG = new QgsVectorLayer( QString( "%1 sslmode=disable key='id_c' table=\"qgis_test\".\"table_c\" sql=" ).arg( dbConn ), "C_PG", "postgres" );
|
||||
QgsVectorLayer* vlX_PG = new QgsVectorLayer( QString( "%1 sslmode=disable key='id_x' table=\"qgis_test\".\"table_x\" sql=" ).arg( dbConn ), "X_PG", "postgres" );
|
||||
QVERIFY( vlA_PG->isValid() );
|
||||
QVERIFY( vlB_PG->isValid() );
|
||||
QVERIFY( vlC_PG->isValid() );
|
||||
QVERIFY( vlX_PG->isValid() );
|
||||
QVERIFY( vlA_PG->fields().count() == 1 );
|
||||
QVERIFY( vlB_PG->fields().count() == 2 );
|
||||
QVERIFY( vlC_PG->fields().count() == 2 );
|
||||
QVERIFY( vlX_PG->fields().count() == 3 );
|
||||
mLayers.insert( QPair<QString, QString>( "A", "PG" ), vlA_PG );
|
||||
mLayers.insert( QPair<QString, QString>( "B", "PG" ), vlB_PG );
|
||||
mLayers.insert( QPair<QString, QString>( "C", "PG" ), vlC_PG );
|
||||
mLayers.insert( QPair<QString, QString>( "X", "PG" ), vlX_PG );
|
||||
mProviders << "PG";
|
||||
#endif
|
||||
|
||||
// Create features
|
||||
QgsFeature fA1( vlA->dataProvider()->fields(), 1 );
|
||||
fA1.setAttribute( "id_a", 1 );
|
||||
QgsFeature fA2( vlA->dataProvider()->fields(), 2 );
|
||||
fA2.setAttribute( "id_a", 2 );
|
||||
QgsFeature fB1( vlB->dataProvider()->fields(), 1 );
|
||||
fB1.setAttribute( "id_b", 1 );
|
||||
fB1.setAttribute( "value_b", 11 );
|
||||
QgsFeature fB2( mLayerB->dataProvider()->fields(), 2 );
|
||||
QgsFeature fB2( vlB->dataProvider()->fields(), 2 );
|
||||
fB2.setAttribute( "id_b", 2 );
|
||||
fB2.setAttribute( "value_b", 12 );
|
||||
mLayerB->dataProvider()->addFeatures( QgsFeatureList() << fB1 << fB2 );
|
||||
QVERIFY( mLayerB->featureCount() == 2 );
|
||||
|
||||
// LAYER C //
|
||||
|
||||
mLayerC = new QgsVectorLayer( "Point?field=id_c:integer&field=value_c", "C", "memory" );
|
||||
QVERIFY( mLayerC->isValid() );
|
||||
QVERIFY( mLayerC->fields().count() == 2 );
|
||||
|
||||
QgsFeature fC1( mLayerC->dataProvider()->fields(), 1 );
|
||||
QgsFeature fC1( vlC->dataProvider()->fields(), 1 );
|
||||
fC1.setAttribute( "id_c", 1 );
|
||||
fC1.setAttribute( "value_c", 101 );
|
||||
mLayerC->dataProvider()->addFeatures( QgsFeatureList() << fC1 );
|
||||
QVERIFY( mLayerC->featureCount() == 1 );
|
||||
QgsFeature fX1( vlX->dataProvider()->fields(), 1 );
|
||||
fX1.setAttribute( "id_x", 1 );
|
||||
fX1.setAttribute( "value_x1", 111 );
|
||||
fX1.setAttribute( "value_x2", 222 );
|
||||
|
||||
QgsMapLayerRegistry::instance()->addMapLayer( mLayerA );
|
||||
QgsMapLayerRegistry::instance()->addMapLayer( mLayerB );
|
||||
QgsMapLayerRegistry::instance()->addMapLayer( mLayerC );
|
||||
// Commit features and layers to qgis
|
||||
Q_FOREACH ( const QString provider, mProviders )
|
||||
{
|
||||
QgsVectorLayer* vl = mLayers.value( QPair<QString, QString>( "A", provider ) );
|
||||
vl->dataProvider()->addFeatures( QgsFeatureList() << fA1 << fA2 );
|
||||
QVERIFY( vl->featureCount() == 2 );
|
||||
QgsMapLayerRegistry::instance()->addMapLayer( vl );
|
||||
}
|
||||
|
||||
Q_FOREACH ( const QString provider, mProviders )
|
||||
{
|
||||
QgsVectorLayer* vl = mLayers.value( QPair<QString, QString>( "B", provider ) );
|
||||
vl->dataProvider()->addFeatures( QgsFeatureList() << fB1 << fB2 );
|
||||
QVERIFY( vl->featureCount() == 2 );
|
||||
QgsMapLayerRegistry::instance()->addMapLayer( vl );
|
||||
}
|
||||
|
||||
Q_FOREACH ( const QString provider, mProviders )
|
||||
{
|
||||
QgsVectorLayer* vl = mLayers.value( QPair<QString, QString>( "C", provider ) );
|
||||
vl->dataProvider()->addFeatures( QgsFeatureList() << fC1 );
|
||||
QVERIFY( vl->featureCount() == 1 );
|
||||
QgsMapLayerRegistry::instance()->addMapLayer( vl );
|
||||
}
|
||||
|
||||
Q_FOREACH ( const QString provider, mProviders )
|
||||
{
|
||||
QgsVectorLayer* vl = mLayers.value( QPair<QString, QString>( "X", provider ) );
|
||||
vl->dataProvider()->addFeatures( QgsFeatureList() << fX1 );
|
||||
QVERIFY( vl->featureCount() == 1 );
|
||||
QgsMapLayerRegistry::instance()->addMapLayer( vl );
|
||||
}
|
||||
|
||||
QVERIFY( QgsMapLayerRegistry::instance()->mapLayers().count() == 4*mProviders.count() );
|
||||
}
|
||||
|
||||
void TestVectorLayerJoinBuffer::init()
|
||||
@ -129,28 +197,39 @@ void TestVectorLayerJoinBuffer::cleanupTestCase()
|
||||
|
||||
void TestVectorLayerJoinBuffer::testJoinBasic_data()
|
||||
{
|
||||
QTest::addColumn<QString>( "provider" );
|
||||
QTest::addColumn<bool>( "memoryCache" );
|
||||
|
||||
QTest::newRow( "with cache" ) << true;
|
||||
QTest::newRow( "without cache" ) << false;
|
||||
QTest::newRow( "memory with cache" ) << "memory" << true ;
|
||||
QTest::newRow( "memory without cache" ) << "memory" << false;
|
||||
|
||||
#ifdef ENABLE_PGTEST
|
||||
QTest::newRow( "postgresql with cache" ) << "PG" << true ;
|
||||
QTest::newRow( "postgresql without cache" ) << "PG" << false;
|
||||
#endif
|
||||
}
|
||||
|
||||
void TestVectorLayerJoinBuffer::testJoinBasic()
|
||||
{
|
||||
QFETCH( bool, memoryCache );
|
||||
QFETCH( QString, provider );
|
||||
|
||||
QVERIFY( mLayerA->fields().count() == 1 );
|
||||
QgsVectorLayer* vlA = mLayers.value( QPair<QString, QString>( "A", provider ) );
|
||||
QgsVectorLayer* vlB = mLayers.value( QPair<QString, QString>( "B", provider ) );
|
||||
|
||||
QVERIFY( vlA->fields().count() == 1 );
|
||||
|
||||
QgsVectorJoinInfo joinInfo;
|
||||
joinInfo.targetFieldName = "id_a";
|
||||
joinInfo.joinLayerId = mLayerB->id();
|
||||
joinInfo.joinLayerId = vlB->id();
|
||||
joinInfo.joinFieldName = "id_b";
|
||||
joinInfo.memoryCache = memoryCache;
|
||||
mLayerA->addJoin( joinInfo );
|
||||
joinInfo.prefix = "B_";
|
||||
vlA->addJoin( joinInfo );
|
||||
|
||||
QVERIFY( mLayerA->fields().count() == 2 );
|
||||
QVERIFY( vlA->fields().count() == 2 );
|
||||
|
||||
QgsFeatureIterator fi = mLayerA->getFeatures();
|
||||
QgsFeatureIterator fi = vlA->getFeatures();
|
||||
QgsFeature fA1, fA2;
|
||||
fi.nextFeature( fA1 );
|
||||
QCOMPARE( fA1.attribute( "id_a" ).toInt(), 1 );
|
||||
@ -159,43 +238,61 @@ void TestVectorLayerJoinBuffer::testJoinBasic()
|
||||
QCOMPARE( fA2.attribute( "id_a" ).toInt(), 2 );
|
||||
QCOMPARE( fA2.attribute( "B_value_b" ).toInt(), 12 );
|
||||
|
||||
mLayerA->removeJoin( mLayerB->id() );
|
||||
vlA->removeJoin( vlB->id() );
|
||||
|
||||
QVERIFY( mLayerA->fields().count() == 1 );
|
||||
QVERIFY( vlA->fields().count() == 1 );
|
||||
}
|
||||
|
||||
void TestVectorLayerJoinBuffer::testJoinTransitive_data()
|
||||
{
|
||||
QTest::addColumn<QString>( "provider" );
|
||||
QTest::newRow( "memory" ) << "memory";
|
||||
#ifdef ENABLE_PGTEST
|
||||
QTest::newRow( "postgresql" ) << "PG";
|
||||
#endif
|
||||
}
|
||||
|
||||
void TestVectorLayerJoinBuffer::testJoinTransitive()
|
||||
{
|
||||
|
||||
QFETCH( QString, provider );
|
||||
|
||||
QgsVectorLayer* vlA = mLayers.value( QPair<QString, QString>( "A", provider ) );
|
||||
QgsVectorLayer* vlB = mLayers.value( QPair<QString, QString>( "B", provider ) );
|
||||
QgsVectorLayer* vlC = mLayers.value( QPair<QString, QString>( "C", provider ) );
|
||||
|
||||
// test join A -> B -> C
|
||||
// first we join A -> B and after that B -> C
|
||||
// layer A should automatically update to include joined data from C
|
||||
|
||||
QVERIFY( mLayerA->fields().count() == 1 ); // id_a
|
||||
QVERIFY( vlA->fields().count() == 1 ); // id_a
|
||||
|
||||
// add join A -> B
|
||||
|
||||
QgsVectorJoinInfo joinInfo1;
|
||||
joinInfo1.targetFieldName = "id_a";
|
||||
joinInfo1.joinLayerId = mLayerB->id();
|
||||
joinInfo1.joinLayerId = vlB->id();
|
||||
joinInfo1.joinFieldName = "id_b";
|
||||
joinInfo1.memoryCache = true;
|
||||
mLayerA->addJoin( joinInfo1 );
|
||||
QVERIFY( mLayerA->fields().count() == 2 ); // id_a, B_value_b
|
||||
joinInfo1.prefix = "B_";
|
||||
vlA->addJoin( joinInfo1 );
|
||||
QVERIFY( vlA->fields().count() == 2 ); // id_a, B_value_b
|
||||
|
||||
// add join B -> C
|
||||
|
||||
QgsVectorJoinInfo joinInfo2;
|
||||
joinInfo2.targetFieldName = "id_b";
|
||||
joinInfo2.joinLayerId = mLayerC->id();
|
||||
joinInfo2.joinLayerId = vlC->id();
|
||||
joinInfo2.joinFieldName = "id_c";
|
||||
joinInfo2.memoryCache = true;
|
||||
mLayerB->addJoin( joinInfo2 );
|
||||
QVERIFY( mLayerB->fields().count() == 3 ); // id_b, value_b, C_value_c
|
||||
joinInfo2.prefix = "C_";
|
||||
vlB->addJoin( joinInfo2 );
|
||||
QVERIFY( vlB->fields().count() == 3 ); // id_b, value_b, C_value_c
|
||||
|
||||
// now layer A must include also data from layer C
|
||||
QVERIFY( mLayerA->fields().count() == 3 ); // id_a, B_value_b, B_C_value_c
|
||||
QVERIFY( vlA->fields().count() == 3 ); // id_a, B_value_b, B_C_value_c
|
||||
|
||||
QgsFeatureIterator fi = mLayerA->getFeatures();
|
||||
QgsFeatureIterator fi = vlA->getFeatures();
|
||||
QgsFeature fA1;
|
||||
fi.nextFeature( fA1 );
|
||||
QCOMPARE( fA1.attribute( "id_a" ).toInt(), 1 );
|
||||
@ -203,143 +300,171 @@ void TestVectorLayerJoinBuffer::testJoinTransitive()
|
||||
QCOMPARE( fA1.attribute( "B_C_value_c" ).toInt(), 101 );
|
||||
|
||||
// test that layer A gets updated when layer C changes its fields
|
||||
mLayerC->addExpressionField( "123", QgsField( "dummy", QVariant::Int ) );
|
||||
QVERIFY( mLayerA->fields().count() == 4 ); // id_a, B_value_b, B_C_value_c, B_C_dummy
|
||||
mLayerC->removeExpressionField( 0 );
|
||||
vlC->addExpressionField( "123", QgsField( "dummy", QVariant::Int ) );
|
||||
QVERIFY( vlA->fields().count() == 4 ); // id_a, B_value_b, B_C_value_c, B_C_dummy
|
||||
vlC->removeExpressionField( 0 );
|
||||
|
||||
// cleanup
|
||||
mLayerA->removeJoin( mLayerB->id() );
|
||||
mLayerB->removeJoin( mLayerC->id() );
|
||||
vlA->removeJoin( vlB->id() );
|
||||
vlB->removeJoin( vlC->id() );
|
||||
}
|
||||
|
||||
void TestVectorLayerJoinBuffer::testJoinDetectCycle_data()
|
||||
{
|
||||
QTest::addColumn<QString>( "provider" );
|
||||
QTest::newRow( "memory" ) << "memory";
|
||||
#ifdef ENABLE_PGTEST
|
||||
QTest::newRow( "postgresql" ) << "PG";
|
||||
#endif
|
||||
}
|
||||
|
||||
void TestVectorLayerJoinBuffer::testJoinDetectCycle()
|
||||
{
|
||||
QFETCH( QString, provider );
|
||||
|
||||
QgsVectorLayer* vlA = mLayers.value( QPair<QString, QString>( "A", provider ) );
|
||||
QgsVectorLayer* vlB = mLayers.value( QPair<QString, QString>( "B", provider ) );
|
||||
|
||||
// if A joins B and B joins A, we may get to an infinite loop if the case is not handled properly
|
||||
|
||||
QgsVectorJoinInfo joinInfo;
|
||||
joinInfo.targetFieldName = "id_a";
|
||||
joinInfo.joinLayerId = mLayerB->id();
|
||||
joinInfo.joinLayerId = vlB->id();
|
||||
joinInfo.joinFieldName = "id_b";
|
||||
joinInfo.memoryCache = true;
|
||||
mLayerA->addJoin( joinInfo );
|
||||
joinInfo.prefix = "B_";
|
||||
vlA->addJoin( joinInfo );
|
||||
|
||||
QgsVectorJoinInfo joinInfo2;
|
||||
joinInfo2.targetFieldName = "id_b";
|
||||
joinInfo2.joinLayerId = mLayerA->id();
|
||||
joinInfo2.joinLayerId = vlA->id();
|
||||
joinInfo2.joinFieldName = "id_a";
|
||||
joinInfo2.memoryCache = true;
|
||||
bool res = mLayerB->addJoin( joinInfo2 );
|
||||
joinInfo2.prefix = "A_";
|
||||
bool res = vlB->addJoin( joinInfo2 );
|
||||
|
||||
QVERIFY( !res );
|
||||
|
||||
// the join in layer B must be rejected
|
||||
QVERIFY( mLayerB->vectorJoins().isEmpty() );
|
||||
QVERIFY( vlB->vectorJoins().isEmpty() );
|
||||
|
||||
mLayerA->removeJoin( mLayerB->id() );
|
||||
vlA->removeJoin( vlB->id() );
|
||||
}
|
||||
|
||||
|
||||
void TestVectorLayerJoinBuffer::testJoinSubset_data()
|
||||
{
|
||||
QTest::addColumn<QString>( "provider" );
|
||||
QTest::addColumn<bool>( "memoryCache" );
|
||||
|
||||
QTest::newRow( "with cache" ) << true;
|
||||
QTest::newRow( "without cache" ) << false;
|
||||
QTest::newRow( "memory with cache" ) << "memory" << true ;
|
||||
QTest::newRow( "memory without cache" ) << "memory" << false;
|
||||
|
||||
#ifdef ENABLE_PGTEST
|
||||
QTest::newRow( "postgresql with cache" ) << "PG" << true ;
|
||||
QTest::newRow( "postgresql without cache" ) << "PG" << false;
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
void TestVectorLayerJoinBuffer::testJoinSubset()
|
||||
{
|
||||
QFETCH( bool, memoryCache );
|
||||
QFETCH( QString, provider );
|
||||
|
||||
QgsVectorLayer* layerX = new QgsVectorLayer( "Point?field=id_x:integer&field=value_x1:integer&field=value_x2", "X", "memory" );
|
||||
QVERIFY( layerX->isValid() );
|
||||
QVERIFY( layerX->fields().count() == 3 );
|
||||
QVERIFY( QgsMapLayerRegistry::instance()->mapLayers().count() == 4*mProviders.count() );
|
||||
|
||||
QgsFeature fX1( layerX->dataProvider()->fields(), 1 );
|
||||
fX1.setAttribute( "id_x", 1 );
|
||||
fX1.setAttribute( "value_x1", 111 );
|
||||
fX1.setAttribute( "value_x2", 222 );
|
||||
layerX->dataProvider()->addFeatures( QgsFeatureList() << fX1 );
|
||||
QVERIFY( layerX->featureCount() == 1 );
|
||||
|
||||
QgsMapLayerRegistry::instance()->addMapLayer( layerX );
|
||||
QgsVectorLayer* vlA = mLayers.value( QPair<QString, QString>( "A", provider ) );
|
||||
QgsVectorLayer* vlX = mLayers.value( QPair<QString, QString>( "X", provider ) );
|
||||
|
||||
// case 1: join without subset
|
||||
|
||||
QgsVectorJoinInfo joinInfo;
|
||||
joinInfo.targetFieldName = "id_a";
|
||||
joinInfo.joinLayerId = layerX->id();
|
||||
joinInfo.joinLayerId = vlX->id();
|
||||
joinInfo.joinFieldName = "id_x";
|
||||
joinInfo.memoryCache = memoryCache;
|
||||
mLayerA->addJoin( joinInfo );
|
||||
joinInfo.prefix = "X_";
|
||||
bool res = vlA->addJoin( joinInfo );
|
||||
QVERIFY( res );
|
||||
|
||||
QCOMPARE( mLayerA->fields().count(), 3 ); // id_a, X_value_x1, X_value_x2
|
||||
QgsFeatureIterator fi = mLayerA->getFeatures();
|
||||
QCOMPARE( vlA->fields().count(), 3 ); // id_a, X_value_x1, X_value_x2
|
||||
QgsFeatureIterator fi = vlA->getFeatures();
|
||||
QgsFeature fAX;
|
||||
fi.nextFeature( fAX );
|
||||
QCOMPARE( fAX.attribute( "id_a" ).toInt(), 1 );
|
||||
QCOMPARE( fAX.attribute( "X_value_x1" ).toInt(), 111 );
|
||||
QCOMPARE( fAX.attribute( "X_value_x2" ).toInt(), 222 );
|
||||
|
||||
mLayerA->removeJoin( layerX->id() );
|
||||
vlA->removeJoin( vlX->id() );
|
||||
|
||||
// case 2: join with subset
|
||||
|
||||
QStringList* subset = new QStringList;
|
||||
*subset << "value_x2";
|
||||
joinInfo.setJoinFieldNamesSubset( subset );
|
||||
mLayerA->addJoin( joinInfo );
|
||||
vlA->addJoin( joinInfo );
|
||||
|
||||
QCOMPARE( mLayerA->fields().count(), 2 ); // id_a, X_value_x2
|
||||
QCOMPARE( vlA->fields().count(), 2 ); // id_a, X_value_x2
|
||||
|
||||
fi = mLayerA->getFeatures();
|
||||
fi = vlA->getFeatures();
|
||||
fi.nextFeature( fAX );
|
||||
QCOMPARE( fAX.attribute( "id_a" ).toInt(), 1 );
|
||||
QCOMPARE( fAX.attribute( "X_value_x2" ).toInt(), 222 );
|
||||
|
||||
mLayerA->removeJoin( layerX->id() );
|
||||
|
||||
QgsMapLayerRegistry::instance()->removeMapLayer( layerX->id() );
|
||||
vlA->removeJoin( vlX->id() );
|
||||
}
|
||||
|
||||
void TestVectorLayerJoinBuffer::testJoinTwoTimes_data()
|
||||
{
|
||||
QTest::addColumn<QString>( "provider" );
|
||||
QTest::newRow( "memory" ) << "memory";
|
||||
#ifdef ENABLE_PGTEST
|
||||
QTest::newRow( "postgresql" ) << "PG";
|
||||
#endif
|
||||
}
|
||||
|
||||
void TestVectorLayerJoinBuffer::testJoinTwoTimes()
|
||||
{
|
||||
QVERIFY( mLayerA->fields().count() == 1 );
|
||||
|
||||
QFETCH( QString, provider );
|
||||
|
||||
QgsVectorLayer* vlA = mLayers.value( QPair<QString, QString>( "A", provider ) );
|
||||
QgsVectorLayer* vlB = mLayers.value( QPair<QString, QString>( "B", provider ) );
|
||||
|
||||
QVERIFY( vlA->fields().count() == 1 );
|
||||
|
||||
QgsVectorJoinInfo joinInfo1;
|
||||
joinInfo1.targetFieldName = "id_a";
|
||||
joinInfo1.joinLayerId = mLayerB->id();
|
||||
joinInfo1.joinLayerId = vlB->id();
|
||||
joinInfo1.joinFieldName = "id_b";
|
||||
joinInfo1.memoryCache = true;
|
||||
joinInfo1.prefix = "j1_";
|
||||
mLayerA->addJoin( joinInfo1 );
|
||||
vlA->addJoin( joinInfo1 );
|
||||
|
||||
QgsVectorJoinInfo joinInfo2;
|
||||
joinInfo2.targetFieldName = "id_a";
|
||||
joinInfo2.joinLayerId = mLayerB->id();
|
||||
joinInfo2.joinLayerId = vlB->id();
|
||||
joinInfo2.joinFieldName = "id_b";
|
||||
joinInfo2.memoryCache = true;
|
||||
joinInfo2.prefix = "j2_";
|
||||
mLayerA->addJoin( joinInfo2 );
|
||||
vlA->addJoin( joinInfo2 );
|
||||
|
||||
QCOMPARE( mLayerA->vectorJoins().count(), 2 );
|
||||
QCOMPARE( vlA->vectorJoins().count(), 2 );
|
||||
|
||||
QVERIFY( mLayerA->fields().count() == 3 );
|
||||
QVERIFY( vlA->fields().count() == 3 );
|
||||
|
||||
QgsFeatureIterator fi = mLayerA->getFeatures();
|
||||
QgsFeatureIterator fi = vlA->getFeatures();
|
||||
QgsFeature fA1; //, fA2;
|
||||
fi.nextFeature( fA1 );
|
||||
QCOMPARE( fA1.attribute( "id_a" ).toInt(), 1 );
|
||||
QCOMPARE( fA1.attribute( "j1_value_b" ).toInt(), 11 );
|
||||
QCOMPARE( fA1.attribute( "j2_value_b" ).toInt(), 11 );
|
||||
|
||||
mLayerA->removeJoin( mLayerB->id() );
|
||||
mLayerA->removeJoin( mLayerB->id() );
|
||||
vlA->removeJoin( vlB->id() );
|
||||
vlA->removeJoin( vlB->id() );
|
||||
|
||||
QCOMPARE( mLayerA->vectorJoins().count(), 0 );
|
||||
QCOMPARE( vlA->vectorJoins().count(), 0 );
|
||||
}
|
||||
|
||||
void TestVectorLayerJoinBuffer::testJoinLayerDefinitionFile()
|
||||
|
@ -1,4 +1,3 @@
|
||||
SET (ENABLE_PGTEST FALSE CACHE BOOL "Enable PostgreSQL provider tests")
|
||||
SET (ENABLE_MSSQLTEST FALSE CACHE BOOL "Enable MsSQL provider tests")
|
||||
|
||||
INCLUDE(UsePythonTest)
|
||||
|
@ -1,6 +1,8 @@
|
||||
-- Table: qgis_test.authors
|
||||
|
||||
-- DROP TABLE qgis_test.authors;
|
||||
DROP TABLE IF EXISTS qgis_test.books_authors;
|
||||
DROP TABLE IF EXISTS qgis_test.authors;
|
||||
DROP TABLE IF EXISTS qgis_test.books;
|
||||
|
||||
CREATE TABLE qgis_test.authors
|
||||
(
|
||||
@ -12,7 +14,6 @@ CREATE TABLE qgis_test.authors
|
||||
|
||||
-- Table: qgis_test.books
|
||||
|
||||
-- DROP TABLE qgis_test.books;
|
||||
|
||||
CREATE TABLE qgis_test.books
|
||||
(
|
||||
@ -24,7 +25,6 @@ CREATE TABLE qgis_test.books
|
||||
|
||||
-- Table: qgis_test.books_authors
|
||||
|
||||
-- DROP TABLE qgis_test.books_authors;
|
||||
|
||||
CREATE TABLE qgis_test.books_authors
|
||||
(
|
37
tests/testdata/provider/testdata_pg_vectorjoin.sql
vendored
Normal file
37
tests/testdata/provider/testdata_pg_vectorjoin.sql
vendored
Normal file
@ -0,0 +1,37 @@
|
||||
|
||||
CREATE SCHEMA IF NOT EXISTS qgis_test;
|
||||
|
||||
|
||||
DROP TABLE IF EXISTS qgis_test.table_a;
|
||||
DROP TABLE IF EXISTS qgis_test.table_b;
|
||||
DROP TABLE IF EXISTS qgis_test.table_c;
|
||||
DROP TABLE IF EXISTS qgis_test.table_x;
|
||||
|
||||
CREATE TABLE qgis_test.table_a
|
||||
(
|
||||
id_a integer NOT NULL,
|
||||
CONSTRAINT table_a_pkey PRIMARY KEY (id_a)
|
||||
);
|
||||
|
||||
CREATE TABLE qgis_test.table_b
|
||||
(
|
||||
id_b integer NOT NULL,
|
||||
value_b integer,
|
||||
CONSTRAINT table_b_pkey PRIMARY KEY (id_b)
|
||||
);
|
||||
|
||||
CREATE TABLE qgis_test.table_c
|
||||
(
|
||||
id_c integer NOT NULL,
|
||||
value_c integer,
|
||||
CONSTRAINT table_c_pkey PRIMARY KEY (id_c)
|
||||
);
|
||||
|
||||
CREATE TABLE qgis_test.table_x
|
||||
(
|
||||
id_x integer NOT NULL,
|
||||
value_x1 integer,
|
||||
value_x2 integer,
|
||||
CONSTRAINT table_x_pkey PRIMARY KEY (id_x)
|
||||
);
|
||||
|
Loading…
x
Reference in New Issue
Block a user