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 |
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 |
1605 |
class PostGISStaticTests(unittest.TestCase, support.FloatComparisonMixin): |
256 |
|
|
|
257 |
|
|
"""Base class for PostGIS tests with static data.""" |
258 |
|
|
|
259 |
|
|
def setUp(self): |
260 |
|
|
"""Start the server and create a database with static data |
261 |
|
|
|
262 |
|
|
This method sets the following instance attributes: |
263 |
|
|
|
264 |
|
|
dbname -- the name of the database |
265 |
|
|
|
266 |
|
|
server -- The server object |
267 |
|
|
|
268 |
|
|
db -- the PostGISConnection object |
269 |
|
|
""" |
270 |
|
|
postgissupport.skip_if_no_postgis() |
271 |
|
|
self.server = postgissupport.get_test_server() |
272 |
|
|
self.postgisdb = self.server.get_default_static_data_db() |
273 |
|
|
self.db = PostGISConnection(dbname = self.postgisdb.dbname, |
274 |
bh |
1634 |
**self.server.connection_params("user")) |
275 |
bh |
1605 |
|
276 |
|
|
def tearDown(self): |
277 |
|
|
"""Close the database connection""" |
278 |
|
|
self.db.Close() |
279 |
|
|
|
280 |
|
|
|
281 |
|
|
class TestPostGISTable(PostGISStaticTests): |
282 |
|
|
|
283 |
|
|
def setUp(self): |
284 |
|
|
"""Extend inherited method to set self.table to a PostGISTable""" |
285 |
|
|
PostGISStaticTests.setUp(self) |
286 |
|
|
self.table = PostGISTable(self.db, "landmarks") |
287 |
|
|
|
288 |
bh |
1638 |
def test_dbconn(self): |
289 |
|
|
"""Test PostGISTable.DBConnection()""" |
290 |
|
|
self.failUnless(self.table.DBConnection() is self.db) |
291 |
|
|
|
292 |
|
|
def test_dbname(self): |
293 |
|
|
"""Test PostGISTable.TableName()""" |
294 |
|
|
self.assertEquals(self.table.TableName(), "landmarks") |
295 |
|
|
|
296 |
bh |
1658 |
def test_title(self): |
297 |
|
|
"""test PostGISTable.Title()""" |
298 |
|
|
# The title is currently equal to the tablename |
299 |
|
|
self.assertEquals(self.table.Title(), "landmarks") |
300 |
|
|
|
301 |
bh |
1605 |
def test_dependencies(self): |
302 |
|
|
"""Test PostGISTable.Dependencies()""" |
303 |
|
|
# A PostGISTable depends on no other data container |
304 |
|
|
self.assertEquals(self.table.Dependencies(), ()) |
305 |
|
|
|
306 |
|
|
def test_num_rows(self): |
307 |
|
|
"""Test PostGISTable.NumRows()""" |
308 |
|
|
self.assertEquals(self.table.NumRows(), 34) |
309 |
|
|
|
310 |
|
|
def test_num_columns(self): |
311 |
|
|
"""Test PostGISTable.NumColumns()""" |
312 |
|
|
# The table in the postgis db has one additional column, "gid", |
313 |
|
|
# so there's one more column in the PostGISTable than in the DBF |
314 |
|
|
self.assertEquals(self.table.NumColumns(), 7) |
315 |
|
|
|
316 |
|
|
def test_columns(self): |
317 |
|
|
"""Test PostGISTable.Columns()""" |
318 |
|
|
self.assertEquals(len(self.table.Columns()), 7) |
319 |
|
|
self.assertEquals(self.table.Columns()[0].name, "gid") |
320 |
|
|
self.assertEquals(self.table.Columns()[0].type, FIELDTYPE_INT) |
321 |
|
|
self.assertEquals(self.table.Columns()[0].index, 0) |
322 |
|
|
self.assertEquals(self.table.Columns()[1].name, "area") |
323 |
|
|
self.assertEquals(self.table.Columns()[1].type, FIELDTYPE_DOUBLE) |
324 |
|
|
self.assertEquals(self.table.Columns()[1].index, 1) |
325 |
|
|
self.assertEquals(self.table.Columns()[5].name, "clptlabel") |
326 |
|
|
self.assertEquals(self.table.Columns()[5].type, FIELDTYPE_STRING) |
327 |
|
|
self.assertEquals(self.table.Columns()[5].index, 5) |
328 |
|
|
|
329 |
|
|
def test_column(self): |
330 |
|
|
"""Test PostGISTable.Column()""" |
331 |
|
|
self.assertEquals(self.table.Column("area").name, "area") |
332 |
|
|
self.assertEquals(self.table.Column("area").type, FIELDTYPE_DOUBLE) |
333 |
|
|
self.assertEquals(self.table.Column("area").index, 1) |
334 |
|
|
|
335 |
|
|
def test_has_column(self): |
336 |
|
|
"""Test PostGISTable.HasColumn()""" |
337 |
|
|
self.assert_(self.table.HasColumn("area")) |
338 |
|
|
self.failIf(self.table.HasColumn("foo")) |
339 |
|
|
|
340 |
|
|
def test_read_row_as_dict(self): |
341 |
|
|
"""Test PostGISTable.ReadRowAsDict()""" |
342 |
bh |
1662 |
self.assertEquals(self.table.ReadRowAsDict(1003), |
343 |
|
|
{"gid": 1003, |
344 |
bh |
1605 |
"area": 0.0, |
345 |
|
|
"perimeter": 0.0, |
346 |
|
|
"clpoint_": 4, |
347 |
|
|
"clpoint_id": 24, |
348 |
|
|
"clptlabel": "RUINS", |
349 |
|
|
"clptflag": 0}) |
350 |
|
|
|
351 |
bh |
1662 |
def test_read_row_as_dict_row_count_mode(self): |
352 |
|
|
"""Test PostGISTable.ReadRowAsDict() row count address mode""" |
353 |
|
|
self.assertEquals(self.table.ReadRowAsDict(3, row_is_ordinal = 1), |
354 |
|
|
{"gid": 1003, |
355 |
|
|
"area": 0.0, |
356 |
|
|
"perimeter": 0.0, |
357 |
|
|
"clpoint_": 4, |
358 |
|
|
"clpoint_id": 24, |
359 |
|
|
"clptlabel": "RUINS", |
360 |
|
|
"clptflag": 0}) |
361 |
|
|
|
362 |
bh |
1605 |
def test_read_value(self): |
363 |
|
|
"""Test PostGISTable.ReadValue()""" |
364 |
bh |
1662 |
self.assertEquals(self.table.ReadValue(1003, 4), 24) |
365 |
|
|
self.assertEquals(self.table.ReadValue(1003, "clpoint_id"), 24) |
366 |
bh |
1605 |
|
367 |
bh |
1662 |
def test_read_value_row_count_mode(self): |
368 |
|
|
"""Test PostGISTable.ReadValue() row count address mode""" |
369 |
|
|
self.assertEquals(self.table.ReadValue(3, 4, row_is_ordinal = 1), 24) |
370 |
|
|
self.assertEquals(self.table.ReadValue(3, "clpoint_id", |
371 |
|
|
row_is_ordinal = 1), |
372 |
|
|
24) |
373 |
|
|
|
374 |
|
|
def test_row_id_to_ordinal(self): |
375 |
|
|
"""Test PostGISTable.RowIdToOrdinal()""" |
376 |
|
|
self.assertEquals(self.table.RowIdToOrdinal(1005), 5) |
377 |
|
|
|
378 |
|
|
def test_row_oridnal_to_id(self): |
379 |
|
|
"""Test PostGISTable.RowOrdinalToId()""" |
380 |
|
|
self.assertEquals(self.table.RowOrdinalToId(5), 1005) |
381 |
|
|
|
382 |
bh |
1605 |
def test_value_range(self): |
383 |
|
|
"""Test PostGISTable.ValueRange()""" |
384 |
|
|
self.assertEquals(self.table.ValueRange("clpoint_id"), (21, 74)) |
385 |
|
|
|
386 |
|
|
def test_unique_values(self): |
387 |
|
|
"""Test PostGISTable.UniqueValues()""" |
388 |
|
|
values = self.table.UniqueValues("clptlabel") |
389 |
|
|
values.sort() |
390 |
|
|
self.assertEquals(values, ["BUILDING", "FARM", "HUT","LIGHTHOUSE", |
391 |
|
|
"OTHER/UNKNOWN", "RUINS"]) |
392 |
|
|
|
393 |
|
|
def test_simple_query(self): |
394 |
|
|
"""Test PostGISTable.SimpleQuery()""" |
395 |
|
|
table = self.table |
396 |
|
|
self.assertEquals(table.SimpleQuery(table.Column("clptlabel"), |
397 |
|
|
"==", "FARM"), |
398 |
bh |
1662 |
[1006]) |
399 |
bh |
1605 |
self.assertEquals(table.SimpleQuery(table.Column("clpoint_id"), |
400 |
|
|
">", 70), |
401 |
bh |
1662 |
[1024, 1025, 1026]) |
402 |
bh |
1605 |
self.assertEquals(table.SimpleQuery(table.Column("clpoint_id"), |
403 |
|
|
"<", table.Column("clpoint_")), |
404 |
bh |
1662 |
[1028, 1029, 1030, 1031, 1032, 1033]) |
405 |
bh |
1605 |
|
406 |
|
|
|
407 |
|
|
class TestPostGISShapestorePoint(PostGISStaticTests): |
408 |
|
|
|
409 |
|
|
"""Tests for PostGISShapeStore objects with POINT data""" |
410 |
|
|
|
411 |
|
|
def setUp(self): |
412 |
|
|
"""Extend inherited method to set self.table to a PostGISShapeStore""" |
413 |
|
|
PostGISStaticTests.setUp(self) |
414 |
|
|
self.store = PostGISShapeStore(self.db, "landmarks") |
415 |
|
|
|
416 |
|
|
# |
417 |
|
|
# First, some tests that should be independend of the shapetype, so |
418 |
|
|
# it shouldn't be necessary to repeat them for other shapetypes |
419 |
|
|
# |
420 |
|
|
|
421 |
|
|
def test_dependencies(self): |
422 |
|
|
"""Test PostGISShapeStore.Dependencies()""" |
423 |
|
|
# A PostGISShapeStore depends on no other data container |
424 |
|
|
self.assertEquals(self.store.Dependencies(), ()) |
425 |
|
|
|
426 |
|
|
def test_table(self): |
427 |
|
|
"""Test PostGISShapeStore.Table() with POINT shapes""" |
428 |
|
|
# A PostGISShapeStore is its own table |
429 |
|
|
self.assert_(self.store.Table() is self.store) |
430 |
|
|
|
431 |
|
|
def test_orig_shapestore(self): |
432 |
|
|
"""Test PostGISShapeStore.OrigShapeStore() with POINT shapes""" |
433 |
|
|
# A PostGISShapeStore is not derived from another shape store |
434 |
|
|
self.assert_(self.store.OrigShapeStore() is None) |
435 |
|
|
|
436 |
|
|
def test_raw_format(self): |
437 |
|
|
"""Test PostGISShapeStore.RawShapeFormat() with POINT shapes""" |
438 |
|
|
self.assertEquals(self.store.RawShapeFormat(), RAW_WKT) |
439 |
|
|
|
440 |
bh |
1658 |
def test_all_shapes(self): |
441 |
|
|
"""Test PostGISShapeStore.AllShapes()""" |
442 |
|
|
self.assertEquals([s.ShapeID() for s in self.store.AllShapes()], |
443 |
bh |
1662 |
range(1000, 1000 + self.store.NumShapes())) |
444 |
bh |
1658 |
|
445 |
bh |
1605 |
# |
446 |
|
|
# Shapetype specific tests |
447 |
|
|
# |
448 |
|
|
|
449 |
|
|
def test_shape_type(self): |
450 |
|
|
"""Test PostGISShapeStore.ShapeType() with POINT shapes""" |
451 |
|
|
self.assertEquals(self.store.ShapeType(), SHAPETYPE_POINT) |
452 |
|
|
|
453 |
|
|
def test_num_shapes(self): |
454 |
|
|
"""Test PostGISShapeStore.NumShapes() with POINT shapes""" |
455 |
|
|
self.assertEquals(self.store.NumShapes(), 34) |
456 |
|
|
|
457 |
|
|
def test_bounding_box(self): |
458 |
|
|
"""Test PostGISShapeStore.BoundingBox() with POINT shapes""" |
459 |
|
|
self.assertFloatSeqEqual(self.store.BoundingBox(), |
460 |
|
|
[-23.806047439575195, 63.405960083007812, |
461 |
|
|
-15.12291431427002, 66.36572265625]) |
462 |
|
|
|
463 |
|
|
def test_shape_shapeid(self): |
464 |
|
|
"""Test PostGISShapeStore.Shape(i).ShapeID() with POINT shapes""" |
465 |
bh |
1662 |
self.assertEquals(self.store.Shape(1005).ShapeID(), 1005) |
466 |
bh |
1605 |
|
467 |
|
|
def test_shape_points(self): |
468 |
|
|
"""Test PostGISShapeStore.Shape(i).Points() with POINT shapes""" |
469 |
bh |
1662 |
self.assertPointListEquals(self.store.Shape(1000).Points(), |
470 |
bh |
1605 |
[[(-22.711074829101562, 66.36572265625)]]) |
471 |
|
|
|
472 |
|
|
def test_shape_raw_data(self): |
473 |
|
|
"""Test PostGISShapeStore.Shape(i).RawData() with POINT shapes""" |
474 |
bh |
1662 |
self.assertEquals(self.store.Shape(1000).RawData(), |
475 |
bh |
1605 |
'POINT(-22.7110748291016 66.36572265625)') |
476 |
|
|
|
477 |
|
|
def test_shapes_in_region(self): |
478 |
|
|
"""Test PostGISShapeStore:ShapesInRegion() with POINT shapes""" |
479 |
|
|
shapes = self.store.ShapesInRegion((-20.0, 64.0, -24.0, 67)) |
480 |
|
|
self.assertEquals([s.ShapeID() for s in shapes], |
481 |
bh |
1662 |
[1000, 1001, 1002, 1003, 1004, 1005, 1027]) |
482 |
bh |
1605 |
|
483 |
|
|
|
484 |
|
|
class TestPostGISShapestoreArc(PostGISStaticTests): |
485 |
|
|
|
486 |
|
|
"""Tests for PostGISShapeStore objects with MULTILINESTRING data""" |
487 |
|
|
|
488 |
|
|
def setUp(self): |
489 |
|
|
"""Extend inherited method to set self.table to a PostGISShapeStore""" |
490 |
|
|
PostGISStaticTests.setUp(self) |
491 |
|
|
self.store = PostGISShapeStore(self.db, "roads") |
492 |
|
|
|
493 |
|
|
def test_shape_type(self): |
494 |
|
|
"""Test PostGISShapeStore.ShapeType() with ARC shapes""" |
495 |
|
|
self.assertEquals(self.store.ShapeType(), SHAPETYPE_ARC) |
496 |
|
|
|
497 |
|
|
def test_num_shapes(self): |
498 |
|
|
"""Test PostGISShapeStore.NumShapes() with ARC shapes""" |
499 |
|
|
self.assertEquals(self.store.NumShapes(), 839) |
500 |
|
|
|
501 |
|
|
def test_bounding_box(self): |
502 |
|
|
"""Test PostGISShapeStore.BoundingBox() with ARC shapes""" |
503 |
|
|
self.assertFloatSeqEqual(self.store.BoundingBox(), |
504 |
|
|
[-24.450359344482422, 63.426830291748047, |
505 |
|
|
-13.55668830871582, 66.520111083984375]) |
506 |
|
|
|
507 |
|
|
def test_shape_shapeid(self): |
508 |
|
|
"""Test PostGISShapeStore.Shape(i).ShapeID() with ARC shapes""" |
509 |
|
|
self.assertEquals(self.store.Shape(5).ShapeID(), 5) |
510 |
|
|
|
511 |
|
|
def test_shape_points(self): |
512 |
|
|
"""Test PostGISShapeStore.Shape(i).Points() with ARC shapes""" |
513 |
|
|
self.assertPointListEquals(self.store.Shape(32).Points(), |
514 |
|
|
[[(-15.0821743011474, 66.2773818969726), |
515 |
|
|
(-15.0263500213623, 66.2733917236328)]]) |
516 |
|
|
|
517 |
|
|
def test_shape_raw_data(self): |
518 |
|
|
"""Test PostGISShapeStore.Shape(i).RawData() with ARC shapes""" |
519 |
|
|
self.assertEquals(self.store.Shape(32).RawData(), |
520 |
|
|
"MULTILINESTRING((-15.0821743011475 66.2773818969727," |
521 |
|
|
"-15.0263500213623 66.2733917236328))") |
522 |
|
|
|
523 |
|
|
def test_shapes_in_region(self): |
524 |
|
|
"""Test PostGISShapeStore.ShapesInRegion() with ARC shapes""" |
525 |
|
|
shapes = self.store.ShapesInRegion((-24.0, 64.5, -23.5, 65.0)) |
526 |
|
|
self.assertEquals([s.ShapeID() for s in shapes], [573, 581, 613]) |
527 |
|
|
|
528 |
|
|
|
529 |
|
|
|
530 |
bh |
1656 |
class PolygonTests: |
531 |
bh |
1605 |
|
532 |
bh |
1656 |
"""Test shared by the PLYGON and MULTIPOLYGON tests |
533 |
bh |
1605 |
|
534 |
bh |
1656 |
The tests are the same because they are based on the same data. |
535 |
|
|
""" |
536 |
|
|
|
537 |
bh |
1605 |
def test_shape_type(self): |
538 |
|
|
"""Test PostGISShapeStore.ShapeType() with POLYGON shapes""" |
539 |
|
|
self.assertEquals(self.store.ShapeType(), SHAPETYPE_POLYGON) |
540 |
|
|
|
541 |
|
|
def test_num_shapes(self): |
542 |
|
|
"""Test PostGISShapeStore.NumShapes() with POLYGON shapes""" |
543 |
|
|
self.assertEquals(self.store.NumShapes(), 156) |
544 |
|
|
|
545 |
|
|
def test_bounding_box(self): |
546 |
|
|
"""Test PostGISShapeStore.BoundingBox() with POLYGON shapes""" |
547 |
|
|
self.assertFloatSeqEqual(self.store.BoundingBox(), |
548 |
|
|
[-24.546524047851562, 63.286754608154297, |
549 |
|
|
-13.495815277099609, 66.563774108886719]) |
550 |
|
|
|
551 |
|
|
def test_shape_shapeid(self): |
552 |
|
|
"""Test PostGISShapeStore.Shape(i).ShapeID() with POLYGON shapes""" |
553 |
|
|
self.assertEquals(self.store.Shape(5).ShapeID(), 5) |
554 |
|
|
|
555 |
|
|
def test_shape_points(self): |
556 |
|
|
"""Test PostGISShapeStore.Shape(i).Points() with POLYGON shapes""" |
557 |
|
|
self.assertPointListEquals(self.store.Shape(4).Points(), |
558 |
|
|
[[(-22.40639114379882, 64.714111328125), |
559 |
|
|
(-22.41621208190918, 64.716003417968), |
560 |
|
|
(-22.40605163574218, 64.719200134277), |
561 |
|
|
(-22.40639114379882, 64.714111328125)]]) |
562 |
|
|
|
563 |
bh |
1656 |
def test_shapes_in_region(self): |
564 |
|
|
"""Test PostGISShapeStore.ShapesInRegion() with POLYGON shapes""" |
565 |
|
|
shapes = self.store.ShapesInRegion((-23.0, 65.5, -22.8, 65.25)) |
566 |
|
|
self.assertEquals([s.ShapeID() for s in shapes], |
567 |
|
|
[47, 56, 59, 61, 62, 71, 144]) |
568 |
|
|
|
569 |
|
|
|
570 |
|
|
class TestPostGISShapestorePolygon(PolygonTests, PostGISStaticTests): |
571 |
|
|
|
572 |
|
|
"""Tests for PostGISShapeStore objects with POLYGON data""" |
573 |
|
|
|
574 |
|
|
def setUp(self): |
575 |
|
|
"""Extend inherited method to set self.table to a PostGISShapeStore""" |
576 |
|
|
PostGISStaticTests.setUp(self) |
577 |
|
|
self.store = PostGISShapeStore(self.db, "political") |
578 |
|
|
|
579 |
|
|
def test_shape_type(self): |
580 |
|
|
"""Test PostGISShapeStore.ShapeType() with POLYGON shapes""" |
581 |
|
|
self.assertEquals(self.store.ShapeType(), SHAPETYPE_POLYGON) |
582 |
|
|
|
583 |
|
|
|
584 |
bh |
1605 |
def test_shape_raw_data(self): |
585 |
|
|
"""Test PostGISShapeStore.Shape(i).RawData() with POLYGON shapes""" |
586 |
|
|
self.assertEquals(self.store.Shape(4).RawData(), |
587 |
|
|
"POLYGON((-22.4063911437988 64.714111328125," |
588 |
|
|
"-22.4162120819092 64.7160034179688," |
589 |
|
|
"-22.4060516357422 64.7192001342773," |
590 |
|
|
"-22.4063911437988 64.714111328125))") |
591 |
|
|
|
592 |
|
|
|
593 |
bh |
1656 |
class TestPostGISShapestoreMultiPolygon(PolygonTests, PostGISStaticTests): |
594 |
bh |
1605 |
|
595 |
bh |
1656 |
"""Tests for PostGISShapeStore objects with MUTLIPOLYGON data""" |
596 |
|
|
|
597 |
|
|
def setUp(self): |
598 |
|
|
"""Extend inherited method to set self.table to a PostGISShapeStore""" |
599 |
|
|
PostGISStaticTests.setUp(self) |
600 |
|
|
self.store = PostGISShapeStore(self.db, "political_multi") |
601 |
|
|
|
602 |
|
|
def test_shape_raw_data(self): |
603 |
|
|
"""Test PostGISShapeStore.Shape(i).RawData() with POLYGON shapes""" |
604 |
|
|
self.assertEquals(self.store.Shape(4).RawData(), |
605 |
|
|
"MULTIPOLYGON(((-22.4063911437988 64.714111328125," |
606 |
|
|
"-22.4162120819092 64.7160034179688," |
607 |
|
|
"-22.4060516357422 64.7192001342773," |
608 |
|
|
"-22.4063911437988 64.714111328125)))") |
609 |
|
|
|
610 |
bh |
1693 |
|
611 |
|
|
|
612 |
bh |
1605 |
if __name__ == "__main__": |
613 |
|
|
support.run_tests() |