/[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 2059 by bh, Wed Feb 11 09:05:40 2004 UTC revision 2642 by bh, Fri Jul 1 20:49:04 2005 UTC
# Line 1  Line 1 
1  # Copyright (c) 2002, 2003, 2004 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 25  support.initthuban() Line 25  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 53  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 65  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-1.0.0.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", "dbshapesource", "dbconnection"]]                    "derivedshapesource", "dbshapesource", "dbconnection"]]
# Line 111  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-1.0.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-1.0.0.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 141  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-1.0.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-1.0.0.dtd">             xmlns="http://thuban.intevation.org/dtds/thuban-1.1-dev.dtd">
148              <fileshapesource id="D1"              <fileshapesource id="D1"
149                  filename="../../Data/iceland/political.shp"                  filename="../../Data/iceland/political.shp"
150                  filetype="shapefile"/>                  filetype="shapefile"/>
# Line 203  class SaveSessionTest(unittest.TestCase, Line 204  class SaveSessionTest(unittest.TestCase,
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-1.0.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-1.0.0.dtd">             xmlns="http://thuban.intevation.org/dtds/thuban-1.1-dev.dtd">
210              <fileshapesource id="D1"              <fileshapesource id="D1"
211                  filename="../../Data/iceland/political.shp"                  filename="../../Data/iceland/political.shp"
212                  filetype="shapefile"/>                  filetype="shapefile"/>
# Line 234  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("%s.thuban" % self.id())          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-1.0.dtd">  
         <session title="single map&amp;layer"  
            xmlns="http://thuban.intevation.org/dtds/thuban-1.0.0.dtd">  
             <map title="Test Map">  
                 <rasterlayer title="My RasterLayer"  
                         filename="../../Data/iceland/island.tif"  
                         visible="true">  
                 </rasterlayer>  
             </map>  
         </session>'''  
         #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 classifications"""          """Save a session with a single map with classifications"""
# Line 307  class SaveSessionTest(unittest.TestCase, Line 326  class SaveSessionTest(unittest.TestCase,
326          layer2.SetClassificationColumn("POPYCOUN")          layer2.SetClassificationColumn("POPYCOUN")
327    
328          # Classification with Latin 1 text          # Classification with Latin 1 text
329          clazz.AppendGroup(ClassGroupSingleton('\xe4\xf6\xfc', # ae, oe, ue          clazz.AppendGroup(ClassGroupSingleton(
330                                                ClassGroupProperties(),              internal_from_unicode(u'\xe4\xf6\xfc'), # ae, oe, ue
331                                                '\xdcml\xe4uts')) # Uemlaeuts              ClassGroupProperties(),
332                internal_from_unicode(u'\xdcml\xe4uts'))) # Uemlaeuts
333    
334    
335          filename = self.temp_file_name("%s.thuban" % self.id())          filename = self.temp_file_name("%s.thuban" % self.id())
# Line 319  class SaveSessionTest(unittest.TestCase, Line 339  class SaveSessionTest(unittest.TestCase,
339          written_contents = file.read()          written_contents = file.read()
340          file.close()          file.close()
341          expected_contents = '''<?xml version="1.0" encoding="UTF-8"?>          expected_contents = '''<?xml version="1.0" encoding="UTF-8"?>
342          <!DOCTYPE session SYSTEM "thuban-1.0.dtd">          <!DOCTYPE session SYSTEM "thuban-1.1.dtd">
343          <session title="Map with Classifications"          <session title="Map with Classifications"
344             xmlns="http://thuban.intevation.org/dtds/thuban-1.0.0.dtd">             xmlns="http://thuban.intevation.org/dtds/thuban-1.1-dev.dtd">
345              <fileshapesource id="D1"              <fileshapesource id="D1"
346                  filename="../../Data/iceland/political.shp"                  filename="../../Data/iceland/political.shp"
347                  filetype="shapefile"/>                  filetype="shapefile"/>
# Line 390  class SaveSessionTest(unittest.TestCase, Line 410  class SaveSessionTest(unittest.TestCase,
410          written_contents = file.read()          written_contents = file.read()
411          file.close()          file.close()
412          expected_contents = '''<?xml version="1.0" encoding="UTF-8"?>          expected_contents = '''<?xml version="1.0" encoding="UTF-8"?>
413          <!DOCTYPE session SYSTEM "thuban-1.0.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-1.0.0.dtd">             xmlns="http://thuban.intevation.org/dtds/thuban-1.1-dev.dtd">
416              <filetable id="D1" filename="../../Data/iceland/political.dbf"              <filetable id="D1" filename="../../Data/iceland/political.dbf"
417                  filetype="DBF" title="political"/>                  filetype="DBF" title="political"/>
418          </session>'''          </session>'''
# Line 448  class SaveSessionTest(unittest.TestCase, Line 468  class SaveSessionTest(unittest.TestCase,
468              written_contents = file.read()              written_contents = file.read()
469              file.close()              file.close()
470              expected_contents = '''<?xml version="1.0" encoding="UTF-8"?>              expected_contents = '''<?xml version="1.0" encoding="UTF-8"?>
471              <!DOCTYPE session SYSTEM "thuban-1.0.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-1.0.0.dtd">               xmlns="http://thuban.intevation.org/dtds/thuban-1.1-dev.dtd">
474                  <fileshapesource filename="../../Data/iceland/roads-line.shp"                  <fileshapesource filename="../../Data/iceland/roads-line.shp"
475                                   filetype="shapefile" id="D142197204"/>                                   filetype="shapefile" id="D142197204"/>
476                  <filetable filename="save_joinedtable.dbf"                  <filetable filename="save_joinedtable.dbf"
# Line 491  class SaveSessionTest(unittest.TestCase, Line 511  class SaveSessionTest(unittest.TestCase,
511              def _fetch_table_information(self):              def _fetch_table_information(self):
512                  # pretend that we've found a geometry column                  # pretend that we've found a geometry column
513                  self.geometry_column = "the_geom"                  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")          session = Session("A PostGIS Session")
521          try:          try:
# Line 513  class SaveSessionTest(unittest.TestCase, Line 538  class SaveSessionTest(unittest.TestCase,
538              written = file.read()              written = file.read()
539              file.close()              file.close()
540              expected = '''<?xml version="1.0" encoding="UTF-8"?>              expected = '''<?xml version="1.0" encoding="UTF-8"?>
541              <!DOCTYPE session SYSTEM "thuban-1.0.dtd">              <!DOCTYPE session SYSTEM "thuban-1.1.dtd">
542              <session title="A PostGIS Session"              <session title="A PostGIS Session"
543               xmlns="http://thuban.intevation.org/dtds/thuban-1.0.0.dtd">               xmlns="http://thuban.intevation.org/dtds/thuban-1.1-dev.dtd">
544                  <dbconnection id="DB"                  <dbconnection id="DB"
545                                dbtype="postgis" dbname="plugh"                                dbtype="postgis" dbname="plugh"
546                                host="xyzzy" port="42"                                host="xyzzy" port="42"
547                                user="grue"/>                                user="grue"/>
548                  <dbshapesource id="roads" dbconn="DB" tablename="roads"/>                  <dbshapesource id="roads" dbconn="DB" tablename="roads"
549                                   id_column="gid" geometry_column="the_geom"/>
550                  <map title="Test Map">                  <map title="Test Map">
551                      <layer title="Roads to Nowhere"                      <layer title="Roads to Nowhere"
552                             shapestore="roads" visible="true"                             shapestore="roads" visible="true"

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26