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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1259 - (hide annotations)
Fri Jun 20 12:31:48 2003 UTC (21 years, 8 months ago) by bh
Original Path: trunk/thuban/test/test_save.py
File MIME type: text/x-python
File size: 12197 byte(s)
* test/test_save.py (SaxEventLister, sax_eventlist): Removed. Use
the implementation in xmlsupport instead.
(SaveSessionTest.compare_xml): sax_eventlist is now in xmlsupport

* test/test_proj.py: Import sax_eventlist from xmlsupport instead
of test_save

1 bh 723 # 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 saving a thuban session as XML
10     """
11    
12     __version__ = "$Revision$"
13     # $Source$
14     # $Id$
15    
16     import os
17     import unittest
18     from StringIO import StringIO
19    
20     import xml.sax
21     import xml.sax.handler
22 jonathan 530 from xml.sax import make_parser, ErrorHandler, SAXNotRecognizedException
23 bh 292
24 bh 1245 import xmlsupport
25    
26 bh 292 import support
27     support.initthuban()
28    
29 jonathan 1168 from Thuban.Model.save import XMLWriter, save_session
30 bh 292 from Thuban.Model.session import Session
31     from Thuban.Model.map import Map
32 jonathan 947 from Thuban.Model.layer import Layer, RasterLayer
33 bh 292 from Thuban.Model.proj import Projection
34    
35 jonathan 1168 from Thuban.Model.classification import ClassGroupSingleton, ClassGroupRange, \
36     ClassGroupProperties
37 bh 292
38 jonathan 1168 from Thuban.Model.range import Range
39    
40    
41     class XMLWriterTest(unittest.TestCase):
42    
43     def testEncode(self):
44     """Test XMLWriter.encode"""
45     writer = XMLWriter()
46 jonathan 1200 eq = self.assertEquals
47 jonathan 1168
48 jonathan 1200 eq(writer.encode("hello world"), "hello world")
49     eq(writer.encode(unicode("hello world")), unicode("hello world"))
50 jonathan 1173
51 jonathan 1200 eq(writer.encode("\x80\x90\xc2\x100"),
52     "\xc2\x80\xc2\x90\xc3\x82\x100")
53     eq(writer.encode(u"\x80\x90\xc2\x100"),
54     "\xc2\x80\xc2\x90\xc3\x82\x100")
55     eq(writer.encode(u"\xFF5E"), "\xc3\xbf5E")
56 jonathan 1173
57 jonathan 1200 eq(writer.encode('&"\'<>'), "&amp;&quot;&apos;&lt;&gt;")
58     eq(writer.encode(unicode('&"\'<>')), "&amp;&quot;&apos;&lt;&gt;")
59 jonathan 1168
60 bh 1245 class SaveSessionTest(unittest.TestCase, support.FileTestMixin,
61     xmlsupport.ValidationTest):
62 bh 292
63     def compare_xml(self, xml1, xml2):
64 bh 1259 self.assertEquals(xmlsupport.sax_eventlist(xml1),
65     xmlsupport.sax_eventlist(xml2))
66 bh 292
67     def testEmptySession(self):
68     """Save an empty session"""
69     session = Session("empty session")
70     filename = self.temp_file_name("save_emptysession.thuban")
71     save_session(session, filename)
72     session.Destroy()
73    
74     file = open(filename)
75     written_contents = file.read()
76     file.close()
77     self.compare_xml(written_contents,
78     '<?xml version="1.0" encoding="UTF-8"?>\n'
79     '<!DOCTYPE session SYSTEM "thuban.dtd">\n'
80     '<session title="empty session">\n</session>\n')
81    
82 bh 1245 self.validate_data(written_contents)
83    
84 bh 292 def testSingleLayer(self):
85     """Save a session with a single map with a single layer"""
86     # deliberately put an apersand in the title :)
87     session = Session("single map&layer")
88     proj = Projection(["zone=26", "proj=utm", "ellps=clrk66"])
89     map = Map("Test Map", projection = proj)
90     session.AddMap(map)
91     # use shapefile from the example data
92     shpfile = os.path.join(os.path.dirname(__file__),
93     os.pardir, "Data", "iceland", "political.shp")
94 bh 723 layer = Layer("My Layer", session.OpenShapefile(shpfile))
95 bh 292 map.AddLayer(layer)
96    
97     filename = self.temp_file_name("save_singlemap.thuban")
98     save_session(session, filename)
99    
100     file = open(filename)
101     written_contents = file.read()
102     file.close()
103 jonathan 775 expected_template = '''<?xml version="1.0" encoding="UTF-8"?>
104 bh 292 <!DOCTYPE session SYSTEM "thuban.dtd">
105     <session title="single map&amp;layer">
106     <map title="Test Map">
107 jonathan 755 <projection name="Unknown">
108 bh 292 <parameter value="zone=26"/>
109     <parameter value="proj=utm"/>
110     <parameter value="ellps=clrk66"/>
111     </projection>
112     <layer title="My Layer" filename="%s"
113 jonathan 775 fill="None" stroke="#000000" stroke_width="1" visible="%s"/>
114 bh 292 </map>
115 jonathan 775 </session>'''
116    
117     expected_contents = expected_template % \
118     (os.path.join("..", "..", "Data", "iceland", "political.shp"),
119     "true")
120    
121 jonathan 494 #print written_contents
122     #print "********************************************"
123     #print expected_contents
124 bh 292 self.compare_xml(written_contents, expected_contents)
125    
126 bh 1245 self.validate_data(written_contents)
127    
128 jonathan 775 layer.SetVisible(False)
129     save_session(session, filename)
130    
131     file = open(filename)
132     written_contents = file.read()
133     file.close()
134     expected_contents = expected_template % \
135     (os.path.join("..", "..", "Data", "iceland", "political.shp"),
136     "false")
137    
138     #print written_contents
139     #print "********************************************"
140     #print expected_contents
141     self.compare_xml(written_contents, expected_contents)
142 bh 1245 self.validate_data(written_contents)
143 jonathan 775
144     session.Destroy()
145    
146 jonathan 755 def testLayerProjection(self):
147     # deliberately put an apersand in the title :)
148     session = Session("single map&layer")
149     proj = Projection(["zone=26", "proj=utm", "ellps=clrk66"])
150     map = Map("Test Map", projection = proj)
151     session.AddMap(map)
152     # use shapefile from the example data
153     shpfile = os.path.join(os.path.dirname(__file__),
154     os.pardir, "Data", "iceland", "political.shp")
155     layer = Layer("My Layer", session.OpenShapefile(shpfile))
156     proj = Projection(["proj=lcc", "ellps=clrk66"], "Layer Projection")
157     layer.SetProjection(proj)
158     map.AddLayer(layer)
159 bh 292
160 jonathan 755 filename = self.temp_file_name("save_singlemap.thuban")
161     save_session(session, filename)
162     session.Destroy()
163 bh 292
164 jonathan 755 file = open(filename)
165     written_contents = file.read()
166     file.close()
167     expected_contents = '''<?xml version="1.0" encoding="UTF-8"?>
168     <!DOCTYPE session SYSTEM "thuban.dtd">
169     <session title="single map&amp;layer">
170     <map title="Test Map">
171     <projection name="Unknown">
172     <parameter value="zone=26"/>
173     <parameter value="proj=utm"/>
174     <parameter value="ellps=clrk66"/>
175     </projection>
176     <layer title="My Layer" filename="%s"
177 jonathan 775 fill="None" stroke="#000000" stroke_width="1" visible="true">
178 jonathan 755 <projection name="Layer Projection">
179     <parameter value="proj=lcc"/>
180     <parameter value="ellps=clrk66"/>
181     </projection>
182     </layer>
183     </map>
184     </session>''' % os.path.join("..", "..", "Data", "iceland",
185     "political.shp")
186     #print written_contents
187     #print "********************************************"
188     #print expected_contents
189     self.compare_xml(written_contents, expected_contents)
190    
191 bh 1245 self.validate_data(written_contents)
192    
193 jonathan 947 def testRasterLayer(self):
194     # deliberately put an apersand in the title :)
195     session = Session("single map&layer")
196     map = Map("Test Map")
197     session.AddMap(map)
198     # use shapefile from the example data
199     imgfile = os.path.join(os.path.dirname(__file__),
200     os.pardir, "Data", "iceland", "island.tif")
201     layer = RasterLayer("My RasterLayer", imgfile)
202     map.AddLayer(layer)
203 bh 1245
204 jonathan 947 filename = self.temp_file_name("save_singlemap.thuban")
205     save_session(session, filename)
206     session.Destroy()
207 bh 1245
208 jonathan 947 file = open(filename)
209     written_contents = file.read()
210     file.close()
211     expected_contents = '''<?xml version="1.0" encoding="UTF-8"?>
212     <!DOCTYPE session SYSTEM "thuban.dtd">
213     <session title="single map&amp;layer">
214     <map title="Test Map">
215     <rasterlayer title="My RasterLayer" filename="%s"
216     visible="true">
217     </rasterlayer>
218     </map>
219     </session>''' % os.path.join(os.path.dirname(__file__),
220     os.pardir, "Data", "iceland",
221     "island.tif")
222     #print written_contents
223     #print "********************************************"
224     #print expected_contents
225     self.compare_xml(written_contents, expected_contents)
226 jonathan 755
227 bh 1245 self.validate_data(written_contents)
228    
229 jonathan 1168 def testClassifiedLayer(self):
230     """Save a session with a single map with a single layer
231     with a classificaton.
232     """
233     # deliberately put an apersand in the title :)
234     session = Session("single map&layer")
235     proj = Projection(["zone=26", "proj=utm", "ellps=clrk66"])
236     map = Map("Test Map", projection = proj)
237     session.AddMap(map)
238     # use shapefile from the example data
239     shpfile = os.path.join(os.path.dirname(__file__),
240     os.pardir, "Data", "iceland", "political.shp")
241     layer = Layer("My Layer", session.OpenShapefile(shpfile))
242     map.AddLayer(layer)
243 jonathan 755
244 jonathan 1168 clazz = layer.GetClassification()
245    
246     clazz.SetField("AREA")
247    
248     clazz.AppendGroup(ClassGroupSingleton(42,
249     ClassGroupProperties(),
250     "single"))
251     clazz.AppendGroup(ClassGroupSingleton("text",
252     ClassGroupProperties(),
253     "single-text"))
254    
255     clazz.AppendGroup(ClassGroupRange(0, 42,
256     ClassGroupProperties(),
257     "range"))
258    
259     range = ClassGroupRange(Range("[0;42]"))
260     range.SetProperties(ClassGroupProperties())
261     range.SetLabel("new-range")
262     clazz.AppendGroup(range)
263    
264     filename = self.temp_file_name("save_singlemap.thuban")
265     save_session(session, filename)
266    
267     file = open(filename)
268     written_contents = file.read()
269     file.close()
270     expected_template = '''<?xml version="1.0" encoding="UTF-8"?>
271     <!DOCTYPE session SYSTEM "thuban.dtd">
272     <session title="single map&amp;layer">
273     <map title="Test Map">
274     <projection name="Unknown">
275     <parameter value="zone=26"/>
276     <parameter value="proj=utm"/>
277     <parameter value="ellps=clrk66"/>
278     </projection>
279     <layer title="My Layer" filename="%s"
280     fill="None" stroke="#000000" stroke_width="1" visible="%s">
281     <classification field="AREA" field_type="double">
282     <clnull label="">
283     <cldata fill="None" stroke="#000000" stroke_width="1"/>
284     </clnull>
285     <clpoint value="42" label="single">
286     <cldata fill="None" stroke="#000000" stroke_width="1"/>
287     </clpoint>
288     <clpoint value="text" label="single-text">
289     <cldata fill="None" stroke="#000000" stroke_width="1"/>
290     </clpoint>
291     <clrange range="[0;42[" label="range">
292     <cldata fill="None" stroke="#000000" stroke_width="1"/>
293     </clrange>
294     <clrange range="[0;42]" label="new-range">
295     <cldata fill="None" stroke="#000000" stroke_width="1"/>
296     </clrange>
297     </classification>
298     </layer>
299     </map>
300 bh 1245 </session>'''
301    
302 jonathan 1168 expected_contents = expected_template % \
303     (os.path.join("..", "..", "Data", "iceland", "political.shp"),
304     "true")
305    
306     #print written_contents
307     #print "********************************************"
308     #print expected_contents
309     self.compare_xml(written_contents, expected_contents)
310    
311 bh 1245 self.validate_data(written_contents)
312    
313 jonathan 1168 session.Destroy()
314    
315    
316 bh 292 if __name__ == "__main__":
317     # Fake the __file__ global because it's needed by a test
318     import sys
319     __file__ = sys.argv[0]
320 bh 723 support.run_tests()

Properties

Name Value
svn:eol-style native
svn:keywords Author Date Id Revision

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26