@@ -91,7 +91,7 @@ def setUpClass(cls):
91
91
sql = "SELECT AddGeometryColumn('test_pg', 'geometry', 4326, 'POLYGON', 'XY')"
92
92
cur .execute (sql )
93
93
sql = "INSERT INTO test_pg (id, name, geometry) "
94
- sql += "VALUES (1, 'toto', GeomFromText('POLYGON((0 0,1 0,1 1,0 1,0 0))', 4326))"
94
+ sql += "VALUES (1, 'toto 1 ', GeomFromText('POLYGON((0 0,1 0,1 1,0 1,0 0))', 4326))"
95
95
cur .execute (sql )
96
96
97
97
# table with Z dimension geometry
@@ -100,7 +100,7 @@ def setUpClass(cls):
100
100
sql = "SELECT AddGeometryColumn('test_z', 'geometry', 4326, 'POINT', 'XYZ')"
101
101
cur .execute (sql )
102
102
sql = "INSERT INTO test_z (id, name, geometry) "
103
- sql += "VALUES (1, 'toto', GeomFromText('POINT Z (0 0 1)', 4326))"
103
+ sql += "VALUES (1, 'toto 2 ', GeomFromText('POINT Z (0 0 1)', 4326))"
104
104
cur .execute (sql )
105
105
106
106
# table with M value geometry
@@ -109,7 +109,7 @@ def setUpClass(cls):
109
109
sql = "SELECT AddGeometryColumn('test_m', 'geometry', 4326, 'POINT', 'XYM')"
110
110
cur .execute (sql )
111
111
sql = "INSERT INTO test_m (id, name, geometry) "
112
- sql += "VALUES (1, 'toto', GeomFromText('POINT M (0 0 1)', 4326))"
112
+ sql += "VALUES (1, 'toto 3 ', GeomFromText('POINT M (0 0 1)', 4326))"
113
113
cur .execute (sql )
114
114
115
115
# table with Z dimension and M value geometry
@@ -118,7 +118,7 @@ def setUpClass(cls):
118
118
sql = "SELECT AddGeometryColumn('test_zm', 'geometry', 4326, 'POINT', 'XYZM')"
119
119
cur .execute (sql )
120
120
sql = "INSERT INTO test_zm (id, name, geometry) "
121
- sql += "VALUES (1, 'toto', GeomFromText('POINT ZM (0 0 1 1)', 4326))"
121
+ sql += "VALUES (1, 'toto 1 ', GeomFromText('POINT ZM (0 0 1 1)', 4326))"
122
122
cur .execute (sql )
123
123
124
124
# table with multiple column primary key
@@ -127,7 +127,7 @@ def setUpClass(cls):
127
127
sql = "SELECT AddGeometryColumn('test_pg_mk', 'geometry', 4326, 'POLYGON', 'XY')"
128
128
cur .execute (sql )
129
129
sql = "INSERT INTO test_pg_mk (id, name, geometry) "
130
- sql += "VALUES (1, 'toto', GeomFromText('POLYGON((0 0,1 0,1 1,0 1,0 0))', 4326))"
130
+ sql += "VALUES (1, 'toto 1 ', GeomFromText('POLYGON((0 0,1 0,1 1,0 1,0 0))', 4326))"
131
131
cur .execute (sql )
132
132
133
133
# simple table with primary key
@@ -136,10 +136,10 @@ def setUpClass(cls):
136
136
sql = "SELECT AddGeometryColumn('test_q', 'geometry', 4326, 'POLYGON', 'XY')"
137
137
cur .execute (sql )
138
138
sql = "INSERT INTO test_q (id, name, geometry) "
139
- sql += "VALUES (11, 'toto', GeomFromText('POLYGON((0 0,1 0,1 1,0 1,0 0))', 4326))"
139
+ sql += "VALUES (11, 'toto 11 ', GeomFromText('POLYGON((0 0,1 0,1 1,0 1,0 0))', 4326))"
140
140
cur .execute (sql )
141
141
sql = "INSERT INTO test_q (id, name, geometry) "
142
- sql += "VALUES (21, 'toto', GeomFromText('POLYGON((0 0,1 0,1 1,0 1,0 0))', 4326))"
142
+ sql += "VALUES (21, 'toto 12 ', GeomFromText('POLYGON((0 0,1 0,1 1,0 1,0 0))', 4326))"
143
143
cur .execute (sql )
144
144
145
145
# simple table with a geometry column named 'Geometry'
@@ -148,10 +148,10 @@ def setUpClass(cls):
148
148
sql = "SELECT AddGeometryColumn('test_n', 'Geometry', 4326, 'POLYGON', 'XY')"
149
149
cur .execute (sql )
150
150
sql = "INSERT INTO test_n (id, name, geometry) "
151
- sql += "VALUES (1, 'toto', GeomFromText('POLYGON((0 0,1 0,1 1,0 1,0 0))', 4326))"
151
+ sql += "VALUES (1, 'toto 1 ', GeomFromText('POLYGON((0 0,1 0,1 1,0 1,0 0))', 4326))"
152
152
cur .execute (sql )
153
153
sql = "INSERT INTO test_n (id, name, geometry) "
154
- sql += "VALUES (2, 'toto', GeomFromText('POLYGON((0 0,1 0,1 1,0 1,0 0))', 4326))"
154
+ sql += "VALUES (2, 'toto 1 ', GeomFromText('POLYGON((0 0,1 0,1 1,0 1,0 0))', 4326))"
155
155
cur .execute (sql )
156
156
157
157
# table with different array types, stored as JSON
@@ -357,7 +357,7 @@ def test_queries(self):
357
357
sum_id1 = sum (f .id () for f in l .getFeatures ())
358
358
# the attribute 'id' works
359
359
sum_id2 = sum (f .attributes ()[0 ] for f in l .getFeatures ())
360
- self .assertEqual (sum_id1 , 3 ) # 1+2
360
+ self .assertEqual (sum_id1 , 32 ) # 11 + 21
361
361
self .assertEqual (sum_id2 , 32 ) # 11 + 21
362
362
363
363
# and now with an id declared
@@ -789,6 +789,65 @@ def testLoadStyle(self):
789
789
err , ok = vl .loadDefaultStyle ()
790
790
self .assertTrue (ok )
791
791
792
+ def testPkLessQuery (self ):
793
+ """Test if features in queries with/without pk can be retrieved by id"""
794
+ # create test db
795
+ dbname = os .path .join (tempfile .gettempdir (), "test_pkless.sqlite" )
796
+ if os .path .exists (dbname ):
797
+ os .remove (dbname )
798
+ con = spatialite_connect (dbname , isolation_level = None )
799
+ cur = con .cursor ()
800
+ cur .execute ("BEGIN" )
801
+ sql = "SELECT InitSpatialMetadata()"
802
+ cur .execute (sql )
803
+
804
+ # simple table with primary key
805
+ sql = "CREATE TABLE test_pk (id INTEGER NOT NULL PRIMARY KEY, name TEXT NOT NULL)"
806
+ cur .execute (sql )
807
+
808
+ sql = "SELECT AddGeometryColumn('test_pk', 'geometry', 4326, 'POINT', 'XY')"
809
+ cur .execute (sql )
810
+
811
+ for i in range (11 , 21 ):
812
+ sql = "INSERT INTO test_pk (id, name, geometry) "
813
+ sql += "VALUES ({id}, 'name {id}', GeomFromText('POINT({id} {id})', 4326))" .format (id = i )
814
+ cur .execute (sql )
815
+
816
+ # simple table without primary key
817
+ sql = "CREATE TABLE test_no_pk (name TEXT NOT NULL)"
818
+ cur .execute (sql )
819
+
820
+ sql = "SELECT AddGeometryColumn('test_no_pk', 'geometry', 4326, 'POINT', 'XY')"
821
+ cur .execute (sql )
822
+
823
+ for i in range (11 , 21 ):
824
+ sql = "INSERT INTO test_no_pk (name, geometry) "
825
+ sql += "VALUES ('name {id}', GeomFromText('POINT({id} {id})', 4326))" .format (id = i )
826
+ cur .execute (sql )
827
+
828
+ cur .execute ("COMMIT" )
829
+ con .close ()
830
+
831
+ def _check_features (vl , offset ):
832
+ self .assertEqual (vl .featureCount (), 10 )
833
+ i = 11
834
+ for f in vl .getFeatures ():
835
+ self .assertTrue (f .isValid ())
836
+ self .assertTrue (vl .getFeature (i - offset ).isValid ())
837
+ self .assertEqual (vl .getFeature (i - offset )['name' ], 'name {id}' .format (id = i ))
838
+ self .assertEqual (f .id (), i - offset )
839
+ self .assertEqual (f ['name' ], 'name {id}' .format (id = i ))
840
+ self .assertEqual (f .geometry ().asWkt (), 'Point ({id} {id})' .format (id = i ))
841
+ i += 1
842
+
843
+ vl_pk = QgsVectorLayer ('dbname=\' %s\' table="(select * from test_pk)" (geometry) sql=' % dbname , 'pk' , 'spatialite' )
844
+ self .assertTrue (vl_pk .isValid ())
845
+ _check_features (vl_pk , 0 )
846
+
847
+ vl_no_pk = QgsVectorLayer ('dbname=\' %s\' table="(select * from test_no_pk)" (geometry) sql=' % dbname , 'pk' , 'spatialite' )
848
+ self .assertTrue (vl_no_pk .isValid ())
849
+ _check_features (vl_no_pk , 10 )
850
+
792
851
793
852
if __name__ == '__main__' :
794
853
unittest .main ()
0 commit comments