Skip to content

Commit

Permalink
Merge pull request #39126 from gacarrillor/pr_test_write_weak_relations
Browse files Browse the repository at this point in the history
Test for writing weak relation attributes to XML (followup #39090)
  • Loading branch information
elpaso committed Oct 2, 2020
2 parents ab6bbfd + 7407e0f commit 7ef302d
Showing 1 changed file with 92 additions and 0 deletions.
92 changes: 92 additions & 0 deletions tests/src/core/testqgsweakrelation.cpp
Expand Up @@ -19,6 +19,7 @@
#include "qgsproject.h"
#include "qgsweakrelation.h"
#include "qgsrelation.h"
#include "qgsrelationmanager.h"

class TestQgsWeakRelation: public QObject
{
Expand All @@ -33,6 +34,8 @@ class TestQgsWeakRelation: public QObject
void testResolved(); // Test if relation can be resolved
void testReadWrite(); // Test if relation can be read and write

void testWriteStyleCategoryRelations(); // Test if weak relation attrs are written well in the style "relations"

private:
};

Expand Down Expand Up @@ -153,5 +156,94 @@ void TestQgsWeakRelation::testReadWrite()
QCOMPARE( weakRelReferencing.referencingLayer().resolve( QgsProject::instance() ), &referencingLayer );
}

void TestQgsWeakRelation::testWriteStyleCategoryRelations()
{
// Create a referencing layer and two referenced layers
QgsVectorLayer mLayer1( QStringLiteral( "Point?crs=epsg:3111&field=pk:int&field=fk1:int&field=fk2:int" ), QStringLiteral( "vl1" ), QStringLiteral( "memory" ) );
QgsProject::instance()->addMapLayer( &mLayer1, false, false );

QgsVectorLayer mLayer2( QStringLiteral( "None?field=pk:int&field=name:string" ), QStringLiteral( "vl2" ), QStringLiteral( "memory" ) );
QgsProject::instance()->addMapLayer( &mLayer2, false, false );

QgsVectorLayer mLayer3( QStringLiteral( "None?field=pk:int&field=name:string" ), QStringLiteral( "vl3" ), QStringLiteral( "memory" ) );
QgsProject::instance()->addMapLayer( &mLayer3, false, false );

// Create relation 1
QgsRelation mRelation1;
mRelation1.setId( QStringLiteral( "vl1.vl2" ) );
mRelation1.setName( QStringLiteral( "vl1.vl2" ) );
mRelation1.setReferencingLayer( mLayer1.id() );
mRelation1.setReferencedLayer( mLayer2.id() );
mRelation1.addFieldPair( QStringLiteral( "fk1" ), QStringLiteral( "pk" ) );
QVERIFY( mRelation1.isValid() );
QgsProject::instance()->relationManager()->addRelation( mRelation1 );

// Create relation 2
QgsRelation mRelation2;
mRelation2.setId( QStringLiteral( "vl1.vl3" ) );
mRelation2.setName( QStringLiteral( "vl1.vl3" ) );
mRelation2.setReferencingLayer( mLayer1.id() );
mRelation2.setReferencedLayer( mLayer3.id() );
mRelation2.addFieldPair( QStringLiteral( "fk2" ), QStringLiteral( "pk" ) );
QVERIFY( mRelation2.isValid() );
QgsProject::instance()->relationManager()->addRelation( mRelation2 );

// Write to XML
QDomImplementation DomImplementation;
QDomDocumentType documentType =
DomImplementation.createDocumentType(
QStringLiteral( "qgis" ), QStringLiteral( "http://mrcc.com/qgis.dtd" ), QStringLiteral( "SYSTEM" ) );
QDomDocument doc( documentType );
QDomElement node = doc.createElement( QStringLiteral( "style_categories_relations" ) );
QString errorMessage;
QgsReadWriteContext context = QgsReadWriteContext();

mLayer1.writeSymbology( node, doc, errorMessage, context, QgsMapLayer::Relations );

// Check XML tags and attributes
QDomElement referencedLayersElement = node.firstChildElement( QStringLiteral( "referencedLayers" ) );
Q_ASSERT( !referencedLayersElement.isNull() );
QDomNodeList relationsNodeList = referencedLayersElement.elementsByTagName( QStringLiteral( "relation" ) );
QCOMPARE( relationsNodeList.size(), 2 );
QDomElement relationElement;

int visitedCount = 0;
for ( int i = 0; i < relationsNodeList.size(); ++i )
{
relationElement = relationsNodeList.at( i ).toElement();

// Common checks
Q_ASSERT( relationElement.hasAttribute( QStringLiteral( "id" ) ) );
Q_ASSERT( relationElement.hasAttribute( QStringLiteral( "name" ) ) );
Q_ASSERT( relationElement.hasAttribute( QStringLiteral( "referencingLayer" ) ) );
Q_ASSERT( relationElement.hasAttribute( QStringLiteral( "referencedLayer" ) ) );
Q_ASSERT( relationElement.hasAttribute( QStringLiteral( "layerId" ) ) ); // Weak relation attribute
Q_ASSERT( relationElement.hasAttribute( QStringLiteral( "layerName" ) ) ); // Weak relation attribute
Q_ASSERT( relationElement.hasAttribute( QStringLiteral( "dataSource" ) ) ); // Weak relation attribute
Q_ASSERT( relationElement.hasAttribute( QStringLiteral( "providerKey" ) ) ); // Weak relation attribute

QCOMPARE( relationElement.attribute( QStringLiteral( "providerKey" ) ), QStringLiteral( "memory" ) );
QCOMPARE( relationElement.attribute( QStringLiteral( "referencingLayer" ) ), mLayer1.id() );

if ( relationElement.attribute( QStringLiteral( "id" ) ) == mRelation1.id() )
{
QCOMPARE( relationElement.attribute( QStringLiteral( "referencedLayer" ) ), mLayer2.id() );
QCOMPARE( relationElement.attribute( QStringLiteral( "dataSource" ) ), mLayer2.publicSource() );
QCOMPARE( relationElement.attribute( QStringLiteral( "layerId" ) ), mLayer2.id() );
QCOMPARE( relationElement.attribute( QStringLiteral( "layerName" ) ), mLayer2.name() );
visitedCount++;
}
else if ( relationElement.attribute( QStringLiteral( "id" ) ) == mRelation2.id() )
{
QCOMPARE( relationElement.attribute( QStringLiteral( "referencedLayer" ) ), mLayer3.id() );
QCOMPARE( relationElement.attribute( QStringLiteral( "dataSource" ) ), mLayer3.publicSource() );
QCOMPARE( relationElement.attribute( QStringLiteral( "layerId" ) ), mLayer3.id() );
QCOMPARE( relationElement.attribute( QStringLiteral( "layerName" ) ), mLayer3.name() );
visitedCount++;
}
}
QCOMPARE( visitedCount, 2 );
}

QGSTEST_MAIN( TestQgsWeakRelation )
#include "testqgsweakrelation.moc"

0 comments on commit 7ef302d

Please sign in to comment.