/[thuban]/trunk/thuban/test/test_save.py
ViewVC logotype

Diff of /trunk/thuban/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 1259 by bh, Fri Jun 20 12:31:48 2003 UTC
# Line 21  import xml.sax Line 21  import xml.sax
21  import xml.sax.handler  import xml.sax.handler
22  from xml.sax import make_parser, ErrorHandler, SAXNotRecognizedException  from xml.sax import make_parser, ErrorHandler, SAXNotRecognizedException
23    
24    import xmlsupport
25    
26  import support  import support
27  support.initthuban()  support.initthuban()
28    
29  from Thuban.Model.save import save_session  from Thuban.Model.save import XMLWriter, save_session
30  from Thuban.Model.session import Session  from Thuban.Model.session import Session
31  from Thuban.Model.map import Map  from Thuban.Model.map import Map
32  from Thuban.Model.layer import Layer, RasterLayer  from Thuban.Model.layer import Layer, RasterLayer
33  from Thuban.Model.proj import Projection  from Thuban.Model.proj import Projection
34    
35    from Thuban.Model.classification import ClassGroupSingleton, ClassGroupRange, \
36        ClassGroupProperties
37    
38    from Thuban.Model.range import Range
39    
40    
41    class XMLWriterTest(unittest.TestCase):
42    
43  class SaxEventLister(xml.sax.handler.ContentHandler):      def testEncode(self):
44            """Test XMLWriter.encode"""
45            writer = XMLWriter()
46            eq = self.assertEquals
47    
48      def __init__(self):          eq(writer.encode("hello world"), "hello world")
49          self.eventlist = []          eq(writer.encode(unicode("hello world")), unicode("hello world"))
50    
51      def startElementNS(self, name, qname, attrs):          eq(writer.encode("\x80\x90\xc2\x100"),
52          items = attrs.items()                           "\xc2\x80\xc2\x90\xc3\x82\x100")
53          items.sort()          eq(writer.encode(u"\x80\x90\xc2\x100"),
54          self.eventlist.append(("start", name, qname, items))                           "\xc2\x80\xc2\x90\xc3\x82\x100")
55            eq(writer.encode(u"\xFF5E"), "\xc3\xbf5E")
     def endElementNS(self, name, qname):  
         self.eventlist.append(("end", name, qname))  
   
   
 def sax_eventlist(data):  
     """Return a list of SAX event generated for the XML data.  
     """  
     handler = SaxEventLister()  
     parser = make_parser()  
     parser.setContentHandler(handler)  
     parser.setErrorHandler(ErrorHandler())  
     parser.setFeature(xml.sax.handler.feature_namespaces, 1)  
   
     #  
     # see comment at the end of Thuban/Model/load.py  
     #  
     try:  
         parser.setFeature(xml.sax.handler.feature_validation, 0)  
         parser.setFeature(xml.sax.handler.feature_external_ges, 0)  
         parser.setFeature(xml.sax.handler.feature_external_pes, 0)  
     except SAXNotRecognizedException:  
         pass  
   
     inpsrc = xml.sax.InputSource()  
     inpsrc.setByteStream(StringIO(data))  
     parser.parse(inpsrc)  
56    
57      return handler.eventlist          eq(writer.encode('&"\'<>'), "&amp;&quot;&apos;&lt;&gt;")
58            eq(writer.encode(unicode('&"\'<>')), "&amp;&quot;&apos;&lt;&gt;")
59    
60  class SaveSessionTest(unittest.TestCase, support.FileTestMixin):  class SaveSessionTest(unittest.TestCase, support.FileTestMixin,
61                          xmlsupport.ValidationTest):
62    
63      def compare_xml(self, xml1, xml2):      def compare_xml(self, xml1, xml2):
64          self.assertEquals(sax_eventlist(xml1), sax_eventlist(xml2))          self.assertEquals(xmlsupport.sax_eventlist(xml1),
65                              xmlsupport.sax_eventlist(xml2))
66    
67      def testEmptySession(self):      def testEmptySession(self):
68          """Save an empty session"""          """Save an empty session"""
# Line 90  class SaveSessionTest(unittest.TestCase, Line 79  class SaveSessionTest(unittest.TestCase,
79                           '<!DOCTYPE session SYSTEM "thuban.dtd">\n'                           '<!DOCTYPE session SYSTEM "thuban.dtd">\n'
80                           '<session title="empty session">\n</session>\n')                           '<session title="empty session">\n</session>\n')
81    
82            self.validate_data(written_contents)
83    
84      def testSingleLayer(self):      def testSingleLayer(self):
85          """Save a session with a single map with a single layer"""          """Save a session with a single map with a single layer"""
86          # deliberately put an apersand in the title :)          # deliberately put an apersand in the title :)
# Line 132  class SaveSessionTest(unittest.TestCase, Line 123  class SaveSessionTest(unittest.TestCase,
123          #print expected_contents          #print expected_contents
124          self.compare_xml(written_contents, expected_contents)          self.compare_xml(written_contents, expected_contents)
125    
126            self.validate_data(written_contents)
127    
128          layer.SetVisible(False)          layer.SetVisible(False)
129          save_session(session, filename)          save_session(session, filename)
130    
# Line 146  class SaveSessionTest(unittest.TestCase, Line 139  class SaveSessionTest(unittest.TestCase,
139          #print "********************************************"          #print "********************************************"
140          #print expected_contents          #print expected_contents
141          self.compare_xml(written_contents, expected_contents)          self.compare_xml(written_contents, expected_contents)
142            self.validate_data(written_contents)
143    
144          session.Destroy()          session.Destroy()
145    
# Line 194  class SaveSessionTest(unittest.TestCase, Line 188  class SaveSessionTest(unittest.TestCase,
188          #print expected_contents          #print expected_contents
189          self.compare_xml(written_contents, expected_contents)          self.compare_xml(written_contents, expected_contents)
190    
191                    self.validate_data(written_contents)
192    
193      def testRasterLayer(self):      def testRasterLayer(self):
194          # deliberately put an apersand in the title :)          # deliberately put an apersand in the title :)
195          session = Session("single map&layer")          session = Session("single map&layer")
# Line 205  class SaveSessionTest(unittest.TestCase, Line 200  class SaveSessionTest(unittest.TestCase,
200                                 os.pardir, "Data", "iceland", "island.tif")                                 os.pardir, "Data", "iceland", "island.tif")
201          layer = RasterLayer("My RasterLayer", imgfile)          layer = RasterLayer("My RasterLayer", imgfile)
202          map.AddLayer(layer)          map.AddLayer(layer)
203                                                                                    
204          filename = self.temp_file_name("save_singlemap.thuban")          filename = self.temp_file_name("save_singlemap.thuban")
205          save_session(session, filename)          save_session(session, filename)
206          session.Destroy()          session.Destroy()
207                                                                                    
208          file = open(filename)          file = open(filename)
209          written_contents = file.read()          written_contents = file.read()
210          file.close()          file.close()
# Line 229  class SaveSessionTest(unittest.TestCase, Line 224  class SaveSessionTest(unittest.TestCase,
224          #print expected_contents          #print expected_contents
225          self.compare_xml(written_contents, expected_contents)          self.compare_xml(written_contents, expected_contents)
226    
227            self.validate_data(written_contents)
228    
229        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    
244            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            </session>'''
301    
302            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            self.validate_data(written_contents)
312    
313            session.Destroy()
314    
315    
316  if __name__ == "__main__":  if __name__ == "__main__":
317      # 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.1259

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26