/[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 1247 by bh, Thu Jun 19 19:53:36 2003 UTC revision 1375 by bh, Tue Jul 8 10:53:05 2003 UTC
# Line 17  Maintenance of the test cases: Line 17  Maintenance of the test cases:
17    
18  When during a development period the file format is changed with respect  When during a development period the file format is changed with respect
19  to the last released version for the first, the tests here should be  to the last released version for the first, the tests here should be
20  copied to the version specific test file.  copied to the version specific test file. The round-trip tests which
21    save the session again and compare the XML files should not be copied
22    over as they only make sense here to make sure th that the files checked
23    here are actually ones that may have been written by the current thuban
24    version.
25  """  """
26    
27  __version__ = "$Revision$"  __version__ = "$Revision$"
# Line 30  import unittest Line 34  import unittest
34  import support  import support
35  support.initthuban()  support.initthuban()
36    
37  from Thuban.Model.load import load_session, parse_color  from xmlsupport import sax_eventlist
38  from Thuban.Model.color import Color  
39    import dbflib
40    
41    from Thuban.Model.save import save_session
42    from Thuban.Model.load import load_session, parse_color, LoadError
43    from Thuban.Model.color import Transparent
44  from Thuban.Model.classification import ClassGroupProperties, ClassGroupRange,\  from Thuban.Model.classification import ClassGroupProperties, ClassGroupRange,\
45      ClassGroupSingleton, ClassGroupDefault      ClassGroupSingleton, ClassGroupDefault
46    
# Line 71  class LoadSessionTest(support.FileLoadTe Line 80  class LoadSessionTest(support.FileLoadTe
80          self.session = None          self.session = None
81    
82    
83        dtd = "http://thuban.intevation.org/dtds/thuban-0.9-dev.dtd"
84        thubanids = [((dtd, n), (None, "id")) for n in
85                     ["fileshapesource", "filetable", "jointable",
86                      "derivedshapesource"]]
87        thubanidrefs = [((dtd, n), (None, m)) for n, m in
88                        [("layer", "shapestore"),
89                         ("jointable", "left"),
90                         ("jointable", "right"),
91                         ("derivedshapesource", "table"),
92                         ("derivedshapesource", "shapesource")]]
93        del n, m, dtd
94    
95        def check_format(self):
96            """Check whether the file we loaded from matches the one that
97            would be written. Call this from each test case after loading
98            the session
99            """
100            filename = self.temp_file_name(self.id() + ".roundtrip.thuban")
101            save_session(self.session, filename)
102            el1 = sax_eventlist(filename = filename, ids = self.thubanids,
103                                idrefs = self.thubanidrefs)
104            el2 = sax_eventlist(filename = self.filename(), ids = self.thubanids,
105                                idrefs = self.thubanidrefs)
106            if 0:
107                for a, b in zip(el1, el2):
108                    print a != b and "***************" or ""
109                    print a
110                    print b
111            self.assertEquals(el1, el2,
112                              "loaded file not equivalent to the saved file")
113    
114    
115  class ClassificationTest(LoadSessionTest):  class ClassificationTest(LoadSessionTest):
116    
117      """      """
# Line 109  class ClassificationTest(LoadSessionTest Line 150  class ClassificationTest(LoadSessionTest
150                  if data[CLASSES][i][GROUP_TYPE] == "default":                  if data[CLASSES][i][GROUP_TYPE] == "default":
151                      g = ClassGroupDefault(props, data[CLASSES][i][GROUP_LABEL])                      g = ClassGroupDefault(props, data[CLASSES][i][GROUP_LABEL])
152                  elif data[CLASSES][i][GROUP_TYPE] == "range":                  elif data[CLASSES][i][GROUP_TYPE] == "range":
153                      g = ClassGroupRange(data[CLASSES][i][GROUP_DATA][0],                      g = ClassGroupRange((data[CLASSES][i][GROUP_DATA][0],
154                                          data[CLASSES][i][GROUP_DATA][1],                                           data[CLASSES][i][GROUP_DATA][1]),
155                                          props, data[CLASSES][i][GROUP_LABEL])                                          props, data[CLASSES][i][GROUP_LABEL])
156                  elif data[CLASSES][i][GROUP_TYPE] == "single":                  elif data[CLASSES][i][GROUP_TYPE] == "single":
157                      g = ClassGroupSingleton(data[CLASSES][i][GROUP_DATA],                      g = ClassGroupSingleton(data[CLASSES][i][GROUP_DATA],
# Line 126  class TestSingleLayer(LoadSessionTest): Line 167  class TestSingleLayer(LoadSessionTest):
167    
168      file_contents = '''\      file_contents = '''\
169  <?xml version="1.0" encoding="UTF-8"?>  <?xml version="1.0" encoding="UTF-8"?>
170  <!DOCTYPE session SYSTEM "thuban.dtd">  <!DOCTYPE session SYSTEM "thuban-0.9.dtd">
171  <session title="single map&amp;layer">  <session xmlns="http://thuban.intevation.org/dtds/thuban-0.9-dev.dtd"
172          <map title="Test Map">          title="single map&amp;layer">
173                  <projection>      <fileshapesource filetype="shapefile" id="D1"
174                          <parameter value="zone=26"/>          filename="../../Data/iceland/political.shp"/>
175                          <parameter value="proj=utm"/>      <map title="Test Map">
176                          <parameter value="ellps=clrk66"/>          <projection name="Unknown">
177                  </projection>              <parameter value="zone=26"/>
178                  <layer title="My Layer" stroke_width="1" fill="None"              <parameter value="proj=utm"/>
179                      filename="../../Data/iceland/political.shp"              <parameter value="ellps=clrk66"/>
180                      stroke="#000000"/>          </projection>
181          </map>          <layer shapestore="D1" visible="true"
182                    stroke="#000000" title="My Layer" stroke_width="1"
183                    fill="None"/>
184        </map>
185  </session>  </session>
186  '''  '''
187    
# Line 170  class TestSingleLayer(LoadSessionTest): Line 214  class TestSingleLayer(LoadSessionTest):
214                                                       os.pardir, os.pardir,                                                       os.pardir, os.pardir,
215                                                       "Data", "iceland",                                                       "Data", "iceland",
216                                                       "political.shp")))                                                       "political.shp")))
217          eq(layer.GetClassification().GetDefaultFill(), Color.Transparent)          eq(layer.GetClassification().GetDefaultFill(), Transparent)
218          eq(layer.GetClassification().GetDefaultLineColor().hex(), "#000000")          eq(layer.GetClassification().GetDefaultLineColor().hex(), "#000000")
219          eq(layer.Visible(), True)          eq(layer.Visible(), True)
220    
221            self.check_format()
222    
223          self.session.Destroy()          self.session.Destroy()
224          self.session = None          self.session = None
225    
# Line 182  class TestLayerVisibility(LoadSessionTes Line 228  class TestLayerVisibility(LoadSessionTes
228    
229      file_contents = '''\      file_contents = '''\
230  <?xml version="1.0" encoding="UTF-8"?>  <?xml version="1.0" encoding="UTF-8"?>
231  <!DOCTYPE session SYSTEM "thuban.dtd">  <!DOCTYPE session SYSTEM "thuban-0.9.dtd">
232  <session title="single map&amp;layer">  <session xmlns="http://thuban.intevation.org/dtds/thuban-0.9-dev.dtd"
233          <map title="Test Map">          title="single map&amp;layer">
234                  <projection>      <fileshapesource filetype="shapefile" id="D1"
235                          <parameter value="zone=26"/>          filename="../../Data/iceland/political.shp"/>
236                          <parameter value="proj=utm"/>      <map title="Test Map">
237                          <parameter value="ellps=clrk66"/>          <projection name="Unknown">
238                  </projection>              <parameter value="zone=26"/>
239                  <layer title="My Layer" stroke_width="1" fill="None"              <parameter value="proj=utm"/>
240                      filename="../../Data/iceland/political.shp"              <parameter value="ellps=clrk66"/>
241                      stroke="#000000" visible="false">          </projection>
242          </layer>          <layer shapestore="D1" visible="false" stroke="#000000"
243                    title="My Layer" stroke_width="1" fill="None"/>
244      </map>      </map>
245  </session>  </session>
246  '''  '''
# Line 212  class TestLayerVisibility(LoadSessionTes Line 259  class TestLayerVisibility(LoadSessionTes
259    
260          eq(layer.Visible(), False)          eq(layer.Visible(), False)
261    
262            self.check_format()
263    
264    
265  class TestClassification(ClassificationTest):  class TestClassification(ClassificationTest):
# Line 300  class TestLabels(ClassificationTest): Line 347  class TestLabels(ClassificationTest):
347    
348      file_contents = '''\      file_contents = '''\
349  <?xml version="1.0" encoding="UTF-8"?>  <?xml version="1.0" encoding="UTF-8"?>
350  <!DOCTYPE session SYSTEM "thuban.dtd">  <!DOCTYPE session SYSTEM "thuban-0.9.dtd">
351  <session title="single map&amp;layer">  <session xmlns="http://thuban.intevation.org/dtds/thuban-0.9-dev.dtd"
352          <map title="Test Map">          title="single map&amp;layer">
353                  <projection>      <fileshapesource filetype="shapefile" id="D1"
354                          <parameter value="zone=26"/>          filename="../../Data/iceland/political.shp"/>
355                          <parameter value="proj=utm"/>      <map title="Test Map">
356                          <parameter value="ellps=clrk66"/>          <projection name="Unknown">
357                  </projection>              <parameter value="zone=26"/>
358                  <layer title="My Layer" stroke_width="1" fill="None"              <parameter value="proj=utm"/>
359                      filename="../../Data/iceland/political.shp"              <parameter value="ellps=clrk66"/>
360                      stroke="#000000">          </projection>
361            <layer shapestore="D1" visible="true" stroke="#000000"
362                    title="My Layer" stroke_width="1" fill="None">
363              <classification field="POPYREG" field_type="string">              <classification field="POPYREG" field_type="string">
364                  <clnull label="hallo">                  <clnull label="hallo">
365                      <cldata stroke="#000000" stroke_width="1" fill="None"/>                      <cldata stroke="#000000" stroke_width="1" fill="None"/>
# Line 339  class TestLabels(ClassificationTest): Line 388  class TestLabels(ClassificationTest):
388                              ("#000000", 2, "None"))])]                              ("#000000", 2, "None"))])]
389    
390          self.TestLayers(map.Layers(), expected)          self.TestLayers(map.Layers(), expected)
391            self.check_format()
392    
393    
394  class TestLayerProjection(LoadSessionTest):  class TestLayerProjection(LoadSessionTest):
395    
396      file_contents = '''\      file_contents = '''\
397  <?xml version="1.0" encoding="UTF-8"?>  <?xml version="1.0" encoding="UTF-8"?>
398  <!DOCTYPE session SYSTEM "thuban.dtd">  <!DOCTYPE session SYSTEM "thuban-0.9.dtd">
399  <session title="single map&amp;layer">  <session xmlns="http://thuban.intevation.org/dtds/thuban-0.9-dev.dtd"
400          <map title="Test Map">          title="single map&amp;layer">
401                  <projection>      <fileshapesource filetype="shapefile" id="D2"
402                          <parameter value="zone=26"/>          filename="../../Data/iceland/roads-line.shp"/>
403                          <parameter value="proj=utm"/>      <fileshapesource filetype="shapefile" id="D4"
404                          <parameter value="ellps=clrk66"/>          filename="../../Data/iceland/political.shp"/>
405                  </projection>      <map title="Test Map">
406                  <layer title="My Layer" stroke_width="1" fill="None"          <projection name="Unknown">
407                      filename="../../Data/iceland/political.shp"              <parameter value="zone=26"/>
408                      stroke="#000000">              <parameter value="proj=utm"/>
409                      <projection name="hello">              <parameter value="ellps=clrk66"/>
410                          <parameter value="zone=13"/>          </projection>
411                          <parameter value="proj=tmerc"/>          <layer shapestore="D4" visible="true" stroke="#000000"
412                          <parameter value="ellps=clrk66"/>                  title="My Layer" stroke_width="1" fill="None">
413                      </projection>              <projection name="hello">
414                    <parameter value="zone=13"/>
415                    <parameter value="proj=tmerc"/>
416                    <parameter value="ellps=clrk66"/>
417                </projection>
418              <classification field="POPYREG" field_type="string">              <classification field="POPYREG" field_type="string">
419                  <clnull label="hallo">                  <clnull label="hallo">
420                      <cldata stroke="#000000" stroke_width="1" fill="None"/>                      <cldata stroke="#000000" stroke_width="1" fill="None"/>
# Line 370  class TestLayerProjection(LoadSessionTes Line 424  class TestLayerProjection(LoadSessionTes
424                  </clpoint>                  </clpoint>
425              </classification>              </classification>
426          </layer>          </layer>
427                  <layer title="My Layer" stroke_width="1" fill="None"          <layer shapestore="D2" visible="true" stroke="#000000"
428                      filename="../../Data/iceland/political.shp"                  title="My Layer" stroke_width="1" fill="None">
429                      stroke="#000000">              <projection name="Unknown">
430                      <projection>                  <parameter value="proj=lcc"/>
431                          <parameter value="proj=lcc"/>                  <parameter value="ellps=clrk66"/>
432                          <parameter value="ellps=clrk66"/>              </projection>
                     </projection>  
433          </layer>          </layer>
434      </map>      </map>
435  </session>  </session>
# Line 409  class TestLayerProjection(LoadSessionTes Line 462  class TestLayerProjection(LoadSessionTes
462          eq(proj.GetParameter("proj"), "lcc")          eq(proj.GetParameter("proj"), "lcc")
463          eq(proj.GetParameter("ellps"), "clrk66")          eq(proj.GetParameter("ellps"), "clrk66")
464    
465            self.check_format()
466    
467    
468  class TestRasterLayer(LoadSessionTest):  class TestRasterLayer(LoadSessionTest):
469    
470      file_contents = '''\      file_contents = '''\
471  <?xml version="1.0" encoding="UTF-8"?>  <?xml version="1.0" encoding="UTF-8"?>
472  <!DOCTYPE session SYSTEM "thuban.dtd">  <!DOCTYPE session SYSTEM "thuban-0.9.dtd">
473  <session title="single map&amp;layer">  <session xmlns="http://thuban.intevation.org/dtds/thuban-0.9-dev.dtd"
474          <map title="Test Map">          title="single map&amp;layer">
475                  <rasterlayer title="My RasterLayer"      <map title="Test Map">
476                       filename="../../Data/iceland/island.tif"          <rasterlayer visible="false" filename="../../Data/iceland/island.tif"
477                       visible="false">                  title="My RasterLayer"/>
         </rasterlayer>  
478      </map>      </map>
479  </session>  </session>
480  '''  '''
# Line 443  class TestRasterLayer(LoadSessionTest): Line 497  class TestRasterLayer(LoadSessionTest):
497                                                       os.pardir, os.pardir,                                                       os.pardir, os.pardir,
498                                                       "Data", "iceland",                                                       "Data", "iceland",
499                                                       "island.tif")))                                                       "island.tif")))
500            self.check_format()
501    
502    
503    class TestJoinedTable(LoadSessionTest):
504    
505        file_contents = '''<?xml version="1.0" encoding="UTF-8"?>
506    <!DOCTYPE session SYSTEM "thuban-0.9.dtd">
507    <session xmlns="http://thuban.intevation.org/dtds/thuban-0.9-dev.dtd" title="A Joined Table session">
508        <fileshapesource filetype="shapefile" id="D137227612"
509            filename="../../Data/iceland/roads-line.shp"/>
510        <filetable filetype="DBF" filename="load_joinedtable.dbf" id="D136171140"
511            title="Some Title"/>
512        <jointable id="D136169900" title="Joined"
513            right="D136171140" left="D137227612"
514            leftcolumn="RDLNTYPE" rightcolumn="RDTYPE"
515            jointype="LEFT OUTER"/>
516        <derivedshapesource table="D136169900" shapesource="D137227612"
517            id="D136170932"/>
518        <map title="Test Map">
519            <layer shapestore="D136170932" visible="true" stroke="#000000"
520                    title="My Layer" stroke_width="1" fill="None"/>
521        </map>
522    </session>
523    '''
524    
525        def setUp(self):
526            """Extend inherited method to create the dbffile for the join"""
527            LoadSessionTest.setUp(self)
528            dbffile = self.temp_file_name("load_joinedtable.dbf")
529            dbf = dbflib.create(dbffile)
530            dbf.add_field("RDTYPE", dbflib.FTInteger, 10, 0)
531            dbf.add_field("TEXT", dbflib.FTString, 10, 0)
532            dbf.write_record(0, {'RDTYPE': 8, "TEXT": "foo"})
533            dbf.write_record(1, {'RDTYPE': 2, "TEXT": "bar"})
534            dbf.write_record(2, {'RDTYPE': 3, "TEXT": "baz"})
535            dbf.close()
536    
537        def test(self):
538            """Test loading a session containing a joined table"""
539            session = load_session(self.filename())
540            self.session = session
541    
542            tables = session.Tables()
543            self.assertEquals(len(tables), 3)
544            # FIXME: The tests shouldn't assume a certain order of the tables
545            self.assertEquals(tables[0].Title(), "Some Title")
546            self.assertEquals(tables[1].Title(), "Joined")
547            self.assertEquals(tables[1].JoinType(), "LEFT OUTER")
548            self.check_format()
549    
550    
551    class TestLoadError(LoadSessionTest):
552    
553        file_contents = '''\
554    <?xml version="1.0" encoding="UTF-8"?>
555    <!DOCTYPE session SYSTEM "thuban-0.9.dtd">
556    <session xmlns="http://thuban.intevation.org/dtds/thuban-0.9-dev.dtd"
557            title="single map&amp;layer">
558        <fileshapesource id="D1" filename="../../Data/iceland/political.shp"/>
559        <map title="Test Map">
560            <projection name="Unknown">
561                <parameter value="zone=26"/>
562                <parameter value="proj=utm"/>
563                <parameter value="ellps=clrk66"/>
564            </projection>
565            <layer shapestore="D1" visible="true"
566                    stroke="#000000" title="My Layer" stroke_width="1"
567                    fill="None"/>
568        </map>
569    </session>
570    '''
571    
572        def test(self):
573            """Test loading a session missing a required attribute"""
574            # Don't use assertRaises to make sure that if a session is
575            # actually returned it gets destroyed properly.
576            try:
577                self.session = load_session(self.filename())
578            except LoadError, value:
579                pass
580            else:
581                self.fail("Missing filetype attribute doesn't raise LoadError")
582    
583  if __name__ == "__main__":  if __name__ == "__main__":
584      unittest.main()      unittest.main()

Legend:
Removed from v.1247  
changed lines
  Added in v.1375

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26