Skip to content

Commit

Permalink
Fixes #38796 : Fix serialization of QgsWeakRelation field pair
Browse files Browse the repository at this point in the history
  • Loading branch information
troopa81 authored and nyalldawson committed Sep 16, 2020
1 parent 1bd7890 commit 7e8c7b3
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/core/qgsweakrelation.cpp
Expand Up @@ -87,12 +87,12 @@ QgsWeakRelation QgsWeakRelation::readXml( const QgsVectorLayer *layer, WeakRelat
}

QList<QgsRelation::FieldPair> fieldPairs;
const QDomNodeList fieldPairNodes { relationElement.elementsByTagName( QStringLiteral( "fieldPair" ) ) };
const QDomNodeList fieldPairNodes { relationElement.elementsByTagName( QStringLiteral( "fieldRef" ) ) };
for ( int j = 0; j < fieldPairNodes.length(); ++j )
{
const QDomElement fieldPairElement = fieldPairNodes.at( j ).toElement();
fieldPairs.push_back( { fieldPairElement.attribute( QStringLiteral( "referencing" ) ),
fieldPairElement.attribute( QStringLiteral( "referenced" ) )
fieldPairs.push_back( { fieldPairElement.attribute( QStringLiteral( "referencingField" ) ),
fieldPairElement.attribute( QStringLiteral( "referencedField" ) )
} );
}

Expand Down
49 changes: 49 additions & 0 deletions tests/src/core/testqgsweakrelation.cpp
Expand Up @@ -31,6 +31,7 @@ class TestQgsWeakRelation: public QObject
void cleanup();// will be called after every testfunction.

void testResolved(); // Test if relation can be resolved
void testReadWrite(); // Test if relation can be read and write

private:
};
Expand Down Expand Up @@ -102,5 +103,53 @@ void TestQgsWeakRelation::testResolved()
QVERIFY( weakRel.resolvedRelation( QgsProject::instance() ).isValid() );
}

void TestQgsWeakRelation::testReadWrite()
{
QList<QgsRelation::FieldPair> fieldPairs {{ "fk_province", "pk" }};

QgsWeakRelation weakRel( QStringLiteral( "my_relation_id" ),
QStringLiteral( "my_relation_name" ),
QgsRelation::RelationStrength::Association,
QStringLiteral( "referencingLayerId" ),
QStringLiteral( "referencingLayerName" ),
QStringLiteral( "Point?crs=epsg:4326&field=pk:int&field=fk_province:int&field=fk_municipality:int" ),
QStringLiteral( "memory" ),
QStringLiteral( "referencedLayerId" ),
QStringLiteral( "referencedLayerName" ),
QStringLiteral( "Polygon?crs=epsg:4326&field=pk:int&field=province:int&field=municipality:string" ),
QStringLiteral( "memory" ),
fieldPairs
);

QgsVectorLayer referencedLayer( QStringLiteral( "Polygon?crs=epsg:4326&field=pk:int&field=province:int&field=municipality:string" ), QStringLiteral( "referencedLayerName" ), QStringLiteral( "memory" ) );
QgsProject::instance()->addMapLayer( &referencedLayer, false, false );

QgsVectorLayer referencingLayer( QStringLiteral( "Point?crs=epsg:4326&field=pk:int&field=fk_province:int&field=fk_municipality:int" ), QStringLiteral( "referencingLayerName" ), QStringLiteral( "memory" ) );
QgsProject::instance()->addMapLayer( &referencingLayer, false, false );

QgsRelation relation( weakRel.resolvedRelation( QgsProject::instance(), QgsVectorLayerRef::MatchType::Name ) );
QVERIFY( relation.isValid() );

QDomImplementation DomImplementation;
QDomDocumentType documentType =
DomImplementation.createDocumentType(
QStringLiteral( "qgis" ), QStringLiteral( "http://mrcc.com/qgis.dtd" ), QStringLiteral( "SYSTEM" ) );
QDomDocument doc( documentType );

QDomElement node = doc.createElement( QStringLiteral( "relation" ) );
QgsWeakRelation::writeXml( &referencedLayer, relation, node, doc );
QgsWeakRelation weakRelReferenced( QgsWeakRelation::readXml( &referencedLayer, QgsWeakRelation::Referenced, node, QgsProject::instance()->pathResolver() ) );
QCOMPARE( weakRelReferenced.fieldPairs(), fieldPairs );
QCOMPARE( weakRelReferenced.strength(), QgsRelation::RelationStrength::Association );
QCOMPARE( weakRelReferenced.referencedLayer().resolve( QgsProject::instance() ), &referencedLayer );

node = doc.createElement( QStringLiteral( "relation" ) );
QgsWeakRelation::writeXml( &referencingLayer, relation, node, doc );
QgsWeakRelation weakRelReferencing( QgsWeakRelation::readXml( &referencingLayer, QgsWeakRelation::Referencing, node, QgsProject::instance()->pathResolver() ) );
QCOMPARE( weakRelReferencing.fieldPairs(), fieldPairs );
QCOMPARE( weakRelReferencing.strength(), QgsRelation::RelationStrength::Association );
QCOMPARE( weakRelReferencing.referencingLayer().resolve( QgsProject::instance() ), &referencingLayer );
}

QGSTEST_MAIN( TestQgsWeakRelation )
#include "testqgsweakrelation.moc"

0 comments on commit 7e8c7b3

Please sign in to comment.