/[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 1016 - (hide annotations)
Fri May 23 11:05:59 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: 12941 byte(s)
* Thuban/Model/session.py (Session.AddShapeStore): Define
AddShapeStore analogously to AddTable.

* test/test_session.py (TestSessionSimple.test_add_shapestore):
New. Test for AddShapeStore

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 987 def test_remove_table(self):
75     """Test Session.RemoveTable()"""
76     session = self.session = Session("Test Session")
77     memtable = MemoryTable([("type", FIELDTYPE_STRING),
78     ("value", FIELDTYPE_DOUBLE),
79     ("code", FIELDTYPE_INT)],
80     [("OTHER/UNKNOWN", -1.5, 11),
81     ("RUINS", 0.0, 1),
82     ("FARM", 3.141, 2),
83     ("BUILDING", 2.5, 3),
84     ("HUT", 1e6, 4),
85     ("LIGHTHOUSE", -0.01, 5)])
86     table = session.AddTable(memtable)
87     self.assertEquals(session.Tables(), [table])
88     session.RemoveTable(table)
89     self.assertEquals(session.Tables(), [])
90     self.assertRaises(ValueError, session.RemoveTable, table)
91    
92 bh 984 def test_open_shapefile(self):
93     """Test Session.OpenShapefile()"""
94     session = self.session = Session("Test Session")
95     filename = os.path.join("..", "Data", "iceland",
96     "roads-line.shp")
97     store = session.OpenShapefile(filename)
98     self.assertEquals(store.FileName(), os.path.abspath(filename))
99     # The filetype of a shapefile is "shapefile"
100     self.assertEquals(store.FileType(), "shapefile")
101     # The shapestore itself depends on nothing else
102     self.assertEquals(store.Dependencies(), ())
103     # The shapestore's table depends on the shapestore
104     self.assertEquals(store.Table().Dependencies(), (store,))
105 bh 982
106 bh 984 self.assertEquals(session.Tables(), [store.Table()])
107    
108 bh 1016 def test_add_shapestore(self):
109     """Test Session.AddShapeStore()"""
110     session = self.session = Session("Test Session")
111     filename = os.path.join("..", "Data", "iceland",
112     "roads-line.shp")
113     try:
114     store = session.OpenShapefile(filename)
115     derived = DerivedShapeStore(store, store.Table())
116     session.AddShapeStore(derived)
117     self.assertEquals(session.ShapeStores(), [store, derived])
118     finally:
119     store = derived = None
120 bh 984
121 bh 1016
122 bh 334 class TestSessionBase(unittest.TestCase, support.SubscriberMixin):
123    
124     """Base class for Session test cases that test the messages"""
125    
126     def setUp(self):
127     """
128     Clear the message list, create a session and subscribe to its messages
129    
130     Bind the session to self.session.
131     """
132     self.clear_messages()
133    
134     # Create a Session and subscribe to all interesting channels.
135     self.session = Session("Test Session")
136 jonathan 584 for channel in (CHANGED,
137     MAPS_CHANGED,
138     FILENAME_CHANGED,
139     MAP_PROJECTION_CHANGED,
140     MAP_LAYERS_CHANGED,
141     LAYER_VISIBILITY_CHANGED,
142     LAYER_CHANGED):
143 bh 334 self.session.Subscribe(channel,
144     self.subscribe_with_params, channel)
145    
146     def tearDown(self):
147     """Destroy self.session and clear the message list"""
148     self.session.Destroy()
149 bh 723 self.session = None
150 bh 334 self.clear_messages()
151    
152    
153     class TestSessionMessages(TestSessionBase):
154    
155     """Simple Session test cases that test messges"""
156    
157     def test_add_map(self):
158     """Test Session.AddMap"""
159     self.failIf(self.session.WasModified())
160     map = Map("Some Map")
161     self.session.AddMap(map)
162     self.assert_(self.session.HasMaps())
163     self.assert_(self.session.WasModified())
164     self.assertEquals(self.session.Maps(), [map])
165     self.check_messages([(MAPS_CHANGED,),
166     (self.session, CHANGED)])
167    
168     def test_set_filename(self):
169     """Test Session.SetFilename"""
170     self.session.SetFilename("session_set_filename_test")
171     self.session.filename = "session_set_filename_test"
172     self.check_messages([(FILENAME_CHANGED,),
173     (self.session, CHANGED)])
174    
175    
176     class TestSessionWithContent(TestSessionBase):
177    
178     """Session test cases that start with a filled session."""
179    
180     def setUp(self):
181     """Extend the inherited method to add a non-empty map to self.session
182     """
183     TestSessionBase.setUp(self)
184 bh 723 open_shp = self.session.OpenShapefile
185 bh 334 self.arc_layer = Layer("Roads",
186 bh 723 open_shp(os.path.join("..", "Data", "iceland",
187     "roads-line.shp")))
188 bh 334 self.poly_layer = Layer("Political",
189 bh 723 open_shp(os.path.join("..", "Data", "iceland",
190     "political.shp")))
191 bh 334 self.map = Map("A Map")
192     self.map.AddLayer(self.arc_layer)
193     self.map.AddLayer(self.poly_layer)
194     self.session.AddMap(self.map)
195     self.session.UnsetModified()
196     self.clear_messages()
197    
198 bh 723 def tearDown(self):
199     TestSessionBase.tearDown(self)
200     self.arc_layer = self.poly_layer = None
201    
202 bh 334 def test_remove_map(self):
203     """Test Session.RemoveMap"""
204     self.session.RemoveMap(self.map)
205     self.assert_(self.session.WasModified())
206     self.failIf(self.session.HasMaps())
207     self.check_messages([(MAPS_CHANGED,),
208     (self.session, CHANGED)])
209    
210     def test_tree_info(self):
211     """Test Session.TreeInfo"""
212     self.assertEquals(self.session.TreeInfo(),
213     ('Session: Test Session',
214     ['Filename:',
215     'Unmodified',
216     self.map]))
217    
218     def test_forward_map_projection(self):
219     """Test Session forwarding of Map.SetProjection messages"""
220     proj = Projection(["zone=26", "proj=utm", "ellps=clrk66"])
221     self.map.SetProjection(proj)
222     self.check_messages([(self.map, MAP_PROJECTION_CHANGED),
223     (self.session, CHANGED)])
224     self.assert_(self.session.WasModified())
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.assert_(self.session.WasModified())
231     self.check_messages([(self.map, MAP_PROJECTION_CHANGED),
232     (self.session, CHANGED)])
233    
234     def test_forwarding_fill(self):
235     """Test Session's forwarding of Layer.SetFill messages"""
236 jonathan 409 self.poly_layer.GetClassification().SetDefaultFill(Color(0.0, 0.5, 1.0))
237 bh 334 self.assert_(self.session.WasModified())
238 jonathan 584 self.check_messages([(self.poly_layer, LAYER_CHANGED),
239 bh 334 (self.session, CHANGED)])
240    
241     def test_forwarding_stroke(self):
242     """Test Session's forwarding of Layer.SetStroke messages"""
243 jonathan 409 self.poly_layer.GetClassification().\
244 jonathan 482 SetDefaultLineColor(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_width(self):
250     """Test Session's forwarding of Layer.SetStrokeWidth messages"""
251 jonathan 482 self.poly_layer.GetClassification().SetDefaultLineWidth(3)
252 bh 334 self.assert_(self.session.WasModified())
253 jonathan 584 self.check_messages([(self.poly_layer, LAYER_CHANGED),
254 bh 334 (self.session, CHANGED)])
255    
256     def test_forwarding_visibility(self):
257     """Test Session's forwarding of Layer.SetVisible messages"""
258     self.poly_layer.SetVisible(0)
259     # Currently changing the visibility of a layer doesn't change
260     # the modification flag.
261     self.failIf(self.session.WasModified())
262     self.check_messages([(self.poly_layer, LAYER_VISIBILITY_CHANGED),
263     (self.session, CHANGED)])
264    
265     def test_unset_modified_map(self):
266     """Test Session.UnsetModified with map level changes"""
267     self.failIf(self.session.WasModified())
268     proj = Projection(["zone=26", "proj=utm", "ellps=clrk66"])
269     self.map.SetProjection(proj)
270     self.assert_(self.session.WasModified())
271     self.session.UnsetModified()
272     self.failIf(self.session.WasModified())
273    
274     def test_unset_modified_layer(self):
275     """Test Session.UnsetModified with layer level changes"""
276     self.failIf(self.session.WasModified())
277 jonathan 482 self.poly_layer.GetClassification().SetDefaultLineWidth(3)
278 bh 334 self.assert_(self.session.WasModified())
279     self.session.UnsetModified()
280     self.failIf(self.session.WasModified())
281 jonathan 584 self.check_messages([(self.poly_layer, LAYER_CHANGED),
282 bh 334 (self.session, CHANGED),
283     (CHANGED,)])
284    
285 bh 851 def test_shape_stores(self):
286     """Test Session.ShapeStores()"""
287     # Strictly speaking the session doesn't make guarantees about
288     # the order of the ShapeStores in the list, but currently it's
289     # deterministic and they're listed in the order in which they
290     # were created
291     self.assertEquals(self.session.ShapeStores(),
292     [self.arc_layer.ShapeStore(),
293     self.poly_layer.ShapeStore()])
294     # If we remove the map from the session and clear our instance
295     # variables that hold the layers and the map the list should
296     # become empty again.
297     self.session.RemoveMap(self.map)
298     self.arc_layer = self.poly_layer = self.map = None
299     self.assertEquals(self.session.ShapeStores(), [])
300 bh 334
301 bh 851 def test_tables(self):
302     """Test Session.Tables()"""
303     # Strictly speaking the session doesn't make guarantees about
304     # the order of the tables in the list, but currently it's
305     # deterministic and they're listed in the order in which they
306     # were opened
307     self.assertEquals(self.session.Tables(),
308     [self.arc_layer.ShapeStore().Table(),
309     self.poly_layer.ShapeStore().Table()])
310     # If we remove the map from the session and clear our instance
311     # variables that hold the layers and the map the list should
312     # become empty again.
313     self.session.RemoveMap(self.map)
314     self.arc_layer = self.poly_layer = self.map = None
315     self.assertEquals(self.session.Tables(), [])
316    
317    
318 bh 334 if __name__ == "__main__":
319     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