/[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 1605 - (hide annotations)
Tue Aug 19 11:00:40 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: 14409 byte(s)
Add very basic postgis database support and the corresponding test
cases. The test cases require a PostgreSQL + postgis installation
but no existing database. The database will be created
automatically by the test cases

* test/README: Add note about skipped tests and the requirements
of the postgis tests.

* Thuban/Model/postgisdb.py: New. Basic postgis database support.

* test/test_postgis_db.py: New. Test cases for the postgis
support.

* Thuban/Model/wellknowntext.py: New. Parser for well-known-text
format

* test/test_wellknowntext.py: New. Test cases for the
wellknowntext parser

* test/postgissupport.py: New. Support module for tests involving
a postgis database.

* test/support.py (execute_as_testsuite): Shut down the postmaster
if it's still running after the tests

* Thuban/Model/data.py (RAW_WKT): New constant for raw data in
well known text format

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