/[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 1620 - (hide annotations)
Wed Aug 20 13:14:22 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: 15162 byte(s)
Add dialogs and commands to open database connections and add
database layers.

* Thuban/UI/mainwindow.py (MainWindow.DatabaseManagement): New
method to open the database connection management dialog
(MainWindow.AddDBLayer): New method to add a layer from a database
(_has_dbconnections): New helper function to use for sensitivity
(database_management command, layer_add_db command): New commands
that call the above new methods.
(main_menu): Add the new commands to the menu.

* Thuban/Model/postgisdb.py (PostGISConnection.__init__)
(PostGISConnection.connect): Establish the actual connection in a
separate method and call it in __init__. This makes it easier to
override the behavior in test cases
(PostGISConnection.BriefDescription): New method to return a brief
description for use in dialogs.

* test/test_postgis_db.py (NonConnection): DB connection that
doesn't actually connect
(TestBriefDescription): New class with tests for the new
BriefDescription method

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