/[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 530 - (hide annotations)
Wed Mar 12 19:58:00 2003 UTC (21 years, 11 months ago) by jonathan
Original Path: trunk/thuban/test/test_save.py
File MIME type: text/x-python
File size: 4406 byte(s)
Fixed problem with xml reading that caused exceptions

1 bh 292 # Copyright (c) 2002 by Intevation GmbH
2     # 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     import support
25     support.initthuban()
26    
27     from Thuban.Model.save import save_session
28     from Thuban.Model.session import Session
29     from Thuban.Model.map import Map
30     from Thuban.Model.layer import Layer
31     from Thuban.Model.proj import Projection
32    
33    
34     class SaxEventLister(xml.sax.handler.ContentHandler):
35    
36     def __init__(self):
37     self.eventlist = []
38    
39     def startElementNS(self, name, qname, attrs):
40     items = attrs.items()
41     items.sort()
42     self.eventlist.append(("start", name, qname, items))
43    
44     def endElementNS(self, name, qname):
45     self.eventlist.append(("end", name, qname))
46    
47    
48     def sax_eventlist(data):
49     """Return a list of SAX event generated for the XML data.
50     """
51     handler = SaxEventLister()
52     parser = make_parser()
53     parser.setContentHandler(handler)
54     parser.setErrorHandler(ErrorHandler())
55     parser.setFeature(xml.sax.handler.feature_namespaces, 1)
56    
57 jonathan 530 #
58     # see comment at the end of Thuban/Model/load.py
59     #
60     try:
61     parser.setFeature(xml.sax.handler.feature_validation, 0)
62     parser.setFeature(xml.sax.handler.feature_external_ges, 0)
63     parser.setFeature(xml.sax.handler.feature_external_pes, 0)
64     except SAXNotRecognizedException:
65     pass
66    
67 bh 292 inpsrc = xml.sax.InputSource()
68     inpsrc.setByteStream(StringIO(data))
69     parser.parse(inpsrc)
70    
71     return handler.eventlist
72    
73     class SaveSessionTest(unittest.TestCase, support.FileTestMixin):
74    
75     def compare_xml(self, xml1, xml2):
76     self.assertEquals(sax_eventlist(xml1), sax_eventlist(xml2))
77    
78     def testEmptySession(self):
79     """Save an empty session"""
80     session = Session("empty session")
81     filename = self.temp_file_name("save_emptysession.thuban")
82     save_session(session, filename)
83     session.Destroy()
84    
85     file = open(filename)
86     written_contents = file.read()
87     file.close()
88     self.compare_xml(written_contents,
89     '<?xml version="1.0" encoding="UTF-8"?>\n'
90     '<!DOCTYPE session SYSTEM "thuban.dtd">\n'
91     '<session title="empty session">\n</session>\n')
92    
93     def testSingleLayer(self):
94     """Save a session with a single map with a single layer"""
95     # deliberately put an apersand in the title :)
96     session = Session("single map&layer")
97     proj = Projection(["zone=26", "proj=utm", "ellps=clrk66"])
98     map = Map("Test Map", projection = proj)
99     session.AddMap(map)
100     # use shapefile from the example data
101     shpfile = os.path.join(os.path.dirname(__file__),
102     os.pardir, "Data", "iceland", "political.shp")
103     layer = Layer("My Layer", shpfile)
104     map.AddLayer(layer)
105    
106     filename = self.temp_file_name("save_singlemap.thuban")
107     save_session(session, filename)
108     session.Destroy()
109    
110     file = open(filename)
111     written_contents = file.read()
112     file.close()
113     expected_contents = '''<?xml version="1.0" encoding="UTF-8"?>
114     <!DOCTYPE session SYSTEM "thuban.dtd">
115     <session title="single map&amp;layer">
116     <map title="Test Map">
117     <projection>
118     <parameter value="zone=26"/>
119     <parameter value="proj=utm"/>
120     <parameter value="ellps=clrk66"/>
121     </projection>
122     <layer title="My Layer" filename="%s"
123     fill="None" stroke="#000000" stroke_width="1"/>
124     </map>
125     </session>''' % os.path.join("..", "..", "Data", "iceland",
126     "political.shp")
127 jonathan 494 #print written_contents
128     #print "********************************************"
129     #print expected_contents
130 bh 292 self.compare_xml(written_contents, expected_contents)
131    
132    
133    
134     if __name__ == "__main__":
135     # Fake the __file__ global because it's needed by a test
136     import sys
137     __file__ = sys.argv[0]
138     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