/[thuban]/branches/WIP-pyshapelib-bramz/test/test_load.py
ViewVC logotype

Diff of /branches/WIP-pyshapelib-bramz/test/test_load.py

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1268 by bh, Fri Jun 20 16:10:12 2003 UTC revision 1646 by bh, Mon Aug 25 13:43:56 2003 UTC
# Line 34  import unittest Line 34  import unittest
34  import support  import support
35  support.initthuban()  support.initthuban()
36    
37    import postgissupport
38  from xmlsupport import sax_eventlist  from xmlsupport import sax_eventlist
39    
40  import dbflib  import dbflib
41    
42  from Thuban.Model.save import save_session  from Thuban.Model.save import save_session
43  from Thuban.Model.load import load_session, parse_color, LoadError  from Thuban.Model.load import load_session, parse_color, LoadError, \
44  from Thuban.Model.color import Color       LoadCancelled
45    from Thuban.Model.color import Transparent
46  from Thuban.Model.classification import ClassGroupProperties, ClassGroupRange,\  from Thuban.Model.classification import ClassGroupProperties, ClassGroupRange,\
47      ClassGroupSingleton, ClassGroupDefault      ClassGroupSingleton, ClassGroupDefault
48    from Thuban.Model.postgisdb import ConnectionError
49    
50  def filenames_equal(name1, name2):  def filenames_equal(name1, name2):
51      """Return true if the filenames name1 and name2 are equal.      """Return true if the filenames name1 and name2 are equal.
# Line 80  class LoadSessionTest(support.FileLoadTe Line 82  class LoadSessionTest(support.FileLoadTe
82          self.session = None          self.session = None
83    
84    
85      dtd = "http://thuban.intevation.org/dtds/thuban-0.8.dtd"      dtd = "http://thuban.intevation.org/dtds/thuban-0.9-dev.dtd"
86      thubanids = [((dtd, n), (None, "id")) for n in      thubanids = [((dtd, n), (None, "id")) for n in
87                   ["fileshapesource", "filetable", "jointable",                   ["fileshapesource", "filetable", "jointable",
88                    "derivedshapesource"]]                    "derivedshapesource"]]
# Line 150  class ClassificationTest(LoadSessionTest Line 152  class ClassificationTest(LoadSessionTest
152                  if data[CLASSES][i][GROUP_TYPE] == "default":                  if data[CLASSES][i][GROUP_TYPE] == "default":
153                      g = ClassGroupDefault(props, data[CLASSES][i][GROUP_LABEL])                      g = ClassGroupDefault(props, data[CLASSES][i][GROUP_LABEL])
154                  elif data[CLASSES][i][GROUP_TYPE] == "range":                  elif data[CLASSES][i][GROUP_TYPE] == "range":
155                      g = ClassGroupRange(data[CLASSES][i][GROUP_DATA][0],                      g = ClassGroupRange((data[CLASSES][i][GROUP_DATA][0],
156                                          data[CLASSES][i][GROUP_DATA][1],                                           data[CLASSES][i][GROUP_DATA][1]),
157                                          props, data[CLASSES][i][GROUP_LABEL])                                          props, data[CLASSES][i][GROUP_LABEL])
158                  elif data[CLASSES][i][GROUP_TYPE] == "single":                  elif data[CLASSES][i][GROUP_TYPE] == "single":
159                      g = ClassGroupSingleton(data[CLASSES][i][GROUP_DATA],                      g = ClassGroupSingleton(data[CLASSES][i][GROUP_DATA],
# Line 167  class TestSingleLayer(LoadSessionTest): Line 169  class TestSingleLayer(LoadSessionTest):
169    
170      file_contents = '''\      file_contents = '''\
171  <?xml version="1.0" encoding="UTF-8"?>  <?xml version="1.0" encoding="UTF-8"?>
172  <!DOCTYPE session SYSTEM "thuban-0.8.dtd">  <!DOCTYPE session SYSTEM "thuban-0.9.dtd">
173  <session xmlns="http://thuban.intevation.org/dtds/thuban-0.8.dtd"  <session xmlns="http://thuban.intevation.org/dtds/thuban-0.9-dev.dtd"
174          title="single map&amp;layer">          title="single map&amp;layer">
175      <fileshapesource filetype="shapefile" id="D1"      <fileshapesource filetype="shapefile" id="D1"
176          filename="../../Data/iceland/political.shp"/>          filename="../../Data/iceland/political.shp"/>
# Line 214  class TestSingleLayer(LoadSessionTest): Line 216  class TestSingleLayer(LoadSessionTest):
216                                                       os.pardir, os.pardir,                                                       os.pardir, os.pardir,
217                                                       "Data", "iceland",                                                       "Data", "iceland",
218                                                       "political.shp")))                                                       "political.shp")))
219          eq(layer.GetClassification().GetDefaultFill(), Color.Transparent)          eq(layer.GetClassification().GetDefaultFill(), Transparent)
220          eq(layer.GetClassification().GetDefaultLineColor().hex(), "#000000")          eq(layer.GetClassification().GetDefaultLineColor().hex(), "#000000")
221          eq(layer.Visible(), True)          eq(layer.Visible(), True)
222    
# Line 228  class TestLayerVisibility(LoadSessionTes Line 230  class TestLayerVisibility(LoadSessionTes
230    
231      file_contents = '''\      file_contents = '''\
232  <?xml version="1.0" encoding="UTF-8"?>  <?xml version="1.0" encoding="UTF-8"?>
233  <!DOCTYPE session SYSTEM "thuban-0.8.dtd">  <!DOCTYPE session SYSTEM "thuban-0.9.dtd">
234  <session xmlns="http://thuban.intevation.org/dtds/thuban-0.8.dtd"  <session xmlns="http://thuban.intevation.org/dtds/thuban-0.9-dev.dtd"
235          title="single map&amp;layer">          title="single map&amp;layer">
236      <fileshapesource filetype="shapefile" id="D1"      <fileshapesource filetype="shapefile" id="D1"
237          filename="../../Data/iceland/political.shp"/>          filename="../../Data/iceland/political.shp"/>
# Line 287  class TestClassification(ClassificationT Line 289  class TestClassification(ClassificationT
289                  <clpoint value="1">                  <clpoint value="1">
290                      <cldata stroke="#000000" stroke_width="10" fill="None"/>                      <cldata stroke="#000000" stroke_width="10" fill="None"/>
291                  </clpoint>                  </clpoint>
292                    <clpoint value="\xc3\xa4\xc3\xb6\xc3\xbc"
293                             label="\xc3\x9cml\xc3\xa4uts">
294                        <cldata fill="None" stroke="#000000" stroke_width="1"/>
295                    </clpoint>
296              </classification>              </classification>
297          </layer>          </layer>
298                  <layer title="My Layer 2" stroke_width="1" fill="None"                  <layer title="My Layer 2" stroke_width="1" fill="None"
# Line 321  class TestClassification(ClassificationT Line 327  class TestClassification(ClassificationT
327    
328          map = self.session.Maps()[0] # only one map in the sample          map = self.session.Maps()[0] # only one map in the sample
329    
330          expected = [("My Layer", 2,          expected = [("My Layer", 3,
331                          [("default", (), "",                          [("default", (), "",
332                              ("#000000", 1, "None")),                              ("#000000", 1, "None")),
333                           ("single", "1", "",                           ("single", "1", "",
334                              ("#000000", 2, "None")),                              ("#000000", 2, "None")),
335                           ("single", "1", "",                           ("single", "1", "",
336                              ("#000000", 10, "None"))]),                              ("#000000", 10, "None")),
337                             ("single", "\xe4\xf6\xfc", "\xdcml\xe4uts",
338                                ("#000000", 1, "None"))]),
339                       ("My Layer 2", 4,                       ("My Layer 2", 4,
340                           [("default", (), "",                           [("default", (), "",
341                              ("#000000", 2, "None")),                              ("#000000", 2, "None")),
# Line 347  class TestLabels(ClassificationTest): Line 355  class TestLabels(ClassificationTest):
355    
356      file_contents = '''\      file_contents = '''\
357  <?xml version="1.0" encoding="UTF-8"?>  <?xml version="1.0" encoding="UTF-8"?>
358  <!DOCTYPE session SYSTEM "thuban-0.8.dtd">  <!DOCTYPE session SYSTEM "thuban-0.9.dtd">
359  <session xmlns="http://thuban.intevation.org/dtds/thuban-0.8.dtd"  <session xmlns="http://thuban.intevation.org/dtds/thuban-0.9-dev.dtd"
360          title="single map&amp;layer">          title="single map&amp;layer">
361      <fileshapesource filetype="shapefile" id="D1"      <fileshapesource filetype="shapefile" id="D1"
362          filename="../../Data/iceland/political.shp"/>          filename="../../Data/iceland/political.shp"/>
# Line 395  class TestLayerProjection(LoadSessionTes Line 403  class TestLayerProjection(LoadSessionTes
403    
404      file_contents = '''\      file_contents = '''\
405  <?xml version="1.0" encoding="UTF-8"?>  <?xml version="1.0" encoding="UTF-8"?>
406  <!DOCTYPE session SYSTEM "thuban-0.8.dtd">  <!DOCTYPE session SYSTEM "thuban-0.9.dtd">
407  <session xmlns="http://thuban.intevation.org/dtds/thuban-0.8.dtd"  <session xmlns="http://thuban.intevation.org/dtds/thuban-0.9-dev.dtd"
408          title="single map&amp;layer">          title="single map&amp;layer">
409      <fileshapesource filetype="shapefile" id="D2"      <fileshapesource filetype="shapefile" id="D2"
410          filename="../../Data/iceland/roads-line.shp"/>          filename="../../Data/iceland/roads-line.shp"/>
# Line 469  class TestRasterLayer(LoadSessionTest): Line 477  class TestRasterLayer(LoadSessionTest):
477    
478      file_contents = '''\      file_contents = '''\
479  <?xml version="1.0" encoding="UTF-8"?>  <?xml version="1.0" encoding="UTF-8"?>
480  <!DOCTYPE session SYSTEM "thuban-0.8.dtd">  <!DOCTYPE session SYSTEM "thuban-0.9.dtd">
481  <session xmlns="http://thuban.intevation.org/dtds/thuban-0.8.dtd"  <session xmlns="http://thuban.intevation.org/dtds/thuban-0.9-dev.dtd"
482          title="single map&amp;layer">          title="single map&amp;layer">
483      <map title="Test Map">      <map title="Test Map">
484          <rasterlayer visible="false" filename="../../Data/iceland/island.tif"          <rasterlayer visible="false" filename="../../Data/iceland/island.tif"
# Line 503  class TestRasterLayer(LoadSessionTest): Line 511  class TestRasterLayer(LoadSessionTest):
511  class TestJoinedTable(LoadSessionTest):  class TestJoinedTable(LoadSessionTest):
512    
513      file_contents = '''<?xml version="1.0" encoding="UTF-8"?>      file_contents = '''<?xml version="1.0" encoding="UTF-8"?>
514  <!DOCTYPE session SYSTEM "thuban-0.8.dtd">  <!DOCTYPE session SYSTEM "thuban-0.9.dtd">
515  <session title="A Joined Table session">  <session xmlns="http://thuban.intevation.org/dtds/thuban-0.9-dev.dtd" title="A Joined Table session">
516      <filetable filename="load_joinedtable.dbf"      <fileshapesource filetype="shapefile" id="D137227612"
517                 title="Some Title"          filename="../../Data/iceland/roads-line.shp"/>
518                 filetype="DBF" id="D141881756"/>      <filetable filetype="DBF" filename="load_joinedtable.dbf" id="D136171140"
519      <fileshapesource filename="../../Data/iceland/roads-line.shp"          title="Some Title"/>
520                       filetype="shapefile" id="D142197204"/>      <jointable id="D136169900" title="Joined"
521      <jointable id="D142180284"          right="D136171140" left="D137227612"
522                 title="Joined"          leftcolumn="RDLNTYPE" rightcolumn="RDTYPE"
523                 leftcolumn="RDLNTYPE" left="D142197204"          jointype="LEFT OUTER"/>
524                 rightcolumn="RDTYPE" right="D141881756"/>      <derivedshapesource table="D136169900" shapesource="D137227612"
525      <derivedshapesource id="D141915644"          id="D136170932"/>
                         table="D142180284" shapesource="D142197204"/>  
526      <map title="Test Map">      <map title="Test Map">
527          <layer title="My Layer"          <layer shapestore="D136170932" visible="true" stroke="#000000"
528                 shapestore="D141915644" visible="true"                  title="My Layer" stroke_width="1" fill="None"/>
                stroke="#000000" stroke_width="1" fill="None"/>  
529      </map>      </map>
530  </session>'''  </session>
531    '''
532    
533      def setUp(self):      def setUp(self):
534          """Extend inherited method to create the dbffile for the join"""          """Extend inherited method to create the dbffile for the join"""
# Line 545  class TestJoinedTable(LoadSessionTest): Line 552  class TestJoinedTable(LoadSessionTest):
552          # FIXME: The tests shouldn't assume a certain order of the tables          # FIXME: The tests shouldn't assume a certain order of the tables
553          self.assertEquals(tables[0].Title(), "Some Title")          self.assertEquals(tables[0].Title(), "Some Title")
554          self.assertEquals(tables[1].Title(), "Joined")          self.assertEquals(tables[1].Title(), "Joined")
555            self.assertEquals(tables[1].JoinType(), "LEFT OUTER")
556            self.check_format()
557    
558    
559    
560    class TestPostGISLayer(LoadSessionTest):
561    
562        file_contents = '''<?xml version="1.0" encoding="UTF-8"?>
563    <!DOCTYPE session SYSTEM "thuban-0.9.dtd">
564    <session xmlns="http://thuban.intevation.org/dtds/thuban-0.9-dev.dtd"
565            title="unnamed session">
566        <dbconnection port="%(port)s" host="%(host)s" user="%(user)s"
567            dbtype="postgis" id="D142684948" dbname="%(dbname)s"/>
568        <dbshapesource tablename="landmarks" id="D143149420" dbconn="D142684948"/>
569        <map title="unnamed map">
570            <layer shapestore="D143149420" visible="true" stroke="#000000"
571                    title="landmarks" stroke_width="1" fill="None"/>
572        </map>
573    </session>
574    '''
575    
576        def setUp(self):
577            """Extend the inherited method to start the postgis server
578    
579            Furthermore, patch the file contents with the real postgis db
580            information
581            """
582            postgissupport.skip_if_no_postgis()
583            self.server = postgissupport.get_test_server()
584            self.postgisdb = self.server.get_default_static_data_db()
585    
586            self.file_contents = self.__class__.file_contents % {
587                "dbname": self.postgisdb.dbname,
588                "user": self.server.user_name,
589                "port": self.server.port,
590                "host": self.server.host}
591            LoadSessionTest.setUp(self)
592    
593        def test(self):
594            """Test loading a session containing a postgis shapestore"""
595            session = load_session(self.filename())
596            self.session = session
597            connections = session.DBConnections()
598            self.assertEquals(len(connections), 1)
599            conn = connections[0]
600            for attr, value in [("host", self.server.host),
601                                ("port", str(self.server.port)),
602                                ("user", self.server.user_name),
603                                ("dbname", self.postgisdb.dbname)]:
604                self.assertEquals(getattr(conn, attr), value)
605            layer = session.Maps()[0].Layers()[0]
606            self.failUnless(layer.ShapeStore().DBConnection() is conn)
607    
608    
609    class TestPostGISLayerPassword(LoadSessionTest):
610    
611        file_contents = '''<?xml version="1.0" encoding="UTF-8"?>
612    <!DOCTYPE session SYSTEM "thuban-0.9.dtd">
613    <session xmlns="http://thuban.intevation.org/dtds/thuban-0.9-dev.dtd"
614            title="unnamed session">
615        <dbconnection port="%(port)s" host="%(host)s" user="%(user)s"
616            dbtype="postgis" id="D142684948" dbname="%(dbname)s"/>
617        <dbshapesource tablename="landmarks" id="D143149420" dbconn="D142684948"/>
618        <map title="unnamed map">
619            <layer shapestore="D143149420" visible="true" stroke="#000000"
620                    title="landmarks" stroke_width="1" fill="None"/>
621        </map>
622    </session>
623    '''
624    
625        def setUp(self):
626            """Extend the inherited method to start the postgis server
627    
628            Furthermore, patch the file contents with the real postgis db
629            information
630            """
631            postgissupport.skip_if_no_postgis()
632            self.server = postgissupport.get_test_server()
633            self.postgisdb = self.server.get_default_static_data_db()
634    
635            self.file_contents = self.__class__.file_contents % {
636                "dbname": self.postgisdb.dbname,
637                "user": self.server.user_name,
638                "port": self.server.port,
639                "host": self.server.host}
640            LoadSessionTest.setUp(self)
641    
642            self.db_connection_callback_called = False
643            self.server.require_authentication(True)
644    
645        def tearDown(self):
646            """Extend the inherited method to switch off postgresql authentication
647            """
648            self.server.require_authentication(False)
649            LoadSessionTest.tearDown(self)
650    
651        def db_connection_callback(self, params, message):
652            """Implementation of Thuban.Model.hooks.query_db_connection_parameters
653            """
654            self.assertEquals(params,
655                              {"dbname": self.postgisdb.dbname,
656                               "user": self.server.user_name,
657                               "port": str(self.server.port),
658                               "host": self.server.host})
659            self.db_connection_callback_called = True
660            params = params.copy()
661            params["password"] = self.server.user_password
662            return params
663    
664        def test_with_callback(self):
665            """Test loading a session with postgis, authentication and a callback
666            """
667            session = load_session(self.filename(),
668                          db_connection_callback = self.db_connection_callback)
669            self.session = session
670            connections = session.DBConnections()
671            self.assertEquals(len(connections), 1)
672            conn = connections[0]
673            for attr, value in [("host", self.server.host),
674                                ("port", str(self.server.port)),
675                                ("user", self.server.user_name),
676                                ("dbname", self.postgisdb.dbname)]:
677                self.assertEquals(getattr(conn, attr), value)
678            layer = session.Maps()[0].Layers()[0]
679            self.failUnless(layer.ShapeStore().DBConnection() is conn)
680            self.failUnless(self.db_connection_callback_called)
681    
682        def test_without_callback(self):
683            """Test loading a session with postgis, authentication and no callback
684            """
685            # A password is required and there's no callback, so we should
686            # get a ConnectionError
687            self.assertRaises(ConnectionError, load_session, self.filename())
688    
689        def test_cancel(self):
690            """Test loading a session with postgis and cancelling authentication
691            """
692            def cancel(*args):
693                self.db_connection_callback_called = True
694                return None
695    
696            # If the user cancels, i.e. if the callbakc returns None, a
697            # LoadCancelled exception is raised.
698            self.assertRaises(LoadCancelled,
699                              load_session, self.filename(), cancel)
700            self.failUnless(self.db_connection_callback_called)
701    
702    
703  class TestLoadError(LoadSessionTest):  class TestLoadError(LoadSessionTest):
704    
705      file_contents = '''\      file_contents = '''\
706  <?xml version="1.0" encoding="UTF-8"?>  <?xml version="1.0" encoding="UTF-8"?>
707  <!DOCTYPE session SYSTEM "thuban-0.8.dtd">  <!DOCTYPE session SYSTEM "thuban-0.9.dtd">
708  <session xmlns="http://thuban.intevation.org/dtds/thuban-0.8.dtd"  <session xmlns="http://thuban.intevation.org/dtds/thuban-0.9-dev.dtd"
709          title="single map&amp;layer">          title="single map&amp;layer">
710      <fileshapesource id="D1" filename="../../Data/iceland/political.shp"/>      <fileshapesource id="D1" filename="../../Data/iceland/political.shp"/>
711      <map title="Test Map">      <map title="Test Map">
# Line 575  class TestLoadError(LoadSessionTest): Line 728  class TestLoadError(LoadSessionTest):
728          try:          try:
729              self.session = load_session(self.filename())              self.session = load_session(self.filename())
730          except LoadError, value:          except LoadError, value:
731              pass              # Check the actual messge in value to make sure the
732                # LoadError really was about the missing attribute
733                self.assertEquals(str(value),
734                  "Element "
735                  "(u'http://thuban.intevation.org/dtds/thuban-0.9-dev.dtd',"
736                  " u'fileshapesource') requires an attribute 'filetype'")
737          else:          else:
738              self.fail("Missing filetype attribute doesn't raise LoadError")              self.fail("Missing filetype attribute doesn't raise LoadError")
739    

Legend:
Removed from v.1268  
changed lines
  Added in v.1646

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26