/[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 1452 by bh, Fri Jul 18 12:57:59 2003 UTC revision 1687 by bh, Fri Aug 29 10:02:16 2003 UTC
# 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()
# Line 43  from Thuban.Model.classification import Line 40  from Thuban.Model.classification import
40    
41  from Thuban.Model.range import Range  from Thuban.Model.range import Range
42    
43    from Thuban.Model.postgisdb import PostGISConnection, PostGISShapeStore
44    
45    
46  class XMLWriterTest(unittest.TestCase):  class XMLWriterTest(unittest.TestCase):
47    
# Line 66  class XMLWriterTest(unittest.TestCase): Line 65  class XMLWriterTest(unittest.TestCase):
65  class SaveSessionTest(unittest.TestCase, support.FileTestMixin,  class SaveSessionTest(unittest.TestCase, support.FileTestMixin,
66                        xmlsupport.ValidationTest):                        xmlsupport.ValidationTest):
67    
68      dtd = "http://thuban.intevation.org/dtds/thuban-0.9-dev.dtd"      dtd = "http://thuban.intevation.org/dtds/thuban-0.9.dtd"
69      thubanids = [((dtd, n), (None, "id")) for n in      thubanids = [((dtd, n), (None, "id")) for n in
70                   ["fileshapesource", "filetable", "jointable",                   ["fileshapesource", "filetable", "jointable",
71                    "derivedshapesource"]]                    "derivedshapesource", "dbshapesource", "dbconnection"]]
72      thubanidrefs = [((dtd, n), (None, m)) for n, m in      thubanidrefs = [((dtd, n), (None, m)) for n, m in
73                      [("layer", "shapestore"),                      [("layer", "shapestore"),
74                       ("jointable", "left"),                       ("jointable", "left"),
75                       ("jointable", "right"),                       ("jointable", "right"),
76                       ("derivedshapesource", "table"),                       ("derivedshapesource", "table"),
77                       ("derivedshapesource", "shapesource")]]                       ("derivedshapesource", "shapesource"),
78                         ("dbshapesource", "dbconn")]]
79      del n, m, dtd      del n, m, dtd
80    
81        def tearDown(self):
82            """Call self.session.Destroy
83    
84            Test cases that create session should bind it to self.session so
85            that it gets destroyed properly
86            """
87            if hasattr(self, "session"):
88                self.session.Destroy()
89                self.session = None
90    
91      def compare_xml(self, xml1, xml2):      def compare_xml(self, xml1, xml2):
92          list1 = xmlsupport.sax_eventlist(xml1, ids = self.thubanids,          list1 = xmlsupport.sax_eventlist(xml1, ids = self.thubanids,
93                                           idrefs = self.thubanidrefs)                                           idrefs = self.thubanidrefs)
# Line 103  class SaveSessionTest(unittest.TestCase, Line 113  class SaveSessionTest(unittest.TestCase,
113                           '<?xml version="1.0" encoding="UTF-8"?>\n'                           '<?xml version="1.0" encoding="UTF-8"?>\n'
114                           '<!DOCTYPE session SYSTEM "thuban-0.9.dtd">\n'                           '<!DOCTYPE session SYSTEM "thuban-0.9.dtd">\n'
115                           '<session title="empty session" '                           '<session title="empty session" '
116           'xmlns="http://thuban.intevation.org/dtds/thuban-0.9-dev.dtd">'           'xmlns="http://thuban.intevation.org/dtds/thuban-0.9.dtd">'
117                           '\n</session>\n')                           '\n</session>\n')
118    
119          self.validate_data(written_contents)          self.validate_data(written_contents)
# Line 130  class SaveSessionTest(unittest.TestCase, Line 140  class SaveSessionTest(unittest.TestCase,
140          expected_template = '''<?xml version="1.0" encoding="UTF-8"?>          expected_template = '''<?xml version="1.0" encoding="UTF-8"?>
141          <!DOCTYPE session SYSTEM "thuban-0.9.dtd">          <!DOCTYPE session SYSTEM "thuban-0.9.dtd">
142          <session title="single map&amp;layer"          <session title="single map&amp;layer"
143             xmlns="http://thuban.intevation.org/dtds/thuban-0.9-dev.dtd">             xmlns="http://thuban.intevation.org/dtds/thuban-0.9.dtd">
144              <fileshapesource id="D1" filename="%s" filetype="shapefile"/>              <fileshapesource id="D1" filename="%s" filetype="shapefile"/>
145              <map title="Test Map">              <map title="Test Map">
146                  <projection name="Unknown">                  <projection name="Unknown">
# Line 168  class SaveSessionTest(unittest.TestCase, Line 178  class SaveSessionTest(unittest.TestCase,
178      def testLayerProjection(self):      def testLayerProjection(self):
179          """Test saving layers with projections"""          """Test saving layers with projections"""
180          # deliberately put an apersand in the title :)          # deliberately put an apersand in the title :)
181          session = Session("single map&layer")          session = self.session = Session("single map&layer")
182          proj = Projection(["zone=26", "proj=utm", "ellps=clrk66"])          proj = Projection(["zone=26", "proj=utm", "ellps=clrk66"])
183          map = Map("Test Map", projection = proj)          map = Map("Test Map", projection = proj)
184          session.AddMap(map)          session.AddMap(map)
# Line 176  class SaveSessionTest(unittest.TestCase, Line 186  class SaveSessionTest(unittest.TestCase,
186          shpfile = os.path.join(os.path.dirname(__file__),          shpfile = os.path.join(os.path.dirname(__file__),
187                                 os.pardir, "Data", "iceland", "political.shp")                                 os.pardir, "Data", "iceland", "political.shp")
188          layer = Layer("My Layer", session.OpenShapefile(shpfile))          layer = Layer("My Layer", session.OpenShapefile(shpfile))
189          proj = Projection(["proj=lcc", "ellps=clrk66"], "Layer Projection")          proj = Projection(["proj=lcc", "ellps=clrk66",
190                               "lat_1=0", "lat_2=20"],
191                              "Layer Projection")
192          layer.SetProjection(proj)          layer.SetProjection(proj)
193          map.AddLayer(layer)          map.AddLayer(layer)
194    
195          filename = self.temp_file_name("save_layerproj.thuban")          filename = self.temp_file_name("save_layerproj.thuban")
196          save_session(session, filename)          save_session(session, filename)
         session.Destroy()  
197    
198          file = open(filename)          file = open(filename)
199          written_contents = file.read()          written_contents = file.read()
# Line 190  class SaveSessionTest(unittest.TestCase, Line 201  class SaveSessionTest(unittest.TestCase,
201          expected_contents = '''<?xml version="1.0" encoding="UTF-8"?>          expected_contents = '''<?xml version="1.0" encoding="UTF-8"?>
202          <!DOCTYPE session SYSTEM "thuban-0.9.dtd">          <!DOCTYPE session SYSTEM "thuban-0.9.dtd">
203          <session title="single map&amp;layer"          <session title="single map&amp;layer"
204             xmlns="http://thuban.intevation.org/dtds/thuban-0.9-dev.dtd">             xmlns="http://thuban.intevation.org/dtds/thuban-0.9.dtd">
205              <fileshapesource id="D1" filename="%s" filetype="shapefile"/>              <fileshapesource id="D1" filename="%s" filetype="shapefile"/>
206              <map title="Test Map">              <map title="Test Map">
207                  <projection name="Unknown">                  <projection name="Unknown">
# Line 203  class SaveSessionTest(unittest.TestCase, Line 214  class SaveSessionTest(unittest.TestCase,
214                      <projection name="Layer Projection">                      <projection name="Layer Projection">
215                          <parameter value="proj=lcc"/>                          <parameter value="proj=lcc"/>
216                          <parameter value="ellps=clrk66"/>                          <parameter value="ellps=clrk66"/>
217                            <parameter value="lat_1=0"/>
218                            <parameter value="lat_2=20"/>
219                      </projection>                      </projection>
220                  </layer>                  </layer>
221              </map>              </map>
# Line 226  class SaveSessionTest(unittest.TestCase, Line 239  class SaveSessionTest(unittest.TestCase,
239          layer = RasterLayer("My RasterLayer", imgfile)          layer = RasterLayer("My RasterLayer", imgfile)
240          map.AddLayer(layer)          map.AddLayer(layer)
241    
242          filename = self.temp_file_name("save_singlemap.thuban")          filename = self.temp_file_name("%s.thuban" % self.id())
243          save_session(session, filename)          save_session(session, filename)
244          session.Destroy()          session.Destroy()
245    
# Line 236  class SaveSessionTest(unittest.TestCase, Line 249  class SaveSessionTest(unittest.TestCase,
249          expected_contents = '''<?xml version="1.0" encoding="UTF-8"?>          expected_contents = '''<?xml version="1.0" encoding="UTF-8"?>
250          <!DOCTYPE session SYSTEM "thuban-0.9.dtd">          <!DOCTYPE session SYSTEM "thuban-0.9.dtd">
251          <session title="single map&amp;layer"          <session title="single map&amp;layer"
252             xmlns="http://thuban.intevation.org/dtds/thuban-0.9-dev.dtd">             xmlns="http://thuban.intevation.org/dtds/thuban-0.9.dtd">
253              <map title="Test Map">              <map title="Test Map">
254                  <rasterlayer title="My RasterLayer" filename="%s"                  <rasterlayer title="My RasterLayer" filename="%s"
255                               visible="true">                               visible="true">
256                  </rasterlayer>                  </rasterlayer>
257              </map>              </map>
258          </session>''' % os.path.join(os.path.dirname(__file__),          </session>''' % os.path.join(os.pardir, os.pardir, "Data", "iceland",
                                      os.pardir, "Data", "iceland",  
259                                       "island.tif")                                       "island.tif")
260          #print written_contents          #print written_contents
261          #print "********************************************"          #print "********************************************"
# Line 304  class SaveSessionTest(unittest.TestCase, Line 316  class SaveSessionTest(unittest.TestCase,
316          expected_template = '''<?xml version="1.0" encoding="UTF-8"?>          expected_template = '''<?xml version="1.0" encoding="UTF-8"?>
317          <!DOCTYPE session SYSTEM "thuban-0.9.dtd">          <!DOCTYPE session SYSTEM "thuban-0.9.dtd">
318          <session title="Map with Classifications"          <session title="Map with Classifications"
319             xmlns="http://thuban.intevation.org/dtds/thuban-0.9-dev.dtd">             xmlns="http://thuban.intevation.org/dtds/thuban-0.9.dtd">
320              <fileshapesource id="D1" filename="%s" filetype="shapefile"/>              <fileshapesource id="D1" filename="%s" filetype="shapefile"/>
321              <map title="Test Map">              <map title="Test Map">
322                  <projection name="Unknown">                  <projection name="Unknown">
# Line 362  class SaveSessionTest(unittest.TestCase, Line 374  class SaveSessionTest(unittest.TestCase,
374    
375      def test_dbf_table(self):      def test_dbf_table(self):
376          """Test saving a session with a dbf table link"""          """Test saving a session with a dbf table link"""
377          session = Session("a DBF Table session")          session = self.session = Session("a DBF Table session")
378          # use shapefile from the example data          # use shapefile from the example data
379          dbffile = os.path.join(os.path.dirname(__file__),          dbffile = os.path.join(os.path.dirname(__file__),
380                                 os.pardir, "Data", "iceland", "political.dbf")                                 os.pardir, "Data", "iceland", "political.dbf")
# Line 377  class SaveSessionTest(unittest.TestCase, Line 389  class SaveSessionTest(unittest.TestCase,
389          expected_template = '''<?xml version="1.0" encoding="UTF-8"?>          expected_template = '''<?xml version="1.0" encoding="UTF-8"?>
390          <!DOCTYPE session SYSTEM "thuban-0.9.dtd">          <!DOCTYPE session SYSTEM "thuban-0.9.dtd">
391          <session title="a DBF Table session"          <session title="a DBF Table session"
392             xmlns="http://thuban.intevation.org/dtds/thuban-0.9-dev.dtd">             xmlns="http://thuban.intevation.org/dtds/thuban-0.9.dtd">
393              <filetable id="D1" filename="%s" filetype="DBF" title="political"/>              <filetable id="D1" filename="%s" filetype="DBF" title="political"/>
394          </session>'''          </session>'''
395    
396          expected_contents = expected_template % dbffile          expected_contents = (expected_template
397                                 % os.path.join(os.pardir, os.pardir, "Data",
398                                                "iceland", "political.dbf"))
399          self.compare_xml(written_contents, expected_contents)          self.compare_xml(written_contents, expected_contents)
400            self.validate_data(written_contents)
401    
402      def test_joined_table(self):      def test_joined_table(self):
403          """Test saving a session with joined table"""          """Test saving a session with joined table"""
# Line 434  class SaveSessionTest(unittest.TestCase, Line 449  class SaveSessionTest(unittest.TestCase,
449              expected_template = '''<?xml version="1.0" encoding="UTF-8"?>              expected_template = '''<?xml version="1.0" encoding="UTF-8"?>
450              <!DOCTYPE session SYSTEM "thuban-0.9.dtd">              <!DOCTYPE session SYSTEM "thuban-0.9.dtd">
451              <session title="A Joined Table session"              <session title="A Joined Table session"
452               xmlns="http://thuban.intevation.org/dtds/thuban-0.9-dev.dtd">               xmlns="http://thuban.intevation.org/dtds/thuban-0.9.dtd">
453                  <fileshapesource filename="%(shpfile)s"                  <fileshapesource filename="%(shpfile)s"
454                                   filetype="shapefile" id="D142197204"/>                                   filetype="shapefile" id="D142197204"/>
455                  <filetable filename="%(dbffile)s"                  <filetable filename="%(dbffile)s"
# Line 460  class SaveSessionTest(unittest.TestCase, Line 475  class SaveSessionTest(unittest.TestCase,
475                  "shpfile": relative_filename(self.temp_dir(), shpfile)                  "shpfile": relative_filename(self.temp_dir(), shpfile)
476                  }                  }
477              self.compare_xml(written_contents, expected_contents)              self.compare_xml(written_contents, expected_contents)
478                self.validate_data(written_contents)
479          finally:          finally:
480              session.Destroy()              session.Destroy()
481              session = None              session = None
482    
483    
484        def test_save_postgis(self):
485            """Test saving a session with a postgis connection"""
486    
487            class NonConnection(PostGISConnection):
488                """connection class that doesn't actually connect """
489                def connect(self):
490                    pass
491    
492            class NonConnectionStore(PostGISShapeStore):
493                """Shapestore that doesn't try to access the server"""
494                def _fetch_table_information(self):
495                    pass
496    
497            session = Session("A PostGIS Session")
498            try:
499                dbconn = NonConnection(dbname="plugh", host="xyzzy", port="42",
500                                       user="grue")
501                session.AddDBConnection(dbconn)
502                map = Map("Test Map")
503                session.AddMap(map)
504                store = NonConnectionStore(dbconn, "roads")
505                session.AddShapeStore(store)
506                layer = Layer("Roads to Nowhere", store)
507                map.AddLayer(layer)
508    
509                # Save the session
510                filename = self.temp_file_name(self.id() + ".thuban")
511                save_session(session, filename)
512    
513                # Read it back and compare
514                file = open(filename)
515                written = file.read()
516                file.close()
517                expected = '''<?xml version="1.0" encoding="UTF-8"?>
518                <!DOCTYPE session SYSTEM "thuban-0.9.dtd">
519                <session title="A PostGIS Session"
520                 xmlns="http://thuban.intevation.org/dtds/thuban-0.9.dtd">
521                    <dbconnection id="DB"
522                                  dbtype="postgis" dbname="plugh"
523                                  host="xyzzy" port="42"
524                                  user="grue"/>
525                    <dbshapesource id="roads" dbconn="DB" tablename="roads"/>
526                    <map title="Test Map">
527                        <layer title="Roads to Nowhere"
528                               shapestore="roads" visible="true"
529                               stroke="#000000" stroke_width="1" fill="None"/>
530                    </map>
531                </session>'''
532                self.compare_xml(written, expected)
533                self.validate_data(written)
534            finally:
535                session.Destroy()
536    
537    
538  class MockDataStore:  class MockDataStore:
539    
540      """A very simple data store that only has dependencies"""      """A very simple data store that only has dependencies"""

Legend:
Removed from v.1452  
changed lines
  Added in v.1687

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26