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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1039 - (hide annotations)
Mon May 26 17:31:24 2003 UTC (21 years, 9 months ago) by bh
Original Path: trunk/thuban/test/test_session.py
File MIME type: text/x-python
File size: 13286 byte(s)
(TestSessionSimple.test_open_table_file):
New. test case for OpenTableFile

1 bh 723 # Copyright (c) 2002, 2003 by Intevation GmbH
2 bh 334 # Authors:
3     # Bernhard Herzog <[email protected]>
4     #
5     # This program is free software under the GPL (>=v2)
6     # Read the file COPYING coming with Thuban for details.
7    
8     """
9     Test the Session class
10     """
11    
12     __version__ = "$Revision$"
13     # $Source$
14     # $Id$
15    
16     import os
17     import unittest
18    
19     import support
20     support.initthuban()
21    
22     from Thuban.Model.messages import CHANGED, MAPS_CHANGED, FILENAME_CHANGED, \
23 jonathan 584 MAP_PROJECTION_CHANGED, MAP_LAYERS_CHANGED, \
24     LAYER_VISIBILITY_CHANGED, LAYER_CHANGED
25 bh 334 from Thuban.Model.session import Session
26     from Thuban.Model.map import Map
27     from Thuban.Model.layer import Layer
28     from Thuban.Model.proj import Projection
29     from Thuban.Model.color import Color
30 bh 982 from Thuban.Model.table import MemoryTable, FIELDTYPE_STRING, \
31     FIELDTYPE_INT, FIELDTYPE_DOUBLE
32 bh 1016 from Thuban.Model.data import DerivedShapeStore
33 bh 334
34 bh 1016
35 bh 334 class TestSessionSimple(unittest.TestCase):
36    
37     """Very simple test cases for Session"""
38    
39 bh 984 def setUp(self):
40     """Initialize self.session to None"""
41     self.session = None
42    
43     def tearDown(self):
44     """Call self.session.Destroy() and reset self.session to None"""
45     self.session.Destroy()
46     self.session = None
47    
48 bh 334 def test_initial_state(self):
49     """Test Session's initial state"""
50 bh 984 session = self.session = Session("Test Session")
51 bh 334 self.assertEquals(session.Title(), "Test Session")
52     self.assertEquals(session.Maps(), [])
53 bh 851 self.assertEquals(session.Tables(), [])
54     self.assertEquals(session.ShapeStores(), [])
55 bh 334 self.assertEquals(session.filename, None)
56     self.failIf(session.HasMaps())
57     self.failIf(session.WasModified())
58    
59 bh 982 def test_add_table(self):
60     """Test Session.AddTable()"""
61 bh 984 session = self.session = Session("Test Session")
62 bh 982 memtable = MemoryTable([("type", FIELDTYPE_STRING),
63     ("value", FIELDTYPE_DOUBLE),
64     ("code", FIELDTYPE_INT)],
65     [("OTHER/UNKNOWN", -1.5, 11),
66     ("RUINS", 0.0, 1),
67     ("FARM", 3.141, 2),
68     ("BUILDING", 2.5, 3),
69     ("HUT", 1e6, 4),
70     ("LIGHTHOUSE", -0.01, 5)])
71     table = session.AddTable(memtable)
72     self.assertEquals(session.Tables(), [table])
73 bh 334
74 bh 1039 def test_open_table_file(self):
75     """Test Session.OpenTableFile()"""
76     session = self.session = Session("Test Session")
77     filename = os.path.join("..", "Data", "iceland",
78     "roads-line.dbf")
79     table = session.OpenTableFile(filename)
80     self.assertEquals(session.Tables(), [table])
81    
82 bh 987 def test_remove_table(self):
83     """Test Session.RemoveTable()"""
84     session = self.session = Session("Test Session")
85     memtable = MemoryTable([("type", FIELDTYPE_STRING),
86     ("value", FIELDTYPE_DOUBLE),
87     ("code", FIELDTYPE_INT)],
88     [("OTHER/UNKNOWN", -1.5, 11),
89     ("RUINS", 0.0, 1),
90     ("FARM", 3.141, 2),
91     ("BUILDING", 2.5, 3),
92     ("HUT", 1e6, 4),
93     ("LIGHTHOUSE", -0.01, 5)])
94     table = session.AddTable(memtable)
95     self.assertEquals(session.Tables(), [table])
96     session.RemoveTable(table)
97     self.assertEquals(session.Tables(), [])
98     self.assertRaises(ValueError, session.RemoveTable, table)
99    
100 bh 984 def test_open_shapefile(self):
101     """Test Session.OpenShapefile()"""
102     session = self.session = Session("Test Session")
103     filename = os.path.join("..", "Data", "iceland",
104     "roads-line.shp")
105     store = session.OpenShapefile(filename)
106     self.assertEquals(store.FileName(), os.path.abspath(filename))
107     # The filetype of a shapefile is "shapefile"
108     self.assertEquals(store.FileType(), "shapefile")
109     # The shapestore itself depends on nothing else
110     self.assertEquals(store.Dependencies(), ())
111     # The shapestore's table depends on the shapestore
112     self.assertEquals(store.Table().Dependencies(), (store,))
113 bh 982
114 bh 984 self.assertEquals(session.Tables(), [store.Table()])
115    
116 bh 1016 def test_add_shapestore(self):
117     """Test Session.AddShapeStore()"""
118     session = self.session = Session("Test Session")
119     filename = os.path.join("..", "Data", "iceland",
120     "roads-line.shp")
121     try:
122     store = session.OpenShapefile(filename)
123     derived = DerivedShapeStore(store, store.Table())
124     session.AddShapeStore(derived)
125     self.assertEquals(session.ShapeStores(), [store, derived])
126     finally:
127     store = derived = None
128 bh 984
129 bh 1016
130 bh 334 class TestSessionBase(unittest.TestCase, support.SubscriberMixin):
131    
132     """Base class for Session test cases that test the messages"""
133    
134     def setUp(self):
135     """
136     Clear the message list, create a session and subscribe to its messages
137    
138     Bind the session to self.session.
139     """
140     self.clear_messages()
141    
142     # Create a Session and subscribe to all interesting channels.
143     self.session = Session("Test Session")
144 jonathan 584 for channel in (CHANGED,
145     MAPS_CHANGED,
146     FILENAME_CHANGED,
147     MAP_PROJECTION_CHANGED,
148     MAP_LAYERS_CHANGED,
149     LAYER_VISIBILITY_CHANGED,
150     LAYER_CHANGED):
151 bh 334 self.session.Subscribe(channel,
152     self.subscribe_with_params, channel)
153    
154     def tearDown(self):
155     """Destroy self.session and clear the message list"""
156     self.session.Destroy()
157 bh 723 self.session = None
158 bh 334 self.clear_messages()
159    
160    
161     class TestSessionMessages(TestSessionBase):
162    
163     """Simple Session test cases that test messges"""
164    
165     def test_add_map(self):
166     """Test Session.AddMap"""
167     self.failIf(self.session.WasModified())
168     map = Map("Some Map")
169     self.session.AddMap(map)
170     self.assert_(self.session.HasMaps())
171     self.assert_(self.session.WasModified())
172     self.assertEquals(self.session.Maps(), [map])
173     self.check_messages([(MAPS_CHANGED,),
174     (self.session, CHANGED)])
175    
176     def test_set_filename(self):
177     """Test Session.SetFilename"""
178     self.session.SetFilename("session_set_filename_test")
179     self.session.filename = "session_set_filename_test"
180     self.check_messages([(FILENAME_CHANGED,),
181     (self.session, CHANGED)])
182    
183    
184     class TestSessionWithContent(TestSessionBase):
185    
186     """Session test cases that start with a filled session."""
187    
188     def setUp(self):
189     """Extend the inherited method to add a non-empty map to self.session
190     """
191     TestSessionBase.setUp(self)
192 bh 723 open_shp = self.session.OpenShapefile
193 bh 334 self.arc_layer = Layer("Roads",
194 bh 723 open_shp(os.path.join("..", "Data", "iceland",
195     "roads-line.shp")))
196 bh 334 self.poly_layer = Layer("Political",
197 bh 723 open_shp(os.path.join("..", "Data", "iceland",
198     "political.shp")))
199 bh 334 self.map = Map("A Map")
200     self.map.AddLayer(self.arc_layer)
201     self.map.AddLayer(self.poly_layer)
202     self.session.AddMap(self.map)
203     self.session.UnsetModified()
204     self.clear_messages()
205    
206 bh 723 def tearDown(self):
207     TestSessionBase.tearDown(self)
208     self.arc_layer = self.poly_layer = None
209    
210 bh 334 def test_remove_map(self):
211     """Test Session.RemoveMap"""
212     self.session.RemoveMap(self.map)
213     self.assert_(self.session.WasModified())
214     self.failIf(self.session.HasMaps())
215     self.check_messages([(MAPS_CHANGED,),
216     (self.session, CHANGED)])
217    
218     def test_tree_info(self):
219     """Test Session.TreeInfo"""
220     self.assertEquals(self.session.TreeInfo(),
221     ('Session: Test Session',
222     ['Filename:',
223     'Unmodified',
224     self.map]))
225    
226     def test_forward_map_projection(self):
227     """Test Session forwarding of Map.SetProjection messages"""
228     proj = Projection(["zone=26", "proj=utm", "ellps=clrk66"])
229     self.map.SetProjection(proj)
230     self.check_messages([(self.map, MAP_PROJECTION_CHANGED),
231     (self.session, CHANGED)])
232     self.assert_(self.session.WasModified())
233    
234     def test_forward_map_projection(self):
235     """Test Session forwarding of Map.SetProjection messages"""
236     proj = Projection(["zone=26", "proj=utm", "ellps=clrk66"])
237     self.map.SetProjection(proj)
238     self.assert_(self.session.WasModified())
239     self.check_messages([(self.map, MAP_PROJECTION_CHANGED),
240     (self.session, CHANGED)])
241    
242     def test_forwarding_fill(self):
243     """Test Session's forwarding of Layer.SetFill messages"""
244 jonathan 409 self.poly_layer.GetClassification().SetDefaultFill(Color(0.0, 0.5, 1.0))
245 bh 334 self.assert_(self.session.WasModified())
246 jonathan 584 self.check_messages([(self.poly_layer, LAYER_CHANGED),
247 bh 334 (self.session, CHANGED)])
248    
249     def test_forwarding_stroke(self):
250     """Test Session's forwarding of Layer.SetStroke messages"""
251 jonathan 409 self.poly_layer.GetClassification().\
252 jonathan 482 SetDefaultLineColor(Color(0.0, 0.5, 1.0))
253 bh 334 self.assert_(self.session.WasModified())
254 jonathan 584 self.check_messages([(self.poly_layer, LAYER_CHANGED),
255 bh 334 (self.session, CHANGED)])
256    
257     def test_forwarding_stroke_width(self):
258     """Test Session's forwarding of Layer.SetStrokeWidth messages"""
259 jonathan 482 self.poly_layer.GetClassification().SetDefaultLineWidth(3)
260 bh 334 self.assert_(self.session.WasModified())
261 jonathan 584 self.check_messages([(self.poly_layer, LAYER_CHANGED),
262 bh 334 (self.session, CHANGED)])
263    
264     def test_forwarding_visibility(self):
265     """Test Session's forwarding of Layer.SetVisible messages"""
266     self.poly_layer.SetVisible(0)
267     # Currently changing the visibility of a layer doesn't change
268     # the modification flag.
269     self.failIf(self.session.WasModified())
270     self.check_messages([(self.poly_layer, LAYER_VISIBILITY_CHANGED),
271     (self.session, CHANGED)])
272    
273     def test_unset_modified_map(self):
274     """Test Session.UnsetModified with map level changes"""
275     self.failIf(self.session.WasModified())
276     proj = Projection(["zone=26", "proj=utm", "ellps=clrk66"])
277     self.map.SetProjection(proj)
278     self.assert_(self.session.WasModified())
279     self.session.UnsetModified()
280     self.failIf(self.session.WasModified())
281    
282     def test_unset_modified_layer(self):
283     """Test Session.UnsetModified with layer level changes"""
284     self.failIf(self.session.WasModified())
285 jonathan 482 self.poly_layer.GetClassification().SetDefaultLineWidth(3)
286 bh 334 self.assert_(self.session.WasModified())
287     self.session.UnsetModified()
288     self.failIf(self.session.WasModified())
289 jonathan 584 self.check_messages([(self.poly_layer, LAYER_CHANGED),
290 bh 334 (self.session, CHANGED),
291     (CHANGED,)])
292    
293 bh 851 def test_shape_stores(self):
294     """Test Session.ShapeStores()"""
295     # Strictly speaking the session doesn't make guarantees about
296     # the order of the ShapeStores in the list, but currently it's
297     # deterministic and they're listed in the order in which they
298     # were created
299     self.assertEquals(self.session.ShapeStores(),
300     [self.arc_layer.ShapeStore(),
301     self.poly_layer.ShapeStore()])
302     # If we remove the map from the session and clear our instance
303     # variables that hold the layers and the map the list should
304     # become empty again.
305     self.session.RemoveMap(self.map)
306     self.arc_layer = self.poly_layer = self.map = None
307     self.assertEquals(self.session.ShapeStores(), [])
308 bh 334
309 bh 851 def test_tables(self):
310     """Test Session.Tables()"""
311     # Strictly speaking the session doesn't make guarantees about
312     # the order of the tables in the list, but currently it's
313     # deterministic and they're listed in the order in which they
314     # were opened
315     self.assertEquals(self.session.Tables(),
316     [self.arc_layer.ShapeStore().Table(),
317     self.poly_layer.ShapeStore().Table()])
318     # If we remove the map from the session and clear our instance
319     # variables that hold the layers and the map the list should
320     # become empty again.
321     self.session.RemoveMap(self.map)
322     self.arc_layer = self.poly_layer = self.map = None
323     self.assertEquals(self.session.Tables(), [])
324    
325    
326 bh 334 if __name__ == "__main__":
327     unittest.main()

Properties

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26