/[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 1636 - (hide annotations)
Fri Aug 22 17:20:29 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: 16016 byte(s)
* Thuban/Model/postgisdb.py (ConnectionError): New exception class
for exceptions occurring when establishing a Database connection
(PostGISConnection.connect): Catch psycopg.OperationalError during
connects and raise ConnectionError.

* test/test_postgis_db.py (TestPostgisDBExceptions): New class for
tests for database exceptions

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 1605 class PostGISStaticTests(unittest.TestCase, support.FloatComparisonMixin):
123    
124     """Base class for PostGIS tests with static data."""
125    
126     def setUp(self):
127     """Start the server and create a database with static data
128    
129     This method sets the following instance attributes:
130    
131     dbname -- the name of the database
132    
133     server -- The server object
134    
135     db -- the PostGISConnection object
136     """
137     postgissupport.skip_if_no_postgis()
138     self.server = postgissupport.get_test_server()
139     self.postgisdb = self.server.get_default_static_data_db()
140     self.db = PostGISConnection(dbname = self.postgisdb.dbname,
141 bh 1634 **self.server.connection_params("user"))
142 bh 1605
143     def tearDown(self):
144     """Close the database connection"""
145     self.db.Close()
146    
147    
148     class TestPostGISTable(PostGISStaticTests):
149    
150     def setUp(self):
151     """Extend inherited method to set self.table to a PostGISTable"""
152     PostGISStaticTests.setUp(self)
153     self.table = PostGISTable(self.db, "landmarks")
154    
155     def test_dependencies(self):
156     """Test PostGISTable.Dependencies()"""
157     # A PostGISTable depends on no other data container
158     self.assertEquals(self.table.Dependencies(), ())
159    
160     def test_num_rows(self):
161     """Test PostGISTable.NumRows()"""
162     self.assertEquals(self.table.NumRows(), 34)
163    
164     def test_num_columns(self):
165     """Test PostGISTable.NumColumns()"""
166     # The table in the postgis db has one additional column, "gid",
167     # so there's one more column in the PostGISTable than in the DBF
168     self.assertEquals(self.table.NumColumns(), 7)
169    
170     def test_columns(self):
171     """Test PostGISTable.Columns()"""
172     self.assertEquals(len(self.table.Columns()), 7)
173     self.assertEquals(self.table.Columns()[0].name, "gid")
174     self.assertEquals(self.table.Columns()[0].type, FIELDTYPE_INT)
175     self.assertEquals(self.table.Columns()[0].index, 0)
176     self.assertEquals(self.table.Columns()[1].name, "area")
177     self.assertEquals(self.table.Columns()[1].type, FIELDTYPE_DOUBLE)
178     self.assertEquals(self.table.Columns()[1].index, 1)
179     self.assertEquals(self.table.Columns()[5].name, "clptlabel")
180     self.assertEquals(self.table.Columns()[5].type, FIELDTYPE_STRING)
181     self.assertEquals(self.table.Columns()[5].index, 5)
182    
183     def test_column(self):
184     """Test PostGISTable.Column()"""
185     self.assertEquals(self.table.Column("area").name, "area")
186     self.assertEquals(self.table.Column("area").type, FIELDTYPE_DOUBLE)
187     self.assertEquals(self.table.Column("area").index, 1)
188    
189     def test_has_column(self):
190     """Test PostGISTable.HasColumn()"""
191     self.assert_(self.table.HasColumn("area"))
192     self.failIf(self.table.HasColumn("foo"))
193    
194     def test_read_row_as_dict(self):
195     """Test PostGISTable.ReadRowAsDict()"""
196     self.assertEquals(self.table.ReadRowAsDict(3),
197     {"gid": 3,
198     "area": 0.0,
199     "perimeter": 0.0,
200     "clpoint_": 4,
201     "clpoint_id": 24,
202     "clptlabel": "RUINS",
203     "clptflag": 0})
204    
205     def test_read_value(self):
206     """Test PostGISTable.ReadValue()"""
207     self.assertEquals(self.table.ReadValue(3, 4), 24)
208     self.assertEquals(self.table.ReadValue(3, "clpoint_id"), 24)
209    
210     def test_value_range(self):
211     """Test PostGISTable.ValueRange()"""
212     self.assertEquals(self.table.ValueRange("clpoint_id"), (21, 74))
213    
214     def test_unique_values(self):
215     """Test PostGISTable.UniqueValues()"""
216     values = self.table.UniqueValues("clptlabel")
217     values.sort()
218     self.assertEquals(values, ["BUILDING", "FARM", "HUT","LIGHTHOUSE",
219     "OTHER/UNKNOWN", "RUINS"])
220    
221     def test_simple_query(self):
222     """Test PostGISTable.SimpleQuery()"""
223     table = self.table
224     self.assertEquals(table.SimpleQuery(table.Column("clptlabel"),
225     "==", "FARM"),
226     [6])
227     self.assertEquals(table.SimpleQuery(table.Column("clpoint_id"),
228     ">", 70),
229     [24, 25, 26])
230     self.assertEquals(table.SimpleQuery(table.Column("clpoint_id"),
231     "<", table.Column("clpoint_")),
232     [28, 29, 30, 31, 32, 33])
233    
234    
235     class TestPostGISShapestorePoint(PostGISStaticTests):
236    
237     """Tests for PostGISShapeStore objects with POINT data"""
238    
239     def setUp(self):
240     """Extend inherited method to set self.table to a PostGISShapeStore"""
241     PostGISStaticTests.setUp(self)
242     self.store = PostGISShapeStore(self.db, "landmarks")
243    
244     #
245     # First, some tests that should be independend of the shapetype, so
246     # it shouldn't be necessary to repeat them for other shapetypes
247     #
248    
249     def test_dependencies(self):
250     """Test PostGISShapeStore.Dependencies()"""
251     # A PostGISShapeStore depends on no other data container
252     self.assertEquals(self.store.Dependencies(), ())
253    
254     def test_table(self):
255     """Test PostGISShapeStore.Table() with POINT shapes"""
256     # A PostGISShapeStore is its own table
257     self.assert_(self.store.Table() is self.store)
258    
259     def test_orig_shapestore(self):
260     """Test PostGISShapeStore.OrigShapeStore() with POINT shapes"""
261     # A PostGISShapeStore is not derived from another shape store
262     self.assert_(self.store.OrigShapeStore() is None)
263    
264     def test_raw_format(self):
265     """Test PostGISShapeStore.RawShapeFormat() with POINT shapes"""
266     self.assertEquals(self.store.RawShapeFormat(), RAW_WKT)
267    
268     #
269     # Shapetype specific tests
270     #
271    
272     def test_shape_type(self):
273     """Test PostGISShapeStore.ShapeType() with POINT shapes"""
274     self.assertEquals(self.store.ShapeType(), SHAPETYPE_POINT)
275    
276     def test_num_shapes(self):
277     """Test PostGISShapeStore.NumShapes() with POINT shapes"""
278     self.assertEquals(self.store.NumShapes(), 34)
279    
280     def test_bounding_box(self):
281     """Test PostGISShapeStore.BoundingBox() with POINT shapes"""
282     self.assertFloatSeqEqual(self.store.BoundingBox(),
283     [-23.806047439575195, 63.405960083007812,
284     -15.12291431427002, 66.36572265625])
285    
286     def test_shape_shapeid(self):
287     """Test PostGISShapeStore.Shape(i).ShapeID() with POINT shapes"""
288     self.assertEquals(self.store.Shape(5).ShapeID(), 5)
289    
290     def test_shape_points(self):
291     """Test PostGISShapeStore.Shape(i).Points() with POINT shapes"""
292     self.assertPointListEquals(self.store.Shape(0).Points(),
293     [[(-22.711074829101562, 66.36572265625)]])
294    
295     def test_shape_raw_data(self):
296     """Test PostGISShapeStore.Shape(i).RawData() with POINT shapes"""
297     self.assertEquals(self.store.Shape(0).RawData(),
298     'POINT(-22.7110748291016 66.36572265625)')
299    
300     def test_shapes_in_region(self):
301     """Test PostGISShapeStore:ShapesInRegion() with POINT shapes"""
302     shapes = self.store.ShapesInRegion((-20.0, 64.0, -24.0, 67))
303     self.assertEquals([s.ShapeID() for s in shapes],
304     [0, 1, 2, 3, 4, 5, 27])
305    
306    
307     class TestPostGISShapestoreArc(PostGISStaticTests):
308    
309     """Tests for PostGISShapeStore objects with MULTILINESTRING data"""
310    
311     def setUp(self):
312     """Extend inherited method to set self.table to a PostGISShapeStore"""
313     PostGISStaticTests.setUp(self)
314     self.store = PostGISShapeStore(self.db, "roads")
315    
316     def test_shape_type(self):
317     """Test PostGISShapeStore.ShapeType() with ARC shapes"""
318     self.assertEquals(self.store.ShapeType(), SHAPETYPE_ARC)
319    
320     def test_num_shapes(self):
321     """Test PostGISShapeStore.NumShapes() with ARC shapes"""
322     self.assertEquals(self.store.NumShapes(), 839)
323    
324     def test_bounding_box(self):
325     """Test PostGISShapeStore.BoundingBox() with ARC shapes"""
326     self.assertFloatSeqEqual(self.store.BoundingBox(),
327     [-24.450359344482422, 63.426830291748047,
328     -13.55668830871582, 66.520111083984375])
329    
330     def test_shape_shapeid(self):
331     """Test PostGISShapeStore.Shape(i).ShapeID() with ARC shapes"""
332     self.assertEquals(self.store.Shape(5).ShapeID(), 5)
333    
334     def test_shape_points(self):
335     """Test PostGISShapeStore.Shape(i).Points() with ARC shapes"""
336     self.assertPointListEquals(self.store.Shape(32).Points(),
337     [[(-15.0821743011474, 66.2773818969726),
338     (-15.0263500213623, 66.2733917236328)]])
339    
340     def test_shape_raw_data(self):
341     """Test PostGISShapeStore.Shape(i).RawData() with ARC shapes"""
342     self.assertEquals(self.store.Shape(32).RawData(),
343     "MULTILINESTRING((-15.0821743011475 66.2773818969727,"
344     "-15.0263500213623 66.2733917236328))")
345    
346     def test_shapes_in_region(self):
347     """Test PostGISShapeStore.ShapesInRegion() with ARC shapes"""
348     shapes = self.store.ShapesInRegion((-24.0, 64.5, -23.5, 65.0))
349     self.assertEquals([s.ShapeID() for s in shapes], [573, 581, 613])
350    
351    
352     class TestPostGISShapestorePolygon(PostGISStaticTests):
353    
354     """Tests for PostGISShapeStore objects with POLYGON data"""
355    
356     def setUp(self):
357     """Extend inherited method to set self.table to a PostGISShapeStore"""
358     PostGISStaticTests.setUp(self)
359     self.store = PostGISShapeStore(self.db, "political")
360    
361     def test_shape_type(self):
362     """Test PostGISShapeStore.ShapeType() with POLYGON shapes"""
363     self.assertEquals(self.store.ShapeType(), SHAPETYPE_POLYGON)
364    
365     def test_num_shapes(self):
366     """Test PostGISShapeStore.NumShapes() with POLYGON shapes"""
367     self.assertEquals(self.store.NumShapes(), 156)
368    
369     def test_bounding_box(self):
370     """Test PostGISShapeStore.BoundingBox() with POLYGON shapes"""
371     self.assertFloatSeqEqual(self.store.BoundingBox(),
372     [-24.546524047851562, 63.286754608154297,
373     -13.495815277099609, 66.563774108886719])
374    
375     def test_shape_shapeid(self):
376     """Test PostGISShapeStore.Shape(i).ShapeID() with POLYGON shapes"""
377     self.assertEquals(self.store.Shape(5).ShapeID(), 5)
378    
379     def test_shape_points(self):
380     """Test PostGISShapeStore.Shape(i).Points() with POLYGON shapes"""
381     self.assertPointListEquals(self.store.Shape(4).Points(),
382     [[(-22.40639114379882, 64.714111328125),
383     (-22.41621208190918, 64.716003417968),
384     (-22.40605163574218, 64.719200134277),
385     (-22.40639114379882, 64.714111328125)]])
386    
387     def test_shape_raw_data(self):
388     """Test PostGISShapeStore.Shape(i).RawData() with POLYGON shapes"""
389     self.assertEquals(self.store.Shape(4).RawData(),
390     "POLYGON((-22.4063911437988 64.714111328125,"
391     "-22.4162120819092 64.7160034179688,"
392     "-22.4060516357422 64.7192001342773,"
393     "-22.4063911437988 64.714111328125))")
394    
395     def test_shapes_in_region(self):
396     """Test PostGISShapeStore.ShapesInRegion() with POLYGON shapes"""
397     shapes = self.store.ShapesInRegion((-23.0, 65.5, -22.8, 65.25))
398     self.assertEquals([s.ShapeID() for s in shapes],
399     [47, 56, 59, 61, 62, 71, 144])
400    
401    
402     if __name__ == "__main__":
403     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