From a368e4483ee6a2c20a8a2e973995a15b8f615a28 Mon Sep 17 00:00:00 2001 From: Alessandro Pasotti Date: Fri, 13 Dec 2019 13:49:37 +0100 Subject: [PATCH] Add tests for field formatter dependencies --- src/core/qgsopenclutils.cpp | 1 - tests/src/core/CMakeLists.txt | 1 + ...testqgsrelationreferencefieldformatter.cpp | 154 ++++++++++++++++++ .../testqgsvaluerelationfieldformatter.cpp | 154 ++++++++++++++++++ 4 files changed, 309 insertions(+), 1 deletion(-) create mode 100644 tests/src/core/testqgsrelationreferencefieldformatter.cpp create mode 100644 tests/src/core/testqgsvaluerelationfieldformatter.cpp diff --git a/src/core/qgsopenclutils.cpp b/src/core/qgsopenclutils.cpp index f00d90b92c2..003fdf0fc26 100644 --- a/src/core/qgsopenclutils.cpp +++ b/src/core/qgsopenclutils.cpp @@ -675,7 +675,6 @@ cl::Program QgsOpenClUtils::buildProgram( const QString &source, QgsOpenClUtils: QString err = QObject::tr( "Error building OpenCL program: %1" ) .arg( build_log ); QgsMessageLog::logMessage( err, LOGMESSAGE_TAG, Qgis::Critical ); - qDebug() << source; if ( exceptionBehavior == Throw ) throw e; } diff --git a/tests/src/core/CMakeLists.txt b/tests/src/core/CMakeLists.txt index 4e5094c1c60..034dbc25999 100644 --- a/tests/src/core/CMakeLists.txt +++ b/tests/src/core/CMakeLists.txt @@ -220,6 +220,7 @@ SET(TESTS testqgstracer.cpp testqgstriangularmesh.cpp testqgsfontutils.cpp + testqgsvaluerelationfieldformatter.cpp testqgsvector.cpp testqgsvectordataprovider.cpp testqgsvectorlayercache.cpp diff --git a/tests/src/core/testqgsrelationreferencefieldformatter.cpp b/tests/src/core/testqgsrelationreferencefieldformatter.cpp new file mode 100644 index 00000000000..3a5094c09aa --- /dev/null +++ b/tests/src/core/testqgsrelationreferencefieldformatter.cpp @@ -0,0 +1,154 @@ +/*************************************************************************** + testqgsrelationreferencefieldformatter.cpp + -------------------------------------- + Date : 13/12/2019 + Copyright : (C) 2019 by Alessandro Pasotti + Email : elpaso at itopen dot it + *************************************************************************** + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 2 of the License, or * + * (at your option) any later version. * + * * + ***************************************************************************/ + +#include +#include + +#include "qgstest.h" +#include "qgsapplication.h" +#include "qgsproject.h" +#include "qgsrelationmanager.h" +#include "qgsfieldformatterregistry.h" + + +//header for class being tested +#include "fieldformatter/qgsrelationreferencefieldformatter.h" + +class TestQgsRelationReferenceFieldFormatter: public QObject +{ + Q_OBJECT + + private slots: + + void initTestCase(); // will be called before the first testfunction is executed. + void cleanupTestCase(); // will be called after the last testfunction was executed. + void init(); // will be called before each testfunction is executed. + void cleanup(); // will be called after every testfunction. + void testDependencies(); + + private: + std::unique_ptr mLayer1; + std::unique_ptr mLayer2; + std::unique_ptr mRelation; + +}; + + +void TestQgsRelationReferenceFieldFormatter::initTestCase() +{ + QgsApplication::init(); + QgsApplication::initQgis(); +} + +void TestQgsRelationReferenceFieldFormatter::cleanupTestCase() +{ + QgsApplication::exitQgis(); +} + +void TestQgsRelationReferenceFieldFormatter::cleanup() +{ + QgsProject::instance()->removeMapLayer( mLayer1.get() ); + QgsProject::instance()->removeMapLayer( mLayer2.get() ); +} + + +void TestQgsRelationReferenceFieldFormatter::init() +{ + // create layer + mLayer1.reset( new QgsVectorLayer( QStringLiteral( "LineString?crs=epsg:3111&field=pk:int&field=fk:int" ), QStringLiteral( "vl1" ), QStringLiteral( "memory" ) ) ); + QgsProject::instance()->addMapLayer( mLayer1.get(), false, false ); + + mLayer2.reset( new QgsVectorLayer( QStringLiteral( "LineString?field=pk:int&field=material:string&field=diameter:int&field=raccord:string" ), QStringLiteral( "vl2" ), QStringLiteral( "memory" ) ) ); + QgsProject::instance()->addMapLayer( mLayer2.get(), false, false ); + + // create relation + mRelation.reset( new QgsRelation() ); + mRelation->setId( QStringLiteral( "vl1.vl2" ) ); + mRelation->setName( QStringLiteral( "vl1.vl2" ) ); + mRelation->setReferencingLayer( mLayer1->id() ); + mRelation->setReferencedLayer( mLayer2->id() ); + mRelation->addFieldPair( QStringLiteral( "fk" ), QStringLiteral( "pk" ) ); + QVERIFY( mRelation->isValid() ); + QgsProject::instance()->relationManager()->addRelation( *mRelation ); + + // add features + QgsFeature ft0( mLayer1->fields() ); + ft0.setAttribute( QStringLiteral( "pk" ), 0 ); + ft0.setAttribute( QStringLiteral( "fk" ), 0 ); + mLayer1->startEditing(); + mLayer1->addFeature( ft0 ); + mLayer1->commitChanges(); + + QgsFeature ft1( mLayer1->fields() ); + ft1.setAttribute( QStringLiteral( "pk" ), 1 ); + ft1.setAttribute( QStringLiteral( "fk" ), 1 ); + mLayer1->startEditing(); + mLayer1->addFeature( ft1 ); + mLayer1->commitChanges(); + + QgsFeature ft2( mLayer2->fields() ); + ft2.setAttribute( QStringLiteral( "pk" ), 10 ); + ft2.setAttribute( QStringLiteral( "material" ), "iron" ); + ft2.setAttribute( QStringLiteral( "diameter" ), 120 ); + ft2.setAttribute( QStringLiteral( "raccord" ), "brides" ); + mLayer2->startEditing(); + mLayer2->addFeature( ft2 ); + mLayer2->commitChanges(); + + QgsFeature ft3( mLayer2->fields() ); + ft3.setAttribute( QStringLiteral( "pk" ), 11 ); + ft3.setAttribute( QStringLiteral( "material" ), "iron" ); + ft3.setAttribute( QStringLiteral( "diameter" ), 120 ); + ft3.setAttribute( QStringLiteral( "raccord" ), "sleeve" ); + mLayer2->startEditing(); + mLayer2->addFeature( ft3 ); + mLayer2->commitChanges(); + + QgsFeature ft4( mLayer2->fields() ); + ft4.setAttribute( QStringLiteral( "pk" ), 12 ); + ft4.setAttribute( QStringLiteral( "material" ), "steel" ); + ft4.setAttribute( QStringLiteral( "diameter" ), 120 ); + ft4.setAttribute( QStringLiteral( "raccord" ), "collar" ); + mLayer2->startEditing(); + mLayer2->addFeature( ft4 ); + mLayer2->commitChanges(); +} + +void TestQgsRelationReferenceFieldFormatter::testDependencies() +{ + // Test dependencies + + const QgsEditorWidgetSetup setup { QStringLiteral( "RelationReference" ), { + { QStringLiteral( "ReferencedLayerDataSource" ), mLayer2->publicSource() }, + { QStringLiteral( "ReferencedLayerProviderKey" ), mLayer2->providerType() }, + { QStringLiteral( "ReferencedLayerId" ), mLayer2->id() }, + { QStringLiteral( "ReferencedLayerName" ), mLayer2->name() }, + }}; + QgsFieldFormatter *fieldFormatter = QgsApplication::fieldFormatterRegistry()->fieldFormatter( setup.type() ); + const QList dependencies = fieldFormatter->layerDependencies( setup.config() ); + QVERIFY( dependencies.count() == 1 ); + const QgsVectorLayerRef dependency = dependencies.first(); + QCOMPARE( dependency.layerId, mLayer2->id() ); + QCOMPARE( dependency.name, mLayer2->name() ); + QCOMPARE( dependency.provider, mLayer2->providerType() ); + QCOMPARE( dependency.source, mLayer2->publicSource() ); +} + +QGSTEST_MAIN( TestQgsRelationReferenceFieldFormatter ) +#include "testqgsrelationreferencefieldformatter.moc" + + + + diff --git a/tests/src/core/testqgsvaluerelationfieldformatter.cpp b/tests/src/core/testqgsvaluerelationfieldformatter.cpp new file mode 100644 index 00000000000..5e70e7a2816 --- /dev/null +++ b/tests/src/core/testqgsvaluerelationfieldformatter.cpp @@ -0,0 +1,154 @@ +/*************************************************************************** + testqgsvaluerelationfieldformatter.cpp + -------------------------------------- + Date : 13/12/2019 + Copyright : (C) 2019 by Alessandro Pasotti + Email : elpaso at itopen dot it + *************************************************************************** + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 2 of the License, or * + * (at your option) any later version. * + * * + ***************************************************************************/ + +#include +#include + +#include "qgstest.h" +#include "qgsapplication.h" +#include "qgsproject.h" +#include "qgsrelationmanager.h" +#include "qgsfieldformatterregistry.h" + + +//header for class being tested +#include "fieldformatter/qgsvaluerelationfieldformatter.h" + +class TestQgsValueRelationFieldFormatter: public QObject +{ + Q_OBJECT + + private slots: + + void initTestCase(); // will be called before the first testfunction is executed. + void cleanupTestCase(); // will be called after the last testfunction was executed. + void init(); // will be called before each testfunction is executed. + void cleanup(); // will be called after every testfunction. + void testDependencies(); + + private: + std::unique_ptr mLayer1; + std::unique_ptr mLayer2; + std::unique_ptr mRelation; + +}; + + +void TestQgsValueRelationFieldFormatter::initTestCase() +{ + QgsApplication::init(); + QgsApplication::initQgis(); +} + +void TestQgsValueRelationFieldFormatter::cleanupTestCase() +{ + QgsApplication::exitQgis(); +} + +void TestQgsValueRelationFieldFormatter::cleanup() +{ + QgsProject::instance()->removeMapLayer( mLayer1.get() ); + QgsProject::instance()->removeMapLayer( mLayer2.get() ); +} + + +void TestQgsValueRelationFieldFormatter::init() +{ + // create layer + mLayer1.reset( new QgsVectorLayer( QStringLiteral( "LineString?crs=epsg:3111&field=pk:int&field=fk:int" ), QStringLiteral( "vl1" ), QStringLiteral( "memory" ) ) ); + QgsProject::instance()->addMapLayer( mLayer1.get(), false, false ); + + mLayer2.reset( new QgsVectorLayer( QStringLiteral( "LineString?field=pk:int&field=material:string&field=diameter:int&field=raccord:string" ), QStringLiteral( "vl2" ), QStringLiteral( "memory" ) ) ); + QgsProject::instance()->addMapLayer( mLayer2.get(), false, false ); + + // create relation + mRelation.reset( new QgsRelation() ); + mRelation->setId( QStringLiteral( "vl1.vl2" ) ); + mRelation->setName( QStringLiteral( "vl1.vl2" ) ); + mRelation->setReferencingLayer( mLayer1->id() ); + mRelation->setReferencedLayer( mLayer2->id() ); + mRelation->addFieldPair( QStringLiteral( "fk" ), QStringLiteral( "pk" ) ); + QVERIFY( mRelation->isValid() ); + QgsProject::instance()->relationManager()->addRelation( *mRelation ); + + // add features + QgsFeature ft0( mLayer1->fields() ); + ft0.setAttribute( QStringLiteral( "pk" ), 0 ); + ft0.setAttribute( QStringLiteral( "fk" ), 0 ); + mLayer1->startEditing(); + mLayer1->addFeature( ft0 ); + mLayer1->commitChanges(); + + QgsFeature ft1( mLayer1->fields() ); + ft1.setAttribute( QStringLiteral( "pk" ), 1 ); + ft1.setAttribute( QStringLiteral( "fk" ), 1 ); + mLayer1->startEditing(); + mLayer1->addFeature( ft1 ); + mLayer1->commitChanges(); + + QgsFeature ft2( mLayer2->fields() ); + ft2.setAttribute( QStringLiteral( "pk" ), 10 ); + ft2.setAttribute( QStringLiteral( "material" ), "iron" ); + ft2.setAttribute( QStringLiteral( "diameter" ), 120 ); + ft2.setAttribute( QStringLiteral( "raccord" ), "brides" ); + mLayer2->startEditing(); + mLayer2->addFeature( ft2 ); + mLayer2->commitChanges(); + + QgsFeature ft3( mLayer2->fields() ); + ft3.setAttribute( QStringLiteral( "pk" ), 11 ); + ft3.setAttribute( QStringLiteral( "material" ), "iron" ); + ft3.setAttribute( QStringLiteral( "diameter" ), 120 ); + ft3.setAttribute( QStringLiteral( "raccord" ), "sleeve" ); + mLayer2->startEditing(); + mLayer2->addFeature( ft3 ); + mLayer2->commitChanges(); + + QgsFeature ft4( mLayer2->fields() ); + ft4.setAttribute( QStringLiteral( "pk" ), 12 ); + ft4.setAttribute( QStringLiteral( "material" ), "steel" ); + ft4.setAttribute( QStringLiteral( "diameter" ), 120 ); + ft4.setAttribute( QStringLiteral( "raccord" ), "collar" ); + mLayer2->startEditing(); + mLayer2->addFeature( ft4 ); + mLayer2->commitChanges(); +} + +void TestQgsValueRelationFieldFormatter::testDependencies() +{ + // Test dependencies + + const QgsEditorWidgetSetup setup { QStringLiteral( "ValueRelation" ), { + { QStringLiteral( "LayerSource" ), mLayer2->publicSource() }, + { QStringLiteral( "LayerProviderName" ), mLayer2->providerType() }, + { QStringLiteral( "LayerName" ), mLayer2->name() }, + { QStringLiteral( "Layer" ), mLayer2->id() } + }}; + QgsFieldFormatter *fieldFormatter = QgsApplication::fieldFormatterRegistry()->fieldFormatter( setup.type() ); + const QList dependencies = fieldFormatter->layerDependencies( setup.config() ); + QVERIFY( dependencies.count() == 1 ); + const QgsVectorLayerRef dependency = dependencies.first(); + QCOMPARE( dependency.layerId, mLayer2->id() ); + QCOMPARE( dependency.name, mLayer2->name() ); + QCOMPARE( dependency.provider, mLayer2->providerType() ); + QCOMPARE( dependency.source, mLayer2->publicSource() ); +} + +QGSTEST_MAIN( TestQgsValueRelationFieldFormatter ) +#include "testqgsvaluerelationfieldformatter.moc" + + + +