Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Provider testing
 * Test more operations
 * Include spatialite tests
  • Loading branch information
m-kuhn committed May 22, 2015
1 parent c4b1d25 commit 2fe3f55
Show file tree
Hide file tree
Showing 9 changed files with 130 additions and 55 deletions.
4 changes: 2 additions & 2 deletions .travis.yml
Expand Up @@ -47,7 +47,7 @@ before_script:
- printf "[qgis_test]\nhost=localhost\ndbname=qgis_test\nuser=postgres" > ~/.pg_service.conf
- psql -c 'CREATE DATABASE qgis_test;' -U postgres
- psql -c 'CREATE EXTENSION postgis;' -U postgres -d qgis_test
- psql -f $TRAVIS_BUILD_DIR/tests/testdata/postgres/testdata.sql -U postgres -d qgis_test
- psql -f $TRAVIS_BUILD_DIR/tests/testdata/provider/testdata.sql -U postgres -d qgis_test

script: xvfb-run ctest -V -E 'PyQgsPalLabelingServer|qgis_wcsprovidertest' -S ../qgis-test-travis.ctest --output-on-failure
script: xvfb-run ctest -V -E 'qgis_openstreetmaptest|PyQgsPalLabelingServer|qgis_wcsprovidertest' -S ../qgis-test-travis.ctest --output-on-failure

3 changes: 2 additions & 1 deletion tests/src/python/CMakeLists.txt
Expand Up @@ -36,7 +36,6 @@ ADD_PYTHON_TEST(PyQgsPalLabelingCanvas test_qgspallabeling_canvas.py)
ADD_PYTHON_TEST(PyQgsPalLabelingComposer test_qgspallabeling_composer.py)
ADD_PYTHON_TEST(PyQgsPalLabelingServer test_qgspallabeling_server.py)
ADD_PYTHON_TEST(PyQgsVectorFileWriter test_qgsvectorfilewriter.py)
ADD_PYTHON_TEST(PyQgsSpatialiteProvider test_qgsspatialiteprovider.py)
ADD_PYTHON_TEST(PyQgsZonalStatistics test_qgszonalstatistics.py)
ADD_PYTHON_TEST(PyQgsAppStartup test_qgsappstartup.py)
ADD_PYTHON_TEST(PyQgsDistanceArea test_qgsdistancearea.py)
Expand All @@ -45,6 +44,8 @@ ADD_PYTHON_TEST(PyQgsNetworkContentFetcher test_qgsnetworkcontentfetcher.py)
ADD_PYTHON_TEST(PyQgsEditWidgets test_qgseditwidgets.py)
ADD_PYTHON_TEST(PyQgsRangeWidgets test_qgsrangewidgets.py)
ADD_PYTHON_TEST(PyQgsPostgresProvider test_provider_postgres.py)
ADD_PYTHON_TEST(PyQgsSpatialiteProvider test_provider_spatialite.py)
ADD_PYTHON_TEST(PyQgsSpatialiteProviderOther test_qgsspatialiteprovider.py)
IF (WITH_APIDOC)
ADD_PYTHON_TEST(PyQgsDocCoverage test_qgsdoccoverage.py)
ENDIF (WITH_APIDOC)
70 changes: 70 additions & 0 deletions tests/src/python/providertestbase.py
@@ -0,0 +1,70 @@
# -*- coding: utf-8 -*-
"""QGIS Unit test utils for provider 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__ = 'Matthias Kuhn'
__date__ = '2015-04-27'
__copyright__ = 'Copyright 2015, The QGIS Project'
# This will get replaced with a git SHA1 when you do a git archive
__revision__ = '$Format:%H$'

from qgis.core import QgsRectangle, QgsFeatureRequest, QgsGeometry, NULL
from utilities import TestCase

class ProviderTestCase(object):

def runGetFeatureTests(self, provider):
assert len( [f for f in provider.getFeatures()] ) == 5
assert len( [f for f in provider.getFeatures( QgsFeatureRequest().setFilterExpression( 'name IS NOT NULL' ) )] ) == 4
assert len( [f for f in provider.getFeatures( QgsFeatureRequest().setFilterExpression('name LIKE \'Apple\'' ) )] ) == 1
assert len( [f for f in provider.getFeatures( QgsFeatureRequest().setFilterExpression('name ILIKE \'aPple\'' ) )] ) == 1
assert len( [f for f in provider.getFeatures( QgsFeatureRequest().setFilterExpression('name ILIKE \'%pp%\'' ) )] ) == 1
assert len( [f for f in provider.getFeatures( QgsFeatureRequest().setFilterExpression('cnt > 0' ) ) ] ) == 4
assert len( [f for f in provider.getFeatures( QgsFeatureRequest().setFilterExpression('cnt < 0' ) ) ] ) == 1
assert len( [f for f in provider.getFeatures( QgsFeatureRequest().setFilterExpression('cnt >= 100' ) ) ] ) == 4
assert len( [f for f in provider.getFeatures( QgsFeatureRequest().setFilterExpression('cnt <= 100' ) ) ] ) == 2
assert len( [f for f in provider.getFeatures( QgsFeatureRequest().setFilterExpression('pk IN (1, 2, 4, 8)' ) )] ) == 3

def testGetFeaturesUncompiled(self):
try:
self.disableCompiler()
except AttributeError:
pass
self.runGetFeatureTests(self.provider)

def testGetFeaturesCompiled(self):
try:
self.enableCompiler()
self.runGetFeatureTests(self.provider)
except AttributeError:
print 'Provider does not support compiling'

def testGetFeaturesFilterRectTests(self):
extent = QgsRectangle(-70, 67, -60, 80)
features = [ f['pk'] for f in self.provider.getFeatures( QgsFeatureRequest().setFilterRect( extent ) ) ]
assert set(features) == set([2L, 4L]), 'Got {} instead'.format(features)

def testMinValue(self):
assert self.provider.minimumValue(1) == -200
assert self.provider.minimumValue(2) == 'Apple'

def testMaxValue(self):
assert self.provider.maximumValue(1) == 400
assert self.provider.maximumValue(2) == 'Pear'

def testExtent(self):
reference = QgsGeometry.fromRect(QgsRectangle(-71.1230000000000047,66.3299999999999983,-65.3199999999999932,78.2999999999999972))
provider_extent=QgsGeometry.fromRect(self.provider.extent())

assert QgsGeometry.compare( provider_extent.asPolygon(), reference.asPolygon(), 0.000001)

def testUnique(self):
assert set(self.provider.uniqueValues(1)) == set([-200, 100, 200, 300, 400])
# assert set([u'Apple', u'Honey', u'Orange', u'Pear', NULL]) == set(self.provider.uniqueValues(2)), 'Got {}'.format(set(self.provider.uniqueValues(2)))

def testFeatureCount(self):
assert self.provider.featureCount() == 5
33 changes: 0 additions & 33 deletions tests/src/python/providertestutils.py

This file was deleted.

29 changes: 11 additions & 18 deletions tests/src/python/test_provider_postgres.py
Expand Up @@ -19,39 +19,32 @@
from PyQt4.QtCore import QSettings
from utilities import (unitTestDataPath,
getQgisTestApp,
TestCase,
unittest
unittest,
TestCase
)
from providertestutils import * # testGetFeaturesUncompiled
from providertestbase import ProviderTestCase

QGISAPP, CANVAS, IFACE, PARENT = getQgisTestApp()
TEST_DATA_DIR = unitTestDataPath()

class TestPyQgsPostgresProvider(TestCase):
class TestPyQgsPostgresProvider(TestCase, ProviderTestCase):
@classmethod
def setUpClass(cls):
"""Run before all tests"""

# Create test database
# Create test layer
cls.vl = QgsVectorLayer( u'dbname=\'qgis_test\' host=localhost port=5432 user=\'postgres\' password=\'postgres\' sslmode=disable key=\'pk\' srid=4326 type=POINT table="qgis_test"."someData" (geom) sql=', 'test', 'postgres' )

assert(cls.vl.isValid())
cls.provider = cls.vl.dataProvider()

@classmethod
def tearDownClass(cls):
"""Run after all tests"""

# Delete test database

def testGetFeaturesUncompiled(self):
QSettings().setValue( "/qgis/postgres/compileExpressions", False )
runGetFeatureTests( self.vl )

def testGetFeaturesCompiled(self):
QSettings().setValue( "/qgis/postgres/compileExpressions", True )
runGetFeatureTests( self.vl )
def enableCompiler(self):
QSettings().setValue(u'/qgis/postgres/compileExpressions', True)

def testGetFeaturesFilterRectTests(self):
runGetFilterRectTests( self.vl )
def disableCompiler(self):
QSettings().setValue(u'/qgis/postgres/compileExpressions', False)

if __name__ == '__main__':
unittest.main()
44 changes: 44 additions & 0 deletions tests/src/python/test_provider_spatialite.py
@@ -0,0 +1,44 @@
# -*- coding: utf-8 -*-
"""QGIS Unit tests for the postgres provider.
.. 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__ = 'Matthias Kuhn'
__date__ = '2015-04-23'
__copyright__ = 'Copyright 2015, The QGIS Project'
# This will get replaced with a git SHA1 when you do a git archive
__revision__ = '$Format:%H$'

import qgis
import os

from qgis.core import QgsVectorLayer, QgsFeatureRequest, QgsFeature, QgsProviderRegistry
from PyQt4.QtCore import QSettings
from utilities import (unitTestDataPath,
getQgisTestApp,
unittest,
TestCase
)
from providertestbase import ProviderTestCase

QGISAPP, CANVAS, IFACE, PARENT = getQgisTestApp()
TEST_DATA_DIR = unitTestDataPath()

class TestPyQgsPostgresProvider(TestCase, ProviderTestCase):
@classmethod
def setUpClass(cls):
"""Run before all tests"""
# Create test layer
cls.vl = QgsVectorLayer( 'dbname=\'{}/provider/spatialite.db\' table="somedata" (geometry) sql='.format(TEST_DATA_DIR), 'test', 'spatialite' )
assert(cls.vl.isValid())
cls.provider = cls.vl.dataProvider()

@classmethod
def tearDownClass(cls):
"""Run after all tests"""

if __name__ == '__main__':
unittest.main()
2 changes: 1 addition & 1 deletion tests/src/python/test_qgsdoccoverage.py
Expand Up @@ -32,7 +32,7 @@
# DON'T LOWER THIS THRESHOLD UNLESS MEMBERS HAVE BEEN REMOVED FROM THE API
# (changes which raise this threshold are welcomed though!)

ACCEPTABLE_COVERAGE = 54.5365
ACCEPTABLE_COVERAGE = 54.6134


def elemIsDocumentableClass(elem):
Expand Down
Binary file added tests/testdata/provider/spatialite.db
Binary file not shown.
File renamed without changes.

0 comments on commit 2fe3f55

Please sign in to comment.