/[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 1357 by jonathan, Wed Jul 2 09:38:27 2003 UTC revision 1678 by bh, Thu Aug 28 13:36:23 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.8.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          if 0:          list1 = xmlsupport.sax_eventlist(xml1, ids = self.thubanids,
93              for a, b in zip(sax_eventlist(xml1, self.thubanids,                                           idrefs = self.thubanidrefs)
94                                          self.thubanidrefs),          list2 = xmlsupport.sax_eventlist(xml2, ids = self.thubanids,
95                            sax_eventlist(xml2, self.thubanids,                                           idrefs = self.thubanidrefs)
96                                          self.thubanidrefs)):          if list1 != list2:
97                print a == b and ' ' or '*****'              for a, b in zip(list1, list2):
98                print a                  if a != b:
99                print b                      self.fail("%r != %r" % (a, b))
100          self.assertEquals(xmlsupport.sax_eventlist(xml1, ids = self.thubanids,  
                                                    idrefs = self.thubanidrefs),  
                           xmlsupport.sax_eventlist(xml2, ids = self.thubanids,  
                                                    idrefs = self.thubanidrefs))  
101    
102      def testEmptySession(self):      def testEmptySession(self):
103          """Save an empty session"""          """Save an empty session"""
# Line 104  class SaveSessionTest(unittest.TestCase, Line 111  class SaveSessionTest(unittest.TestCase,
111          file.close()          file.close()
112          self.compare_xml(written_contents,          self.compare_xml(written_contents,
113                           '<?xml version="1.0" encoding="UTF-8"?>\n'                           '<?xml version="1.0" encoding="UTF-8"?>\n'
114                           '<!DOCTYPE session SYSTEM "thuban-0.8.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.8.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 131  class SaveSessionTest(unittest.TestCase, Line 138  class SaveSessionTest(unittest.TestCase,
138          written_contents = file.read()          written_contents = file.read()
139          file.close()          file.close()
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.8.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.8.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 171  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 185  class SaveSessionTest(unittest.TestCase, Line 192  class SaveSessionTest(unittest.TestCase,
192    
193          filename = self.temp_file_name("save_layerproj.thuban")          filename = self.temp_file_name("save_layerproj.thuban")
194          save_session(session, filename)          save_session(session, filename)
         session.Destroy()  
195    
196          file = open(filename)          file = open(filename)
197          written_contents = file.read()          written_contents = file.read()
198          file.close()          file.close()
199          expected_contents = '''<?xml version="1.0" encoding="UTF-8"?>          expected_contents = '''<?xml version="1.0" encoding="UTF-8"?>
200          <!DOCTYPE session SYSTEM "thuban-0.8.dtd">          <!DOCTYPE session SYSTEM "thuban-0.9.dtd">
201          <session title="single map&amp;layer"          <session title="single map&amp;layer"
202             xmlns="http://thuban.intevation.org/dtds/thuban-0.8.dtd">             xmlns="http://thuban.intevation.org/dtds/thuban-0.9.dtd">
203              <fileshapesource id="D1" filename="%s" filetype="shapefile"/>              <fileshapesource id="D1" filename="%s" filetype="shapefile"/>
204              <map title="Test Map">              <map title="Test Map">
205                  <projection name="Unknown">                  <projection name="Unknown">
# Line 229  class SaveSessionTest(unittest.TestCase, Line 235  class SaveSessionTest(unittest.TestCase,
235          layer = RasterLayer("My RasterLayer", imgfile)          layer = RasterLayer("My RasterLayer", imgfile)
236          map.AddLayer(layer)          map.AddLayer(layer)
237    
238          filename = self.temp_file_name("save_singlemap.thuban")          filename = self.temp_file_name("%s.thuban" % self.id())
239          save_session(session, filename)          save_session(session, filename)
240          session.Destroy()          session.Destroy()
241    
# Line 237  class SaveSessionTest(unittest.TestCase, Line 243  class SaveSessionTest(unittest.TestCase,
243          written_contents = file.read()          written_contents = file.read()
244          file.close()          file.close()
245          expected_contents = '''<?xml version="1.0" encoding="UTF-8"?>          expected_contents = '''<?xml version="1.0" encoding="UTF-8"?>
246          <!DOCTYPE session SYSTEM "thuban-0.8.dtd">          <!DOCTYPE session SYSTEM "thuban-0.9.dtd">
247          <session title="single map&amp;layer"          <session title="single map&amp;layer"
248             xmlns="http://thuban.intevation.org/dtds/thuban-0.8.dtd">             xmlns="http://thuban.intevation.org/dtds/thuban-0.9.dtd">
249              <map title="Test Map">              <map title="Test Map">
250                  <rasterlayer title="My RasterLayer" filename="%s"                  <rasterlayer title="My RasterLayer" filename="%s"
251                               visible="true">                               visible="true">
252                  </rasterlayer>                  </rasterlayer>
253              </map>              </map>
254          </session>''' % os.path.join(os.path.dirname(__file__),          </session>''' % os.path.join(os.pardir, os.pardir, "Data", "iceland",
                                      os.pardir, "Data", "iceland",  
255                                       "island.tif")                                       "island.tif")
256          #print written_contents          #print written_contents
257          #print "********************************************"          #print "********************************************"
# Line 256  class SaveSessionTest(unittest.TestCase, Line 261  class SaveSessionTest(unittest.TestCase,
261          self.validate_data(written_contents)          self.validate_data(written_contents)
262    
263      def testClassifiedLayer(self):      def testClassifiedLayer(self):
264          """Save a session with a single map with a single layer          """Save a session with a single map with classifications"""
            with a classificaton.  
         """  
265          # deliberately put an apersand in the title :)          # deliberately put an apersand in the title :)
266          session = Session("single map&layer")          session = Session("Map with Classifications")
267          proj = Projection(["zone=26", "proj=utm", "ellps=clrk66"])          proj = Projection(["zone=26", "proj=utm", "ellps=clrk66"])
268          map = Map("Test Map", projection = proj)          map = Map("Test Map", projection = proj)
269          session.AddMap(map)          session.AddMap(map)
# Line 269  class SaveSessionTest(unittest.TestCase, Line 272  class SaveSessionTest(unittest.TestCase,
272                                 os.pardir, "Data", "iceland", "political.shp")                                 os.pardir, "Data", "iceland", "political.shp")
273          layer = Layer("My Layer", session.OpenShapefile(shpfile))          layer = Layer("My Layer", session.OpenShapefile(shpfile))
274          map.AddLayer(layer)          map.AddLayer(layer)
275            layer2 = Layer("My Layer", layer.ShapeStore())
276            map.AddLayer(layer2)
277    
278          clazz = layer.GetClassification()          clazz = layer.GetClassification()
279    
280          clazz.SetFieldInfo("AREA", None)          layer.SetClassificationColumn("AREA")
281    
282          clazz.AppendGroup(ClassGroupSingleton(42,          clazz.AppendGroup(ClassGroupSingleton(42, ClassGroupProperties(),
283                                             ClassGroupProperties(),                                                "single"))
284                                             "single"))          clazz.AppendGroup(ClassGroupSingleton("text", ClassGroupProperties(),
285          clazz.AppendGroup(ClassGroupSingleton("text",                                                "single-text"))
                                            ClassGroupProperties(),  
                                            "single-text"))  
286    
287          clazz.AppendGroup(ClassGroupRange((0, 42),          clazz.AppendGroup(ClassGroupRange((0, 42),
288                                             ClassGroupProperties(),                                             ClassGroupProperties(),
# Line 290  class SaveSessionTest(unittest.TestCase, Line 293  class SaveSessionTest(unittest.TestCase,
293          range.SetLabel("new-range")          range.SetLabel("new-range")
294          clazz.AppendGroup(range)          clazz.AppendGroup(range)
295    
296          filename = self.temp_file_name("save_singlemap.thuban")  
297            clazz = layer2.GetClassification()
298            layer2.SetClassificationColumn("POPYCOUN")
299    
300            # Classification with Latin 1 text
301            clazz.AppendGroup(ClassGroupSingleton('\xe4\xf6\xfc', # ae, oe, ue
302                                                  ClassGroupProperties(),
303                                                  '\xdcml\xe4uts')) # Uemlaeuts
304    
305    
306            filename = self.temp_file_name("%s.thuban" % self.id())
307          save_session(session, filename)          save_session(session, filename)
308    
309          file = open(filename)          file = open(filename)
310          written_contents = file.read()          written_contents = file.read()
311          file.close()          file.close()
312          expected_template = '''<?xml version="1.0" encoding="UTF-8"?>          expected_template = '''<?xml version="1.0" encoding="UTF-8"?>
313          <!DOCTYPE session SYSTEM "thuban-0.8.dtd">          <!DOCTYPE session SYSTEM "thuban-0.9.dtd">
314          <session title="single map&amp;layer"          <session title="Map with Classifications"
315             xmlns="http://thuban.intevation.org/dtds/thuban-0.8.dtd">             xmlns="http://thuban.intevation.org/dtds/thuban-0.9.dtd">
316              <fileshapesource id="D1" filename="%s" filetype="shapefile"/>              <fileshapesource id="D1" filename="%s" filetype="shapefile"/>
317              <map title="Test Map">              <map title="Test Map">
318                  <projection name="Unknown">                  <projection name="Unknown">
# Line 327  class SaveSessionTest(unittest.TestCase, Line 340  class SaveSessionTest(unittest.TestCase,
340                          </clrange>                          </clrange>
341                      </classification>                      </classification>
342                  </layer>                  </layer>
343                    <layer title="My Layer" shapestore="D1"
344                    fill="None" stroke="#000000" stroke_width="1" visible="true">
345                        <classification field="POPYCOUN" field_type="string">
346                            <clnull label="">
347                                <cldata fill="None" stroke="#000000" stroke_width="1"/>
348                            </clnull>
349                            <clpoint value="\xc3\xa4\xc3\xb6\xc3\xbc"
350                                 label="\xc3\x9cml\xc3\xa4uts">
351                                <cldata fill="None" stroke="#000000" stroke_width="1"/>
352                            </clpoint>
353                        </classification>
354                    </layer>
355              </map>              </map>
356          </session>'''          </session>'''
357    
# Line 345  class SaveSessionTest(unittest.TestCase, Line 370  class SaveSessionTest(unittest.TestCase,
370    
371      def test_dbf_table(self):      def test_dbf_table(self):
372          """Test saving a session with a dbf table link"""          """Test saving a session with a dbf table link"""
373          session = Session("a DBF Table session")          session = self.session = Session("a DBF Table session")
374          # use shapefile from the example data          # use shapefile from the example data
375          dbffile = os.path.join(os.path.dirname(__file__),          dbffile = os.path.join(os.path.dirname(__file__),
376                                 os.pardir, "Data", "iceland", "political.dbf")                                 os.pardir, "Data", "iceland", "political.dbf")
# Line 358  class SaveSessionTest(unittest.TestCase, Line 383  class SaveSessionTest(unittest.TestCase,
383          written_contents = file.read()          written_contents = file.read()
384          file.close()          file.close()
385          expected_template = '''<?xml version="1.0" encoding="UTF-8"?>          expected_template = '''<?xml version="1.0" encoding="UTF-8"?>
386          <!DOCTYPE session SYSTEM "thuban-0.8.dtd">          <!DOCTYPE session SYSTEM "thuban-0.9.dtd">
387          <session title="a DBF Table session"          <session title="a DBF Table session"
388             xmlns="http://thuban.intevation.org/dtds/thuban-0.8.dtd">             xmlns="http://thuban.intevation.org/dtds/thuban-0.9.dtd">
389              <filetable id="D1" filename="%s" filetype="DBF" title="political"/>              <filetable id="D1" filename="%s" filetype="DBF" title="political"/>
390          </session>'''          </session>'''
391    
392          expected_contents = expected_template % dbffile          expected_contents = (expected_template
393                                 % os.path.join(os.pardir, os.pardir, "Data",
394                                                "iceland", "political.dbf"))
395          self.compare_xml(written_contents, expected_contents)          self.compare_xml(written_contents, expected_contents)
396            self.validate_data(written_contents)
397    
398      def test_joined_table(self):      def test_joined_table(self):
399          """Test saving a session with joined table"""          """Test saving a session with joined table"""
# Line 401  class SaveSessionTest(unittest.TestCase, Line 429  class SaveSessionTest(unittest.TestCase,
429              #    print col.name              #    print col.name
430              joined = TransientJoinedTable(session.TransientDB(),              joined = TransientJoinedTable(session.TransientDB(),
431                                            store.Table(), "RDLNTYPE",                                            store.Table(), "RDLNTYPE",
432                                            dbftable, "RDTYPE")                                            dbftable, "RDTYPE",
433                                              outer_join = True)
434              store = session.AddShapeStore(DerivedShapeStore(store, joined))              store = session.AddShapeStore(DerivedShapeStore(store, joined))
435              layer.SetShapeStore(store)              layer.SetShapeStore(store)
436    
# Line 414  class SaveSessionTest(unittest.TestCase, Line 443  class SaveSessionTest(unittest.TestCase,
443              written_contents = file.read()              written_contents = file.read()
444              file.close()              file.close()
445              expected_template = '''<?xml version="1.0" encoding="UTF-8"?>              expected_template = '''<?xml version="1.0" encoding="UTF-8"?>
446              <!DOCTYPE session SYSTEM "thuban-0.8.dtd">              <!DOCTYPE session SYSTEM "thuban-0.9.dtd">
447              <session title="A Joined Table session"              <session title="A Joined Table session"
448               xmlns="http://thuban.intevation.org/dtds/thuban-0.8.dtd">               xmlns="http://thuban.intevation.org/dtds/thuban-0.9.dtd">
449                  <fileshapesource filename="%(shpfile)s"                  <fileshapesource filename="%(shpfile)s"
450                                   filetype="shapefile" id="D142197204"/>                                   filetype="shapefile" id="D142197204"/>
451                  <filetable filename="%(dbffile)s"                  <filetable filename="%(dbffile)s"
# Line 425  class SaveSessionTest(unittest.TestCase, Line 454  class SaveSessionTest(unittest.TestCase,
454                  <jointable id="D142180284"                  <jointable id="D142180284"
455                             title="Join of roads-line and save_joinedtable"                             title="Join of roads-line and save_joinedtable"
456                             leftcolumn="RDLNTYPE" left="D142197204"                             leftcolumn="RDLNTYPE" left="D142197204"
457                             rightcolumn="RDTYPE" right="D141881756"/>                             rightcolumn="RDTYPE" right="D141881756"
458                               jointype="LEFT OUTER" />
459                  <derivedshapesource id="D141915644"                  <derivedshapesource id="D141915644"
460                                      table="D142180284"                                      table="D142180284"
461                                      shapesource="D142197204"/>                                      shapesource="D142197204"/>
# Line 441  class SaveSessionTest(unittest.TestCase, Line 471  class SaveSessionTest(unittest.TestCase,
471                  "shpfile": relative_filename(self.temp_dir(), shpfile)                  "shpfile": relative_filename(self.temp_dir(), shpfile)
472                  }                  }
473              self.compare_xml(written_contents, expected_contents)              self.compare_xml(written_contents, expected_contents)
474                self.validate_data(written_contents)
475          finally:          finally:
476              session.Destroy()              session.Destroy()
477              session = None              session = None
478    
479    
480        def test_save_postgis(self):
481            """Test saving a session with a postgis connection"""
482    
483            class NonConnection(PostGISConnection):
484                """connection class that doesn't actually connect """
485                def connect(self):
486                    pass
487    
488            class NonConnectionStore(PostGISShapeStore):
489                """Shapestore that doesn't try to access the server"""
490                def _fetch_table_information(self):
491                    pass
492    
493            session = Session("A PostGIS Session")
494            try:
495                dbconn = NonConnection(dbname="plugh", host="xyzzy", port="42",
496                                       user="grue")
497                session.AddDBConnection(dbconn)
498                map = Map("Test Map")
499                session.AddMap(map)
500                store = NonConnectionStore(dbconn, "roads")
501                session.AddShapeStore(store)
502                layer = Layer("Roads to Nowhere", store)
503                map.AddLayer(layer)
504    
505                # Save the session
506                filename = self.temp_file_name(self.id() + ".thuban")
507                save_session(session, filename)
508    
509                # Read it back and compare
510                file = open(filename)
511                written = file.read()
512                file.close()
513                expected = '''<?xml version="1.0" encoding="UTF-8"?>
514                <!DOCTYPE session SYSTEM "thuban-0.9.dtd">
515                <session title="A PostGIS Session"
516                 xmlns="http://thuban.intevation.org/dtds/thuban-0.9.dtd">
517                    <dbconnection id="DB"
518                                  dbtype="postgis" dbname="plugh"
519                                  host="xyzzy" port="42"
520                                  user="grue"/>
521                    <dbshapesource id="roads" dbconn="DB" tablename="roads"/>
522                    <map title="Test Map">
523                        <layer title="Roads to Nowhere"
524                               shapestore="roads" visible="true"
525                               stroke="#000000" stroke_width="1" fill="None"/>
526                    </map>
527                </session>'''
528                self.compare_xml(written, expected)
529                self.validate_data(written)
530            finally:
531                session.Destroy()
532    
533    
534  class MockDataStore:  class MockDataStore:
535    
536      """A very simple data store that only has dependencies"""      """A very simple data store that only has dependencies"""

Legend:
Removed from v.1357  
changed lines
  Added in v.1678

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26