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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1257 - (hide annotations)
Fri Jun 20 12:22:25 2003 UTC (21 years, 8 months ago) by bh
Original Path: trunk/thuban/test/test_load.py
File MIME type: text/x-python
File size: 15695 byte(s)
* test/test_load.py (LoadSessionTest.check_format): New helper
method to make sure the test files we load might have been written
by the current thuban version.
(ClassificationTest.TestLayers, TestSingleLayer.test)
(TestLayerVisibility.test, TestClassification.test)
(TestLabels.test, TestLayerProjection.test, TestRasterLayer.test):
Add check_format() calls and fix the thuban data to match the data
that would be written by saving the session loaded from it.

* test/xmlsupport.py (SaxEventLister, sax_eventlist): Copies of
the same class and function in test_save.

* test/test_xmlsupport.py (TestEventList): New. test cases for
sax_eventlist

1 bh 765 # Copyright (c) 2002, 2003 by Intevation GmbH
2 bh 292 # 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 loading a thuban session from a file
10 bh 1247
11     The tests in this file (test_load.py) are always be the tests for the
12     current version of the thuban file format. Tests for older versions can
13     be found in the version specific test modules, e.g. test_load_0_2 for
14     files created by Thuban 0.2.
15    
16     Maintenance of the test cases:
17    
18     When during a development period the file format is changed with respect
19     to the last released version for the first, the tests here should be
20 bh 1257 copied to the version specific test file. The round-trip tests which
21     save the session again and compare the XML files should not be copied
22     over as they only make sense here to make sure th that the files checked
23     here are actually ones that may have been written by the current thuban
24     version.
25 bh 292 """
26    
27     __version__ = "$Revision$"
28     # $Source$
29     # $Id$
30    
31     import os
32     import unittest
33    
34     import support
35     support.initthuban()
36    
37 bh 1257 from xmlsupport import sax_eventlist
38     from Thuban.Model.save import save_session
39 jonathan 684 from Thuban.Model.load import load_session, parse_color
40 jonathan 409 from Thuban.Model.color import Color
41 jonathan 684 from Thuban.Model.classification import ClassGroupProperties, ClassGroupRange,\
42     ClassGroupSingleton, ClassGroupDefault
43    
44 bh 957
45 bh 292 def filenames_equal(name1, name2):
46     """Return true if the filenames name1 and name2 are equal.
47    
48     On systems where it is available, simply use os.path.samefile,
49     otherwise return whether the normalized versions of the filenames
50     according to os.path.normpath are equal.
51     """
52     if hasattr(os.path, "samefile"):
53     return os.path.samefile(name1, name2)
54     return os.path.normpath(name1) == os.path.normpath(name2)
55    
56    
57 bh 957
58     class LoadSessionTest(support.FileLoadTestCase):
59    
60     """Base class for .thuban file loading tests
61    
62     Basically the same as the FileLoadTestCase, except that all tests
63     use the '.thuban' extension by default and that setUp and tearDown
64     handle sessions.
65     """
66    
67     file_extension = ".thuban"
68    
69     def setUp(self):
70     """Create the test files"""
71     support.FileLoadTestCase.setUp(self)
72     self.session = None
73    
74     def tearDown(self):
75     if self.session is not None:
76     self.session.Destroy()
77     self.session = None
78    
79 bh 1257 def check_format(self):
80     """Check whether the file we loaded from matches the one that
81     would be written. Call this from each test case after loading
82     the session
83     """
84     filename = self.temp_file_name(self.id() + ".roundtrip.thuban")
85     save_session(self.session, filename)
86     el1 = sax_eventlist(filename = filename)
87     el2 = sax_eventlist(filename = self.filename())
88     self.assertEquals(el1, el2)
89 bh 957
90 bh 1257
91 bh 957 class ClassificationTest(LoadSessionTest):
92    
93     """
94     Base class for tests that do some detailed checking of classifications
95     """
96    
97     def TestLayers(self, layers, expected):
98     TITLE = 0
99     NUM_GROUPS = 1
100     CLASSES = 2
101     GROUP_TYPE = 0
102     GROUP_DATA = 1
103     GROUP_LABEL = 2
104     GROUP_PROPS = 3
105    
106     eq = self.assertEquals
107    
108     eq(len(layers), len(expected))
109    
110     for layer, data in zip(layers, expected):
111     eq(layer.Title(), data[TITLE])
112    
113     clazz = layer.GetClassification()
114     eq(clazz.GetNumGroups(), data[NUM_GROUPS])
115     eq(clazz.GetNumGroups() + 1, len(data[CLASSES]))
116    
117     i = 0
118     for group in clazz:
119     props = ClassGroupProperties()
120     props.SetLineColor(
121     parse_color(data[CLASSES][i][GROUP_PROPS][0]))
122     props.SetLineWidth(data[CLASSES][i][GROUP_PROPS][1])
123     props.SetFill(
124     parse_color(data[CLASSES][i][GROUP_PROPS][2]))
125    
126     if data[CLASSES][i][GROUP_TYPE] == "default":
127     g = ClassGroupDefault(props, data[CLASSES][i][GROUP_LABEL])
128     elif data[CLASSES][i][GROUP_TYPE] == "range":
129     g = ClassGroupRange(data[CLASSES][i][GROUP_DATA][0],
130     data[CLASSES][i][GROUP_DATA][1],
131     props, data[CLASSES][i][GROUP_LABEL])
132     elif data[CLASSES][i][GROUP_TYPE] == "single":
133     g = ClassGroupSingleton(data[CLASSES][i][GROUP_DATA],
134     props, data[CLASSES][i][GROUP_LABEL])
135    
136     eq(group, g)
137    
138     i += 1
139    
140    
141    
142     class TestSingleLayer(LoadSessionTest):
143    
144     file_contents = '''\
145 bh 292 <?xml version="1.0" encoding="UTF-8"?>
146     <!DOCTYPE session SYSTEM "thuban.dtd">
147     <session title="single map&amp;layer">
148     <map title="Test Map">
149 bh 1257 <projection name="Unknown">
150 bh 292 <parameter value="zone=26"/>
151     <parameter value="proj=utm"/>
152     <parameter value="ellps=clrk66"/>
153     </projection>
154     <layer title="My Layer" stroke_width="1" fill="None"
155     filename="../../Data/iceland/political.shp"
156 bh 1257 stroke="#000000" visible="true"/>
157 bh 292 </map>
158     </session>
159     '''
160    
161 bh 957 def test(self):
162     """Load a session with a single map with a single layer"""
163     eq = self.assertEquals
164     session = load_session(self.filename())
165     self.session = session
166    
167     # Check the title
168     eq(session.Title(), "single map&layer")
169    
170     # the session has one map.
171     maps = session.Maps()
172     eq(len(maps), 1)
173    
174     # Check the map's attributes
175     map = maps[0]
176     eq(map.Title(), "Test Map")
177    
178     # the map has a single layer
179     layers = map.Layers()
180     eq(len(layers), 1)
181    
182     # Check the layer attributes
183     layer = layers[0]
184     eq(layer.Title(), "My Layer")
185 bh 1219 self.failUnless(filenames_equal(layer.ShapeStore().FileName(),
186 bh 957 os.path.join(self.temp_dir(),
187     os.pardir, os.pardir,
188     "Data", "iceland",
189     "political.shp")))
190     eq(layer.GetClassification().GetDefaultFill(), Color.Transparent)
191     eq(layer.GetClassification().GetDefaultLineColor().hex(), "#000000")
192     eq(layer.Visible(), True)
193    
194 bh 1257 self.check_format()
195    
196 bh 957 self.session.Destroy()
197     self.session = None
198    
199    
200     class TestLayerVisibility(LoadSessionTest):
201    
202     file_contents = '''\
203 jonathan 690 <?xml version="1.0" encoding="UTF-8"?>
204     <!DOCTYPE session SYSTEM "thuban.dtd">
205     <session title="single map&amp;layer">
206     <map title="Test Map">
207 bh 1257 <projection name="Unknown">
208 jonathan 690 <parameter value="zone=26"/>
209     <parameter value="proj=utm"/>
210     <parameter value="ellps=clrk66"/>
211     </projection>
212     <layer title="My Layer" stroke_width="1" fill="None"
213     filename="../../Data/iceland/political.shp"
214 bh 957 stroke="#000000" visible="false">
215     </layer>
216     </map>
217     </session>
218     '''
219    
220     def test(self):
221     """Test that the visible flag is correctly loaded for a layer."""
222     eq = self.assertEquals
223     session = load_session(self.filename())
224     self.session = session
225     maps = session.Maps()
226     eq(len(maps), 1)
227     map = maps[0]
228     layers = map.Layers()
229     eq(len(layers), 1)
230     layer = layers[0]
231    
232     eq(layer.Visible(), False)
233    
234 bh 1257 self.check_format()
235 bh 957
236    
237     class TestClassification(ClassificationTest):
238    
239     file_contents = '''\
240     <?xml version="1.0" encoding="UTF-8"?>
241     <!DOCTYPE session SYSTEM "thuban.dtd">
242     <session title="single map&amp;layer">
243     <map title="Test Map">
244     <projection>
245     <parameter value="zone=26"/>
246     <parameter value="proj=utm"/>
247     <parameter value="ellps=clrk66"/>
248     </projection>
249     <layer title="My Layer" stroke_width="1" fill="None"
250     filename="../../Data/iceland/political.shp"
251 jonathan 690 stroke="#000000">
252     <classification field="POPYREG" field_type="string">
253     <clnull>
254     <cldata stroke="#000000" stroke_width="1" fill="None"/>
255     </clnull>
256     <clpoint value="1">
257     <cldata stroke="#000000" stroke_width="2" fill="None"/>
258     </clpoint>
259     <clpoint value="1">
260     <cldata stroke="#000000" stroke_width="10" fill="None"/>
261     </clpoint>
262     </classification>
263     </layer>
264     <layer title="My Layer 2" stroke_width="1" fill="None"
265     filename="../../Data/iceland/political.shp"
266     stroke="#000000">
267     <classification field="AREA" field_type="double">
268     <clnull>
269     <cldata stroke="#000000" stroke_width="2" fill="None"/>
270     </clnull>
271     <clrange min="0" max="1">
272 jonathan 692 <cldata stroke="#111111" stroke_width="1" fill="None"/>
273 jonathan 690 </clrange>
274     <clpoint value=".5">
275 jonathan 692 <cldata stroke="#000000" stroke_width="1" fill="#111111"/>
276 jonathan 690 </clpoint>
277     <clrange min="-1" max="0">
278     <cldata stroke="#000000" stroke_width="1" fill="None"/>
279     </clrange>
280     <clpoint value="-.5">
281     <cldata stroke="#000000" stroke_width="1" fill="None"/>
282     </clpoint>
283     </classification>
284     </layer>
285     </map>
286     </session>
287     '''
288 bh 292
289 bh 957 def test(self):
290 bh 1247 """Load a Thuban session with a map and classified layers."""
291 bh 957 session = load_session(self.filename())
292     self.session = session
293    
294     map = self.session.Maps()[0] # only one map in the sample
295    
296     expected = [("My Layer", 2,
297     [("default", (), "",
298     ("#000000", 1, "None")),
299     ("single", "1", "",
300     ("#000000", 2, "None")),
301     ("single", "1", "",
302     ("#000000", 10, "None"))]),
303     ("My Layer 2", 4,
304     [("default", (), "",
305     ("#000000", 2, "None")),
306     ("range", (0, 1), "",
307     ("#111111", 1, "None")),
308     ("single", .5, "",
309     ("#000000", 1, "#111111")),
310     ("range", (-1, 0), "",
311     ("#000000", 1, "None")),
312     ("single", -.5, "",
313     ("#000000", 1, "None"))])]
314    
315     self.TestLayers(map.Layers(), expected)
316    
317    
318     class TestLabels(ClassificationTest):
319    
320     file_contents = '''\
321 jonathan 690 <?xml version="1.0" encoding="UTF-8"?>
322     <!DOCTYPE session SYSTEM "thuban.dtd">
323     <session title="single map&amp;layer">
324     <map title="Test Map">
325 bh 1257 <projection name="Unknown">
326 jonathan 690 <parameter value="zone=26"/>
327     <parameter value="proj=utm"/>
328     <parameter value="ellps=clrk66"/>
329     </projection>
330     <layer title="My Layer" stroke_width="1" fill="None"
331     filename="../../Data/iceland/political.shp"
332 bh 1257 stroke="#000000" visible="true">
333 jonathan 690 <classification field="POPYREG" field_type="string">
334     <clnull label="hallo">
335     <cldata stroke="#000000" stroke_width="1" fill="None"/>
336     </clnull>
337     <clpoint label="welt" value="1">
338     <cldata stroke="#000000" stroke_width="2" fill="None"/>
339     </clpoint>
340     </classification>
341     </layer>
342     </map>
343     </session>
344     '''
345    
346 bh 957 def test(self):
347     """Load a session and test for reading the group labels."""
348     eq = self.assertEquals
349     session = load_session(self.filename())
350     self.session = session
351    
352     map = self.session.Maps()[0] # only one map in the sample
353    
354     expected = [("My Layer", 1,
355     [("default", (), "hallo",
356     ("#000000", 1, "None")),
357     ("single", "1", "welt",
358     ("#000000", 2, "None"))])]
359    
360     self.TestLayers(map.Layers(), expected)
361 bh 1257 self.check_format()
362 bh 957
363    
364     class TestLayerProjection(LoadSessionTest):
365    
366     file_contents = '''\
367 jonathan 746 <?xml version="1.0" encoding="UTF-8"?>
368     <!DOCTYPE session SYSTEM "thuban.dtd">
369     <session title="single map&amp;layer">
370     <map title="Test Map">
371 bh 1257 <projection name="Unknown">
372 jonathan 746 <parameter value="zone=26"/>
373     <parameter value="proj=utm"/>
374     <parameter value="ellps=clrk66"/>
375     </projection>
376     <layer title="My Layer" stroke_width="1" fill="None"
377     filename="../../Data/iceland/political.shp"
378 bh 1257 stroke="#000000" visible="true">
379 jonathan 746 <projection name="hello">
380     <parameter value="zone=13"/>
381     <parameter value="proj=tmerc"/>
382     <parameter value="ellps=clrk66"/>
383     </projection>
384     <classification field="POPYREG" field_type="string">
385     <clnull label="hallo">
386     <cldata stroke="#000000" stroke_width="1" fill="None"/>
387     </clnull>
388     <clpoint label="welt" value="1">
389     <cldata stroke="#000000" stroke_width="2" fill="None"/>
390     </clpoint>
391     </classification>
392     </layer>
393     <layer title="My Layer" stroke_width="1" fill="None"
394     filename="../../Data/iceland/political.shp"
395 bh 1257 stroke="#000000" visible="true">
396     <projection name="Unknown">
397 jonathan 746 <parameter value="proj=lcc"/>
398     <parameter value="ellps=clrk66"/>
399     </projection>
400     </layer>
401     </map>
402     </session>
403     '''
404    
405 bh 957 def test(self):
406     """Test loading layers with projections"""
407 bh 292 eq = self.assertEquals
408 jonathan 746 neq = self.assertNotEqual
409    
410 bh 957 session = load_session(self.filename())
411 jonathan 746 self.session = session
412    
413     map = self.session.Maps()[0] # only one map in the sample
414    
415     layers = map.Layers() # two layers in the sample
416    
417     # test layer with a named projection
418     proj = layers[0].GetProjection()
419     neq(proj, None)
420     eq(proj.GetName(), "hello")
421     eq(proj.GetParameter("proj"), "tmerc")
422     eq(proj.GetParameter("zone"), "13")
423     eq(proj.GetParameter("ellps"), "clrk66")
424    
425     # test layer with an unnamed projection
426     proj = layers[1].GetProjection()
427     neq(proj, None)
428     eq(proj.GetName(), "Unknown")
429     eq(proj.GetParameter("proj"), "lcc")
430     eq(proj.GetParameter("ellps"), "clrk66")
431    
432 bh 1257 self.check_format()
433 bh 957
434 bh 1257
435 bh 957 class TestRasterLayer(LoadSessionTest):
436    
437     file_contents = '''\
438     <?xml version="1.0" encoding="UTF-8"?>
439     <!DOCTYPE session SYSTEM "thuban.dtd">
440     <session title="single map&amp;layer">
441     <map title="Test Map">
442     <rasterlayer title="My RasterLayer"
443     filename="../../Data/iceland/island.tif"
444     visible="false">
445     </rasterlayer>
446     </map>
447     </session>
448     '''
449    
450     def test(self):
451 jonathan 947 eq = self.assertEquals
452     neq = self.assertNotEqual
453    
454 bh 957 session = load_session(self.filename())
455 jonathan 947 self.session = session
456    
457     map = self.session.Maps()[0] # only one map in the sample
458    
459     layer = map.Layers()[0] # one layer in the sample
460    
461     eq(layer.Title(), "My RasterLayer")
462     self.failIf(layer.Visible())
463     self.failUnless(filenames_equal(layer.GetImageFilename(),
464     os.path.join(self.temp_dir(),
465     os.pardir, os.pardir,
466     "Data", "iceland",
467     "island.tif")))
468 bh 1257 self.check_format()
469 jonathan 947
470 bh 292 if __name__ == "__main__":
471     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