/[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 982 - (hide annotations)
Thu May 22 12:02:15 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: 10450 byte(s)
* Thuban/Model/session.py (Session.AddTable): New method to
register tables with the session.
(Session.Tables): Return the tables registered with AddTable too.

* test/test_session.py (TestSessionSimple.test_add_table): New.
Test case for the AddTable method

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