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

Legend:
Removed from v.1348  
changed lines
  Added in v.1677

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26