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

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

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 947 by jonathan, Tue May 20 15:27:19 2003 UTC revision 1173 by jonathan, Thu Jun 12 13:37:18 2003 UTC
# Line 24  from xml.sax import make_parser, ErrorHa Line 24  from xml.sax import make_parser, ErrorHa
24  import support  import support
25  support.initthuban()  support.initthuban()
26    
27  from Thuban.Model.save import save_session  from Thuban.Model.save import XMLWriter, save_session
28  from Thuban.Model.session import Session  from Thuban.Model.session import Session
29  from Thuban.Model.map import Map  from Thuban.Model.map import Map
30  from Thuban.Model.layer import Layer, RasterLayer  from Thuban.Model.layer import Layer, RasterLayer
31  from Thuban.Model.proj import Projection  from Thuban.Model.proj import Projection
32    
33    from Thuban.Model.classification import ClassGroupSingleton, ClassGroupRange, \
34        ClassGroupProperties
35    
36    from Thuban.Model.range import Range
37    
38    
39  class SaxEventLister(xml.sax.handler.ContentHandler):  class SaxEventLister(xml.sax.handler.ContentHandler):
40    
# Line 70  def sax_eventlist(data): Line 75  def sax_eventlist(data):
75    
76      return handler.eventlist      return handler.eventlist
77    
78    class XMLWriterTest(unittest.TestCase):
79    
80        def testEncode(self):
81            """Test XMLWriter.encode"""
82            writer = XMLWriter()
83    
84            writer.encode("hello world")
85            writer.encode(unicode("hello world"))
86    
87            writer.encode("\x80\x90\xc2\x100")
88            writer.encode(u"\x80\x90\xc2\x100")
89            writer.encode(u"\xFF5E")
90    
91            self.assertEquals(writer.encode('&"\'<>'),
92                              "&amp;&quot;&apos;&lt;&gt;")
93            self.assertEquals(writer.encode(unicode('&"\'<>')),
94                              "&amp;&quot;&apos;&lt;&gt;")
95    
96  class SaveSessionTest(unittest.TestCase, support.FileTestMixin):  class SaveSessionTest(unittest.TestCase, support.FileTestMixin):
97    
98      def compare_xml(self, xml1, xml2):      def compare_xml(self, xml1, xml2):
# Line 229  class SaveSessionTest(unittest.TestCase, Line 252  class SaveSessionTest(unittest.TestCase,
252          #print expected_contents          #print expected_contents
253          self.compare_xml(written_contents, expected_contents)          self.compare_xml(written_contents, expected_contents)
254    
255        def testClassifiedLayer(self):
256            """Save a session with a single map with a single layer
257               with a classificaton.
258            """
259            # deliberately put an apersand in the title :)
260            session = Session("single map&layer")
261            proj = Projection(["zone=26", "proj=utm", "ellps=clrk66"])
262            map = Map("Test Map", projection = proj)
263            session.AddMap(map)
264            # use shapefile from the example data
265            shpfile = os.path.join(os.path.dirname(__file__),
266                                   os.pardir, "Data", "iceland", "political.shp")
267            layer = Layer("My Layer", session.OpenShapefile(shpfile))
268            map.AddLayer(layer)
269    
270            clazz = layer.GetClassification()
271    
272            clazz.SetField("AREA")
273    
274            clazz.AppendGroup(ClassGroupSingleton(42,
275                                               ClassGroupProperties(),
276                                               "single"))
277            clazz.AppendGroup(ClassGroupSingleton("text",
278                                               ClassGroupProperties(),
279                                               "single-text"))
280    
281            clazz.AppendGroup(ClassGroupRange(0, 42,
282                                               ClassGroupProperties(),
283                                               "range"))
284    
285            range = ClassGroupRange(Range("[0;42]"))
286            range.SetProperties(ClassGroupProperties())
287            range.SetLabel("new-range")
288            clazz.AppendGroup(range)
289    
290            filename = self.temp_file_name("save_singlemap.thuban")
291            save_session(session, filename)
292    
293            file = open(filename)
294            written_contents = file.read()
295            file.close()
296            expected_template = '''<?xml version="1.0" encoding="UTF-8"?>
297            <!DOCTYPE session SYSTEM "thuban.dtd">
298            <session title="single map&amp;layer">
299                <map title="Test Map">
300                    <projection name="Unknown">
301                        <parameter value="zone=26"/>
302                        <parameter value="proj=utm"/>
303                        <parameter value="ellps=clrk66"/>
304                    </projection>
305                    <layer title="My Layer" filename="%s"
306                    fill="None" stroke="#000000" stroke_width="1" visible="%s">
307                        <classification field="AREA" field_type="double">
308                            <clnull label="">
309                                <cldata fill="None" stroke="#000000" stroke_width="1"/>
310                            </clnull>
311                            <clpoint value="42" label="single">
312                                <cldata fill="None" stroke="#000000" stroke_width="1"/>
313                            </clpoint>
314                            <clpoint value="text" label="single-text">
315                                <cldata fill="None" stroke="#000000" stroke_width="1"/>
316                            </clpoint>
317                            <clrange range="[0;42[" label="range">
318                                <cldata fill="None" stroke="#000000" stroke_width="1"/>
319                            </clrange>
320                            <clrange range="[0;42]" label="new-range">
321                                <cldata fill="None" stroke="#000000" stroke_width="1"/>
322                            </clrange>
323                        </classification>
324                    </layer>
325                </map>
326            </session>'''
327            
328            expected_contents = expected_template % \
329                (os.path.join("..", "..", "Data", "iceland", "political.shp"),
330                 "true")
331    
332            #print written_contents
333            #print "********************************************"
334            #print expected_contents
335            self.compare_xml(written_contents, expected_contents)
336    
337            session.Destroy()
338    
339    
340  if __name__ == "__main__":  if __name__ == "__main__":
341      # Fake the __file__ global because it's needed by a test      # Fake the __file__ global because it's needed by a test

Legend:
Removed from v.947  
changed lines
  Added in v.1173

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26