Skip to content
Permalink
Browse files

Add tests

  • Loading branch information
pblottiere committed Aug 21, 2017
1 parent 384e85c commit 4a084bf961574b8c009fd554daf275386bc11632
Showing with 87 additions and 1 deletion.
  1. +82 −1 tests/src/python/test_provider_postgres.py
  2. +5 −0 tests/testdata/provider/testdata_pg.sql
@@ -28,11 +28,14 @@
NULL,
QgsVectorLayerUtils,
QgsSettings,
QgsTransactionGroup
QgsTransactionGroup,
QgsReadWriteContext,
QgsRectangle
)
from qgis.gui import QgsGui
from qgis.PyQt.QtCore import QDate, QTime, QDateTime, QVariant, QDir
from qgis.testing import start_app, unittest
from qgis.PyQt.QtXml import QDomDocument
from utilities import unitTestDataPath
from providertestbase import ProviderTestCase

@@ -718,6 +721,84 @@ def testStyle(self):
self.assertEqual(desclist, [])
self.assertEqual(errmsg, "")

def testHasMetadata(self):
# views don't have metadata
vl = QgsVectorLayer('{} table="qgis_test"."{}" key="pk" sql='.format(self.dbconn, 'bikes_view'), "bikes_view", "postgres")
self.assertTrue(vl.isValid())
self.assertFalse(vl.dataProvider().hasMetadata())

# ordinary tables have metadata
vl = QgsVectorLayer('%s table="qgis_test"."someData" sql=' % (self.dbconn), "someData", "postgres")
self.assertTrue(vl.isValid())
self.assertTrue(vl.dataProvider().hasMetadata())

def testReadExtentOnView(self):
# vector layer based on view
vl0 = QgsVectorLayer(self.dbconn + ' sslmode=disable key=\'pk\' srid=4326 type=POLYGON table="qgis_test"."some_poly_data_view" (geom) sql=', 'test', 'postgres')
self.assertTrue(vl0.isValid())
self.assertFalse(vl0.dataProvider().hasMetadata())

# set a custom extent
originalExtent = vl0.extent()

customExtent = QgsRectangle(-80, 80, -70, 90)
vl0.setExtent(customExtent)

# write xml
doc = QDomDocument("testdoc")
elem = doc.createElement("maplayer")
self.assertTrue(vl0.writeLayerXml(elem, doc, QgsReadWriteContext()))

# read xml with the custom extent. It should not be used by default
vl1 = QgsVectorLayer()
vl1.readLayerXml(elem, QgsReadWriteContext())
self.assertTrue(vl1.isValid())

self.assertEqual(vl1.extent(), originalExtent)

# read xml with custom extent with readExtent option. Extent read from
# xml document should be used because we have a view
vl2 = QgsVectorLayer()
vl2.setReadExtent(True)
vl2.readLayerXml(elem, QgsReadWriteContext())
self.assertTrue(vl2.isValid())

self.assertEqual(vl2.extent(), customExtent)

def testReadExtentOnTable(self):
# vector layer based on a standard table
vl0 = QgsVectorLayer(self.dbconn + ' sslmode=disable key=\'pk\' srid=4326 type=POLYGON table="qgis_test"."some_poly_data" (geom) sql=', 'test', 'postgres')
self.assertTrue(vl0.isValid())
self.assertTrue(vl0.dataProvider().hasMetadata())

# set a custom extent
originalExtent = vl0.extent()

customExtent = QgsRectangle(-80, 80, -70, 90)
vl0.setExtent(customExtent)

# write xml
doc = QDomDocument("testdoc")
elem = doc.createElement("maplayer")
self.assertTrue(vl0.writeLayerXml(elem, doc, QgsReadWriteContext()))

# read xml with the custom extent. It should not be used by default
vl1 = QgsVectorLayer()
vl1.readLayerXml(elem, QgsReadWriteContext())
self.assertTrue(vl1.isValid())

self.assertEqual(vl1.extent(), originalExtent)

# read xml with custom extent with readExtent option. Extent read from
# xml document should NOT be used because we don't have a view or a
# materialized view
vl2 = QgsVectorLayer()
vl2.setReadExtent(True)
vl2.readLayerXml(elem, QgsReadWriteContext())
self.assertTrue(vl2.isValid())

self.assertEqual(vl2.extent(), originalExtent)


class TestPyQgsPostgresProviderCompoundKey(unittest.TestCase, ProviderTestCase):

@@ -47,6 +47,11 @@ CREATE TABLE qgis_test."some_poly_data" (
geom public.geometry(Polygon,4326)
);

CREATE OR REPLACE VIEW qgis_test.some_poly_data_view
AS
SELECT *
FROM qgis_test.some_poly_data;

--
-- TOC entry 4068 (class 0 OID 377761)
-- Dependencies: 171

0 comments on commit 4a084bf

Please sign in to comment.
You can’t perform that action at this time.