1 |
bh |
1605 |
# Copyright (C) 2003 by Intevation GmbH |
2 |
|
|
# 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 |
|
|
from Thuban.Model.postgisdb import PostGISConnection, PostGISTable, \ |
28 |
|
|
PostGISShapeStore |
29 |
|
|
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 |
|
|
class TestBriefDescription(unittest.TestCase): |
45 |
|
|
|
46 |
|
|
def test(self): |
47 |
|
|
"""Test PostGISConnection.BriefDescription()""" |
48 |
|
|
self.assertEquals(NonConnection("somedb").BriefDescription(), |
49 |
|
|
"postgis://@:/somedb") |
50 |
|
|
self.assertEquals(NonConnection("db", host="here", |
51 |
|
|
port="123").BriefDescription(), |
52 |
|
|
"postgis://@here:123/db") |
53 |
|
|
self.assertEquals(NonConnection("db", user="me", |
54 |
|
|
port="123").BriefDescription(), |
55 |
|
|
"postgis://me@:123/db") |
56 |
|
|
|
57 |
|
|
|
58 |
bh |
1605 |
class TestPostGISConnection(unittest.TestCase): |
59 |
|
|
|
60 |
|
|
def setUp(self): |
61 |
|
|
"""Start the server and create a database. |
62 |
|
|
|
63 |
|
|
The database name will be stored in self.dbname, the server |
64 |
|
|
object in self.server and the db object in self.db. |
65 |
|
|
""" |
66 |
|
|
postgissupport.skip_if_no_postgis() |
67 |
|
|
self.server = postgissupport.get_test_server() |
68 |
|
|
self.dbname = ".".join(self.id().split(".")[-2:])[-31:] |
69 |
|
|
self.db = self.server.new_postgis_db(self.dbname) |
70 |
|
|
|
71 |
|
|
def test_gis_tables_empty(self): |
72 |
|
|
"""Test PostGISConnection.GISTables() on empty DB""" |
73 |
bh |
1634 |
db = PostGISConnection(dbname = self.dbname, |
74 |
|
|
**self.server.connection_params("user")) |
75 |
bh |
1605 |
|
76 |
|
|
# An empty database doesn't have any GIS tables |
77 |
|
|
self.assertEquals(db.GeometryTables(), []) |
78 |
|
|
|
79 |
|
|
def test_gis_tables_non_empty(self): |
80 |
|
|
"""Test PostGISConnection.GISTables() on non-empty DB""" |
81 |
bh |
1634 |
db = PostGISConnection(dbname = self.dbname, |
82 |
|
|
**self.server.connection_params("user")) |
83 |
bh |
1605 |
|
84 |
bh |
1634 |
conn = psycopg.connect("dbname=%s " % self.dbname |
85 |
|
|
+ self.server.connection_string("admin")) |
86 |
bh |
1605 |
cursor = conn.cursor() |
87 |
|
|
cursor.execute("CREATE TABLE test (A INT);") |
88 |
|
|
cursor.execute("SELECT AddGeometryColumn(%(dbname)s, 'test'," |
89 |
|
|
" 'geometry', -1, 'POINT', 2);", |
90 |
|
|
{"dbname": self.dbname}) |
91 |
|
|
conn.commit() |
92 |
|
|
|
93 |
|
|
# An empty database doesn't have any GIS tables |
94 |
|
|
self.assertEquals(db.GeometryTables(), ["test"]) |
95 |
|
|
|
96 |
|
|
|
97 |
|
|
class PostGISStaticTests(unittest.TestCase, support.FloatComparisonMixin): |
98 |
|
|
|
99 |
|
|
"""Base class for PostGIS tests with static data.""" |
100 |
|
|
|
101 |
|
|
def setUp(self): |
102 |
|
|
"""Start the server and create a database with static data |
103 |
|
|
|
104 |
|
|
This method sets the following instance attributes: |
105 |
|
|
|
106 |
|
|
dbname -- the name of the database |
107 |
|
|
|
108 |
|
|
server -- The server object |
109 |
|
|
|
110 |
|
|
db -- the PostGISConnection object |
111 |
|
|
""" |
112 |
|
|
postgissupport.skip_if_no_postgis() |
113 |
|
|
self.server = postgissupport.get_test_server() |
114 |
|
|
self.postgisdb = self.server.get_default_static_data_db() |
115 |
|
|
self.db = PostGISConnection(dbname = self.postgisdb.dbname, |
116 |
bh |
1634 |
**self.server.connection_params("user")) |
117 |
bh |
1605 |
|
118 |
|
|
def tearDown(self): |
119 |
|
|
"""Close the database connection""" |
120 |
|
|
self.db.Close() |
121 |
|
|
|
122 |
|
|
|
123 |
|
|
class TestPostGISTable(PostGISStaticTests): |
124 |
|
|
|
125 |
|
|
def setUp(self): |
126 |
|
|
"""Extend inherited method to set self.table to a PostGISTable""" |
127 |
|
|
PostGISStaticTests.setUp(self) |
128 |
|
|
self.table = PostGISTable(self.db, "landmarks") |
129 |
|
|
|
130 |
|
|
def test_dependencies(self): |
131 |
|
|
"""Test PostGISTable.Dependencies()""" |
132 |
|
|
# A PostGISTable depends on no other data container |
133 |
|
|
self.assertEquals(self.table.Dependencies(), ()) |
134 |
|
|
|
135 |
|
|
def test_num_rows(self): |
136 |
|
|
"""Test PostGISTable.NumRows()""" |
137 |
|
|
self.assertEquals(self.table.NumRows(), 34) |
138 |
|
|
|
139 |
|
|
def test_num_columns(self): |
140 |
|
|
"""Test PostGISTable.NumColumns()""" |
141 |
|
|
# The table in the postgis db has one additional column, "gid", |
142 |
|
|
# so there's one more column in the PostGISTable than in the DBF |
143 |
|
|
self.assertEquals(self.table.NumColumns(), 7) |
144 |
|
|
|
145 |
|
|
def test_columns(self): |
146 |
|
|
"""Test PostGISTable.Columns()""" |
147 |
|
|
self.assertEquals(len(self.table.Columns()), 7) |
148 |
|
|
self.assertEquals(self.table.Columns()[0].name, "gid") |
149 |
|
|
self.assertEquals(self.table.Columns()[0].type, FIELDTYPE_INT) |
150 |
|
|
self.assertEquals(self.table.Columns()[0].index, 0) |
151 |
|
|
self.assertEquals(self.table.Columns()[1].name, "area") |
152 |
|
|
self.assertEquals(self.table.Columns()[1].type, FIELDTYPE_DOUBLE) |
153 |
|
|
self.assertEquals(self.table.Columns()[1].index, 1) |
154 |
|
|
self.assertEquals(self.table.Columns()[5].name, "clptlabel") |
155 |
|
|
self.assertEquals(self.table.Columns()[5].type, FIELDTYPE_STRING) |
156 |
|
|
self.assertEquals(self.table.Columns()[5].index, 5) |
157 |
|
|
|
158 |
|
|
def test_column(self): |
159 |
|
|
"""Test PostGISTable.Column()""" |
160 |
|
|
self.assertEquals(self.table.Column("area").name, "area") |
161 |
|
|
self.assertEquals(self.table.Column("area").type, FIELDTYPE_DOUBLE) |
162 |
|
|
self.assertEquals(self.table.Column("area").index, 1) |
163 |
|
|
|
164 |
|
|
def test_has_column(self): |
165 |
|
|
"""Test PostGISTable.HasColumn()""" |
166 |
|
|
self.assert_(self.table.HasColumn("area")) |
167 |
|
|
self.failIf(self.table.HasColumn("foo")) |
168 |
|
|
|
169 |
|
|
def test_read_row_as_dict(self): |
170 |
|
|
"""Test PostGISTable.ReadRowAsDict()""" |
171 |
|
|
self.assertEquals(self.table.ReadRowAsDict(3), |
172 |
|
|
{"gid": 3, |
173 |
|
|
"area": 0.0, |
174 |
|
|
"perimeter": 0.0, |
175 |
|
|
"clpoint_": 4, |
176 |
|
|
"clpoint_id": 24, |
177 |
|
|
"clptlabel": "RUINS", |
178 |
|
|
"clptflag": 0}) |
179 |
|
|
|
180 |
|
|
def test_read_value(self): |
181 |
|
|
"""Test PostGISTable.ReadValue()""" |
182 |
|
|
self.assertEquals(self.table.ReadValue(3, 4), 24) |
183 |
|
|
self.assertEquals(self.table.ReadValue(3, "clpoint_id"), 24) |
184 |
|
|
|
185 |
|
|
def test_value_range(self): |
186 |
|
|
"""Test PostGISTable.ValueRange()""" |
187 |
|
|
self.assertEquals(self.table.ValueRange("clpoint_id"), (21, 74)) |
188 |
|
|
|
189 |
|
|
def test_unique_values(self): |
190 |
|
|
"""Test PostGISTable.UniqueValues()""" |
191 |
|
|
values = self.table.UniqueValues("clptlabel") |
192 |
|
|
values.sort() |
193 |
|
|
self.assertEquals(values, ["BUILDING", "FARM", "HUT","LIGHTHOUSE", |
194 |
|
|
"OTHER/UNKNOWN", "RUINS"]) |
195 |
|
|
|
196 |
|
|
def test_simple_query(self): |
197 |
|
|
"""Test PostGISTable.SimpleQuery()""" |
198 |
|
|
table = self.table |
199 |
|
|
self.assertEquals(table.SimpleQuery(table.Column("clptlabel"), |
200 |
|
|
"==", "FARM"), |
201 |
|
|
[6]) |
202 |
|
|
self.assertEquals(table.SimpleQuery(table.Column("clpoint_id"), |
203 |
|
|
">", 70), |
204 |
|
|
[24, 25, 26]) |
205 |
|
|
self.assertEquals(table.SimpleQuery(table.Column("clpoint_id"), |
206 |
|
|
"<", table.Column("clpoint_")), |
207 |
|
|
[28, 29, 30, 31, 32, 33]) |
208 |
|
|
|
209 |
|
|
|
210 |
|
|
class TestPostGISShapestorePoint(PostGISStaticTests): |
211 |
|
|
|
212 |
|
|
"""Tests for PostGISShapeStore objects with POINT data""" |
213 |
|
|
|
214 |
|
|
def setUp(self): |
215 |
|
|
"""Extend inherited method to set self.table to a PostGISShapeStore""" |
216 |
|
|
PostGISStaticTests.setUp(self) |
217 |
|
|
self.store = PostGISShapeStore(self.db, "landmarks") |
218 |
|
|
|
219 |
|
|
# |
220 |
|
|
# First, some tests that should be independend of the shapetype, so |
221 |
|
|
# it shouldn't be necessary to repeat them for other shapetypes |
222 |
|
|
# |
223 |
|
|
|
224 |
|
|
def test_dependencies(self): |
225 |
|
|
"""Test PostGISShapeStore.Dependencies()""" |
226 |
|
|
# A PostGISShapeStore depends on no other data container |
227 |
|
|
self.assertEquals(self.store.Dependencies(), ()) |
228 |
|
|
|
229 |
|
|
def test_table(self): |
230 |
|
|
"""Test PostGISShapeStore.Table() with POINT shapes""" |
231 |
|
|
# A PostGISShapeStore is its own table |
232 |
|
|
self.assert_(self.store.Table() is self.store) |
233 |
|
|
|
234 |
|
|
def test_orig_shapestore(self): |
235 |
|
|
"""Test PostGISShapeStore.OrigShapeStore() with POINT shapes""" |
236 |
|
|
# A PostGISShapeStore is not derived from another shape store |
237 |
|
|
self.assert_(self.store.OrigShapeStore() is None) |
238 |
|
|
|
239 |
|
|
def test_raw_format(self): |
240 |
|
|
"""Test PostGISShapeStore.RawShapeFormat() with POINT shapes""" |
241 |
|
|
self.assertEquals(self.store.RawShapeFormat(), RAW_WKT) |
242 |
|
|
|
243 |
|
|
# |
244 |
|
|
# Shapetype specific tests |
245 |
|
|
# |
246 |
|
|
|
247 |
|
|
def test_shape_type(self): |
248 |
|
|
"""Test PostGISShapeStore.ShapeType() with POINT shapes""" |
249 |
|
|
self.assertEquals(self.store.ShapeType(), SHAPETYPE_POINT) |
250 |
|
|
|
251 |
|
|
def test_num_shapes(self): |
252 |
|
|
"""Test PostGISShapeStore.NumShapes() with POINT shapes""" |
253 |
|
|
self.assertEquals(self.store.NumShapes(), 34) |
254 |
|
|
|
255 |
|
|
def test_bounding_box(self): |
256 |
|
|
"""Test PostGISShapeStore.BoundingBox() with POINT shapes""" |
257 |
|
|
self.assertFloatSeqEqual(self.store.BoundingBox(), |
258 |
|
|
[-23.806047439575195, 63.405960083007812, |
259 |
|
|
-15.12291431427002, 66.36572265625]) |
260 |
|
|
|
261 |
|
|
def test_shape_shapeid(self): |
262 |
|
|
"""Test PostGISShapeStore.Shape(i).ShapeID() with POINT shapes""" |
263 |
|
|
self.assertEquals(self.store.Shape(5).ShapeID(), 5) |
264 |
|
|
|
265 |
|
|
def test_shape_points(self): |
266 |
|
|
"""Test PostGISShapeStore.Shape(i).Points() with POINT shapes""" |
267 |
|
|
self.assertPointListEquals(self.store.Shape(0).Points(), |
268 |
|
|
[[(-22.711074829101562, 66.36572265625)]]) |
269 |
|
|
|
270 |
|
|
def test_shape_raw_data(self): |
271 |
|
|
"""Test PostGISShapeStore.Shape(i).RawData() with POINT shapes""" |
272 |
|
|
self.assertEquals(self.store.Shape(0).RawData(), |
273 |
|
|
'POINT(-22.7110748291016 66.36572265625)') |
274 |
|
|
|
275 |
|
|
def test_shapes_in_region(self): |
276 |
|
|
"""Test PostGISShapeStore:ShapesInRegion() with POINT shapes""" |
277 |
|
|
shapes = self.store.ShapesInRegion((-20.0, 64.0, -24.0, 67)) |
278 |
|
|
self.assertEquals([s.ShapeID() for s in shapes], |
279 |
|
|
[0, 1, 2, 3, 4, 5, 27]) |
280 |
|
|
|
281 |
|
|
|
282 |
|
|
class TestPostGISShapestoreArc(PostGISStaticTests): |
283 |
|
|
|
284 |
|
|
"""Tests for PostGISShapeStore objects with MULTILINESTRING data""" |
285 |
|
|
|
286 |
|
|
def setUp(self): |
287 |
|
|
"""Extend inherited method to set self.table to a PostGISShapeStore""" |
288 |
|
|
PostGISStaticTests.setUp(self) |
289 |
|
|
self.store = PostGISShapeStore(self.db, "roads") |
290 |
|
|
|
291 |
|
|
def test_shape_type(self): |
292 |
|
|
"""Test PostGISShapeStore.ShapeType() with ARC shapes""" |
293 |
|
|
self.assertEquals(self.store.ShapeType(), SHAPETYPE_ARC) |
294 |
|
|
|
295 |
|
|
def test_num_shapes(self): |
296 |
|
|
"""Test PostGISShapeStore.NumShapes() with ARC shapes""" |
297 |
|
|
self.assertEquals(self.store.NumShapes(), 839) |
298 |
|
|
|
299 |
|
|
def test_bounding_box(self): |
300 |
|
|
"""Test PostGISShapeStore.BoundingBox() with ARC shapes""" |
301 |
|
|
self.assertFloatSeqEqual(self.store.BoundingBox(), |
302 |
|
|
[-24.450359344482422, 63.426830291748047, |
303 |
|
|
-13.55668830871582, 66.520111083984375]) |
304 |
|
|
|
305 |
|
|
def test_shape_shapeid(self): |
306 |
|
|
"""Test PostGISShapeStore.Shape(i).ShapeID() with ARC shapes""" |
307 |
|
|
self.assertEquals(self.store.Shape(5).ShapeID(), 5) |
308 |
|
|
|
309 |
|
|
def test_shape_points(self): |
310 |
|
|
"""Test PostGISShapeStore.Shape(i).Points() with ARC shapes""" |
311 |
|
|
self.assertPointListEquals(self.store.Shape(32).Points(), |
312 |
|
|
[[(-15.0821743011474, 66.2773818969726), |
313 |
|
|
(-15.0263500213623, 66.2733917236328)]]) |
314 |
|
|
|
315 |
|
|
def test_shape_raw_data(self): |
316 |
|
|
"""Test PostGISShapeStore.Shape(i).RawData() with ARC shapes""" |
317 |
|
|
self.assertEquals(self.store.Shape(32).RawData(), |
318 |
|
|
"MULTILINESTRING((-15.0821743011475 66.2773818969727," |
319 |
|
|
"-15.0263500213623 66.2733917236328))") |
320 |
|
|
|
321 |
|
|
def test_shapes_in_region(self): |
322 |
|
|
"""Test PostGISShapeStore.ShapesInRegion() with ARC shapes""" |
323 |
|
|
shapes = self.store.ShapesInRegion((-24.0, 64.5, -23.5, 65.0)) |
324 |
|
|
self.assertEquals([s.ShapeID() for s in shapes], [573, 581, 613]) |
325 |
|
|
|
326 |
|
|
|
327 |
|
|
class TestPostGISShapestorePolygon(PostGISStaticTests): |
328 |
|
|
|
329 |
|
|
"""Tests for PostGISShapeStore objects with POLYGON data""" |
330 |
|
|
|
331 |
|
|
def setUp(self): |
332 |
|
|
"""Extend inherited method to set self.table to a PostGISShapeStore""" |
333 |
|
|
PostGISStaticTests.setUp(self) |
334 |
|
|
self.store = PostGISShapeStore(self.db, "political") |
335 |
|
|
|
336 |
|
|
def test_shape_type(self): |
337 |
|
|
"""Test PostGISShapeStore.ShapeType() with POLYGON shapes""" |
338 |
|
|
self.assertEquals(self.store.ShapeType(), SHAPETYPE_POLYGON) |
339 |
|
|
|
340 |
|
|
def test_num_shapes(self): |
341 |
|
|
"""Test PostGISShapeStore.NumShapes() with POLYGON shapes""" |
342 |
|
|
self.assertEquals(self.store.NumShapes(), 156) |
343 |
|
|
|
344 |
|
|
def test_bounding_box(self): |
345 |
|
|
"""Test PostGISShapeStore.BoundingBox() with POLYGON shapes""" |
346 |
|
|
self.assertFloatSeqEqual(self.store.BoundingBox(), |
347 |
|
|
[-24.546524047851562, 63.286754608154297, |
348 |
|
|
-13.495815277099609, 66.563774108886719]) |
349 |
|
|
|
350 |
|
|
def test_shape_shapeid(self): |
351 |
|
|
"""Test PostGISShapeStore.Shape(i).ShapeID() with POLYGON shapes""" |
352 |
|
|
self.assertEquals(self.store.Shape(5).ShapeID(), 5) |
353 |
|
|
|
354 |
|
|
def test_shape_points(self): |
355 |
|
|
"""Test PostGISShapeStore.Shape(i).Points() with POLYGON shapes""" |
356 |
|
|
self.assertPointListEquals(self.store.Shape(4).Points(), |
357 |
|
|
[[(-22.40639114379882, 64.714111328125), |
358 |
|
|
(-22.41621208190918, 64.716003417968), |
359 |
|
|
(-22.40605163574218, 64.719200134277), |
360 |
|
|
(-22.40639114379882, 64.714111328125)]]) |
361 |
|
|
|
362 |
|
|
def test_shape_raw_data(self): |
363 |
|
|
"""Test PostGISShapeStore.Shape(i).RawData() with POLYGON shapes""" |
364 |
|
|
self.assertEquals(self.store.Shape(4).RawData(), |
365 |
|
|
"POLYGON((-22.4063911437988 64.714111328125," |
366 |
|
|
"-22.4162120819092 64.7160034179688," |
367 |
|
|
"-22.4060516357422 64.7192001342773," |
368 |
|
|
"-22.4063911437988 64.714111328125))") |
369 |
|
|
|
370 |
|
|
def test_shapes_in_region(self): |
371 |
|
|
"""Test PostGISShapeStore.ShapesInRegion() with POLYGON shapes""" |
372 |
|
|
shapes = self.store.ShapesInRegion((-23.0, 65.5, -22.8, 65.25)) |
373 |
|
|
self.assertEquals([s.ShapeID() for s in shapes], |
374 |
|
|
[47, 56, 59, 61, 62, 71, 144]) |
375 |
|
|
|
376 |
|
|
|
377 |
|
|
if __name__ == "__main__": |
378 |
|
|
support.run_tests() |