test_qgsissue6483.py

Vincent Mora, 2013-07-11 05:58 AM

Download (2.76 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
import random
17

    
18
from qgis.core import *
19
from qgis.gui import *
20

    
21
from utilities import (getQgisTestApp,
22
                       TestCase,
23
                       unittest
24
                       )
25

    
26
from pyspatialite import dbapi2 as sqlite3
27

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

    
31

    
32
def die(error_message):
33
    raise Exception(error_message)
34

    
35
class TestQgsSpatialiteProvider(TestCase):
36

    
37
    @classmethod
38
    def setUpClass(cls):
39
        """Run before all tests"""
40
        # create test db
41
        if os.path.exists("test.sqlite") :
42
            os.remove("test.sqlite")
43
        con = sqlite3.connect("test.sqlite")
44
        cur = con.cursor()
45
        sql = "SELECT InitSpatialMetadata()"
46
        cur.execute(sql)
47

    
48
        # simple table with primary key
49
        sql = "CREATE TABLE test_mpg (id SERIAL PRIMARY KEY, height DOUBLE)"
50
        cur.execute(sql)
51
        sql = "SELECT AddGeometryColumn('test_mpg', 'geometry', 4326, 'MULTIPOLYGON', 'XY')"
52
        cur.execute(sql)
53
        for i in range(0,253):
54
            for j in range (0,253):
55
                sql = "INSERT INTO test_mpg (height, geometry) "
56
                sql +=    "VALUES ("+str(random.random())+", GeomFromText('MULTIPOLYGON((("
57
                sql += str(i)   + " " + str(j)   + ","
58
                sql += str(i+1) + " " + str(j)   + ","
59
                sql += str(i+1) + " " + str(j+1) + ","
60
                sql += str(i)   + " " + str(j+1) + ","
61
                sql += str(i)   + " " + str(j) + ")))', 4326))"
62
                cur.execute(sql)
63

    
64
        con.commit()
65
        con.close()
66

    
67
    @classmethod
68
    def tearDownClass(cls):
69
        """Run after all tests"""
70
        # for the time beeing, keep the file to check with qgis
71
        #if os.path.exists("test.sqlite") :
72
        #    os.remove("test.sqlite")
73
        pass
74

    
75
    def setUp(self):
76
        """Run before each test."""
77
        pass
78

    
79
    def tearDown(self):
80
        """Run after each test."""
81
        pass
82

    
83
    def test_Nothing(self):
84
        """Create spatialite database"""
85
        layer = QgsVectorLayer("dbname=test.sqlite table=test_mpg (geometry)", "test_mpg", "spatialite")
86
        assert(layer.isValid())
87
        assert(layer.hasGeometryType())
88

    
89

    
90

    
91
if __name__ == '__main__':
92
    unittest.main()
93

    
94