Skip to content

Commit

Permalink
Update unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Dec 15, 2017
1 parent 670858d commit f55da4a
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 22 deletions.
36 changes: 17 additions & 19 deletions tests/src/core/testqgscoordinatetransform.cpp
Expand Up @@ -183,36 +183,34 @@ void TestQgsCoordinateTransform::isShortCircuited()

void TestQgsCoordinateTransform::contextShared()
{
#ifdef singlesourcedest
//test implicit sharing of QgsCoordinateTransformContext
QgsCoordinateTransformContext original;
original.addDestinationDatumTransform( QgsCoordinateReferenceSystem( 3111 ), 1 );
original.addSourceDestinationDatumTransform( QgsCoordinateReferenceSystem( 3111 ), QgsCoordinateReferenceSystem( 3113 ), 1, 2 );

QgsCoordinateTransformContext copy( original );
QMap< QString, int > expected;
expected.insert( "EPSG:3111", 1 );
QCOMPARE( original.destinationDatumTransforms(), expected );
QCOMPARE( copy.destinationDatumTransforms(), expected );
QMap< QPair< QString, QString >, QPair< int, int > > expected;
expected.insert( qMakePair( QStringLiteral( "EPSG:3111" ), QStringLiteral( "EPSG:3113" ) ), qMakePair( 1, 2 ) );
QCOMPARE( original.sourceDestinationDatumTransforms(), expected );
QCOMPARE( copy.sourceDestinationDatumTransforms(), expected );

// trigger detach
copy.addDestinationDatumTransform( QgsCoordinateReferenceSystem( 3111 ), 2 );
QCOMPARE( original.destinationDatumTransforms(), expected );
copy.addSourceDestinationDatumTransform( QgsCoordinateReferenceSystem( 3111 ), QgsCoordinateReferenceSystem( 3113 ), 3, 4 );
QCOMPARE( original.sourceDestinationDatumTransforms(), expected );

expected.insert( "EPSG:3111", 2 );
QCOMPARE( copy.destinationDatumTransforms(), expected );
expected.insert( qMakePair( QStringLiteral( "EPSG:3111" ), QStringLiteral( "EPSG:3113" ) ), qMakePair( 3, 4 ) );
QCOMPARE( copy.sourceDestinationDatumTransforms(), expected );

// copy via assignment
QgsCoordinateTransformContext copy2;
copy2 = original;
expected.insert( "EPSG:3111", 1 );
QCOMPARE( original.destinationDatumTransforms(), expected );
QCOMPARE( copy2.destinationDatumTransforms(), expected );

copy2.addDestinationDatumTransform( QgsCoordinateReferenceSystem( 3111 ), 3 );
QCOMPARE( original.destinationDatumTransforms(), expected );
expected.insert( "EPSG:3111", 3 );
QCOMPARE( copy2.destinationDatumTransforms(), expected );
#endif
expected.insert( qMakePair( QStringLiteral( "EPSG:3111" ), QStringLiteral( "EPSG:3113" ) ), qMakePair( 1, 2 ) );
QCOMPARE( original.sourceDestinationDatumTransforms(), expected );
QCOMPARE( copy2.sourceDestinationDatumTransforms(), expected );

copy2.addSourceDestinationDatumTransform( QgsCoordinateReferenceSystem( 3111 ), QgsCoordinateReferenceSystem( 3113 ), 3, 4 );
QCOMPARE( original.sourceDestinationDatumTransforms(), expected );
expected.insert( qMakePair( QStringLiteral( "EPSG:3111" ), QStringLiteral( "EPSG:3113" ) ), qMakePair( 3, 4 ) );
QCOMPARE( copy2.sourceDestinationDatumTransforms(), expected );
}


Expand Down
57 changes: 54 additions & 3 deletions tests/src/python/test_qgscoordinatetransformcontext.py
Expand Up @@ -27,6 +27,7 @@

class TestQgsCoordinateTransformContext(unittest.TestCase):

@unittest.skip('ifdefed out in c++ until required')
def testSourceDatumTransforms(self):
context = QgsCoordinateTransformContext()
self.assertEqual(context.sourceDatumTransforms(), {})
Expand Down Expand Up @@ -58,6 +59,7 @@ def testSourceDatumTransforms(self):
context.clear()
self.assertEqual(context.sourceDatumTransforms(), {})

@unittest.skip('ifdefed out in c++ until required')
def testDestDatumTransforms(self):
context = QgsCoordinateTransformContext()
self.assertEqual(context.destinationDatumTransforms(), {})
Expand Down Expand Up @@ -171,6 +173,7 @@ def testSourceDestinationDatumTransforms(self):
context.clear()
self.assertEqual(context.sourceDestinationDatumTransforms(), {})

@unittest.skip('ifdefed out in c++ until required')
def testCalculate(self):
context = QgsCoordinateTransformContext()

Expand Down Expand Up @@ -217,7 +220,30 @@ def testCalculate(self):
QgsCoordinateReferenceSystem('EPSG:3111')),
(1, -1))

def testWriteReadXml(self):
def testCalculateSourceDest(self):
context = QgsCoordinateTransformContext()

#empty context
self.assertEqual(context.calculateDatumTransforms(QgsCoordinateReferenceSystem('EPSG:3111'),
QgsCoordinateReferenceSystem('EPSG:4283')),
(-1, -1))

#add specific source/dest pair - should take precedence
context.addSourceDestinationDatumTransform(QgsCoordinateReferenceSystem('EPSG:28356'),
QgsCoordinateReferenceSystem('EPSG:4283'),
3, 4)
self.assertEqual(context.calculateDatumTransforms(QgsCoordinateReferenceSystem('EPSG:28356'),
QgsCoordinateReferenceSystem('EPSG:4283')),
(3, 4))
self.assertEqual(context.calculateDatumTransforms(QgsCoordinateReferenceSystem('EPSG:3111'),
QgsCoordinateReferenceSystem('EPSG:4283')),
(-1, -1))
self.assertEqual(context.calculateDatumTransforms(QgsCoordinateReferenceSystem('EPSG:28356'),
QgsCoordinateReferenceSystem('EPSG:3111')),
(-1, -1))

@unittest.skip('ifdefed out in c++ until required')
def testWriteReadXmlSingleVariant(self):
# setup a context
context = QgsCoordinateTransformContext()
self.assertTrue(context.addSourceDatumTransform(QgsCoordinateReferenceSystem('EPSG:3111'), 1))
Expand Down Expand Up @@ -249,17 +275,42 @@ def testWriteReadXml(self):
self.assertEqual(context2.sourceDestinationDatumTransforms(), {('EPSG:3111', 'EPSG:4283'): (1, 2),
('EPSG:28356', 'EPSG:4283'): (3, 4)})

def testWriteReadXml(self):
# setup a context
context = QgsCoordinateTransformContext()
self.assertTrue(context.addSourceDestinationDatumTransform(QgsCoordinateReferenceSystem('EPSG:3111'),
QgsCoordinateReferenceSystem('EPSG:4283'), 1, 2))
self.assertTrue(context.addSourceDestinationDatumTransform(QgsCoordinateReferenceSystem('EPSG:28356'),
QgsCoordinateReferenceSystem(4283), 3, 4))

self.assertEqual(context.sourceDestinationDatumTransforms(), {('EPSG:3111', 'EPSG:4283'): (1, 2),
('EPSG:28356', 'EPSG:4283'): (3, 4)})

# save to xml
doc = QDomDocument("testdoc")
elem = doc.createElement("test")
context.writeXml(elem, doc, QgsReadWriteContext())

# restore from xml
context2 = QgsCoordinateTransformContext()
context2.readXml(elem, doc, QgsReadWriteContext())

# check result
self.assertEqual(context2.sourceDestinationDatumTransforms(), {('EPSG:3111', 'EPSG:4283'): (1, 2),
('EPSG:28356', 'EPSG:4283'): (3, 4)})

def testProject(self):
"""
Test project's transform context
"""
project = QgsProject()
context_changed_spy = QSignalSpy(project.transformContextChanged)
context = project.transformContext()
context.addSourceDatumTransform(QgsCoordinateReferenceSystem('EPSG:3111'), 1)
context.addSourceDestinationDatumTransform(QgsCoordinateReferenceSystem('EPSG:3111'),
QgsCoordinateReferenceSystem('EPSG:4283'), 1, 2)
project.setTransformContext(context)
self.assertEqual(len(context_changed_spy), 1)
self.assertEqual(project.transformContext().sourceDatumTransforms(), {'EPSG:3111': 1})
self.assertEqual(project.transformContext().sourceDestinationDatumTransforms(), {('EPSG:3111', 'EPSG:4283'): (1, 2)})


if __name__ == '__main__':
Expand Down

0 comments on commit f55da4a

Please sign in to comment.