1 |
bh |
2057 |
# Copyright (C) 2003, 2004 by Intevation GmbH |
2 |
bh |
1605 |
# Authors: |
3 |
|
|
# Bernhard Herzog <[email protected]> |
4 |
|
|
# |
5 |
|
|
# This program is free software under the GPL (>=v2) |
6 |
|
|
# Read the file COPYING coming with the software for details. |
7 |
|
|
|
8 |
|
|
"""Test for Thuban.Model.postgisdb""" |
9 |
|
|
|
10 |
|
|
import os |
11 |
|
|
import unittest |
12 |
|
|
|
13 |
|
|
|
14 |
|
|
try: |
15 |
|
|
import psycopg |
16 |
|
|
except ImportError: |
17 |
|
|
# No psycopg available. Nothing to be done here because the |
18 |
|
|
# postgis.py support module determines this too and the tests will |
19 |
|
|
# be skipped completely. |
20 |
|
|
pass |
21 |
|
|
|
22 |
|
|
import postgissupport |
23 |
|
|
|
24 |
|
|
import support |
25 |
|
|
support.initthuban() |
26 |
|
|
|
27 |
bh |
1636 |
from Thuban.Model.postgisdb import ConnectionError, PostGISConnection, \ |
28 |
|
|
PostGISTable, PostGISShapeStore |
29 |
bh |
1605 |
from Thuban.Model.table import FIELDTYPE_INT, FIELDTYPE_STRING, \ |
30 |
|
|
FIELDTYPE_DOUBLE |
31 |
|
|
from Thuban.Model.data import SHAPETYPE_POINT, SHAPETYPE_POLYGON, \ |
32 |
|
|
SHAPETYPE_ARC, RAW_WKT |
33 |
|
|
|
34 |
|
|
|
35 |
bh |
1620 |
class NonConnection(PostGISConnection): |
36 |
|
|
|
37 |
|
|
"""Special connection class that doesn't actually connect""" |
38 |
|
|
|
39 |
|
|
def connect(self): |
40 |
|
|
pass |
41 |
|
|
|
42 |
|
|
|
43 |
|
|
|
44 |
bh |
1957 |
class TestPostGISSimple(unittest.TestCase): |
45 |
bh |
1620 |
|
46 |
bh |
1957 |
"""Some simple PostGISConnection tests that don't need a real connection""" |
47 |
|
|
|
48 |
|
|
def test_brief_description(self): |
49 |
bh |
1620 |
"""Test PostGISConnection.BriefDescription()""" |
50 |
|
|
self.assertEquals(NonConnection("somedb").BriefDescription(), |
51 |
|
|
"postgis://@:/somedb") |
52 |
|
|
self.assertEquals(NonConnection("db", host="here", |
53 |
|
|
port="123").BriefDescription(), |
54 |
|
|
"postgis://@here:123/db") |
55 |
|
|
self.assertEquals(NonConnection("db", user="me", |
56 |
|
|
port="123").BriefDescription(), |
57 |
|
|
"postgis://me@:123/db") |
58 |
|
|
|
59 |
bh |
1957 |
def test_matches_parameters(self): |
60 |
|
|
"""Test PostGISConnection.MatchesParameters()""" |
61 |
|
|
def do_test(testfn, conn, host = "", port="", dbname = "", user = ""): |
62 |
|
|
testfn(conn.MatchesParameters({"host": host, "port": port, |
63 |
|
|
"dbname": dbname, "user": user})) |
64 |
bh |
1620 |
|
65 |
bh |
1957 |
do_test(self.assert_, NonConnection("somedb"), |
66 |
|
|
dbname="somedb") |
67 |
|
|
do_test(self.assert_, NonConnection("somedb", host="here"), |
68 |
|
|
dbname="somedb", host="here") |
69 |
|
|
do_test(self.assert_, NonConnection("db", user="me", port="123"), |
70 |
|
|
dbname="db", port="123", user="me") |
71 |
|
|
|
72 |
|
|
do_test(self.failIf, NonConnection("somedb"), |
73 |
|
|
dbname="someotherdb") |
74 |
|
|
do_test(self.failIf, NonConnection("somedb", host="here"), |
75 |
|
|
dbname="somedb", host="here", port="765") |
76 |
|
|
do_test(self.failIf, NonConnection("db", user="me", port="123"), |
77 |
|
|
dbname="db", port="123", user="you") |
78 |
|
|
|
79 |
|
|
|
80 |
bh |
1605 |
class TestPostGISConnection(unittest.TestCase): |
81 |
|
|
|
82 |
|
|
def setUp(self): |
83 |
|
|
"""Start the server and create a database. |
84 |
|
|
|
85 |
|
|
The database name will be stored in self.dbname, the server |
86 |
|
|
object in self.server and the db object in self.db. |
87 |
|
|
""" |
88 |
|
|
postgissupport.skip_if_no_postgis() |
89 |
|
|
self.server = postgissupport.get_test_server() |
90 |
|
|
self.dbname = ".".join(self.id().split(".")[-2:])[-31:] |
91 |
|
|
self.db = self.server.new_postgis_db(self.dbname) |
92 |
|
|
|
93 |
|
|
def test_gis_tables_empty(self): |
94 |
|
|
"""Test PostGISConnection.GISTables() on empty DB""" |
95 |
bh |
1634 |
db = PostGISConnection(dbname = self.dbname, |
96 |
|
|
**self.server.connection_params("user")) |
97 |
bh |
1605 |
|
98 |
|
|
# An empty database doesn't have any GIS tables |
99 |
|
|
self.assertEquals(db.GeometryTables(), []) |
100 |
|
|
|
101 |
|
|
def test_gis_tables_non_empty(self): |
102 |
|
|
"""Test PostGISConnection.GISTables() on non-empty DB""" |
103 |
bh |
1634 |
db = PostGISConnection(dbname = self.dbname, |
104 |
|
|
**self.server.connection_params("user")) |
105 |
bh |
1605 |
|
106 |
bh |
1634 |
conn = psycopg.connect("dbname=%s " % self.dbname |
107 |
|
|
+ self.server.connection_string("admin")) |
108 |
bh |
1605 |
cursor = conn.cursor() |
109 |
|
|
cursor.execute("CREATE TABLE test (A INT);") |
110 |
|
|
cursor.execute("SELECT AddGeometryColumn(%(dbname)s, 'test'," |
111 |
|
|
" 'geometry', -1, 'POINT', 2);", |
112 |
|
|
{"dbname": self.dbname}) |
113 |
|
|
conn.commit() |
114 |
|
|
|
115 |
|
|
# An empty database doesn't have any GIS tables |
116 |
|
|
self.assertEquals(db.GeometryTables(), ["test"]) |
117 |
|
|
|
118 |
|
|
|
119 |
bh |
1636 |
class TestPostgisDBExceptions(unittest.TestCase): |
120 |
|
|
|
121 |
|
|
def setUp(self): |
122 |
|
|
"""Start the postgis server and switch on authentication""" |
123 |
|
|
postgissupport.skip_if_no_postgis() |
124 |
|
|
self.server = postgissupport.get_test_server() |
125 |
|
|
self.postgisdb = self.server.get_default_static_data_db() |
126 |
|
|
self.server.require_authentication(True) |
127 |
|
|
|
128 |
|
|
def tearDown(self): |
129 |
|
|
"""Extend the inherited method to switch off postgresql authentication |
130 |
|
|
""" |
131 |
|
|
self.server.require_authentication(False) |
132 |
|
|
|
133 |
|
|
def test_no_password(self): |
134 |
|
|
"""Test PostGISConnection with omitted but required password""" |
135 |
|
|
connection_params = self.server.connection_params("user") |
136 |
|
|
# remove the password deliberately |
137 |
|
|
del connection_params["password"] |
138 |
|
|
|
139 |
|
|
self.assertRaises(ConnectionError, |
140 |
|
|
PostGISConnection, dbname = self.postgisdb.dbname, |
141 |
|
|
**connection_params) |
142 |
|
|
|
143 |
|
|
|
144 |
bh |
1948 |
class TestPostGISSpecialCases(unittest.TestCase): |
145 |
bh |
1693 |
|
146 |
bh |
1948 |
"""Tests for special cases of PostGIS table usage""" |
147 |
|
|
|
148 |
bh |
1693 |
def setUp(self): |
149 |
|
|
"""Start the server and create a database. |
150 |
|
|
|
151 |
|
|
The database name will be stored in self.dbname, the server |
152 |
|
|
object in self.server and the db object in self.db. |
153 |
|
|
""" |
154 |
|
|
postgissupport.skip_if_no_postgis() |
155 |
|
|
self.server = postgissupport.get_test_server() |
156 |
|
|
self.dbname = ".".join(self.id().split(".")[-2:])[-31:] |
157 |
|
|
self.db = self.server.new_postgis_db(self.dbname) |
158 |
|
|
|
159 |
bh |
1948 |
def test_unsupported_types(self): |
160 |
bh |
1693 |
"""test PostGISTable on a table with unsupported data types""" |
161 |
|
|
stmt = """CREATE TABLE foo ( |
162 |
|
|
gid integer, |
163 |
|
|
ignored bigint, |
164 |
|
|
length float); |
165 |
|
|
GRANT SELECT ON foo TO PUBLIC; |
166 |
|
|
""" |
167 |
|
|
self.server.execute_sql(self.dbname, "admin", stmt) |
168 |
|
|
|
169 |
|
|
db = PostGISConnection(dbname = self.dbname, |
170 |
|
|
**self.server.connection_params("user")) |
171 |
|
|
table = PostGISTable(db, "foo") |
172 |
|
|
|
173 |
|
|
# The bigint column will be ignored because it's not mapped to a |
174 |
|
|
# known integer type, so there are only two colunns |
175 |
|
|
self.assertEquals(table.NumColumns(), 2) |
176 |
|
|
self.assertEquals(table.Column(0).name, "gid") |
177 |
|
|
self.assertEquals(table.Column(1).name, "length") |
178 |
|
|
|
179 |
bh |
1948 |
def test_table_name_quoting(self): |
180 |
|
|
"""Test whether PostGISTable quotes table names properly""" |
181 |
|
|
stmt = '''CREATE TABLE "name with ""quotes"" and spaces" (gid integer); |
182 |
|
|
GRANT SELECT ON "name with ""quotes"" and spaces" TO PUBLIC;''' |
183 |
|
|
self.server.execute_sql(self.dbname, "admin", stmt) |
184 |
bh |
1693 |
|
185 |
bh |
1948 |
db = PostGISConnection(dbname = self.dbname, |
186 |
|
|
**self.server.connection_params("user")) |
187 |
|
|
table = PostGISTable(db, 'name with "quotes" and spaces') |
188 |
|
|
|
189 |
|
|
self.assertEquals(table.NumColumns(), 1) |
190 |
|
|
self.assertEquals(table.Column(0).name, "gid") |
191 |
|
|
|
192 |
|
|
def test_column_name_quoting(self): |
193 |
|
|
"""Test whether PostGISTable quotes column names properly""" |
194 |
|
|
stmt = '''CREATE TABLE unusual_column_names ( |
195 |
|
|
gid integer, |
196 |
|
|
"with space" float, |
197 |
|
|
"with "" quote" integer); |
198 |
|
|
INSERT INTO unusual_column_names VALUES (1, 1.0, 0); |
199 |
|
|
INSERT INTO unusual_column_names VALUES (2, 2.0, 1); |
200 |
|
|
INSERT INTO unusual_column_names VALUES (3, 3.0, 1); |
201 |
|
|
GRANT SELECT ON unusual_column_names TO PUBLIC;''' |
202 |
|
|
self.server.execute_sql(self.dbname, "admin", stmt) |
203 |
|
|
|
204 |
|
|
db = PostGISConnection(dbname = self.dbname, |
205 |
|
|
**self.server.connection_params("user")) |
206 |
|
|
table = PostGISTable(db, 'unusual_column_names') |
207 |
|
|
|
208 |
|
|
self.assertEquals(table.NumColumns(), 3) |
209 |
|
|
self.assertEquals(table.Column(0).name, "gid") |
210 |
|
|
self.assertEquals(table.Column(1).name, "with space") |
211 |
|
|
self.assertEquals(table.Column(2).name, "with \" quote") |
212 |
|
|
|
213 |
|
|
# do some queries where the names have to be quoted |
214 |
|
|
self.assertEquals(table.ReadRowAsDict(1), |
215 |
|
|
{"gid": 1, "with space": 1.0, "with \" quote": 0}) |
216 |
|
|
self.assertEquals(table.ReadValue(2, 1, row_is_ordinal = True), 3.0) |
217 |
|
|
self.assertEquals(table.ReadValue(2, 1, row_is_ordinal = False), 2.0) |
218 |
|
|
self.assertEquals(table.ValueRange("with space"), (1.0, 3.0)) |
219 |
|
|
self.assertEquals(table.UniqueValues("with \" quote"), [0, 1]) |
220 |
|
|
self.assertEquals(table.SimpleQuery(table.Columns()[2], "==", 1), |
221 |
|
|
[2, 3]) |
222 |
|
|
self.assertEquals(table.SimpleQuery(table.Columns()[0], "==", |
223 |
|
|
table.Columns()[1]), [1, 2, 3]) |
224 |
|
|
|
225 |
|
|
def test_shapestore_name_quoting(self): |
226 |
|
|
"""Test whether PostGISShapeStore quotes table names properly""" |
227 |
|
|
postgissupport.skip_if_addgeometrycolumn_does_not_use_quote_ident() |
228 |
|
|
|
229 |
|
|
# Create a table with a name that needs quoting and a geometry |
230 |
|
|
# column whose name has to be quoted |
231 |
|
|
stmt = '''CREATE TABLE "table "" name" (gid integer); |
232 |
|
|
GRANT SELECT ON "table "" name" TO PUBLIC;''' |
233 |
|
|
self.server.execute_sql(self.dbname, "admin", stmt) |
234 |
|
|
stmt = """select AddGeometryColumn('%s', 'table \" name', |
235 |
|
|
'the \" geom', '-1', 'POINT', 2);""" |
236 |
|
|
self.server.execute_sql(self.dbname, "admin", stmt % self.dbname) |
237 |
|
|
stmt = '''INSERT INTO "table "" name" VALUES (0, |
238 |
|
|
GeometryFromText('POINT(0 0)', -1));''' |
239 |
|
|
self.server.execute_sql(self.dbname, "admin", stmt) |
240 |
|
|
|
241 |
|
|
# Instantiate the table |
242 |
|
|
db = PostGISConnection(dbname = self.dbname, |
243 |
|
|
**self.server.connection_params("user")) |
244 |
|
|
store = PostGISShapeStore(db, "table \" name") |
245 |
|
|
|
246 |
|
|
# do some queries where the names have to be quoted |
247 |
|
|
self.assertEquals(store.NumShapes(), 1) |
248 |
|
|
self.assertEquals(store.ShapeType(), SHAPETYPE_POINT) |
249 |
|
|
self.assertEquals(store.BoundingBox(), (0, 0, 0, 0)) |
250 |
|
|
self.assertEquals(store.Shape(0).ShapeID(), 0) |
251 |
|
|
self.assertEquals([s.ShapeID() for s in store.AllShapes()], [0]) |
252 |
|
|
self.assertEquals([s.ShapeID() |
253 |
|
|
for s in store.ShapesInRegion((-1, -1, 1, 1))], [0]) |
254 |
|
|
|
255 |
bh |
2057 |
def test_shapestore_empty_table(self): |
256 |
|
|
"""Test PostGISShapeStore with emtpy table""" |
257 |
|
|
conn = psycopg.connect("dbname=%s " % self.dbname |
258 |
|
|
+ self.server.connection_string("admin")) |
259 |
|
|
cursor = conn.cursor() |
260 |
|
|
cursor.execute("CREATE TABLE test (A INT);") |
261 |
|
|
cursor.execute("SELECT AddGeometryColumn(%(dbname)s, 'test'," |
262 |
|
|
" 'geometry', -1, 'POINT', 2);", |
263 |
|
|
{"dbname": self.dbname}) |
264 |
|
|
cursor.execute("GRANT SELECT ON test TO PUBLIC;") |
265 |
|
|
conn.commit() |
266 |
|
|
|
267 |
|
|
db = PostGISConnection(dbname = self.dbname, |
268 |
|
|
**self.server.connection_params("user")) |
269 |
|
|
store = PostGISShapeStore(db, "test") |
270 |
|
|
self.assertEquals(store.BoundingBox(), None) |
271 |
|
|
|
272 |
bh |
2059 |
def test_shapestore_two_geom_cols(self): |
273 |
|
|
"""Test PostGISShapeStore with two geometry columns""" |
274 |
|
|
conn = psycopg.connect("dbname=%s " % self.dbname |
275 |
|
|
+ self.server.connection_string("admin")) |
276 |
|
|
cursor = conn.cursor() |
277 |
|
|
cursor.execute("INSERT INTO spatial_ref_sys VALUES" |
278 |
|
|
" (1, '', 1, '', 'proj=longlat datum=WGS84')") |
279 |
|
|
cursor.execute("INSERT INTO spatial_ref_sys VALUES" |
280 |
|
|
" (2, '', 2, '', 'proj=longlat datum=WGS84')") |
281 |
bh |
2057 |
|
282 |
bh |
2059 |
cursor.execute("CREATE TABLE two_geom_cols" |
283 |
|
|
" (gid INTEGER PRIMARY KEY);") |
284 |
|
|
cursor.execute("SELECT AddGeometryColumn(%(dbname)s, 'two_geom_cols'," |
285 |
|
|
" 'point', 1, 'POINT', 2);", |
286 |
|
|
{"dbname": self.dbname}) |
287 |
|
|
cursor.execute("SELECT AddGeometryColumn(%(dbname)s, 'two_geom_cols'," |
288 |
|
|
" 'poly', 2, 'POLYGON', 2);", |
289 |
|
|
{"dbname": self.dbname}) |
290 |
|
|
cursor.execute("INSERT INTO two_geom_cols(gid, point, poly) VALUES (1," |
291 |
|
|
" GeometryFromText('POINT(1 1)', 1)," |
292 |
|
|
" GeometryFromText('POLYGON((1 1, 1 2, 2 2,1 1))',2));") |
293 |
|
|
cursor.execute("GRANT SELECT ON two_geom_cols TO PUBLIC;") |
294 |
|
|
conn.commit() |
295 |
|
|
|
296 |
|
|
db = PostGISConnection(dbname = self.dbname, |
297 |
|
|
**self.server.connection_params("user")) |
298 |
|
|
|
299 |
|
|
# The table has two geometry columns, and will raise a TypeError |
300 |
|
|
# when instantiated without specifying which one to use. |
301 |
|
|
self.assertRaises(TypeError, |
302 |
|
|
PostGISShapeStore, db, "two_geom_cols") |
303 |
|
|
|
304 |
|
|
# If the geometry_column is not really a geometry column, we get |
305 |
|
|
# a TypeError |
306 |
|
|
self.assertRaises(TypeError, |
307 |
|
|
PostGISShapeStore, db, "two_geom_cols", |
308 |
|
|
geometry_column = "gid") |
309 |
|
|
|
310 |
|
|
# Instantiate two shape stores, one for each of the geometry |
311 |
|
|
# columns, and test whether they determine the right shape types |
312 |
|
|
# and srids (getting the srid wrong would lead to an exception |
313 |
|
|
# in ShapesInRegion). |
314 |
|
|
store = PostGISShapeStore(db, "two_geom_cols", |
315 |
|
|
geometry_column = "point") |
316 |
|
|
self.assertEquals(store.ShapeType(), SHAPETYPE_POINT) |
317 |
|
|
self.assertEquals([s.ShapeID() |
318 |
|
|
for s in store.ShapesInRegion((0, 0, 100,100))], |
319 |
|
|
[1]) |
320 |
|
|
|
321 |
|
|
store = PostGISShapeStore(db, "two_geom_cols", |
322 |
|
|
geometry_column = "poly") |
323 |
|
|
self.assertEquals(store.ShapeType(), SHAPETYPE_POLYGON) |
324 |
|
|
self.assertEquals([s.ShapeID() |
325 |
|
|
for s in store.ShapesInRegion((0, 0, 100,100))], |
326 |
|
|
[1]) |
327 |
|
|
|
328 |
|
|
|
329 |
bh |
1605 |
class PostGISStaticTests(unittest.TestCase, support.FloatComparisonMixin): |
330 |
|
|
|
331 |
|
|
"""Base class for PostGIS tests with static data.""" |
332 |
|
|
|
333 |
|
|
def setUp(self): |
334 |
|
|
"""Start the server and create a database with static data |
335 |
|
|
|
336 |
|
|
This method sets the following instance attributes: |
337 |
|
|
|
338 |
|
|
dbname -- the name of the database |
339 |
|
|
|
340 |
|
|
server -- The server object |
341 |
|
|
|
342 |
|
|
db -- the PostGISConnection object |
343 |
|
|
""" |
344 |
|
|
postgissupport.skip_if_no_postgis() |
345 |
|
|
self.server = postgissupport.get_test_server() |
346 |
|
|
self.postgisdb = self.server.get_default_static_data_db() |
347 |
|
|
self.db = PostGISConnection(dbname = self.postgisdb.dbname, |
348 |
bh |
1634 |
**self.server.connection_params("user")) |
349 |
bh |
1605 |
|
350 |
|
|
def tearDown(self): |
351 |
|
|
"""Close the database connection""" |
352 |
|
|
self.db.Close() |
353 |
|
|
|
354 |
|
|
|
355 |
|
|
class TestPostGISTable(PostGISStaticTests): |
356 |
|
|
|
357 |
|
|
def setUp(self): |
358 |
|
|
"""Extend inherited method to set self.table to a PostGISTable""" |
359 |
|
|
PostGISStaticTests.setUp(self) |
360 |
|
|
self.table = PostGISTable(self.db, "landmarks") |
361 |
|
|
|
362 |
bh |
1638 |
def test_dbconn(self): |
363 |
|
|
"""Test PostGISTable.DBConnection()""" |
364 |
|
|
self.failUnless(self.table.DBConnection() is self.db) |
365 |
|
|
|
366 |
|
|
def test_dbname(self): |
367 |
|
|
"""Test PostGISTable.TableName()""" |
368 |
|
|
self.assertEquals(self.table.TableName(), "landmarks") |
369 |
|
|
|
370 |
bh |
1658 |
def test_title(self): |
371 |
|
|
"""test PostGISTable.Title()""" |
372 |
|
|
# The title is currently equal to the tablename |
373 |
|
|
self.assertEquals(self.table.Title(), "landmarks") |
374 |
|
|
|
375 |
bh |
1605 |
def test_dependencies(self): |
376 |
|
|
"""Test PostGISTable.Dependencies()""" |
377 |
|
|
# A PostGISTable depends on no other data container |
378 |
|
|
self.assertEquals(self.table.Dependencies(), ()) |
379 |
|
|
|
380 |
|
|
def test_num_rows(self): |
381 |
|
|
"""Test PostGISTable.NumRows()""" |
382 |
|
|
self.assertEquals(self.table.NumRows(), 34) |
383 |
|
|
|
384 |
|
|
def test_num_columns(self): |
385 |
|
|
"""Test PostGISTable.NumColumns()""" |
386 |
|
|
# The table in the postgis db has one additional column, "gid", |
387 |
|
|
# so there's one more column in the PostGISTable than in the DBF |
388 |
|
|
self.assertEquals(self.table.NumColumns(), 7) |
389 |
|
|
|
390 |
|
|
def test_columns(self): |
391 |
|
|
"""Test PostGISTable.Columns()""" |
392 |
|
|
self.assertEquals(len(self.table.Columns()), 7) |
393 |
|
|
self.assertEquals(self.table.Columns()[0].name, "gid") |
394 |
|
|
self.assertEquals(self.table.Columns()[0].type, FIELDTYPE_INT) |
395 |
|
|
self.assertEquals(self.table.Columns()[0].index, 0) |
396 |
|
|
self.assertEquals(self.table.Columns()[1].name, "area") |
397 |
|
|
self.assertEquals(self.table.Columns()[1].type, FIELDTYPE_DOUBLE) |
398 |
|
|
self.assertEquals(self.table.Columns()[1].index, 1) |
399 |
|
|
self.assertEquals(self.table.Columns()[5].name, "clptlabel") |
400 |
|
|
self.assertEquals(self.table.Columns()[5].type, FIELDTYPE_STRING) |
401 |
|
|
self.assertEquals(self.table.Columns()[5].index, 5) |
402 |
|
|
|
403 |
|
|
def test_column(self): |
404 |
|
|
"""Test PostGISTable.Column()""" |
405 |
|
|
self.assertEquals(self.table.Column("area").name, "area") |
406 |
|
|
self.assertEquals(self.table.Column("area").type, FIELDTYPE_DOUBLE) |
407 |
|
|
self.assertEquals(self.table.Column("area").index, 1) |
408 |
|
|
|
409 |
|
|
def test_has_column(self): |
410 |
|
|
"""Test PostGISTable.HasColumn()""" |
411 |
|
|
self.assert_(self.table.HasColumn("area")) |
412 |
|
|
self.failIf(self.table.HasColumn("foo")) |
413 |
|
|
|
414 |
|
|
def test_read_row_as_dict(self): |
415 |
|
|
"""Test PostGISTable.ReadRowAsDict()""" |
416 |
bh |
1662 |
self.assertEquals(self.table.ReadRowAsDict(1003), |
417 |
|
|
{"gid": 1003, |
418 |
bh |
1605 |
"area": 0.0, |
419 |
|
|
"perimeter": 0.0, |
420 |
|
|
"clpoint_": 4, |
421 |
|
|
"clpoint_id": 24, |
422 |
|
|
"clptlabel": "RUINS", |
423 |
|
|
"clptflag": 0}) |
424 |
|
|
|
425 |
bh |
1662 |
def test_read_row_as_dict_row_count_mode(self): |
426 |
|
|
"""Test PostGISTable.ReadRowAsDict() row count address mode""" |
427 |
|
|
self.assertEquals(self.table.ReadRowAsDict(3, row_is_ordinal = 1), |
428 |
|
|
{"gid": 1003, |
429 |
|
|
"area": 0.0, |
430 |
|
|
"perimeter": 0.0, |
431 |
|
|
"clpoint_": 4, |
432 |
|
|
"clpoint_id": 24, |
433 |
|
|
"clptlabel": "RUINS", |
434 |
|
|
"clptflag": 0}) |
435 |
|
|
|
436 |
bh |
1605 |
def test_read_value(self): |
437 |
|
|
"""Test PostGISTable.ReadValue()""" |
438 |
bh |
1662 |
self.assertEquals(self.table.ReadValue(1003, 4), 24) |
439 |
|
|
self.assertEquals(self.table.ReadValue(1003, "clpoint_id"), 24) |
440 |
bh |
1605 |
|
441 |
bh |
1662 |
def test_read_value_row_count_mode(self): |
442 |
|
|
"""Test PostGISTable.ReadValue() row count address mode""" |
443 |
|
|
self.assertEquals(self.table.ReadValue(3, 4, row_is_ordinal = 1), 24) |
444 |
|
|
self.assertEquals(self.table.ReadValue(3, "clpoint_id", |
445 |
|
|
row_is_ordinal = 1), |
446 |
|
|
24) |
447 |
|
|
|
448 |
|
|
def test_row_id_to_ordinal(self): |
449 |
|
|
"""Test PostGISTable.RowIdToOrdinal()""" |
450 |
|
|
self.assertEquals(self.table.RowIdToOrdinal(1005), 5) |
451 |
|
|
|
452 |
|
|
def test_row_oridnal_to_id(self): |
453 |
|
|
"""Test PostGISTable.RowOrdinalToId()""" |
454 |
|
|
self.assertEquals(self.table.RowOrdinalToId(5), 1005) |
455 |
|
|
|
456 |
bh |
1605 |
def test_value_range(self): |
457 |
|
|
"""Test PostGISTable.ValueRange()""" |
458 |
|
|
self.assertEquals(self.table.ValueRange("clpoint_id"), (21, 74)) |
459 |
|
|
|
460 |
|
|
def test_unique_values(self): |
461 |
|
|
"""Test PostGISTable.UniqueValues()""" |
462 |
|
|
values = self.table.UniqueValues("clptlabel") |
463 |
|
|
values.sort() |
464 |
|
|
self.assertEquals(values, ["BUILDING", "FARM", "HUT","LIGHTHOUSE", |
465 |
|
|
"OTHER/UNKNOWN", "RUINS"]) |
466 |
|
|
|
467 |
|
|
def test_simple_query(self): |
468 |
|
|
"""Test PostGISTable.SimpleQuery()""" |
469 |
|
|
table = self.table |
470 |
|
|
self.assertEquals(table.SimpleQuery(table.Column("clptlabel"), |
471 |
|
|
"==", "FARM"), |
472 |
bh |
1662 |
[1006]) |
473 |
bh |
1605 |
self.assertEquals(table.SimpleQuery(table.Column("clpoint_id"), |
474 |
|
|
">", 70), |
475 |
bh |
1662 |
[1024, 1025, 1026]) |
476 |
bh |
1605 |
self.assertEquals(table.SimpleQuery(table.Column("clpoint_id"), |
477 |
|
|
"<", table.Column("clpoint_")), |
478 |
bh |
1662 |
[1028, 1029, 1030, 1031, 1032, 1033]) |
479 |
bh |
1605 |
|
480 |
|
|
|
481 |
bh |
2057 |
class PointTests: |
482 |
bh |
1605 |
|
483 |
bh |
2057 |
"""Mix-in class for the tests on POINT tables""" |
484 |
bh |
1605 |
|
485 |
|
|
# |
486 |
|
|
# First, some tests that should be independend of the shapetype, so |
487 |
|
|
# it shouldn't be necessary to repeat them for other shapetypes |
488 |
|
|
# |
489 |
|
|
|
490 |
|
|
def test_dependencies(self): |
491 |
|
|
"""Test PostGISShapeStore.Dependencies()""" |
492 |
|
|
# A PostGISShapeStore depends on no other data container |
493 |
|
|
self.assertEquals(self.store.Dependencies(), ()) |
494 |
|
|
|
495 |
|
|
def test_table(self): |
496 |
|
|
"""Test PostGISShapeStore.Table() with POINT shapes""" |
497 |
|
|
# A PostGISShapeStore is its own table |
498 |
|
|
self.assert_(self.store.Table() is self.store) |
499 |
|
|
|
500 |
|
|
def test_orig_shapestore(self): |
501 |
|
|
"""Test PostGISShapeStore.OrigShapeStore() with POINT shapes""" |
502 |
|
|
# A PostGISShapeStore is not derived from another shape store |
503 |
|
|
self.assert_(self.store.OrigShapeStore() is None) |
504 |
|
|
|
505 |
|
|
def test_raw_format(self): |
506 |
|
|
"""Test PostGISShapeStore.RawShapeFormat() with POINT shapes""" |
507 |
|
|
self.assertEquals(self.store.RawShapeFormat(), RAW_WKT) |
508 |
|
|
|
509 |
bh |
1658 |
def test_all_shapes(self): |
510 |
|
|
"""Test PostGISShapeStore.AllShapes()""" |
511 |
|
|
self.assertEquals([s.ShapeID() for s in self.store.AllShapes()], |
512 |
bh |
1662 |
range(1000, 1000 + self.store.NumShapes())) |
513 |
bh |
1658 |
|
514 |
bh |
1605 |
# |
515 |
|
|
# Shapetype specific tests |
516 |
|
|
# |
517 |
|
|
|
518 |
|
|
def test_shape_type(self): |
519 |
|
|
"""Test PostGISShapeStore.ShapeType() with POINT shapes""" |
520 |
|
|
self.assertEquals(self.store.ShapeType(), SHAPETYPE_POINT) |
521 |
|
|
|
522 |
|
|
def test_num_shapes(self): |
523 |
|
|
"""Test PostGISShapeStore.NumShapes() with POINT shapes""" |
524 |
|
|
self.assertEquals(self.store.NumShapes(), 34) |
525 |
|
|
|
526 |
|
|
def test_bounding_box(self): |
527 |
|
|
"""Test PostGISShapeStore.BoundingBox() with POINT shapes""" |
528 |
|
|
self.assertFloatSeqEqual(self.store.BoundingBox(), |
529 |
|
|
[-23.806047439575195, 63.405960083007812, |
530 |
|
|
-15.12291431427002, 66.36572265625]) |
531 |
|
|
|
532 |
|
|
def test_shape_shapeid(self): |
533 |
|
|
"""Test PostGISShapeStore.Shape(i).ShapeID() with POINT shapes""" |
534 |
bh |
1662 |
self.assertEquals(self.store.Shape(1005).ShapeID(), 1005) |
535 |
bh |
1605 |
|
536 |
|
|
def test_shape_points(self): |
537 |
|
|
"""Test PostGISShapeStore.Shape(i).Points() with POINT shapes""" |
538 |
bh |
1662 |
self.assertPointListEquals(self.store.Shape(1000).Points(), |
539 |
bh |
1605 |
[[(-22.711074829101562, 66.36572265625)]]) |
540 |
|
|
|
541 |
|
|
def test_shape_raw_data(self): |
542 |
|
|
"""Test PostGISShapeStore.Shape(i).RawData() with POINT shapes""" |
543 |
bh |
1662 |
self.assertEquals(self.store.Shape(1000).RawData(), |
544 |
bh |
1605 |
'POINT(-22.7110748291016 66.36572265625)') |
545 |
|
|
|
546 |
|
|
def test_shapes_in_region(self): |
547 |
|
|
"""Test PostGISShapeStore:ShapesInRegion() with POINT shapes""" |
548 |
|
|
shapes = self.store.ShapesInRegion((-20.0, 64.0, -24.0, 67)) |
549 |
|
|
self.assertEquals([s.ShapeID() for s in shapes], |
550 |
bh |
1662 |
[1000, 1001, 1002, 1003, 1004, 1005, 1027]) |
551 |
bh |
1605 |
|
552 |
|
|
|
553 |
bh |
2057 |
class TestPostGISShapestorePoint(PointTests, PostGISStaticTests): |
554 |
|
|
|
555 |
|
|
"""Tests for PostGISShapeStore objects with POINT data an no SRID""" |
556 |
|
|
|
557 |
|
|
def setUp(self): |
558 |
|
|
"""Extend inherited method to set self.table to a PostGISShapeStore""" |
559 |
|
|
PostGISStaticTests.setUp(self) |
560 |
|
|
self.store = PostGISShapeStore(self.db, "landmarks") |
561 |
|
|
|
562 |
|
|
|
563 |
|
|
|
564 |
|
|
class TestPostGISShapestorePointSRID(PointTests, PostGISStaticTests): |
565 |
|
|
|
566 |
|
|
"""Tests for PostGISShapeStore objects with POINT data and an SRID""" |
567 |
|
|
|
568 |
|
|
def setUp(self): |
569 |
|
|
"""Extend inherited method to set self.table to a PostGISShapeStore""" |
570 |
|
|
PostGISStaticTests.setUp(self) |
571 |
|
|
self.store = PostGISShapeStore(self.db, "landmarks_srid") |
572 |
|
|
|
573 |
|
|
|
574 |
|
|
|
575 |
bh |
1605 |
class TestPostGISShapestoreArc(PostGISStaticTests): |
576 |
|
|
|
577 |
|
|
"""Tests for PostGISShapeStore objects with MULTILINESTRING data""" |
578 |
|
|
|
579 |
|
|
def setUp(self): |
580 |
|
|
"""Extend inherited method to set self.table to a PostGISShapeStore""" |
581 |
|
|
PostGISStaticTests.setUp(self) |
582 |
|
|
self.store = PostGISShapeStore(self.db, "roads") |
583 |
|
|
|
584 |
|
|
def test_shape_type(self): |
585 |
|
|
"""Test PostGISShapeStore.ShapeType() with ARC shapes""" |
586 |
|
|
self.assertEquals(self.store.ShapeType(), SHAPETYPE_ARC) |
587 |
|
|
|
588 |
|
|
def test_num_shapes(self): |
589 |
|
|
"""Test PostGISShapeStore.NumShapes() with ARC shapes""" |
590 |
|
|
self.assertEquals(self.store.NumShapes(), 839) |
591 |
|
|
|
592 |
|
|
def test_bounding_box(self): |
593 |
|
|
"""Test PostGISShapeStore.BoundingBox() with ARC shapes""" |
594 |
|
|
self.assertFloatSeqEqual(self.store.BoundingBox(), |
595 |
|
|
[-24.450359344482422, 63.426830291748047, |
596 |
|
|
-13.55668830871582, 66.520111083984375]) |
597 |
|
|
|
598 |
|
|
def test_shape_shapeid(self): |
599 |
|
|
"""Test PostGISShapeStore.Shape(i).ShapeID() with ARC shapes""" |
600 |
|
|
self.assertEquals(self.store.Shape(5).ShapeID(), 5) |
601 |
|
|
|
602 |
|
|
def test_shape_points(self): |
603 |
|
|
"""Test PostGISShapeStore.Shape(i).Points() with ARC shapes""" |
604 |
|
|
self.assertPointListEquals(self.store.Shape(32).Points(), |
605 |
|
|
[[(-15.0821743011474, 66.2773818969726), |
606 |
|
|
(-15.0263500213623, 66.2733917236328)]]) |
607 |
|
|
|
608 |
|
|
def test_shape_raw_data(self): |
609 |
|
|
"""Test PostGISShapeStore.Shape(i).RawData() with ARC shapes""" |
610 |
|
|
self.assertEquals(self.store.Shape(32).RawData(), |
611 |
|
|
"MULTILINESTRING((-15.0821743011475 66.2773818969727," |
612 |
|
|
"-15.0263500213623 66.2733917236328))") |
613 |
|
|
|
614 |
|
|
def test_shapes_in_region(self): |
615 |
|
|
"""Test PostGISShapeStore.ShapesInRegion() with ARC shapes""" |
616 |
|
|
shapes = self.store.ShapesInRegion((-24.0, 64.5, -23.5, 65.0)) |
617 |
|
|
self.assertEquals([s.ShapeID() for s in shapes], [573, 581, 613]) |
618 |
|
|
|
619 |
|
|
|
620 |
|
|
|
621 |
bh |
1656 |
class PolygonTests: |
622 |
bh |
1605 |
|
623 |
bh |
2057 |
"""Test shared by the POLYGON and MULTIPOLYGON tests |
624 |
bh |
1605 |
|
625 |
bh |
1656 |
The tests are the same because they are based on the same data. |
626 |
|
|
""" |
627 |
|
|
|
628 |
bh |
1605 |
def test_shape_type(self): |
629 |
|
|
"""Test PostGISShapeStore.ShapeType() with POLYGON shapes""" |
630 |
|
|
self.assertEquals(self.store.ShapeType(), SHAPETYPE_POLYGON) |
631 |
|
|
|
632 |
|
|
def test_num_shapes(self): |
633 |
|
|
"""Test PostGISShapeStore.NumShapes() with POLYGON shapes""" |
634 |
|
|
self.assertEquals(self.store.NumShapes(), 156) |
635 |
|
|
|
636 |
|
|
def test_bounding_box(self): |
637 |
|
|
"""Test PostGISShapeStore.BoundingBox() with POLYGON shapes""" |
638 |
|
|
self.assertFloatSeqEqual(self.store.BoundingBox(), |
639 |
|
|
[-24.546524047851562, 63.286754608154297, |
640 |
|
|
-13.495815277099609, 66.563774108886719]) |
641 |
|
|
|
642 |
|
|
def test_shape_shapeid(self): |
643 |
|
|
"""Test PostGISShapeStore.Shape(i).ShapeID() with POLYGON shapes""" |
644 |
|
|
self.assertEquals(self.store.Shape(5).ShapeID(), 5) |
645 |
|
|
|
646 |
|
|
def test_shape_points(self): |
647 |
|
|
"""Test PostGISShapeStore.Shape(i).Points() with POLYGON shapes""" |
648 |
|
|
self.assertPointListEquals(self.store.Shape(4).Points(), |
649 |
|
|
[[(-22.40639114379882, 64.714111328125), |
650 |
|
|
(-22.41621208190918, 64.716003417968), |
651 |
|
|
(-22.40605163574218, 64.719200134277), |
652 |
|
|
(-22.40639114379882, 64.714111328125)]]) |
653 |
|
|
|
654 |
bh |
1656 |
def test_shapes_in_region(self): |
655 |
|
|
"""Test PostGISShapeStore.ShapesInRegion() with POLYGON shapes""" |
656 |
|
|
shapes = self.store.ShapesInRegion((-23.0, 65.5, -22.8, 65.25)) |
657 |
|
|
self.assertEquals([s.ShapeID() for s in shapes], |
658 |
|
|
[47, 56, 59, 61, 62, 71, 144]) |
659 |
|
|
|
660 |
|
|
|
661 |
|
|
class TestPostGISShapestorePolygon(PolygonTests, PostGISStaticTests): |
662 |
|
|
|
663 |
|
|
"""Tests for PostGISShapeStore objects with POLYGON data""" |
664 |
|
|
|
665 |
|
|
def setUp(self): |
666 |
|
|
"""Extend inherited method to set self.table to a PostGISShapeStore""" |
667 |
|
|
PostGISStaticTests.setUp(self) |
668 |
|
|
self.store = PostGISShapeStore(self.db, "political") |
669 |
|
|
|
670 |
|
|
def test_shape_type(self): |
671 |
|
|
"""Test PostGISShapeStore.ShapeType() with POLYGON shapes""" |
672 |
|
|
self.assertEquals(self.store.ShapeType(), SHAPETYPE_POLYGON) |
673 |
|
|
|
674 |
|
|
|
675 |
bh |
1605 |
def test_shape_raw_data(self): |
676 |
|
|
"""Test PostGISShapeStore.Shape(i).RawData() with POLYGON shapes""" |
677 |
|
|
self.assertEquals(self.store.Shape(4).RawData(), |
678 |
|
|
"POLYGON((-22.4063911437988 64.714111328125," |
679 |
|
|
"-22.4162120819092 64.7160034179688," |
680 |
|
|
"-22.4060516357422 64.7192001342773," |
681 |
|
|
"-22.4063911437988 64.714111328125))") |
682 |
|
|
|
683 |
|
|
|
684 |
bh |
1656 |
class TestPostGISShapestoreMultiPolygon(PolygonTests, PostGISStaticTests): |
685 |
bh |
1605 |
|
686 |
bh |
1656 |
"""Tests for PostGISShapeStore objects with MUTLIPOLYGON data""" |
687 |
|
|
|
688 |
|
|
def setUp(self): |
689 |
|
|
"""Extend inherited method to set self.table to a PostGISShapeStore""" |
690 |
|
|
PostGISStaticTests.setUp(self) |
691 |
|
|
self.store = PostGISShapeStore(self.db, "political_multi") |
692 |
|
|
|
693 |
|
|
def test_shape_raw_data(self): |
694 |
|
|
"""Test PostGISShapeStore.Shape(i).RawData() with POLYGON shapes""" |
695 |
|
|
self.assertEquals(self.store.Shape(4).RawData(), |
696 |
|
|
"MULTIPOLYGON(((-22.4063911437988 64.714111328125," |
697 |
|
|
"-22.4162120819092 64.7160034179688," |
698 |
|
|
"-22.4060516357422 64.7192001342773," |
699 |
|
|
"-22.4063911437988 64.714111328125)))") |
700 |
|
|
|
701 |
bh |
1693 |
|
702 |
|
|
|
703 |
bh |
1605 |
if __name__ == "__main__": |
704 |
|
|
support.run_tests() |