test_qgsspatialiteprovider.py

Vincent Mora, 2013-07-09 06:48 AM

Download (2.52 KB)

 
1
# -*- coding: utf-8 -*-
2
"""QGIS Unit tests for QgsSpatialiteProvider
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__ = 'Vincent Mora'
10
__date__ = '09/07/2013'
11
__copyright__ = 'Copyright 2013, 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 os
16

    
17
from qgis.core import *
18

    
19
from utilities import (getQgisTestApp,
20
                       TestCase,
21
                       unittest
22
                       )
23

    
24
from pyspatialite import dbapi2 as sqlite3
25

    
26
# Convenience instances in case you may need them
27
QGISAPP, CANVAS, IFACE, PARENT = getQgisTestApp()
28

    
29

    
30
def die(error_message):
31
    raise Exception(error_message)
32

    
33
class TestQgsSpatialiteProvider(TestCase):
34

    
35
    @classmethod
36
    def setUpClass(cls):
37
        """Run before all tests"""
38
        if os.path.exists("test.sqlite") :
39
            os.remove("test.sqlite")
40
        con = sqlite3.connect("test.sqlite")
41
        cur = con.cursor()
42
        sql = "SELECT InitSpatialMetadata()"
43
        cur.execute(sql)
44
        sql = "CREATE TABLE test_pg (id INTEGER NOT NULL PRIMARY KEY, name TEXT NOT NULL)"
45
        cur.execute(sql)
46
        sql = "SELECT AddGeometryColumn('test_pg', 'geometry', 4326, 'POLYGON', 'XY')"
47
        cur.execute(sql)
48
        sql = "INSERT INTO test_pg (id, name, geometry) "
49
        sql +=    "VALUES (11, 'toto', GeomFromText('POLYGON((0 0,1 0,1 1,0 1,0 0))', 4326))"
50
        rs = cur.execute(sql)
51
        con.commit()
52
        con.close()
53
        
54
    @classmethod
55
    def tearDownClass(cls):
56
        """Run after all tests"""
57
        # for the time beeing, keep the file to check with qgis
58
        #if os.path.exists("test.sqlite") :
59
        #    os.remove("test.sqlite")
60
        pass
61

    
62
    def setUp(self):
63
        """Run before each test."""
64
        pass
65

    
66
    def tearDown(self):
67
        """Run after each test."""
68
        pass
69

    
70
    def test_SplitFeature(self):
71
        """Create spatialite database"""
72
        layer = QgsVectorLayer("dbname=test.sqlite table=test_pg (geometry)", "test_pg", "spatialite")
73
        assert(layer.isValid())
74
        assert(layer.hasGeometryType())
75
        layer.startEditing()
76
        layer.splitFeatures([QgsPoint(0.5, -0.5), QgsPoint(0.5, 1.5)], 0)==0 or die("error in split")
77
        layer.commitChanges() or die("error in commit")
78
        
79

    
80

    
81
if __name__ == '__main__':
82
    unittest.main()
83

    
84