/[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 984 - (hide annotations)
Thu May 22 16:37:48 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: 11474 byte(s)
Implement a way to discover dependencies between tables and
shapestores.

* Thuban/Model/transientdb.py (TransientTableBase.Dependencies)
(TransientJoinedTable.Dependencies)
(AutoTransientTable.SimpleQuery): New. Implement the dependencies
interface
(TransientJoinedTable.__init__): Keep tack of the original table
objects in addition to the corresponding transient tables.

* Thuban/Model/table.py (DBFTable.Dependencies)
(MemoryTable.Dependencies): New. Implement the dependencies
interface

* Thuban/Model/data.py (ShapeTable): New. Helper class for
ShapefileStore
(ShapefileStore.__init__): Use ShapeTable instead of
AutoTransientTable
(ShapefileStore.Table, ShapefileStore.Shapefile): Add doc-strings
(ShapefileStore.FileName, ShapefileStore.FileType): New. Accessor
methods for filename and type
(ShapefileStore.Dependencies): New. Implement the dependencies
interface
(DerivedShapeStore): New class to replace SimpleStore. The main
difference to SimpleStore is that it depends not on a shapefile
but another shapestore which expresses the dependencies a bit
better
(SimpleStore.__init__): Add deprecation warning.

* test/test_dbf_table.py (TestDBFTable.test_dependencies): New.
Test for the Dependencies method.

* test/test_memory_table.py (TestMemoryTable.test_dependencies):
New. Test for the Dependencies method.

* test/test_transientdb.py
(TestTransientTable.test_auto_transient_table_dependencies): New.
Test for the Dependencies method.
(TestTransientTable.test_transient_joined_table): Add test for the
Dependencies method.

* test/test_session.py (TestSessionSimple.setUp)
(TestSessionSimple.tearDown): New. Implement a better way to
destroy the sessions.
(TestSessionSimple.test_initial_state)
(TestSessionSimple.test_add_table): Bind session to self.session
so that it's destroyed by tearDown
(TestSessionSimple.test_open_shapefile): New. Test for
OpenShapefile and the object it returns

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