/[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 1658 - (hide annotations)
Tue Aug 26 10:34:48 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: 17861 byte(s)
* Thuban/Model/postgisdb.py (PostGISTable.Title)
(PostGISShapeStore.AllShapes): Add these missing methods.
(PostGISShapeStore.ShapesInRegion): No need to raise
StopIteration. We can simply return

* test/test_postgis_db.py (TestPostGISTable.test_title)
(TestPostGISShapestorePoint.test_all_shapes): New tests for the
new methods

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 bh 1638 def test_dbconn(self):
156     """Test PostGISTable.DBConnection()"""
157     self.failUnless(self.table.DBConnection() is self.db)
158    
159     def test_dbname(self):
160     """Test PostGISTable.TableName()"""
161     self.assertEquals(self.table.TableName(), "landmarks")
162    
163 bh 1658 def test_title(self):
164     """test PostGISTable.Title()"""
165     # The title is currently equal to the tablename
166     self.assertEquals(self.table.Title(), "landmarks")
167    
168 bh 1605 def test_dependencies(self):
169     """Test PostGISTable.Dependencies()"""
170     # A PostGISTable depends on no other data container
171     self.assertEquals(self.table.Dependencies(), ())
172    
173     def test_num_rows(self):
174     """Test PostGISTable.NumRows()"""
175     self.assertEquals(self.table.NumRows(), 34)
176    
177     def test_num_columns(self):
178     """Test PostGISTable.NumColumns()"""
179     # The table in the postgis db has one additional column, "gid",
180     # so there's one more column in the PostGISTable than in the DBF
181     self.assertEquals(self.table.NumColumns(), 7)
182    
183     def test_columns(self):
184     """Test PostGISTable.Columns()"""
185     self.assertEquals(len(self.table.Columns()), 7)
186     self.assertEquals(self.table.Columns()[0].name, "gid")
187     self.assertEquals(self.table.Columns()[0].type, FIELDTYPE_INT)
188     self.assertEquals(self.table.Columns()[0].index, 0)
189     self.assertEquals(self.table.Columns()[1].name, "area")
190     self.assertEquals(self.table.Columns()[1].type, FIELDTYPE_DOUBLE)
191     self.assertEquals(self.table.Columns()[1].index, 1)
192     self.assertEquals(self.table.Columns()[5].name, "clptlabel")
193     self.assertEquals(self.table.Columns()[5].type, FIELDTYPE_STRING)
194     self.assertEquals(self.table.Columns()[5].index, 5)
195    
196     def test_column(self):
197     """Test PostGISTable.Column()"""
198     self.assertEquals(self.table.Column("area").name, "area")
199     self.assertEquals(self.table.Column("area").type, FIELDTYPE_DOUBLE)
200     self.assertEquals(self.table.Column("area").index, 1)
201    
202     def test_has_column(self):
203     """Test PostGISTable.HasColumn()"""
204     self.assert_(self.table.HasColumn("area"))
205     self.failIf(self.table.HasColumn("foo"))
206    
207     def test_read_row_as_dict(self):
208     """Test PostGISTable.ReadRowAsDict()"""
209     self.assertEquals(self.table.ReadRowAsDict(3),
210     {"gid": 3,
211     "area": 0.0,
212     "perimeter": 0.0,
213     "clpoint_": 4,
214     "clpoint_id": 24,
215     "clptlabel": "RUINS",
216     "clptflag": 0})
217    
218     def test_read_value(self):
219     """Test PostGISTable.ReadValue()"""
220     self.assertEquals(self.table.ReadValue(3, 4), 24)
221     self.assertEquals(self.table.ReadValue(3, "clpoint_id"), 24)
222    
223     def test_value_range(self):
224     """Test PostGISTable.ValueRange()"""
225     self.assertEquals(self.table.ValueRange("clpoint_id"), (21, 74))
226    
227     def test_unique_values(self):
228     """Test PostGISTable.UniqueValues()"""
229     values = self.table.UniqueValues("clptlabel")
230     values.sort()
231     self.assertEquals(values, ["BUILDING", "FARM", "HUT","LIGHTHOUSE",
232     "OTHER/UNKNOWN", "RUINS"])
233    
234     def test_simple_query(self):
235     """Test PostGISTable.SimpleQuery()"""
236     table = self.table
237     self.assertEquals(table.SimpleQuery(table.Column("clptlabel"),
238     "==", "FARM"),
239     [6])
240     self.assertEquals(table.SimpleQuery(table.Column("clpoint_id"),
241     ">", 70),
242     [24, 25, 26])
243     self.assertEquals(table.SimpleQuery(table.Column("clpoint_id"),
244     "<", table.Column("clpoint_")),
245     [28, 29, 30, 31, 32, 33])
246    
247    
248     class TestPostGISShapestorePoint(PostGISStaticTests):
249    
250     """Tests for PostGISShapeStore objects with POINT data"""
251    
252     def setUp(self):
253     """Extend inherited method to set self.table to a PostGISShapeStore"""
254     PostGISStaticTests.setUp(self)
255     self.store = PostGISShapeStore(self.db, "landmarks")
256    
257     #
258     # First, some tests that should be independend of the shapetype, so
259     # it shouldn't be necessary to repeat them for other shapetypes
260     #
261    
262     def test_dependencies(self):
263     """Test PostGISShapeStore.Dependencies()"""
264     # A PostGISShapeStore depends on no other data container
265     self.assertEquals(self.store.Dependencies(), ())
266    
267     def test_table(self):
268     """Test PostGISShapeStore.Table() with POINT shapes"""
269     # A PostGISShapeStore is its own table
270     self.assert_(self.store.Table() is self.store)
271    
272     def test_orig_shapestore(self):
273     """Test PostGISShapeStore.OrigShapeStore() with POINT shapes"""
274     # A PostGISShapeStore is not derived from another shape store
275     self.assert_(self.store.OrigShapeStore() is None)
276    
277     def test_raw_format(self):
278     """Test PostGISShapeStore.RawShapeFormat() with POINT shapes"""
279     self.assertEquals(self.store.RawShapeFormat(), RAW_WKT)
280    
281 bh 1658 def test_all_shapes(self):
282     """Test PostGISShapeStore.AllShapes()"""
283     self.assertEquals([s.ShapeID() for s in self.store.AllShapes()],
284     range(self.store.NumShapes()))
285    
286 bh 1605 #
287     # Shapetype specific tests
288     #
289    
290     def test_shape_type(self):
291     """Test PostGISShapeStore.ShapeType() with POINT shapes"""
292     self.assertEquals(self.store.ShapeType(), SHAPETYPE_POINT)
293    
294     def test_num_shapes(self):
295     """Test PostGISShapeStore.NumShapes() with POINT shapes"""
296     self.assertEquals(self.store.NumShapes(), 34)
297    
298     def test_bounding_box(self):
299     """Test PostGISShapeStore.BoundingBox() with POINT shapes"""
300     self.assertFloatSeqEqual(self.store.BoundingBox(),
301     [-23.806047439575195, 63.405960083007812,
302     -15.12291431427002, 66.36572265625])
303    
304     def test_shape_shapeid(self):
305     """Test PostGISShapeStore.Shape(i).ShapeID() with POINT shapes"""
306     self.assertEquals(self.store.Shape(5).ShapeID(), 5)
307    
308     def test_shape_points(self):
309     """Test PostGISShapeStore.Shape(i).Points() with POINT shapes"""
310     self.assertPointListEquals(self.store.Shape(0).Points(),
311     [[(-22.711074829101562, 66.36572265625)]])
312    
313     def test_shape_raw_data(self):
314     """Test PostGISShapeStore.Shape(i).RawData() with POINT shapes"""
315     self.assertEquals(self.store.Shape(0).RawData(),
316     'POINT(-22.7110748291016 66.36572265625)')
317    
318     def test_shapes_in_region(self):
319     """Test PostGISShapeStore:ShapesInRegion() with POINT shapes"""
320     shapes = self.store.ShapesInRegion((-20.0, 64.0, -24.0, 67))
321     self.assertEquals([s.ShapeID() for s in shapes],
322     [0, 1, 2, 3, 4, 5, 27])
323    
324    
325     class TestPostGISShapestoreArc(PostGISStaticTests):
326    
327     """Tests for PostGISShapeStore objects with MULTILINESTRING data"""
328    
329     def setUp(self):
330     """Extend inherited method to set self.table to a PostGISShapeStore"""
331     PostGISStaticTests.setUp(self)
332     self.store = PostGISShapeStore(self.db, "roads")
333    
334     def test_shape_type(self):
335     """Test PostGISShapeStore.ShapeType() with ARC shapes"""
336     self.assertEquals(self.store.ShapeType(), SHAPETYPE_ARC)
337    
338     def test_num_shapes(self):
339     """Test PostGISShapeStore.NumShapes() with ARC shapes"""
340     self.assertEquals(self.store.NumShapes(), 839)
341    
342     def test_bounding_box(self):
343     """Test PostGISShapeStore.BoundingBox() with ARC shapes"""
344     self.assertFloatSeqEqual(self.store.BoundingBox(),
345     [-24.450359344482422, 63.426830291748047,
346     -13.55668830871582, 66.520111083984375])
347    
348     def test_shape_shapeid(self):
349     """Test PostGISShapeStore.Shape(i).ShapeID() with ARC shapes"""
350     self.assertEquals(self.store.Shape(5).ShapeID(), 5)
351    
352     def test_shape_points(self):
353     """Test PostGISShapeStore.Shape(i).Points() with ARC shapes"""
354     self.assertPointListEquals(self.store.Shape(32).Points(),
355     [[(-15.0821743011474, 66.2773818969726),
356     (-15.0263500213623, 66.2733917236328)]])
357    
358     def test_shape_raw_data(self):
359     """Test PostGISShapeStore.Shape(i).RawData() with ARC shapes"""
360     self.assertEquals(self.store.Shape(32).RawData(),
361     "MULTILINESTRING((-15.0821743011475 66.2773818969727,"
362     "-15.0263500213623 66.2733917236328))")
363    
364     def test_shapes_in_region(self):
365     """Test PostGISShapeStore.ShapesInRegion() with ARC shapes"""
366     shapes = self.store.ShapesInRegion((-24.0, 64.5, -23.5, 65.0))
367     self.assertEquals([s.ShapeID() for s in shapes], [573, 581, 613])
368    
369    
370    
371 bh 1656 class PolygonTests:
372 bh 1605
373 bh 1656 """Test shared by the PLYGON and MULTIPOLYGON tests
374 bh 1605
375 bh 1656 The tests are the same because they are based on the same data.
376     """
377    
378 bh 1605 def test_shape_type(self):
379     """Test PostGISShapeStore.ShapeType() with POLYGON shapes"""
380     self.assertEquals(self.store.ShapeType(), SHAPETYPE_POLYGON)
381    
382     def test_num_shapes(self):
383     """Test PostGISShapeStore.NumShapes() with POLYGON shapes"""
384     self.assertEquals(self.store.NumShapes(), 156)
385    
386     def test_bounding_box(self):
387     """Test PostGISShapeStore.BoundingBox() with POLYGON shapes"""
388     self.assertFloatSeqEqual(self.store.BoundingBox(),
389     [-24.546524047851562, 63.286754608154297,
390     -13.495815277099609, 66.563774108886719])
391    
392     def test_shape_shapeid(self):
393     """Test PostGISShapeStore.Shape(i).ShapeID() with POLYGON shapes"""
394     self.assertEquals(self.store.Shape(5).ShapeID(), 5)
395    
396     def test_shape_points(self):
397     """Test PostGISShapeStore.Shape(i).Points() with POLYGON shapes"""
398     self.assertPointListEquals(self.store.Shape(4).Points(),
399     [[(-22.40639114379882, 64.714111328125),
400     (-22.41621208190918, 64.716003417968),
401     (-22.40605163574218, 64.719200134277),
402     (-22.40639114379882, 64.714111328125)]])
403    
404 bh 1656 def test_shapes_in_region(self):
405     """Test PostGISShapeStore.ShapesInRegion() with POLYGON shapes"""
406     shapes = self.store.ShapesInRegion((-23.0, 65.5, -22.8, 65.25))
407     self.assertEquals([s.ShapeID() for s in shapes],
408     [47, 56, 59, 61, 62, 71, 144])
409    
410    
411     class TestPostGISShapestorePolygon(PolygonTests, PostGISStaticTests):
412    
413     """Tests for PostGISShapeStore objects with POLYGON data"""
414    
415     def setUp(self):
416     """Extend inherited method to set self.table to a PostGISShapeStore"""
417     PostGISStaticTests.setUp(self)
418     self.store = PostGISShapeStore(self.db, "political")
419    
420     def test_shape_type(self):
421     """Test PostGISShapeStore.ShapeType() with POLYGON shapes"""
422     self.assertEquals(self.store.ShapeType(), SHAPETYPE_POLYGON)
423    
424    
425 bh 1605 def test_shape_raw_data(self):
426     """Test PostGISShapeStore.Shape(i).RawData() with POLYGON shapes"""
427     self.assertEquals(self.store.Shape(4).RawData(),
428     "POLYGON((-22.4063911437988 64.714111328125,"
429     "-22.4162120819092 64.7160034179688,"
430     "-22.4060516357422 64.7192001342773,"
431     "-22.4063911437988 64.714111328125))")
432    
433    
434 bh 1656 class TestPostGISShapestoreMultiPolygon(PolygonTests, PostGISStaticTests):
435 bh 1605
436 bh 1656 """Tests for PostGISShapeStore objects with MUTLIPOLYGON data"""
437    
438     def setUp(self):
439     """Extend inherited method to set self.table to a PostGISShapeStore"""
440     PostGISStaticTests.setUp(self)
441     self.store = PostGISShapeStore(self.db, "political_multi")
442    
443     def test_shape_raw_data(self):
444     """Test PostGISShapeStore.Shape(i).RawData() with POLYGON shapes"""
445     self.assertEquals(self.store.Shape(4).RawData(),
446     "MULTIPOLYGON(((-22.4063911437988 64.714111328125,"
447     "-22.4162120819092 64.7160034179688,"
448     "-22.4060516357422 64.7192001342773,"
449     "-22.4063911437988 64.714111328125)))")
450    
451 bh 1605 if __name__ == "__main__":
452     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