test_connpool.py

Hugo Mercier, 2015-02-25 05:19 AM

Download (2.58 KB)

 
1
# -*- coding: utf-8 -*-
2
"""QGIS Unit tests for QgsVirtualLayerProvider
3

4
.. note:: This program is free software; you can redistribute it and/or modify
5
it under the terms of the GNU General Public License as published by
6
the Free Software Foundation; either version 2 of the License, or
7
(at your option) any later version.
8
"""
9
__author__ = 'Hugo Mercier'
10
__date__ = '26/01/2015'
11
__copyright__ = 'Copyright 2015, The QGIS Project'
12
# This will get replaced with a git SHA1 when you do a git archive
13
__revision__ = '$Format:%H$'
14

    
15
import qgis
16

    
17
from PyQt4.QtCore import *
18

    
19
from qgis.core import (QGis,
20
                       QgsVectorLayer,
21
                       QgsFeature,
22
                       QgsFeatureRequest,
23
                       QgsField,
24
                       QgsGeometry,
25
                       QgsPoint,
26
                       QgsMapLayerRegistry
27
                      )
28

    
29
from utilities import (getQgisTestApp,
30
                       TestCase,
31
                       unittest,
32
                       unitTestDataPath
33
                       )
34
QGISAPP, CANVAS, IFACE, PARENT = getQgisTestApp()
35

    
36
import os
37
import time
38

    
39
import pyspatialite.dbapi2 as db
40

    
41
class TestConnPool(TestCase):
42
    def test(self):
43
        # always use the same file
44
        fn = '/tmp/t.sqlite'
45

    
46
        for it in range(2):
47
            print it
48
            # make sure the file does not exist
49
            if os.path.exists(fn):
50
                os.unlink(fn)
51
            conn = db.connect(fn, isolation_level = None)
52
            # create a simple geometry table with 2 features
53
            cur = conn.cursor()
54
            cur.execute("BEGIN")
55
            cur.execute("SELECT InitSpatialMetadata()")
56
            cur.execute("CREATE TABLE t%d(id INTEGER PRIMARY KEY)" % it)
57
            cur.execute("SELECT AddGeometryColumn('t%d', 'geometry', 4326, 'POINT', 'XY')" % it)
58
            cur.execute("INSERT INTO t%d (id, geometry) VALUES(1, GeomFromText('POINT(0 0)', 4326))" % it)
59
            cur.execute("INSERT INTO t%d (id, geometry) VALUES(2, GeomFromText('POINT(1 1)', 4326))" % it)
60
            cur.execute("COMMIT")
61
            conn.close()
62

    
63
            l = QgsVectorLayer("dbname=%s table=t%d (geometry)" % (fn,it), "tt", "spatialite", False)
64
            assert l.isValid()
65
            QgsMapLayerRegistry.instance().addMapLayer(l)
66
            assert l.hasGeometryType()
67
            # iterate over features
68
            assert sum(f.id() for f in l.getFeatures()) == 3
69
            # => fails the second time it is executed : "no such table t1"
70

    
71
            QgsMapLayerRegistry.instance().removeMapLayer(l.id())
72

    
73
if __name__ == '__main__':
74
    unittest.main()
75