/[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 987 - (hide annotations)
Thu May 22 16:46:14 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: 12356 byte(s)
* Thuban/Model/session.py (Session.RemoveTable): New method to
remove tables

* test/test_session.py (TestSessionSimple.test_remove_table): New.
Test for RemoveTable

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