/[thuban]/branches/WIP-pyshapelib-bramz/test/test_postgis_db.py
ViewVC logotype

Annotation of /branches/WIP-pyshapelib-bramz/test/test_postgis_db.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1693 - (hide annotations)
Mon Sep 1 11:23:26 2003 UTC (21 years, 6 months ago) by bh
Original Path: trunk/thuban/test/test_postgis_db.py
File MIME type: text/x-python
File size: 20425 byte(s)
* Thuban/Model/postgisdb.py
(PostGISTable): Extend doc-string
(PostGISTable._fetch_table_information): Set the column index
correctly, pretending ignored columns don't exist.

* test/test_postgis_db.py (TestPostGISIgnoredColumns): New tests
for postgis tables with data types not yet supported by thuban.

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     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 bh 1636 class TestPostgisDBExceptions(unittest.TestCase):
98    
99     def setUp(self):
100     """Start the postgis server and switch on authentication"""
101     postgissupport.skip_if_no_postgis()
102     self.server = postgissupport.get_test_server()
103     self.postgisdb = self.server.get_default_static_data_db()
104     self.server.require_authentication(True)
105    
106     def tearDown(self):
107     """Extend the inherited method to switch off postgresql authentication
108     """
109     self.server.require_authentication(False)
110    
111     def test_no_password(self):
112     """Test PostGISConnection with omitted but required password"""
113     connection_params = self.server.connection_params("user")
114     # remove the password deliberately
115     del connection_params["password"]
116    
117     self.assertRaises(ConnectionError,
118     PostGISConnection, dbname = self.postgisdb.dbname,
119     **connection_params)
120    
121    
122 bh 1693 class TestPostGISIgnoredColumns(unittest.TestCase):
123    
124     def setUp(self):
125     """Start the server and create a database.
126    
127     The database name will be stored in self.dbname, the server
128     object in self.server and the db object in self.db.
129     """
130     postgissupport.skip_if_no_postgis()
131     self.server = postgissupport.get_test_server()
132     self.dbname = ".".join(self.id().split(".")[-2:])[-31:]
133     self.db = self.server.new_postgis_db(self.dbname)
134    
135     def test(self):
136     """test PostGISTable on a table with unsupported data types"""
137     stmt = """CREATE TABLE foo (
138     gid integer,
139     ignored bigint,
140     length float);
141     GRANT SELECT ON foo TO PUBLIC;
142     """
143     self.server.execute_sql(self.dbname, "admin", stmt)
144    
145     db = PostGISConnection(dbname = self.dbname,
146     **self.server.connection_params("user"))
147     table = PostGISTable(db, "foo")
148    
149     # The bigint column will be ignored because it's not mapped to a
150     # known integer type, so there are only two colunns
151     self.assertEquals(table.NumColumns(), 2)
152     self.assertEquals(table.Column(0).name, "gid")
153     self.assertEquals(table.Column(1).name, "length")
154    
155    
156 bh 1605 class PostGISStaticTests(unittest.TestCase, support.FloatComparisonMixin):
157    
158     """Base class for PostGIS tests with static data."""
159    
160     def setUp(self):
161     """Start the server and create a database with static data
162    
163     This method sets the following instance attributes:
164    
165     dbname -- the name of the database
166    
167     server -- The server object
168    
169     db -- the PostGISConnection object
170     """
171     postgissupport.skip_if_no_postgis()
172     self.server = postgissupport.get_test_server()
173     self.postgisdb = self.server.get_default_static_data_db()
174     self.db = PostGISConnection(dbname = self.postgisdb.dbname,
175 bh 1634 **self.server.connection_params("user"))
176 bh 1605
177     def tearDown(self):
178     """Close the database connection"""
179     self.db.Close()
180    
181    
182     class TestPostGISTable(PostGISStaticTests):
183    
184     def setUp(self):
185     """Extend inherited method to set self.table to a PostGISTable"""
186     PostGISStaticTests.setUp(self)
187     self.table = PostGISTable(self.db, "landmarks")
188    
189 bh 1638 def test_dbconn(self):
190     """Test PostGISTable.DBConnection()"""
191     self.failUnless(self.table.DBConnection() is self.db)
192    
193     def test_dbname(self):
194     """Test PostGISTable.TableName()"""
195     self.assertEquals(self.table.TableName(), "landmarks")
196    
197 bh 1658 def test_title(self):
198     """test PostGISTable.Title()"""
199     # The title is currently equal to the tablename
200     self.assertEquals(self.table.Title(), "landmarks")
201    
202 bh 1605 def test_dependencies(self):
203     """Test PostGISTable.Dependencies()"""
204     # A PostGISTable depends on no other data container
205     self.assertEquals(self.table.Dependencies(), ())
206    
207     def test_num_rows(self):
208     """Test PostGISTable.NumRows()"""
209     self.assertEquals(self.table.NumRows(), 34)
210    
211     def test_num_columns(self):
212     """Test PostGISTable.NumColumns()"""
213     # The table in the postgis db has one additional column, "gid",
214     # so there's one more column in the PostGISTable than in the DBF
215     self.assertEquals(self.table.NumColumns(), 7)
216    
217     def test_columns(self):
218     """Test PostGISTable.Columns()"""
219     self.assertEquals(len(self.table.Columns()), 7)
220     self.assertEquals(self.table.Columns()[0].name, "gid")
221     self.assertEquals(self.table.Columns()[0].type, FIELDTYPE_INT)
222     self.assertEquals(self.table.Columns()[0].index, 0)
223     self.assertEquals(self.table.Columns()[1].name, "area")
224     self.assertEquals(self.table.Columns()[1].type, FIELDTYPE_DOUBLE)
225     self.assertEquals(self.table.Columns()[1].index, 1)
226     self.assertEquals(self.table.Columns()[5].name, "clptlabel")
227     self.assertEquals(self.table.Columns()[5].type, FIELDTYPE_STRING)
228     self.assertEquals(self.table.Columns()[5].index, 5)
229    
230     def test_column(self):
231     """Test PostGISTable.Column()"""
232     self.assertEquals(self.table.Column("area").name, "area")
233     self.assertEquals(self.table.Column("area").type, FIELDTYPE_DOUBLE)
234     self.assertEquals(self.table.Column("area").index, 1)
235    
236     def test_has_column(self):
237     """Test PostGISTable.HasColumn()"""
238     self.assert_(self.table.HasColumn("area"))
239     self.failIf(self.table.HasColumn("foo"))
240    
241     def test_read_row_as_dict(self):
242     """Test PostGISTable.ReadRowAsDict()"""
243 bh 1662 self.assertEquals(self.table.ReadRowAsDict(1003),
244     {"gid": 1003,
245 bh 1605 "area": 0.0,
246     "perimeter": 0.0,
247     "clpoint_": 4,
248     "clpoint_id": 24,
249     "clptlabel": "RUINS",
250     "clptflag": 0})
251    
252 bh 1662 def test_read_row_as_dict_row_count_mode(self):
253     """Test PostGISTable.ReadRowAsDict() row count address mode"""
254     self.assertEquals(self.table.ReadRowAsDict(3, row_is_ordinal = 1),
255     {"gid": 1003,
256     "area": 0.0,
257     "perimeter": 0.0,
258     "clpoint_": 4,
259     "clpoint_id": 24,
260     "clptlabel": "RUINS",
261     "clptflag": 0})
262    
263 bh 1605 def test_read_value(self):
264     """Test PostGISTable.ReadValue()"""
265 bh 1662 self.assertEquals(self.table.ReadValue(1003, 4), 24)
266     self.assertEquals(self.table.ReadValue(1003, "clpoint_id"), 24)
267 bh 1605
268 bh 1662 def test_read_value_row_count_mode(self):
269     """Test PostGISTable.ReadValue() row count address mode"""
270     self.assertEquals(self.table.ReadValue(3, 4, row_is_ordinal = 1), 24)
271     self.assertEquals(self.table.ReadValue(3, "clpoint_id",
272     row_is_ordinal = 1),
273     24)
274    
275     def test_row_id_to_ordinal(self):
276     """Test PostGISTable.RowIdToOrdinal()"""
277     self.assertEquals(self.table.RowIdToOrdinal(1005), 5)
278    
279     def test_row_oridnal_to_id(self):
280     """Test PostGISTable.RowOrdinalToId()"""
281     self.assertEquals(self.table.RowOrdinalToId(5), 1005)
282    
283 bh 1605 def test_value_range(self):
284     """Test PostGISTable.ValueRange()"""
285     self.assertEquals(self.table.ValueRange("clpoint_id"), (21, 74))
286    
287     def test_unique_values(self):
288     """Test PostGISTable.UniqueValues()"""
289     values = self.table.UniqueValues("clptlabel")
290     values.sort()
291     self.assertEquals(values, ["BUILDING", "FARM", "HUT","LIGHTHOUSE",
292     "OTHER/UNKNOWN", "RUINS"])
293    
294     def test_simple_query(self):
295     """Test PostGISTable.SimpleQuery()"""
296     table = self.table
297     self.assertEquals(table.SimpleQuery(table.Column("clptlabel"),
298     "==", "FARM"),
299 bh 1662 [1006])
300 bh 1605 self.assertEquals(table.SimpleQuery(table.Column("clpoint_id"),
301     ">", 70),
302 bh 1662 [1024, 1025, 1026])
303 bh 1605 self.assertEquals(table.SimpleQuery(table.Column("clpoint_id"),
304     "<", table.Column("clpoint_")),
305 bh 1662 [1028, 1029, 1030, 1031, 1032, 1033])
306 bh 1605
307    
308     class TestPostGISShapestorePoint(PostGISStaticTests):
309    
310     """Tests for PostGISShapeStore objects with POINT data"""
311    
312     def setUp(self):
313     """Extend inherited method to set self.table to a PostGISShapeStore"""
314     PostGISStaticTests.setUp(self)
315     self.store = PostGISShapeStore(self.db, "landmarks")
316    
317     #
318     # First, some tests that should be independend of the shapetype, so
319     # it shouldn't be necessary to repeat them for other shapetypes
320     #
321    
322     def test_dependencies(self):
323     """Test PostGISShapeStore.Dependencies()"""
324     # A PostGISShapeStore depends on no other data container
325     self.assertEquals(self.store.Dependencies(), ())
326    
327     def test_table(self):
328     """Test PostGISShapeStore.Table() with POINT shapes"""
329     # A PostGISShapeStore is its own table
330     self.assert_(self.store.Table() is self.store)
331    
332     def test_orig_shapestore(self):
333     """Test PostGISShapeStore.OrigShapeStore() with POINT shapes"""
334     # A PostGISShapeStore is not derived from another shape store
335     self.assert_(self.store.OrigShapeStore() is None)
336    
337     def test_raw_format(self):
338     """Test PostGISShapeStore.RawShapeFormat() with POINT shapes"""
339     self.assertEquals(self.store.RawShapeFormat(), RAW_WKT)
340    
341 bh 1658 def test_all_shapes(self):
342     """Test PostGISShapeStore.AllShapes()"""
343     self.assertEquals([s.ShapeID() for s in self.store.AllShapes()],
344 bh 1662 range(1000, 1000 + self.store.NumShapes()))
345 bh 1658
346 bh 1605 #
347     # Shapetype specific tests
348     #
349    
350     def test_shape_type(self):
351     """Test PostGISShapeStore.ShapeType() with POINT shapes"""
352     self.assertEquals(self.store.ShapeType(), SHAPETYPE_POINT)
353    
354     def test_num_shapes(self):
355     """Test PostGISShapeStore.NumShapes() with POINT shapes"""
356     self.assertEquals(self.store.NumShapes(), 34)
357    
358     def test_bounding_box(self):
359     """Test PostGISShapeStore.BoundingBox() with POINT shapes"""
360     self.assertFloatSeqEqual(self.store.BoundingBox(),
361     [-23.806047439575195, 63.405960083007812,
362     -15.12291431427002, 66.36572265625])
363    
364     def test_shape_shapeid(self):
365     """Test PostGISShapeStore.Shape(i).ShapeID() with POINT shapes"""
366 bh 1662 self.assertEquals(self.store.Shape(1005).ShapeID(), 1005)
367 bh 1605
368     def test_shape_points(self):
369     """Test PostGISShapeStore.Shape(i).Points() with POINT shapes"""
370 bh 1662 self.assertPointListEquals(self.store.Shape(1000).Points(),
371 bh 1605 [[(-22.711074829101562, 66.36572265625)]])
372    
373     def test_shape_raw_data(self):
374     """Test PostGISShapeStore.Shape(i).RawData() with POINT shapes"""
375 bh 1662 self.assertEquals(self.store.Shape(1000).RawData(),
376 bh 1605 'POINT(-22.7110748291016 66.36572265625)')
377    
378     def test_shapes_in_region(self):
379     """Test PostGISShapeStore:ShapesInRegion() with POINT shapes"""
380     shapes = self.store.ShapesInRegion((-20.0, 64.0, -24.0, 67))
381     self.assertEquals([s.ShapeID() for s in shapes],
382 bh 1662 [1000, 1001, 1002, 1003, 1004, 1005, 1027])
383 bh 1605
384    
385     class TestPostGISShapestoreArc(PostGISStaticTests):
386    
387     """Tests for PostGISShapeStore objects with MULTILINESTRING data"""
388    
389     def setUp(self):
390     """Extend inherited method to set self.table to a PostGISShapeStore"""
391     PostGISStaticTests.setUp(self)
392     self.store = PostGISShapeStore(self.db, "roads")
393    
394     def test_shape_type(self):
395     """Test PostGISShapeStore.ShapeType() with ARC shapes"""
396     self.assertEquals(self.store.ShapeType(), SHAPETYPE_ARC)
397    
398     def test_num_shapes(self):
399     """Test PostGISShapeStore.NumShapes() with ARC shapes"""
400     self.assertEquals(self.store.NumShapes(), 839)
401    
402     def test_bounding_box(self):
403     """Test PostGISShapeStore.BoundingBox() with ARC shapes"""
404     self.assertFloatSeqEqual(self.store.BoundingBox(),
405     [-24.450359344482422, 63.426830291748047,
406     -13.55668830871582, 66.520111083984375])
407    
408     def test_shape_shapeid(self):
409     """Test PostGISShapeStore.Shape(i).ShapeID() with ARC shapes"""
410     self.assertEquals(self.store.Shape(5).ShapeID(), 5)
411    
412     def test_shape_points(self):
413     """Test PostGISShapeStore.Shape(i).Points() with ARC shapes"""
414     self.assertPointListEquals(self.store.Shape(32).Points(),
415     [[(-15.0821743011474, 66.2773818969726),
416     (-15.0263500213623, 66.2733917236328)]])
417    
418     def test_shape_raw_data(self):
419     """Test PostGISShapeStore.Shape(i).RawData() with ARC shapes"""
420     self.assertEquals(self.store.Shape(32).RawData(),
421     "MULTILINESTRING((-15.0821743011475 66.2773818969727,"
422     "-15.0263500213623 66.2733917236328))")
423    
424     def test_shapes_in_region(self):
425     """Test PostGISShapeStore.ShapesInRegion() with ARC shapes"""
426     shapes = self.store.ShapesInRegion((-24.0, 64.5, -23.5, 65.0))
427     self.assertEquals([s.ShapeID() for s in shapes], [573, 581, 613])
428    
429    
430    
431 bh 1656 class PolygonTests:
432 bh 1605
433 bh 1656 """Test shared by the PLYGON and MULTIPOLYGON tests
434 bh 1605
435 bh 1656 The tests are the same because they are based on the same data.
436     """
437    
438 bh 1605 def test_shape_type(self):
439     """Test PostGISShapeStore.ShapeType() with POLYGON shapes"""
440     self.assertEquals(self.store.ShapeType(), SHAPETYPE_POLYGON)
441    
442     def test_num_shapes(self):
443     """Test PostGISShapeStore.NumShapes() with POLYGON shapes"""
444     self.assertEquals(self.store.NumShapes(), 156)
445    
446     def test_bounding_box(self):
447     """Test PostGISShapeStore.BoundingBox() with POLYGON shapes"""
448     self.assertFloatSeqEqual(self.store.BoundingBox(),
449     [-24.546524047851562, 63.286754608154297,
450     -13.495815277099609, 66.563774108886719])
451    
452     def test_shape_shapeid(self):
453     """Test PostGISShapeStore.Shape(i).ShapeID() with POLYGON shapes"""
454     self.assertEquals(self.store.Shape(5).ShapeID(), 5)
455    
456     def test_shape_points(self):
457     """Test PostGISShapeStore.Shape(i).Points() with POLYGON shapes"""
458     self.assertPointListEquals(self.store.Shape(4).Points(),
459     [[(-22.40639114379882, 64.714111328125),
460     (-22.41621208190918, 64.716003417968),
461     (-22.40605163574218, 64.719200134277),
462     (-22.40639114379882, 64.714111328125)]])
463    
464 bh 1656 def test_shapes_in_region(self):
465     """Test PostGISShapeStore.ShapesInRegion() with POLYGON shapes"""
466     shapes = self.store.ShapesInRegion((-23.0, 65.5, -22.8, 65.25))
467     self.assertEquals([s.ShapeID() for s in shapes],
468     [47, 56, 59, 61, 62, 71, 144])
469    
470    
471     class TestPostGISShapestorePolygon(PolygonTests, PostGISStaticTests):
472    
473     """Tests for PostGISShapeStore objects with POLYGON data"""
474    
475     def setUp(self):
476     """Extend inherited method to set self.table to a PostGISShapeStore"""
477     PostGISStaticTests.setUp(self)
478     self.store = PostGISShapeStore(self.db, "political")
479    
480     def test_shape_type(self):
481     """Test PostGISShapeStore.ShapeType() with POLYGON shapes"""
482     self.assertEquals(self.store.ShapeType(), SHAPETYPE_POLYGON)
483    
484    
485 bh 1605 def test_shape_raw_data(self):
486     """Test PostGISShapeStore.Shape(i).RawData() with POLYGON shapes"""
487     self.assertEquals(self.store.Shape(4).RawData(),
488     "POLYGON((-22.4063911437988 64.714111328125,"
489     "-22.4162120819092 64.7160034179688,"
490     "-22.4060516357422 64.7192001342773,"
491     "-22.4063911437988 64.714111328125))")
492    
493    
494 bh 1656 class TestPostGISShapestoreMultiPolygon(PolygonTests, PostGISStaticTests):
495 bh 1605
496 bh 1656 """Tests for PostGISShapeStore objects with MUTLIPOLYGON data"""
497    
498     def setUp(self):
499     """Extend inherited method to set self.table to a PostGISShapeStore"""
500     PostGISStaticTests.setUp(self)
501     self.store = PostGISShapeStore(self.db, "political_multi")
502    
503     def test_shape_raw_data(self):
504     """Test PostGISShapeStore.Shape(i).RawData() with POLYGON shapes"""
505     self.assertEquals(self.store.Shape(4).RawData(),
506     "MULTIPOLYGON(((-22.4063911437988 64.714111328125,"
507     "-22.4162120819092 64.7160034179688,"
508     "-22.4060516357422 64.7192001342773,"
509     "-22.4063911437988 64.714111328125)))")
510    
511 bh 1693
512    
513 bh 1605 if __name__ == "__main__":
514     support.run_tests()

Properties

Name Value
svn:eol-style native
svn:keywords Author Date Id Revision

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26