52 changes: 21 additions & 31 deletions tests/src/python/test_qgscomposition.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,22 @@
# -*- coding: utf-8 -*-
'''
test_qgscomposerhtml.py
--------------------------------------
Date : September 2012
Copyright : (C) 2012 by Tim Sutton
email : tim@linfiniti.com
***************************************************************************
* *
* 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. *
* *
***************************************************************************/
'''
import unittest
import sys
"""QGIS Unit tests for QgsComposition.
.. note:: 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.
"""
__author__ = '(C) 2012 by Tim Sutton'
__date__ = '20/08/2012'
__copyright__ = 'Copyright 2012, The Quantum GIS Project'
# This will get replaced with a git SHA1 when you do a git archive
__revision__ = '$Format:%H$'

import os
from utilities import unitTestDataPath, getQgisTestApp

from PyQt4.QtCore import QFileInfo, QDir, QStringList
from PyQt4.QtXml import QDomDocument

from qgis.core import (QgsComposition,
QgsPoint,
QgsRasterLayer,
Expand All @@ -28,16 +25,12 @@
QgsMapRenderer
)

# support python < 2.7 via unittest2
# needed for expected failure decorator
if sys.version_info[0:2] < (2,7):
try:
from unittest2 import TestCase, expectedFailure
except ImportError:
print "You should install unittest2 to run the salt tests"
sys.exit(0)
else:
from unittest import TestCase, expectedFailure
from utilities import (unitTestDataPath,
getQgisTestApp,
TestCase,
unittest
#expectedFailure
)

QGISAPP, CANVAS, IFACE, PARENT = getQgisTestApp()
TEST_DATA_DIR = unitTestDataPath()
Expand Down Expand Up @@ -149,8 +142,5 @@ def testPrintMapFromTemplate(self):
(myExpectedFileSize, myFileSize))
assert myFileSize > myExpectedFileSize, myMessage




if __name__ == '__main__':
unittest.main()
24 changes: 20 additions & 4 deletions tests/src/python/test_qgscoordinatetransform.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,32 @@
import unittest
# -*- coding: utf-8 -*-
"""QGIS Unit tests for QgsComposition.
.. note:: 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.
"""
__author__ = '(C) 2012 by Tim Sutton'
__date__ = '20/08/2012'
__copyright__ = 'Copyright 2012, The Quantum GIS Project'
# This will get replaced with a git SHA1 when you do a git archive
__revision__ = '$Format:%H$'

from qgis.core import (QgsRectangle,
QgsCoordinateReferenceSystem,
QgsCoordinateTransform,
QGis)

from utilities import (unitTestDataPath,
getQgisTestApp,
TestCase,
unittest
#expectedFailure
)
# Convenience instances in case you may need them
# not used in this test
from utilities import getQgisTestApp
QGISAPP, CANVAS, IFACE, PARENT = getQgisTestApp()

class TestQgsCoordinateTransform(unittest.TestCase):
class TestQgsCoordinateTransform(TestCase):

def testTransformBoundingBox(self):
"""Test that we can transform a rectangular bbox from utm56s to LonLat"""
Expand Down
124 changes: 95 additions & 29 deletions tests/src/python/test_qgsgeometry.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,31 @@
import unittest
# -*- coding: utf-8 -*-
"""QGIS Unit tests for QgsComposition.
.. note:: 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.
"""
__author__ = 'Tim Sutton'
__date__ = '20/08/2012'
__copyright__ = 'Copyright 2012, The Quantum GIS Project'
# This will get replaced with a git SHA1 when you do a git archive
__revision__ = '$Format:%H$'

from qgis.core import (QgsGeometry,
QgsVectorLayer,
QgsFeature,
QgsPoint,
QGis)

from utilities import (getQgisTestApp,
TestCase,
unittest)
# Convenience instances in case you may need them
# not used in this test
from utilities import getQgisTestApp
QGISAPP, CANVAS, IFACE, PARENT = getQgisTestApp()

class TestQgsGeometry(unittest.TestCase):
class TestQgsGeometry(TestCase):

def testWktPointLoading(self):
myWKT='POINT(10 10)'
Expand All @@ -27,7 +41,8 @@ def testFromPoint(self):
assert myPoint.wkbType() == QGis.WKBPoint, myMessage

def testFromMultiPoint(self):
myMultiPoint = QgsGeometry.fromMultiPoint([(QgsPoint(0, 0)),(QgsPoint(1, 1))])
myMultiPoint = QgsGeometry.fromMultiPoint([
(QgsPoint(0, 0)),(QgsPoint(1, 1))])
myMessage = ('Expected:\n%s\nGot:\n%s\n' %
(QGis.WKBMultiPoint, myMultiPoint.type()))
assert myMultiPoint.wkbType() == QGis.WKBMultiPoint, myMessage
Expand All @@ -39,45 +54,58 @@ def testFromLine(self):
assert myLine.wkbType() == QGis.WKBLineString, myMessage

def testFromMultiLine(self):
myMultiPolyline = QgsGeometry.fromMultiPolyline([[QgsPoint(0, 0),QgsPoint(1, 1)],[QgsPoint(0, 1), QgsPoint(2, 1)]])
myMultiPolyline = QgsGeometry.fromMultiPolyline(
[[QgsPoint(0, 0),QgsPoint(1, 1)],[QgsPoint(0, 1), QgsPoint(2, 1)]])
myMessage = ('Expected:\n%s\nGot:\n%s\n' %
(QGis.WKBMultiLineString, myMultiPolyline.type()))
assert myMultiPolyline.wkbType() == QGis.WKBMultiLineString, myMessage

def testFromPolygon(self):
myPolygon = QgsGeometry.fromPolygon([[QgsPoint(1, 1), QgsPoint(2, 2), QgsPoint(1, 2), QgsPoint(1, 1)]])
myPolygon = QgsGeometry.fromPolygon(
[[QgsPoint(1, 1), QgsPoint(2, 2), QgsPoint(1, 2), QgsPoint(1, 1)]])
myMessage = ('Expected:\n%s\nGot:\n%s\n' %
(QGis.WKBPolygon, myPolygon.type()))
assert myPolygon.wkbType() == QGis.WKBPolygon, myMessage

def testFromMultiPolygon(self):
myMultiPolygon = QgsGeometry.fromMultiPolygon([[[QgsPoint(1, 1), QgsPoint(2, 2), QgsPoint(1, 2), QgsPoint(1, 1)]],
[[QgsPoint(2, 2), QgsPoint(3, 3), QgsPoint(3, 1), QgsPoint(2, 2)]]])
myMultiPolygon = QgsGeometry.fromMultiPolygon([
[[QgsPoint(1, 1),
QgsPoint(2, 2),
QgsPoint(1, 2),
QgsPoint(1, 1)]],
[[QgsPoint(2, 2),
QgsPoint(3, 3),
QgsPoint(3, 1),
QgsPoint(2, 2)]]
])
myMessage = ('Expected:\n%s\nGot:\n%s\n' %
(QGis.WKBMultiPolygon, myMultiPolygon.type()))
assert myMultiPolygon.wkbType() == QGis.WKBMultiPolygon, myMessage

def testIntersection(self):
myLine = QgsGeometry.fromPolyline([QgsPoint(0, 0),QgsPoint(1, 1),QgsPoint(2, 2)])
myLine = QgsGeometry.fromPolyline([
QgsPoint(0, 0),
QgsPoint(1, 1),
QgsPoint(2, 2)])
myPoint = QgsGeometry.fromPoint(QgsPoint(1, 1))
intersectionGeom = QgsGeometry.intersection(myLine, myPoint)
myMessage = ('Expected:\n%s\nGot:\n%s\n' %
(QGis.Point, intersectionGeom.type()))
assert intersectionGeom.wkbType() == QGis.WKBPoint, myMessage

layer = QgsVectorLayer("Point", "intersection", "memory")
assert layer.isValid(), "Failed to create valid point memory layer"

provider = layer.dataProvider()

ft = QgsFeature()
ft.setGeometry(intersectionGeom)
provider.addFeatures([ft])

myMessage = ('Expected:\n%s\nGot:\n%s\n' %
(1, layer.featureCount()))
assert layer.featureCount() == 1, myMessage

def testBuffer(self):
myPoint = QgsGeometry.fromPoint(QgsPoint(1, 1))
bufferGeom = myPoint.buffer(10, 5)
Expand All @@ -86,50 +114,88 @@ def testBuffer(self):
assert bufferGeom.wkbType() == QGis.WKBPolygon, myMessage
myTestPoint = QgsGeometry.fromPoint(QgsPoint(3, 3))
assert bufferGeom.intersects(myTestPoint)

def testContains(self):
myPoly = QgsGeometry.fromPolygon([[QgsPoint(0, 0),QgsPoint(2, 0),QgsPoint(2, 2),QgsPoint(0, 2), QgsPoint(0, 0)]])
myPoly = QgsGeometry.fromPolygon(
[[QgsPoint(0, 0),
QgsPoint(2, 0),
QgsPoint(2, 2),
QgsPoint(0, 2),
QgsPoint(0, 0)]])
myPoint = QgsGeometry.fromPoint(QgsPoint(1, 1))
containsGeom = QgsGeometry.contains(myPoly, myPoint)
myMessage = ('Expected:\n%s\nGot:\n%s\n' %
("True", containsGeom))
assert containsGeom == True, myMessage

def testTouches(self):
myLine = QgsGeometry.fromPolyline([QgsPoint(0, 0),QgsPoint(1, 1),QgsPoint(2, 2)])
myPoly = QgsGeometry.fromPolygon([[QgsPoint(0, 0),QgsPoint(1, 1),QgsPoint(2, 0),QgsPoint(0, 0)]])
myLine = QgsGeometry.fromPolyline([
QgsPoint(0, 0),
QgsPoint(1, 1),
QgsPoint(2, 2)])
myPoly = QgsGeometry.fromPolygon([[
QgsPoint(0, 0),
QgsPoint(1, 1),
QgsPoint(2, 0),
QgsPoint(0, 0)]])
touchesGeom = QgsGeometry.touches(myLine, myPoly)
myMessage = ('Expected:\n%s\nGot:\n%s\n' %
("True", touchesGeom))
assert touchesGeom == True, myMessage

def testOverlaps(self):
myPolyA = QgsGeometry.fromPolygon([[QgsPoint(0, 0),QgsPoint(1, 3),QgsPoint(2, 0),QgsPoint(0, 0)]])
myPolyB = QgsGeometry.fromPolygon([[QgsPoint(0, 0),QgsPoint(2, 0),QgsPoint(2, 2),QgsPoint(0, 2), QgsPoint(0, 0)]])
myPolyA = QgsGeometry.fromPolygon([[
QgsPoint(0, 0),
QgsPoint(1, 3),
QgsPoint(2, 0),
QgsPoint(0, 0)]])
myPolyB = QgsGeometry.fromPolygon([[
QgsPoint(0, 0),
QgsPoint(2, 0),
QgsPoint(2, 2),
QgsPoint(0, 2),
QgsPoint(0, 0)]])
overlapsGeom = QgsGeometry.overlaps(myPolyA, myPolyB)
myMessage = ('Expected:\n%s\nGot:\n%s\n' %
("True", overlapsGeom))
assert overlapsGeom == True, myMessage

def testWithin(self):
myLine = QgsGeometry.fromPolyline([QgsPoint(0.5, 0.5),QgsPoint(1, 1),QgsPoint(1.5, 1.5)])
myPoly = QgsGeometry.fromPolygon([[QgsPoint(0, 0),QgsPoint(2, 0),QgsPoint(2, 2),QgsPoint(0, 2), QgsPoint(0, 0)]])
myLine = QgsGeometry.fromPolyline([
QgsPoint(0.5, 0.5),
QgsPoint(1, 1),
QgsPoint(1.5, 1.5)
])
myPoly = QgsGeometry.fromPolygon([[
QgsPoint(0, 0),
QgsPoint(2, 0),
QgsPoint(2, 2),
QgsPoint(0, 2),
QgsPoint(0, 0)]])
withinGeom = QgsGeometry.within(myLine, myPoly)
myMessage = ('Expected:\n%s\nGot:\n%s\n' %
("True", withinGeom))
assert withinGeom == True, myMessage

def testEquals(self):
myPointA = QgsGeometry.fromPoint(QgsPoint(1, 1))
myPointB = QgsGeometry.fromPoint(QgsPoint(1, 1))
equalsGeom = QgsGeometry.equals(myPointA, myPointB)
myMessage = ('Expected:\n%s\nGot:\n%s\n' %
("True", equalsGeom))
assert equalsGeom == True, myMessage

def testCrosses(self):
myLine = QgsGeometry.fromPolyline([QgsPoint(0, 0),QgsPoint(1, 1),QgsPoint(3, 3)])
myPoly = QgsGeometry.fromPolygon([[QgsPoint(1, 0),QgsPoint(2, 0),QgsPoint(2, 2),QgsPoint(1, 2), QgsPoint(1, 0)]])
myLine = QgsGeometry.fromPolyline([
QgsPoint(0, 0),
QgsPoint(1, 1),
QgsPoint(3, 3)])
myPoly = QgsGeometry.fromPolygon([[
QgsPoint(1, 0),
QgsPoint(2, 0),
QgsPoint(2, 2),
QgsPoint(1, 2),
QgsPoint(1, 0)]])
crossesGeom = QgsGeometry.crosses(myLine, myPoly)
myMessage = ('Expected:\n%s\nGot:\n%s\n' %
("True", crossesGeom))
Expand Down
31 changes: 23 additions & 8 deletions tests/src/python/test_qgslogger.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,30 @@
# -*- coding: utf-8 -*-
"""QGIS Unit tests for QgsComposition.
.. note:: 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.
"""
__author__ = 'Tim Sutton'
__date__ = '20/08/2012'
__copyright__ = 'Copyright 2012, The Quantum GIS Project'
# This will get replaced with a git SHA1 when you do a git archive
__revision__ = '$Format:%H$'

import tempfile
import os
import unittest

from qgis.core import QgsLogger

from utilities import (TestCase,
unittest
#expectedFailure
)
# Convenience instances in case you may need them
# not used in this test
#from utilities import getQgisTestApp
#QGISAPP, CANVAS, IFACE, PARENT = getQgisTestApp()

class TestQgsLogger(unittest.TestCase):
class TestQgsLogger(TestCase):

def testLogger(self):
(myFileHandle, myFilename) = tempfile.mkstemp()
Expand All @@ -21,17 +36,17 @@ def testLogger(self):
os.environ['QGIS_LOG_FILE'] = myFilename
myLogger = QgsLogger()
myLogger.debug('This is a debug')
myLogger.warning('This is a warning')
myLogger.warning('This is a warning')
myLogger.critical('This is critical')
#myLogger.fatal('Aaaargh...fatal'); #kills QGIS not testable
myFile = open(myFilename, 'rt')
myText = myFile.readlines()
myFile.close()
myExpectedText = ['QGIS Logger Unit Test\n',
myExpectedText = ['QGIS Logger Unit Test\n',
'This is a debug\n',
'This is a warning\n',
'This is a warning\n',
'This is critical\n']
myMessage = ('Expected:\n---\n%s\n---\nGot:\n---\n%s\n---\n' %
myMessage = ('Expected:\n---\n%s\n---\nGot:\n---\n%s\n---\n' %
(myExpectedText, myText))
self.assertEquals(myText, myExpectedText, myMessage)
finally:
Expand Down
41 changes: 20 additions & 21 deletions tests/src/python/test_qgsmemoryprovider.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,18 @@
# -*- coding: utf-8 -*-
'''
test_qgsmemoryprovider.py
--------------------------------------
Date : 17 Aug 2012
Copyright : (C) 2012 by Alexander Bruy
email : alexander dot bruy at gmail dot com
***************************************************************************
* *
* 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 3 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
'''

import unittest
"""QGIS Unit tests for QgsMemoryProvider.
.. note:: 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.
"""
__author__ = 'Alexander Bruy'
__date__ = '20/08/2012'
__copyright__ = 'Copyright 2012, The Quantum GIS Project'
# This will get replaced with a git SHA1 when you do a git archive
__revision__ = '$Format:%H$'

from PyQt4.QtCore import QVariant

from qgis.core import (QGis,
QgsVectorLayer,
Expand All @@ -24,12 +21,14 @@
QgsGeometry,
QgsPoint)

from PyQt4.QtCore import QVariant

from utilities import getQgisTestApp
from utilities import (getQgisTestApp,
TestCase,
unittest
#expectedFailure
)
QGISAPP, CANVAS, IFACE, PARENT = getQgisTestApp()

class TestQgsMemoryProvider(unittest.TestCase):
class TestQgsMemoryProvider(TestCase):

def testPointCtor(self):
layer = QgsVectorLayer("Point", "test", "memory")
Expand Down
72 changes: 47 additions & 25 deletions tests/src/python/test_qgsrasterfilewriter.py
Original file line number Diff line number Diff line change
@@ -1,73 +1,95 @@
import os, glob
import unittest
# -*- coding: utf-8 -*-
"""QGIS Unit tests for QgsRasterFileWriter.
from qgis.core import QgsRasterLayer, QgsRasterChecker, QgsRasterPipe, QgsRasterFileWriter, QgsRasterProjector
from PyQt4.QtCore import QFileInfo, QString, QStringList, QTemporaryFile, QDir
.. note:: 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.
"""
__author__ = 'Radim Blazek'
__date__ = '20/08/2012'
__copyright__ = 'Copyright 2012, The Quantum GIS Project'
# This will get replaced with a git SHA1 when you do a git archive
__revision__ = '$Format:%H$'

import os
import glob

from PyQt4.QtCore import (QTemporaryFile,
QDir)
from qgis.core import (QgsRasterLayer,
QgsRasterChecker,
QgsRasterPipe,
QgsRasterFileWriter,
QgsRasterProjector)
from utilities import (unitTestDataPath,
getQgisTestApp,
TestCase,
unittest
#expectedFailure
)
# Convenience instances in case you may need them
# not used in this test
from utilities import getQgisTestApp
QGISAPP, CANVAS, IFACE, PARENT = getQgisTestApp()

class TestQgsRasterFileWriter(unittest.TestCase):
class TestQgsRasterFileWriter(TestCase):

def __init__(self,methodName):
unittest.TestCase.__init__(self,methodName)
self.testDataDir = os.path.abspath(os.path.join(__file__, '..', '..', '..', 'testdata'))
self.testDataDir = unitTestDataPath()
self.report = "<h1>Python Raster File Writer Tests</h1>\n"

def write(self, theRasterName):
print theRasterName

path = "%s/%s" % ( self.testDataDir, theRasterName )
#myFileInfo = QFileInfo( path )
#myBaseName = myFileInfo.baseName()
rasterLayer = QgsRasterLayer(path, "test")
if not rasterLayer.isValid(): return False
provider = rasterLayer.dataProvider()

tmpFile = QTemporaryFile()
tmpFile.open() # fileName is no avialable until open
tmpName = tmpFile.fileName()
tmpFile.close();
# do not remove when class is destroyd so that we can read the file and see difference
tmpFile.close()
# do not remove when class is destroyed so that we can read
# the file and see difference
tmpFile.setAutoRemove ( False )

fileWriter = QgsRasterFileWriter ( tmpName )
pipe = QgsRasterPipe()
if not pipe.set( provider.clone() ):
if not pipe.set( provider.clone() ):
print "Cannot set pipe provider"
return False

#nuller = QgsRasterNuller()
#nuller.setNoData( ... )
#if not pipe.insert( 1, nuller ):
# print "Cannot set pipe nuller"
# return False

projector = QgsRasterProjector()
projector.setCRS( provider.crs(), provider.crs() )
if not pipe.insert( 2, projector ):
print "Cannot set pipe projector"
return False

fileWriter.writeRaster( pipe, provider.xSize(), provider.ySize(), provider.extent(), provider.crs() )
fileWriter.writeRaster(
pipe,
provider.xSize(),
provider.ySize(),
provider.extent(),
provider.crs() )

checker = QgsRasterChecker()
ok = checker.runTest( "gdal", tmpName, "gdal", path );
self.report += checker.report();
ok = checker.runTest( "gdal", tmpName, "gdal", path )
self.report += checker.report()

# All OK, we can delete the file
tmpFile.setAutoRemove ( ok );
tmpFile.setAutoRemove ( ok )

return ok

def testWrite(self):
for name in glob.glob( "%s/raster/*.tif" % self.testDataDir ):
baseName = os.path.basename ( name )
allOk = True
ok = self.write( "raster/%s" % baseName )
if not ok: allOk = False

reportFilePath = "%s/qgistest.html" % QDir.tempPath()
reportFile = open(reportFilePath,'a')
reportFile.write( self.report )
Expand Down
167 changes: 102 additions & 65 deletions tests/src/python/test_qgsrasterlayer.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,24 @@
# -*- coding: utf-8 -*-
"""QGIS Unit tests for QgsRasterLayer.
.. note:: 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.
"""
__author__ = 'Tim Sutton'
__date__ = '20/08/2012'
__copyright__ = 'Copyright 2012, The Quantum GIS Project'
# This will get replaced with a git SHA1 when you do a git archive
__revision__ = '$Format:%H$'

import os
import unittest

from PyQt4.QtCore import QFileInfo, QString, QStringList
from PyQt4 import QtGui

from qgis.core import (QgsRasterLayer,
from qgis.core import (QgsRasterLayer,
QgsColorRampShader,
QgsContrastEnhancement,
QgsMapLayerRegistry,
Expand All @@ -15,16 +29,20 @@
QgsRenderChecker,
QgsSingleBandGrayRenderer,
QgsSingleBandPseudoColorRenderer)

from utilities import (unitTestDataPath,
getQgisTestApp,
TestCase,
unittest
#expectedFailure
)
# Convenience instances in case you may need them
# not used in this test
from utilities import getQgisTestApp
QGISAPP, CANVAS, IFACE, PARENT = getQgisTestApp()

class TestQgsRasterLayer(unittest.TestCase):
class TestQgsRasterLayer(TestCase):

def testIdentify(self):
myPath = os.path.abspath(os.path.join(__file__, '..', '..', '..', 'testdata', 'landsat.tif'))
myPath = os.path.join(unitTestDataPath(), 'landsat.tif')
myFileInfo = QFileInfo(myPath)
myBaseName = myFileInfo.baseName()
myRasterLayer = QgsRasterLayer(myPath, myBaseName)
Expand All @@ -35,7 +53,7 @@ def testIdentify(self):
myResult, myRasterValues = myRasterLayer.identify(myPoint)
assert myResult
# Get the name of the first band
myBandName = myRasterValues.keys()[0]
myBandName = myRasterValues.keys()[0]
myExpectedName = QString('Band 1')
myMessage = 'Expected "%s" got "%s" for first raster band name' % (
myExpectedName, myBandName)
Expand All @@ -53,111 +71,130 @@ def testIdentify(self):
self.assertEquals(myValues, myExpectedValues, myMessage)

def testTransparency(self):
myPath = os.path.abspath(os.path.join(__file__, '..', '..', '..', 'testdata', 'raster', 'band1_float32_noct_epsg4326.tif'))
myPath = os.path.join(unitTestDataPath('raster'),
'band1_float32_noct_epsg4326.tif')
myFileInfo = QFileInfo(myPath)
myBaseName = myFileInfo.baseName()
myRasterLayer = QgsRasterLayer(myPath, myBaseName)
myMessage = 'Raster not loaded: %s' % myPath
assert myRasterLayer.isValid(), myMessage

renderer = QgsSingleBandGrayRenderer( myRasterLayer.dataProvider(), 1 );
myRasterLayer.setRenderer( renderer );
myRasterLayer.setContrastEnhancementAlgorithm( QgsContrastEnhancement.StretchToMinimumMaximum, QgsRasterLayer.ContrastEnhancementMinMax );
renderer = QgsSingleBandGrayRenderer(myRasterLayer.dataProvider(), 1)
myRasterLayer.setRenderer(renderer)
myRasterLayer.setContrastEnhancementAlgorithm(
QgsContrastEnhancement.StretchToMinimumMaximum,
QgsRasterLayer.ContrastEnhancementMinMax)

myContrastEnhancement = myRasterLayer.renderer().contrastEnhancement()
#print "myContrastEnhancement.minimumValue = %.17g" % myContrastEnhancement.minimumValue()
#print "myContrastEnhancement.maximumValue = %.17g" % myContrastEnhancement.maximumValue()
#print ("myContrastEnhancement.minimumValue = %.17g" %
# myContrastEnhancement.minimumValue())
#print ("myContrastEnhancement.maximumValue = %.17g" %
# myContrastEnhancement.maximumValue())

# Unfortunately the minimum/maximum values calculated in C++ and Python
# are slightely different (e.g. 3.3999999521443642e+38 x 3.3999999521444001e+38)
# It is not clear where the precision is lost. We set the same values as C++.
myContrastEnhancement.setMinimumValue( -3.3319999287625854e+38 )
myContrastEnhancement.setMaximumValue( 3.3999999521443642e+38 )
#myType = myRasterLayer.dataProvider().dataType( 1 );
#myEnhancement = QgsContrastEnhancement( myType );

# are slightly different (e.g. 3.3999999521443642e+38 x
# 3.3999999521444001e+38)
# It is not clear where the precision is lost.
# We set the same values as C++.
myContrastEnhancement.setMinimumValue(-3.3319999287625854e+38)
myContrastEnhancement.setMaximumValue(3.3999999521443642e+38)
#myType = myRasterLayer.dataProvider().dataType(1);
#myEnhancement = QgsContrastEnhancement(myType);



myTransparentSingleValuePixelList = []
rasterTransparency = QgsRasterTransparency()

myTransparentPixel1 = QgsRasterTransparency.TransparentSingleValuePixel()
myTransparentPixel1 = \
QgsRasterTransparency.TransparentSingleValuePixel()
myTransparentPixel1.min = -2.5840000772112106e+38
myTransparentPixel1.max = -1.0879999684602689e+38
myTransparentPixel1.percentTransparent = 50
myTransparentSingleValuePixelList.append( myTransparentPixel1 )
myTransparentSingleValuePixelList.append(myTransparentPixel1)

myTransparentPixel2 = QgsRasterTransparency.TransparentSingleValuePixel()
myTransparentPixel2 = \
QgsRasterTransparency.TransparentSingleValuePixel()
myTransparentPixel2.min = 1.359999960575336e+37
myTransparentPixel2.max = 9.520000231087593e+37
myTransparentPixel2.percentTransparent = 70
myTransparentSingleValuePixelList.append( myTransparentPixel2 )
myTransparentSingleValuePixelList.append(myTransparentPixel2)

rasterTransparency.setTransparentSingleValuePixelList( myTransparentSingleValuePixelList )
rasterTransparency.setTransparentSingleValuePixelList(
myTransparentSingleValuePixelList)

rasterRenderer = myRasterLayer.renderer()
assert rasterRenderer

rasterRenderer.setRasterTransparency( rasterTransparency )
rasterRenderer.setRasterTransparency(rasterTransparency)

QgsMapLayerRegistry.instance().addMapLayers( [ myRasterLayer, ] )
QgsMapLayerRegistry.instance().addMapLayers([ myRasterLayer, ])

myMapRenderer = QgsMapRenderer()

myLayers = QStringList()
myLayers.append( myRasterLayer.id() )
myMapRenderer.setLayerSet( myLayers )
myMapRenderer.setExtent( myRasterLayer.extent() )
myLayers.append(myRasterLayer.id())
myMapRenderer.setLayerSet(myLayers)
myMapRenderer.setExtent(myRasterLayer.extent())

myChecker = QgsRenderChecker()
myChecker.setControlName( "expected_raster_transparency" )
myChecker.setMapRenderer( myMapRenderer )
myChecker.setControlName("expected_raster_transparency")
myChecker.setMapRenderer(myMapRenderer)

myResultFlag = myChecker.runTest( "raster_transparency_python" );
myResultFlag = myChecker.runTest("raster_transparency_python");
assert myResultFlag, "Raster transparency rendering test failed"

def testShaderCrash(self):
"""Check if we assign a shader and then reassign it no crash occurs."""
myPath = os.path.abspath(os.path.join(__file__, '..', '..', '..', 'testdata', 'raster', 'band1_float32_noct_epsg4326.tif'))
myPath = os.path.join(unitTestDataPath('raster'),
'band1_float32_noct_epsg4326.tif')
myFileInfo = QFileInfo(myPath)
myBaseName = myFileInfo.baseName()
myRasterLayer = QgsRasterLayer(myPath, myBaseName)
myMessage = 'Raster not loaded: %s' % myPath
assert myRasterLayer.isValid(), myMessage

myRasterShader = QgsRasterShader()
myColorRampShader = QgsColorRampShader()
myColorRampShader.setColorRampType(QgsColorRampShader.INTERPOLATED)
myItems = []
myItem = QgsColorRampShader.ColorRampItem(10, QtGui.QColor('#ffff00'), 'foo')
myItems.append(myItem)
myItem = QgsColorRampShader.ColorRampItem(100, QtGui.QColor('#ff00ff'), 'bar')
myItems.append(myItem)
myItem = QgsColorRampShader.ColorRampItem(1000, QtGui.QColor('#00ff00'), 'kazam')
myItems.append(myItem)
myColorRampShader.setColorRampItemList(myItems)
myRasterShader.setRasterShaderFunction(myColorRampShader)
myPseudoRenderer = QgsSingleBandPseudoColorRenderer(myRasterLayer.dataProvider(), 1, myRasterShader)
myRasterLayer.setRenderer(myPseudoRenderer)
myRasterShader = QgsRasterShader()
myColorRampShader = QgsColorRampShader()
myColorRampShader.setColorRampType(QgsColorRampShader.INTERPOLATED)
myItems = []
myItem = QgsColorRampShader.ColorRampItem(10,
QtGui.QColor('#ffff00'), 'foo')
myItems.append(myItem)
myItem = QgsColorRampShader.ColorRampItem(100,
QtGui.QColor('#ff00ff'), 'bar')
myItems.append(myItem)
myItem = QgsColorRampShader.ColorRampItem(1000,
QtGui.QColor('#00ff00'), 'kazam')
myItems.append(myItem)
myColorRampShader.setColorRampItemList(myItems)
myRasterShader.setRasterShaderFunction(myColorRampShader)
myPseudoRenderer = QgsSingleBandPseudoColorRenderer(
myRasterLayer.dataProvider(), 1, myRasterShader)
myRasterLayer.setRenderer(myPseudoRenderer)

return
######## works first time #############

myRasterShader = QgsRasterShader()
myColorRampShader = QgsColorRampShader()
myColorRampShader.setColorRampType(QgsColorRampShader.INTERPOLATED)
myItems = []
myItem = QgsColorRampShader.ColorRampItem(10, QtGui.QColor('#ffff00'), 'foo')
myItems.append(myItem)
myItem = QgsColorRampShader.ColorRampItem(100, QtGui.QColor('#ff00ff'), 'bar')
myItems.append(myItem)
myItem = QgsColorRampShader.ColorRampItem(1000, QtGui.QColor('#00ff00'), 'kazam')
myItems.append(myItem)
myColorRampShader.setColorRampItemList(myItems)
myRasterShader.setRasterShaderFunction(myColorRampShader)
######## crash on next line ##################
myPseudoRenderer = QgsSingleBandPseudoColorRenderer(myRasterLayer.dataProvider(), 1, myRasterShader)
myRasterLayer.setRenderer(myPseudoRenderer)
######## works first time #############

myRasterShader = QgsRasterShader()
myColorRampShader = QgsColorRampShader()
myColorRampShader.setColorRampType(QgsColorRampShader.INTERPOLATED)
myItems = []
myItem = QgsColorRampShader.ColorRampItem(10,
QtGui.QColor('#ffff00'), 'foo')
myItems.append(myItem)
myItem = QgsColorRampShader.ColorRampItem(100,
QtGui.QColor('#ff00ff'), 'bar')
myItems.append(myItem)
myItem = QgsColorRampShader.ColorRampItem(1000,
QtGui.QColor('#00ff00'), 'kazam')
myItems.append(myItem)
myColorRampShader.setColorRampItemList(myItems)
myRasterShader.setRasterShaderFunction(myColorRampShader)
######## crash on next line (fixed now)##################
myPseudoRenderer = QgsSingleBandPseudoColorRenderer(
myRasterLayer.dataProvider(), 1, myRasterShader)
myRasterLayer.setRenderer(myPseudoRenderer)

if __name__ == '__main__':
unittest.main()
61 changes: 25 additions & 36 deletions tests/src/python/test_qgsrectangle.py
Original file line number Diff line number Diff line change
@@ -1,39 +1,26 @@
# -*- coding: utf-8 -*-
'''
test_qgsrectangle.py
--------------------------------------
Date : 07 Sep 2012
Copyright : (C) 2012 by Alexander Bruy
email : alexander dot bruy at gmail dot com
***************************************************************************
* *
* 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 3 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
'''

import unittest

from qgis.core import (QGis,
QgsRectangle,
"""QGIS Unit tests for QgsComposition.
.. note:: 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.
"""
__author__ = 'Tim Sutton'
__date__ = '20/08/2012'
__copyright__ = 'Copyright 2012, The Quantum GIS Project'
# This will get replaced with a git SHA1 when you do a git archive
__revision__ = '$Format:%H$'

from qgis.core import (QgsRectangle,
QgsPoint)

from utilities import getQgisTestApp
import sys

# support python < 2.7 via unittest2
# needed for expected failure decorator
if sys.version_info[0:2] < (2,7):
try:
from unittest2 import TestCase, expectedFailure
except ImportError:
print "You should install unittest2 to run the salt tests"
sys.exit(0)
else:
from unittest import TestCase, expectedFailure
from utilities import (unitTestDataPath,
getQgisTestApp,
TestCase,
unittest,
expectedFailure
)

QGISAPP, CANVAS, IFACE, PARENT = getQgisTestApp()

Expand Down Expand Up @@ -166,7 +153,8 @@ def testUnion(self):
assert rect1.contains(rect2), myMessage

print rect1.toString()
assert rect1 == QgsRectangle(0.0, 0.0, 7.0, 7.0), "Wrong combine with rectangle result"
assert (rect1 == QgsRectangle(0.0, 0.0, 7.0, 7.0),
'Wrong combine with rectangle result')

rect1 = QgsRectangle( 0.0, 0.0, 5.0, 5.0)
rect1.combineExtentWith(6.0, 2.0)
Expand All @@ -191,14 +179,15 @@ def testUnion(self):
def testAsWktCoordinates(self):
"""Test that we can get a proper wkt representation fo the rect"""
rect1 = QgsRectangle( 0.0, 0.0, 5.0, 5.0)
myExpectedWkt = '0.0000000000000000 0.0000000000000000, 5.0000000000000000 5.0000000000000000'
myExpectedWkt = ('0.0000000000000000 0.0000000000000000, '
'5.0000000000000000 5.0000000000000000')
myWkt = rect1.asWktCoordinates()
myMessage = ('Expected: %s\nGot: %s\n' %
(myExpectedWkt, myWkt))
self.assertEquals(myWkt, myExpectedWkt, myMessage)

def testAsWktPolygon(self):
"""Test that we can get a proper wkt polygon representation fo the rect"""
"""Test that we can get a proper rect wkt polygon representation for rect"""
rect1 = QgsRectangle( 0.0, 0.0, 5.0, 5.0)
myExpectedWkt = ('POLYGON((0.0000000000000000 0.0000000000000000, '
'5.0000000000000000 0.0000000000000000, '
Expand Down
30 changes: 12 additions & 18 deletions tests/src/python/test_qgsspatialindex.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
# -*- coding: utf-8 -*-
'''
test_qgsspatialindex.py
--------------------------------------
Date : 07 Sep 2012
Copyright : (C) 2012 by Alexander Bruy
email : alexander dot bruy at gmail dot com
***************************************************************************
* *
* 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 3 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
'''
"""QGIS Unit tests for QgsSpatialIndex.
.. note:: 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.
"""
__author__ = 'Alexander Bruy'
__date__ = '20/01/2011'
__copyright__ = 'Copyright 2012, The Quantum GIS Project'
# This will get replaced with a git SHA1 when you do a git archive
__revision__ = '$Format:%H$'

import unittest

Expand Down Expand Up @@ -61,6 +58,3 @@ def testIndex(self):
myMessage = ('Expected: %s\nGot: %s\n' %
([0, 1, 5], fids))
assert fids == [0, 1, 5], myMessage

if __name__ == '__main__':
unittest.main()
314 changes: 182 additions & 132 deletions tests/src/python/test_qgssymbollayerv2.py

Large diffs are not rendered by default.

29 changes: 22 additions & 7 deletions tests/src/python/test_qgsvectorlayer.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,31 @@
import os
import unittest
# -*- coding: utf-8 -*-
"""QGIS Unit tests for QgsVectorLayer.
from PyQt4.QtCore import QDir
.. note:: 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.
"""
__author__ = 'Tim Sutton'
__date__ = '20/08/2012'
__copyright__ = 'Copyright 2012, The Quantum GIS Project'
# This will get replaced with a git SHA1 when you do a git archive
__revision__ = '$Format:%H$'

from qgis.core import QgsVectorLayer
from utilities import getQgisTestApp, unitTestDataPath
import os

from qgis.core import QgsVectorLayer
from utilities import (unitTestDataPath,
getQgisTestApp,
TestCase,
unittest,
#expectedFailure
)
QGISAPP, CANVAS, IFACE, PARENT = getQgisTestApp()


class TestQgsVectorLayer(unittest.TestCase):
class TestQgsVectorLayer(TestCase):

def test_FeatureCount(self):
myPath = os.path.join(unitTestDataPath(), 'lines.shp')
myLayer = QgsVectorLayer(myPath, 'Lines', 'ogr')
Expand Down
34 changes: 31 additions & 3 deletions tests/src/python/utilities.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,39 @@
"""Helper utilities for QGIS python unit tests.
.. note:: 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.
"""
__author__ = 'Tim Sutton (tim@linfiniti.com)'
__date__ = '20/01/2011'
__copyright__ = 'Copyright 2012, The Quantum GIS Project'
# This will get replaced with a git SHA1 when you do a git archive
__revision__ = '$Format:%H$'

import os
import sys
from PyQt4 import QtGui, QtCore
from qgis.core import (QgsApplication,
QgsCoordinateReferenceSystem)
QgsCoordinateReferenceSystem)
from qgis.gui import QgsMapCanvas
from qgis_interface import QgisInterface
import hashlib

# Support python < 2.7 via unittest2 needed for expected failure decorator.
# Note that you should ignore unused import warnings here as these are imported
# from this module by other tests.
if sys.version_info[0:2] < (2, 7):
try:
from unittest2 import TestCase, expectedFailure
import unittest2 as unittest
except ImportError:
print "You should install unittest2 to run the salt tests"
sys.exit(0)
else:
from unittest import TestCase, expectedFailure
import unittest

QGISAPP = None # Static variable used to hold hand to running QGis app
CANVAS = None
PARENT = None
Expand All @@ -24,7 +51,7 @@ def assertHashesForFile(theHashes, theFilename):
'\nPlease check graphics %s visually '
'and add to list of expected hashes '
'if it is OK on this platform.'
% (myHash, theHashes, theFilename))
% (myHash, theHashes, theFilename))
assert myHash in theHashes, myMessage


Expand Down Expand Up @@ -112,6 +139,7 @@ def unitTestDataPath(theSubdir=None):
myPath = os.path.abspath(os.path.join(myPath[0], 'testdata'))
return myPath


def setCanvasCrs(theEpsgId, theOtfpFlag=False):
"""Helper to set the crs for the CANVAS before a test is run.
Expand All @@ -121,7 +149,7 @@ def setCanvasCrs(theEpsgId, theOtfpFlag=False):
* theOtfpFlag - whether on the fly projections should be enabled
on the CANVAS. Default to False.
"""
# Enable on-the-fly reprojection
# Enable on-the-fly reprojection
CANVAS.mapRenderer().setProjectionsEnabled(theOtfpFlag)

# Create CRS Instance
Expand Down