/[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 775 - (hide annotations)
Tue Apr 29 14:34:57 2003 UTC (21 years, 10 months ago) by jonathan
Original Path: trunk/thuban/test/test_save.py
File MIME type: text/x-python
File size: 7041 byte(s)
SaveSessionTest.testSingleLayer): Add test for saving an invisible layer.

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     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 bh 723 layer = Layer("My Layer", session.OpenShapefile(shpfile))
104 bh 292 map.AddLayer(layer)
105    
106     filename = self.temp_file_name("save_singlemap.thuban")
107     save_session(session, filename)
108    
109     file = open(filename)
110     written_contents = file.read()
111     file.close()
112 jonathan 775 expected_template = '''<?xml version="1.0" encoding="UTF-8"?>
113 bh 292 <!DOCTYPE session SYSTEM "thuban.dtd">
114     <session title="single map&amp;layer">
115     <map title="Test Map">
116 jonathan 755 <projection name="Unknown">
117 bh 292 <parameter value="zone=26"/>
118     <parameter value="proj=utm"/>
119     <parameter value="ellps=clrk66"/>
120     </projection>
121     <layer title="My Layer" filename="%s"
122 jonathan 775 fill="None" stroke="#000000" stroke_width="1" visible="%s"/>
123 bh 292 </map>
124 jonathan 775 </session>'''
125    
126     expected_contents = expected_template % \
127     (os.path.join("..", "..", "Data", "iceland", "political.shp"),
128     "true")
129    
130 jonathan 494 #print written_contents
131     #print "********************************************"
132     #print expected_contents
133 bh 292 self.compare_xml(written_contents, expected_contents)
134    
135 jonathan 775 layer.SetVisible(False)
136     save_session(session, filename)
137    
138     file = open(filename)
139     written_contents = file.read()
140     file.close()
141     expected_contents = expected_template % \
142     (os.path.join("..", "..", "Data", "iceland", "political.shp"),
143     "false")
144    
145     #print written_contents
146     #print "********************************************"
147     #print expected_contents
148     self.compare_xml(written_contents, expected_contents)
149    
150     session.Destroy()
151    
152 jonathan 755 def testLayerProjection(self):
153     # deliberately put an apersand in the title :)
154     session = Session("single map&layer")
155     proj = Projection(["zone=26", "proj=utm", "ellps=clrk66"])
156     map = Map("Test Map", projection = proj)
157     session.AddMap(map)
158     # use shapefile from the example data
159     shpfile = os.path.join(os.path.dirname(__file__),
160     os.pardir, "Data", "iceland", "political.shp")
161     layer = Layer("My Layer", session.OpenShapefile(shpfile))
162     proj = Projection(["proj=lcc", "ellps=clrk66"], "Layer Projection")
163     layer.SetProjection(proj)
164     map.AddLayer(layer)
165 bh 292
166 jonathan 755 filename = self.temp_file_name("save_singlemap.thuban")
167     save_session(session, filename)
168     session.Destroy()
169 bh 292
170 jonathan 755 file = open(filename)
171     written_contents = file.read()
172     file.close()
173     expected_contents = '''<?xml version="1.0" encoding="UTF-8"?>
174     <!DOCTYPE session SYSTEM "thuban.dtd">
175     <session title="single map&amp;layer">
176     <map title="Test Map">
177     <projection name="Unknown">
178     <parameter value="zone=26"/>
179     <parameter value="proj=utm"/>
180     <parameter value="ellps=clrk66"/>
181     </projection>
182     <layer title="My Layer" filename="%s"
183 jonathan 775 fill="None" stroke="#000000" stroke_width="1" visible="true">
184 jonathan 755 <projection name="Layer Projection">
185     <parameter value="proj=lcc"/>
186     <parameter value="ellps=clrk66"/>
187     </projection>
188     </layer>
189     </map>
190     </session>''' % os.path.join("..", "..", "Data", "iceland",
191     "political.shp")
192     #print written_contents
193     #print "********************************************"
194     #print expected_contents
195     self.compare_xml(written_contents, expected_contents)
196    
197    
198    
199    
200    
201 bh 292 if __name__ == "__main__":
202     # Fake the __file__ global because it's needed by a test
203     import sys
204     __file__ = sys.argv[0]
205 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