/[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 1375 by bh, Tue Jul 8 10:53:05 2003 UTC revision 2642 by bh, Fri Jul 1 20:49:04 2005 UTC
# Line 1  Line 1 
1  # Copyright (c) 2002, 2003 by Intevation GmbH  # Copyright (c) 2002, 2003, 2004, 2005 by Intevation GmbH
2  # Authors:  # Authors:
3  # Bernhard Herzog <[email protected]>  # Bernhard Herzog <[email protected]>
4  #  #
# Line 17  import os Line 17  import os
17  import unittest  import unittest
18  from StringIO import StringIO  from StringIO import StringIO
19    
 import xml.sax  
 import xml.sax.handler  
 from xml.sax import make_parser, ErrorHandler, SAXNotRecognizedException  
   
20  import xmlsupport  import xmlsupport
21    import postgissupport
22    
23  import support  import support
24  support.initthuban()  support.initthuban()
25    
26  import dbflib  import dbflib
27    
28    from Thuban import internal_from_unicode
29  from Thuban.Lib.fileutil import relative_filename  from Thuban.Lib.fileutil import relative_filename
30  from Thuban.Model.save import XMLWriter, save_session, sort_data_stores  from Thuban.Model.save import XMLWriter, save_session, sort_data_stores
31  from Thuban.Model.session import Session  from Thuban.Model.session import Session
# Line 43  from Thuban.Model.classification import Line 41  from Thuban.Model.classification import
41    
42  from Thuban.Model.range import Range  from Thuban.Model.range import Range
43    
44    from Thuban.Model.postgisdb import PostGISConnection, PostGISShapeStore
45    
46    
47  class XMLWriterTest(unittest.TestCase):  class XMLWriterTest(unittest.TestCase):
48    
# Line 54  class XMLWriterTest(unittest.TestCase): Line 54  class XMLWriterTest(unittest.TestCase):
54          eq(writer.encode("hello world"), "hello world")          eq(writer.encode("hello world"), "hello world")
55          eq(writer.encode(unicode("hello world")), unicode("hello world"))          eq(writer.encode(unicode("hello world")), unicode("hello world"))
56    
57          eq(writer.encode("\x80\x90\xc2\x100"),          eq(writer.encode(internal_from_unicode(u"\x80\x90\xc2\x100")),
58                           "\xc2\x80\xc2\x90\xc3\x82\x100")                           "\xc2\x80\xc2\x90\xc3\x82\x100")
59          eq(writer.encode(u"\x80\x90\xc2\x100"),          eq(writer.encode(u"\x80\x90\xc2\x100"),
60                           "\xc2\x80\xc2\x90\xc3\x82\x100")                           "\xc2\x80\xc2\x90\xc3\x82\x100")
61          eq(writer.encode(u"\xFF5E"), "\xc3\xbf5E")          eq(writer.encode(u"\xFF5E"), "\xc3\xbf5E")
62    
# Line 66  class XMLWriterTest(unittest.TestCase): Line 66  class XMLWriterTest(unittest.TestCase):
66  class SaveSessionTest(unittest.TestCase, support.FileTestMixin,  class SaveSessionTest(unittest.TestCase, support.FileTestMixin,
67                        xmlsupport.ValidationTest):                        xmlsupport.ValidationTest):
68    
69      dtd = "http://thuban.intevation.org/dtds/thuban-0.9-dev.dtd"      dtd = "http://thuban.intevation.org/dtds/thuban-1.1-dev.dtd"
70      thubanids = [((dtd, n), (None, "id")) for n in      thubanids = [((dtd, n), (None, "id")) for n in
71                   ["fileshapesource", "filetable", "jointable",                   ["fileshapesource", "filetable", "jointable",
72                    "derivedshapesource"]]                    "derivedshapesource", "dbshapesource", "dbconnection"]]
73      thubanidrefs = [((dtd, n), (None, m)) for n, m in      thubanidrefs = [((dtd, n), (None, m)) for n, m in
74                      [("layer", "shapestore"),                      [("layer", "shapestore"),
75                       ("jointable", "left"),                       ("jointable", "left"),
76                       ("jointable", "right"),                       ("jointable", "right"),
77                       ("derivedshapesource", "table"),                       ("derivedshapesource", "table"),
78                       ("derivedshapesource", "shapesource")]]                       ("derivedshapesource", "shapesource"),
79                         ("dbshapesource", "dbconn")]]
80      del n, m, dtd      del n, m, dtd
81    
82        def tearDown(self):
83            """Call self.session.Destroy
84    
85            Test cases that create session should bind it to self.session so
86            that it gets destroyed properly
87            """
88            if hasattr(self, "session"):
89                self.session.Destroy()
90                self.session = None
91    
92      def compare_xml(self, xml1, xml2):      def compare_xml(self, xml1, xml2):
93          list1 = xmlsupport.sax_eventlist(xml1, ids = self.thubanids,          list1 = xmlsupport.sax_eventlist(xml1, ids = self.thubanids,
94                                           idrefs = self.thubanidrefs)                                           idrefs = self.thubanidrefs)
# Line 101  class SaveSessionTest(unittest.TestCase, Line 112  class SaveSessionTest(unittest.TestCase,
112          file.close()          file.close()
113          self.compare_xml(written_contents,          self.compare_xml(written_contents,
114                           '<?xml version="1.0" encoding="UTF-8"?>\n'                           '<?xml version="1.0" encoding="UTF-8"?>\n'
115                           '<!DOCTYPE session SYSTEM "thuban-0.9.dtd">\n'                           '<!DOCTYPE session SYSTEM "thuban-1.1.dtd">\n'
116                           '<session title="empty session" '                           '<session title="empty session" '
117           'xmlns="http://thuban.intevation.org/dtds/thuban-0.9-dev.dtd">'           'xmlns="http://thuban.intevation.org/dtds/thuban-1.1-dev.dtd">'
118                           '\n</session>\n')                           '\n</session>\n')
119    
120          self.validate_data(written_contents)          self.validate_data(written_contents)
# Line 112  class SaveSessionTest(unittest.TestCase, Line 123  class SaveSessionTest(unittest.TestCase,
123          """Save a session with a single map with a single layer"""          """Save a session with a single map with a single layer"""
124          # deliberately put an apersand in the title :)          # deliberately put an apersand in the title :)
125          session = Session("single map&layer")          session = Session("single map&layer")
126          proj = Projection(["zone=26", "proj=utm", "ellps=clrk66"])          proj = Projection(["proj=utm", "zone=27", "ellps=WGS84",
127                               "datum=WGS84", "units=m"],
128                              name = "WGS 84 / UTM zone 27N",
129                              epsg = "32627")
130          map = Map("Test Map", projection = proj)          map = Map("Test Map", projection = proj)
131          session.AddMap(map)          session.AddMap(map)
132          # use shapefile from the example data          # use shapefile from the example data
# Line 128  class SaveSessionTest(unittest.TestCase, Line 142  class SaveSessionTest(unittest.TestCase,
142          written_contents = file.read()          written_contents = file.read()
143          file.close()          file.close()
144          expected_template = '''<?xml version="1.0" encoding="UTF-8"?>          expected_template = '''<?xml version="1.0" encoding="UTF-8"?>
145          <!DOCTYPE session SYSTEM "thuban-0.9.dtd">          <!DOCTYPE session SYSTEM "thuban-1.1.dtd">
146          <session title="single map&amp;layer"          <session title="single map&amp;layer"
147             xmlns="http://thuban.intevation.org/dtds/thuban-0.9-dev.dtd">             xmlns="http://thuban.intevation.org/dtds/thuban-1.1-dev.dtd">
148              <fileshapesource id="D1" filename="%s" filetype="shapefile"/>              <fileshapesource id="D1"
149                    filename="../../Data/iceland/political.shp"
150                    filetype="shapefile"/>
151              <map title="Test Map">              <map title="Test Map">
152                  <projection name="Unknown">                  <projection epsg="32627" name="WGS 84 / UTM zone 27N">
                     <parameter value="zone=26"/>  
153                      <parameter value="proj=utm"/>                      <parameter value="proj=utm"/>
154                      <parameter value="ellps=clrk66"/>                      <parameter value="zone=27"/>
155                        <parameter value="ellps=WGS84"/>
156                        <parameter value="datum=WGS84"/>
157                        <parameter value="units=m"/>
158                  </projection>                  </projection>
159                  <layer title="My Layer" shapestore="D1"                  <layer title="My Layer" shapestore="D1"
160                  fill="None" stroke="#000000" stroke_width="1" visible="%s"/>                  fill="None" stroke="#000000" stroke_width="1" visible="%s"/>
161              </map>              </map>
162          </session>'''          </session>'''
163    
164          expected_contents = expected_template % \          expected_contents = expected_template % "true"
             (os.path.join("..", "..", "Data", "iceland", "political.shp"),  
              "true")  
165    
166          self.compare_xml(written_contents, expected_contents)          self.compare_xml(written_contents, expected_contents)
167    
168          self.validate_data(written_contents)          self.validate_data(written_contents)
169    
170            # Repeat with an invisible layer
171          layer.SetVisible(False)          layer.SetVisible(False)
172          save_session(session, filename)          save_session(session, filename)
173    
174          file = open(filename)          file = open(filename)
175          written_contents = file.read()          written_contents = file.read()
176          file.close()          file.close()
177          expected_contents = expected_template % \          expected_contents = expected_template % "false"
             (os.path.join("..", "..", "Data", "iceland", "political.shp"),  
              "false")  
178          self.compare_xml(written_contents, expected_contents)          self.compare_xml(written_contents, expected_contents)
179          self.validate_data(written_contents)          self.validate_data(written_contents)
180    
# Line 168  class SaveSessionTest(unittest.TestCase, Line 183  class SaveSessionTest(unittest.TestCase,
183      def testLayerProjection(self):      def testLayerProjection(self):
184          """Test saving layers with projections"""          """Test saving layers with projections"""
185          # deliberately put an apersand in the title :)          # deliberately put an apersand in the title :)
186          session = Session("single map&layer")          session = self.session = Session("single map&layer")
187          proj = Projection(["zone=26", "proj=utm", "ellps=clrk66"])          proj = Projection(["zone=26", "proj=utm", "ellps=clrk66"])
188          map = Map("Test Map", projection = proj)          map = Map("Test Map", projection = proj)
189          session.AddMap(map)          session.AddMap(map)
# Line 176  class SaveSessionTest(unittest.TestCase, Line 191  class SaveSessionTest(unittest.TestCase,
191          shpfile = os.path.join(os.path.dirname(__file__),          shpfile = os.path.join(os.path.dirname(__file__),
192                                 os.pardir, "Data", "iceland", "political.shp")                                 os.pardir, "Data", "iceland", "political.shp")
193          layer = Layer("My Layer", session.OpenShapefile(shpfile))          layer = Layer("My Layer", session.OpenShapefile(shpfile))
194          proj = Projection(["proj=lcc", "ellps=clrk66"], "Layer Projection")          proj = Projection(["proj=lcc", "ellps=clrk66",
195                               "lat_1=0", "lat_2=20"],
196                              "Layer Projection")
197          layer.SetProjection(proj)          layer.SetProjection(proj)
198          map.AddLayer(layer)          map.AddLayer(layer)
199    
200          filename = self.temp_file_name("save_layerproj.thuban")          filename = self.temp_file_name("save_layerproj.thuban")
201          save_session(session, filename)          save_session(session, filename)
         session.Destroy()  
202    
203          file = open(filename)          file = open(filename)
204          written_contents = file.read()          written_contents = file.read()
205          file.close()          file.close()
206          expected_contents = '''<?xml version="1.0" encoding="UTF-8"?>          expected_contents = '''<?xml version="1.0" encoding="UTF-8"?>
207          <!DOCTYPE session SYSTEM "thuban-0.9.dtd">          <!DOCTYPE session SYSTEM "thuban-1.1.dtd">
208          <session title="single map&amp;layer"          <session title="single map&amp;layer"
209             xmlns="http://thuban.intevation.org/dtds/thuban-0.9-dev.dtd">             xmlns="http://thuban.intevation.org/dtds/thuban-1.1-dev.dtd">
210              <fileshapesource id="D1" filename="%s" filetype="shapefile"/>              <fileshapesource id="D1"
211                    filename="../../Data/iceland/political.shp"
212                    filetype="shapefile"/>
213              <map title="Test Map">              <map title="Test Map">
214                  <projection name="Unknown">                  <projection name="Unknown">
215                      <parameter value="zone=26"/>                      <parameter value="zone=26"/>
# Line 203  class SaveSessionTest(unittest.TestCase, Line 221  class SaveSessionTest(unittest.TestCase,
221                      <projection name="Layer Projection">                      <projection name="Layer Projection">
222                          <parameter value="proj=lcc"/>                          <parameter value="proj=lcc"/>
223                          <parameter value="ellps=clrk66"/>                          <parameter value="ellps=clrk66"/>
224                            <parameter value="lat_1=0"/>
225                            <parameter value="lat_2=20"/>
226                      </projection>                      </projection>
227                  </layer>                  </layer>
228              </map>              </map>
229          </session>''' % os.path.join("..", "..", "Data", "iceland",          </session>'''
                                      "political.shp")  
230          #print written_contents          #print written_contents
231          #print "********************************************"          #print "********************************************"
232          #print expected_contents          #print expected_contents
# Line 216  class SaveSessionTest(unittest.TestCase, Line 235  class SaveSessionTest(unittest.TestCase,
235          self.validate_data(written_contents)          self.validate_data(written_contents)
236    
237      def testRasterLayer(self):      def testRasterLayer(self):
         # deliberately put an apersand in the title :)  
         session = Session("single map&layer")  
         map = Map("Test Map")  
         session.AddMap(map)  
         # use shapefile from the example data  
         imgfile = os.path.join(os.path.dirname(__file__),  
                                os.pardir, "Data", "iceland", "island.tif")  
         layer = RasterLayer("My RasterLayer", imgfile)  
         map.AddLayer(layer)  
238    
239          filename = self.temp_file_name("save_singlemap.thuban")          MASK_NONE = RasterLayer.MASK_NONE
240          save_session(session, filename)          MASK_BIT = RasterLayer.MASK_BIT
241          session.Destroy()          MASK_ALPHA = RasterLayer.MASK_ALPHA
242    
243            for opacity, masktype, opname, maskname in \
244                [(1,  MASK_BIT,   '', ''),
245                 (.2, MASK_BIT,   'opacity="0.2"', ''),
246                 (1,  MASK_ALPHA, '',              'masktype="alpha"'),
247                 (.5, MASK_ALPHA, 'opacity="0.5"', 'masktype="alpha"'),
248                 (1,  MASK_NONE,  '',              'masktype="none"'),
249                 (0,  MASK_NONE,  'opacity="0"',   'masktype="none"') ]:
250    
         file = open(filename)  
         written_contents = file.read()  
         file.close()  
         expected_contents = '''<?xml version="1.0" encoding="UTF-8"?>  
         <!DOCTYPE session SYSTEM "thuban-0.9.dtd">  
         <session title="single map&amp;layer"  
            xmlns="http://thuban.intevation.org/dtds/thuban-0.9-dev.dtd">  
             <map title="Test Map">  
                 <rasterlayer title="My RasterLayer" filename="%s"  
                              visible="true">  
                 </rasterlayer>  
             </map>  
         </session>''' % os.path.join(os.path.dirname(__file__),  
                                      os.pardir, "Data", "iceland",  
                                      "island.tif")  
         #print written_contents  
         #print "********************************************"  
         #print expected_contents  
         self.compare_xml(written_contents, expected_contents)  
251    
252          self.validate_data(written_contents)              # deliberately put an apersand in the title :)
253                session = Session("single map&layer")
254                map = Map("Test Map")
255                session.AddMap(map)
256                # use shapefile from the example data
257                imgfile = os.path.join(os.path.dirname(__file__),
258                                       os.pardir, "Data", "iceland", "island.tif")
259                layer = RasterLayer("My RasterLayer", imgfile)
260    
261                layer.SetOpacity(opacity)
262                layer.SetMaskType(masktype)
263    
264                map.AddLayer(layer)
265    
266                filename = self.temp_file_name("%s.thuban" % self.id())
267                save_session(session, filename)
268                session.Destroy()
269    
270                file = open(filename)
271                written_contents = file.read()
272                file.close()
273                expected_contents = '''<?xml version="1.0" encoding="UTF-8"?>
274                <!DOCTYPE session SYSTEM "thuban-1.1.dtd">
275                <session title="single map&amp;layer"
276                   xmlns="http://thuban.intevation.org/dtds/thuban-1.1-dev.dtd">
277                    <map title="Test Map">
278                        <rasterlayer title="My RasterLayer"
279                                filename="../../Data/iceland/island.tif"
280                                visible="true" %s %s>
281                        </rasterlayer>
282                    </map>
283                </session>''' % (opname, maskname)
284                #print written_contents
285                #print "********************************************"
286                #print expected_contents
287                self.compare_xml(written_contents, expected_contents)
288    
289                self.validate_data(written_contents)
290    
291      def testClassifiedLayer(self):      def testClassifiedLayer(self):
292          """Save a session with a single map with a single layer          """Save a session with a single map with classifications"""
            with a classificaton.  
         """  
293          # deliberately put an apersand in the title :)          # deliberately put an apersand in the title :)
294          session = Session("single map&layer")          session = Session("Map with Classifications")
295          proj = Projection(["zone=26", "proj=utm", "ellps=clrk66"])          proj = Projection(["zone=26", "proj=utm", "ellps=clrk66"])
296          map = Map("Test Map", projection = proj)          map = Map("Test Map", projection = proj)
297          session.AddMap(map)          session.AddMap(map)
# Line 266  class SaveSessionTest(unittest.TestCase, Line 300  class SaveSessionTest(unittest.TestCase,
300                                 os.pardir, "Data", "iceland", "political.shp")                                 os.pardir, "Data", "iceland", "political.shp")
301          layer = Layer("My Layer", session.OpenShapefile(shpfile))          layer = Layer("My Layer", session.OpenShapefile(shpfile))
302          map.AddLayer(layer)          map.AddLayer(layer)
303            layer2 = Layer("My Layer", layer.ShapeStore())
304            map.AddLayer(layer2)
305    
306          clazz = layer.GetClassification()          clazz = layer.GetClassification()
307    
308          clazz.SetFieldInfo("AREA", None)          layer.SetClassificationColumn("AREA")
309    
310          clazz.AppendGroup(ClassGroupSingleton(42,          clazz.AppendGroup(ClassGroupSingleton(42, ClassGroupProperties(),
311                                             ClassGroupProperties(),                                                "single"))
312                                             "single"))          clazz.AppendGroup(ClassGroupSingleton("text", ClassGroupProperties(),
313          clazz.AppendGroup(ClassGroupSingleton("text",                                                "single-text"))
                                            ClassGroupProperties(),  
                                            "single-text"))  
314    
315          clazz.AppendGroup(ClassGroupRange((0, 42),          clazz.AppendGroup(ClassGroupRange((0, 42),
316                                             ClassGroupProperties(),                                             ClassGroupProperties(),
# Line 287  class SaveSessionTest(unittest.TestCase, Line 321  class SaveSessionTest(unittest.TestCase,
321          range.SetLabel("new-range")          range.SetLabel("new-range")
322          clazz.AppendGroup(range)          clazz.AppendGroup(range)
323    
324          filename = self.temp_file_name("save_singlemap.thuban")  
325            clazz = layer2.GetClassification()
326            layer2.SetClassificationColumn("POPYCOUN")
327    
328            # Classification with Latin 1 text
329            clazz.AppendGroup(ClassGroupSingleton(
330                internal_from_unicode(u'\xe4\xf6\xfc'), # ae, oe, ue
331                ClassGroupProperties(),
332                internal_from_unicode(u'\xdcml\xe4uts'))) # Uemlaeuts
333    
334    
335            filename = self.temp_file_name("%s.thuban" % self.id())
336          save_session(session, filename)          save_session(session, filename)
337    
338          file = open(filename)          file = open(filename)
339          written_contents = file.read()          written_contents = file.read()
340          file.close()          file.close()
341          expected_template = '''<?xml version="1.0" encoding="UTF-8"?>          expected_contents = '''<?xml version="1.0" encoding="UTF-8"?>
342          <!DOCTYPE session SYSTEM "thuban-0.9.dtd">          <!DOCTYPE session SYSTEM "thuban-1.1.dtd">
343          <session title="single map&amp;layer"          <session title="Map with Classifications"
344             xmlns="http://thuban.intevation.org/dtds/thuban-0.9-dev.dtd">             xmlns="http://thuban.intevation.org/dtds/thuban-1.1-dev.dtd">
345              <fileshapesource id="D1" filename="%s" filetype="shapefile"/>              <fileshapesource id="D1"
346                    filename="../../Data/iceland/political.shp"
347                    filetype="shapefile"/>
348              <map title="Test Map">              <map title="Test Map">
349                  <projection name="Unknown">                  <projection name="Unknown">
350                      <parameter value="zone=26"/>                      <parameter value="zone=26"/>
# Line 305  class SaveSessionTest(unittest.TestCase, Line 352  class SaveSessionTest(unittest.TestCase,
352                      <parameter value="ellps=clrk66"/>                      <parameter value="ellps=clrk66"/>
353                  </projection>                  </projection>
354                  <layer title="My Layer" shapestore="D1"                  <layer title="My Layer" shapestore="D1"
355                  fill="None" stroke="#000000" stroke_width="1" visible="%s">                  fill="None" stroke="#000000" stroke_width="1" visible="true">
356                      <classification field="AREA" field_type="double">                      <classification field="AREA" field_type="double">
357                          <clnull label="">                          <clnull label="">
358                              <cldata fill="None" stroke="#000000" stroke_width="1"/>                              <cldata fill="None" stroke="#000000" stroke_width="1"/>
# Line 324  class SaveSessionTest(unittest.TestCase, Line 371  class SaveSessionTest(unittest.TestCase,
371                          </clrange>                          </clrange>
372                      </classification>                      </classification>
373                  </layer>                  </layer>
374                    <layer title="My Layer" shapestore="D1"
375                    fill="None" stroke="#000000" stroke_width="1" visible="true">
376                        <classification field="POPYCOUN" field_type="string">
377                            <clnull label="">
378                                <cldata fill="None" stroke="#000000" stroke_width="1"/>
379                            </clnull>
380                            <clpoint value="\xc3\xa4\xc3\xb6\xc3\xbc"
381                                 label="\xc3\x9cml\xc3\xa4uts">
382                                <cldata fill="None" stroke="#000000" stroke_width="1"/>
383                            </clpoint>
384                        </classification>
385                    </layer>
386              </map>              </map>
387          </session>'''          </session>'''
388    
         expected_contents = expected_template % \  
             (os.path.join("..", "..", "Data", "iceland", "political.shp"),  
              "true")  
   
389          #print written_contents          #print written_contents
390          #print "********************************************"          #print "********************************************"
391          #print expected_contents          #print expected_contents
# Line 342  class SaveSessionTest(unittest.TestCase, Line 397  class SaveSessionTest(unittest.TestCase,
397    
398      def test_dbf_table(self):      def test_dbf_table(self):
399          """Test saving a session with a dbf table link"""          """Test saving a session with a dbf table link"""
400          session = Session("a DBF Table session")          session = self.session = Session("a DBF Table session")
401          # use shapefile from the example data          # use shapefile from the example data
402          dbffile = os.path.join(os.path.dirname(__file__),          dbffile = os.path.join(os.path.dirname(__file__),
403                                 os.pardir, "Data", "iceland", "political.dbf")                                 os.pardir, "Data", "iceland", "political.dbf")
# Line 354  class SaveSessionTest(unittest.TestCase, Line 409  class SaveSessionTest(unittest.TestCase,
409          file = open(filename)          file = open(filename)
410          written_contents = file.read()          written_contents = file.read()
411          file.close()          file.close()
412          expected_template = '''<?xml version="1.0" encoding="UTF-8"?>          expected_contents = '''<?xml version="1.0" encoding="UTF-8"?>
413          <!DOCTYPE session SYSTEM "thuban-0.9.dtd">          <!DOCTYPE session SYSTEM "thuban-1.1.dtd">
414          <session title="a DBF Table session"          <session title="a DBF Table session"
415             xmlns="http://thuban.intevation.org/dtds/thuban-0.9-dev.dtd">             xmlns="http://thuban.intevation.org/dtds/thuban-1.1-dev.dtd">
416              <filetable id="D1" filename="%s" filetype="DBF" title="political"/>              <filetable id="D1" filename="../../Data/iceland/political.dbf"
417                    filetype="DBF" title="political"/>
418          </session>'''          </session>'''
419    
         expected_contents = expected_template % dbffile  
420          self.compare_xml(written_contents, expected_contents)          self.compare_xml(written_contents, expected_contents)
421            self.validate_data(written_contents)
422    
423      def test_joined_table(self):      def test_joined_table(self):
424          """Test saving a session with joined table"""          """Test saving a session with joined table"""
# Line 411  class SaveSessionTest(unittest.TestCase, Line 467  class SaveSessionTest(unittest.TestCase,
467              file = open(filename)              file = open(filename)
468              written_contents = file.read()              written_contents = file.read()
469              file.close()              file.close()
470              expected_template = '''<?xml version="1.0" encoding="UTF-8"?>              expected_contents = '''<?xml version="1.0" encoding="UTF-8"?>
471              <!DOCTYPE session SYSTEM "thuban-0.9.dtd">              <!DOCTYPE session SYSTEM "thuban-1.1.dtd">
472              <session title="A Joined Table session"              <session title="A Joined Table session"
473               xmlns="http://thuban.intevation.org/dtds/thuban-0.9-dev.dtd">               xmlns="http://thuban.intevation.org/dtds/thuban-1.1-dev.dtd">
474                  <fileshapesource filename="%(shpfile)s"                  <fileshapesource filename="../../Data/iceland/roads-line.shp"
475                                   filetype="shapefile" id="D142197204"/>                                   filetype="shapefile" id="D142197204"/>
476                  <filetable filename="%(dbffile)s"                  <filetable filename="save_joinedtable.dbf"
477                             title="save_joinedtable"                             title="save_joinedtable"
478                             filetype="DBF" id="D141881756"/>                             filetype="DBF" id="D141881756"/>
479                  <jointable id="D142180284"                  <jointable id="D142180284"
# Line 435  class SaveSessionTest(unittest.TestCase, Line 491  class SaveSessionTest(unittest.TestCase,
491                  </map>                  </map>
492              </session>'''              </session>'''
493    
             expected_contents = expected_template % {  
                 "dbffile": relative_filename(self.temp_dir(), dbffile),  
                 "shpfile": relative_filename(self.temp_dir(), shpfile)  
                 }  
494              self.compare_xml(written_contents, expected_contents)              self.compare_xml(written_contents, expected_contents)
495                self.validate_data(written_contents)
496          finally:          finally:
497              session.Destroy()              session.Destroy()
498              session = None              session = None
499    
500    
501        def test_save_postgis(self):
502            """Test saving a session with a postgis connection"""
503    
504            class NonConnection(PostGISConnection):
505                """connection class that doesn't actually connect """
506                def connect(self):
507                    pass
508    
509            class NonConnectionStore(PostGISShapeStore):
510                """Shapestore that doesn't try to access the server"""
511                def _fetch_table_information(self):
512                    # pretend that we've found a geometry column
513                    self.geometry_column = "the_geom"
514                def IDColumn(self):
515                    """Return an object with a name attribute with value 'gid'"""
516                    class dummycol:
517                        name = "gid"
518                    return dummycol
519    
520            session = Session("A PostGIS Session")
521            try:
522                dbconn = NonConnection(dbname="plugh", host="xyzzy", port="42",
523                                       user="grue")
524                session.AddDBConnection(dbconn)
525                map = Map("Test Map")
526                session.AddMap(map)
527                store = NonConnectionStore(dbconn, "roads")
528                session.AddShapeStore(store)
529                layer = Layer("Roads to Nowhere", store)
530                map.AddLayer(layer)
531    
532                # Save the session
533                filename = self.temp_file_name(self.id() + ".thuban")
534                save_session(session, filename)
535    
536                # Read it back and compare
537                file = open(filename)
538                written = file.read()
539                file.close()
540                expected = '''<?xml version="1.0" encoding="UTF-8"?>
541                <!DOCTYPE session SYSTEM "thuban-1.1.dtd">
542                <session title="A PostGIS Session"
543                 xmlns="http://thuban.intevation.org/dtds/thuban-1.1-dev.dtd">
544                    <dbconnection id="DB"
545                                  dbtype="postgis" dbname="plugh"
546                                  host="xyzzy" port="42"
547                                  user="grue"/>
548                    <dbshapesource id="roads" dbconn="DB" tablename="roads"
549                                   id_column="gid" geometry_column="the_geom"/>
550                    <map title="Test Map">
551                        <layer title="Roads to Nowhere"
552                               shapestore="roads" visible="true"
553                               stroke="#000000" stroke_width="1" fill="None"/>
554                    </map>
555                </session>'''
556                self.compare_xml(written, expected)
557                self.validate_data(written)
558            finally:
559                session.Destroy()
560    
561    
562  class MockDataStore:  class MockDataStore:
563    
564      """A very simple data store that only has dependencies"""      """A very simple data store that only has dependencies"""

Legend:
Removed from v.1375  
changed lines
  Added in v.2642

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26