/[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 1219 by bh, Mon Jun 16 17:42:54 2003 UTC revision 1642 by bh, Mon Aug 25 10:54:31 2003 UTC
# Line 7  Line 7 
7    
8  """  """
9  Test loading a thuban session from a file  Test loading a thuban session from a file
10    
11    The tests in this file (test_load.py) are always be the tests for the
12    current version of the thuban file format. Tests for older versions can
13    be found in the version specific test modules, e.g. test_load_0_2 for
14    files created by Thuban 0.2.
15    
16    Maintenance of the test cases:
17    
18    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
20    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 19  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 60  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 98  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 115  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 159  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 171  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 201  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 229  class TestClassification(ClassificationT Line 287  class TestClassification(ClassificationT
287                  <clpoint value="1">                  <clpoint value="1">
288                      <cldata stroke="#000000" stroke_width="10" fill="None"/>                      <cldata stroke="#000000" stroke_width="10" fill="None"/>
289                  </clpoint>                  </clpoint>
290                    <clpoint value="\xc3\xa4\xc3\xb6\xc3\xbc"
291                             label="\xc3\x9cml\xc3\xa4uts">
292                        <cldata fill="None" stroke="#000000" stroke_width="1"/>
293                    </clpoint>
294              </classification>              </classification>
295          </layer>          </layer>
296                  <layer title="My Layer 2" stroke_width="1" fill="None"                  <layer title="My Layer 2" stroke_width="1" fill="None"
# Line 257  class TestClassification(ClassificationT Line 319  class TestClassification(ClassificationT
319  '''  '''
320    
321      def test(self):      def test(self):
322          """Load a Thuban 0.2 session with a map and classified layers."""          """Load a Thuban session with a map and classified layers."""
323          session = load_session(self.filename())          session = load_session(self.filename())
324          self.session = session          self.session = session
325    
326          map = self.session.Maps()[0] # only one map in the sample          map = self.session.Maps()[0] # only one map in the sample
327    
328          expected = [("My Layer", 2,          expected = [("My Layer", 3,
329                          [("default", (), "",                          [("default", (), "",
330                              ("#000000", 1, "None")),                              ("#000000", 1, "None")),
331                           ("single", "1", "",                           ("single", "1", "",
332                              ("#000000", 2, "None")),                              ("#000000", 2, "None")),
333                           ("single", "1", "",                           ("single", "1", "",
334                              ("#000000", 10, "None"))]),                              ("#000000", 10, "None")),
335                             ("single", "\xe4\xf6\xfc", "\xdcml\xe4uts",
336                                ("#000000", 1, "None"))]),
337                       ("My Layer 2", 4,                       ("My Layer 2", 4,
338                           [("default", (), "",                           [("default", (), "",
339                              ("#000000", 2, "None")),                              ("#000000", 2, "None")),
# Line 289  class TestLabels(ClassificationTest): Line 353  class TestLabels(ClassificationTest):
353    
354      file_contents = '''\      file_contents = '''\
355  <?xml version="1.0" encoding="UTF-8"?>  <?xml version="1.0" encoding="UTF-8"?>
356  <!DOCTYPE session SYSTEM "thuban.dtd">  <!DOCTYPE session SYSTEM "thuban-0.9.dtd">
357  <session title="single map&amp;layer">  <session xmlns="http://thuban.intevation.org/dtds/thuban-0.9-dev.dtd"
358          <map title="Test Map">          title="single map&amp;layer">
359                  <projection>      <fileshapesource filetype="shapefile" id="D1"
360                          <parameter value="zone=26"/>          filename="../../Data/iceland/political.shp"/>
361                          <parameter value="proj=utm"/>      <map title="Test Map">
362                          <parameter value="ellps=clrk66"/>          <projection name="Unknown">
363                  </projection>              <parameter value="zone=26"/>
364                  <layer title="My Layer" stroke_width="1" fill="None"              <parameter value="proj=utm"/>
365                      filename="../../Data/iceland/political.shp"              <parameter value="ellps=clrk66"/>
366                      stroke="#000000">          </projection>
367            <layer shapestore="D1" visible="true" stroke="#000000"
368                    title="My Layer" stroke_width="1" fill="None">
369              <classification field="POPYREG" field_type="string">              <classification field="POPYREG" field_type="string">
370                  <clnull label="hallo">                  <clnull label="hallo">
371                      <cldata stroke="#000000" stroke_width="1" fill="None"/>                      <cldata stroke="#000000" stroke_width="1" fill="None"/>
# Line 328  class TestLabels(ClassificationTest): Line 394  class TestLabels(ClassificationTest):
394                              ("#000000", 2, "None"))])]                              ("#000000", 2, "None"))])]
395    
396          self.TestLayers(map.Layers(), expected)          self.TestLayers(map.Layers(), expected)
397            self.check_format()
398    
399    
400  class TestLayerProjection(LoadSessionTest):  class TestLayerProjection(LoadSessionTest):
401    
402      file_contents = '''\      file_contents = '''\
403  <?xml version="1.0" encoding="UTF-8"?>  <?xml version="1.0" encoding="UTF-8"?>
404  <!DOCTYPE session SYSTEM "thuban.dtd">  <!DOCTYPE session SYSTEM "thuban-0.9.dtd">
405  <session title="single map&amp;layer">  <session xmlns="http://thuban.intevation.org/dtds/thuban-0.9-dev.dtd"
406          <map title="Test Map">          title="single map&amp;layer">
407                  <projection>      <fileshapesource filetype="shapefile" id="D2"
408                          <parameter value="zone=26"/>          filename="../../Data/iceland/roads-line.shp"/>
409                          <parameter value="proj=utm"/>      <fileshapesource filetype="shapefile" id="D4"
410                          <parameter value="ellps=clrk66"/>          filename="../../Data/iceland/political.shp"/>
411                  </projection>      <map title="Test Map">
412                  <layer title="My Layer" stroke_width="1" fill="None"          <projection name="Unknown">
413                      filename="../../Data/iceland/political.shp"              <parameter value="zone=26"/>
414                      stroke="#000000">              <parameter value="proj=utm"/>
415                      <projection name="hello">              <parameter value="ellps=clrk66"/>
416                          <parameter value="zone=13"/>          </projection>
417                          <parameter value="proj=tmerc"/>          <layer shapestore="D4" visible="true" stroke="#000000"
418                          <parameter value="ellps=clrk66"/>                  title="My Layer" stroke_width="1" fill="None">
419                      </projection>              <projection name="hello">
420                    <parameter value="zone=13"/>
421                    <parameter value="proj=tmerc"/>
422                    <parameter value="ellps=clrk66"/>
423                </projection>
424              <classification field="POPYREG" field_type="string">              <classification field="POPYREG" field_type="string">
425                  <clnull label="hallo">                  <clnull label="hallo">
426                      <cldata stroke="#000000" stroke_width="1" fill="None"/>                      <cldata stroke="#000000" stroke_width="1" fill="None"/>
# Line 359  class TestLayerProjection(LoadSessionTes Line 430  class TestLayerProjection(LoadSessionTes
430                  </clpoint>                  </clpoint>
431              </classification>              </classification>
432          </layer>          </layer>
433                  <layer title="My Layer" stroke_width="1" fill="None"          <layer shapestore="D2" visible="true" stroke="#000000"
434                      filename="../../Data/iceland/political.shp"                  title="My Layer" stroke_width="1" fill="None">
435                      stroke="#000000">              <projection name="Unknown">
436                      <projection>                  <parameter value="proj=lcc"/>
437                          <parameter value="proj=lcc"/>                  <parameter value="ellps=clrk66"/>
438                          <parameter value="ellps=clrk66"/>              </projection>
                     </projection>  
439          </layer>          </layer>
440      </map>      </map>
441  </session>  </session>
# Line 398  class TestLayerProjection(LoadSessionTes Line 468  class TestLayerProjection(LoadSessionTes
468          eq(proj.GetParameter("proj"), "lcc")          eq(proj.GetParameter("proj"), "lcc")
469          eq(proj.GetParameter("ellps"), "clrk66")          eq(proj.GetParameter("ellps"), "clrk66")
470    
471            self.check_format()
472    
473    
474  class TestRasterLayer(LoadSessionTest):  class TestRasterLayer(LoadSessionTest):
475    
476      file_contents = '''\      file_contents = '''\
477  <?xml version="1.0" encoding="UTF-8"?>  <?xml version="1.0" encoding="UTF-8"?>
478  <!DOCTYPE session SYSTEM "thuban.dtd">  <!DOCTYPE session SYSTEM "thuban-0.9.dtd">
479  <session title="single map&amp;layer">  <session xmlns="http://thuban.intevation.org/dtds/thuban-0.9-dev.dtd"
480          <map title="Test Map">          title="single map&amp;layer">
481                  <rasterlayer title="My RasterLayer"      <map title="Test Map">
482                       filename="../../Data/iceland/island.tif"          <rasterlayer visible="false" filename="../../Data/iceland/island.tif"
483                       visible="false">                  title="My RasterLayer"/>
         </rasterlayer>  
484      </map>      </map>
485  </session>  </session>
486  '''  '''
# Line 432  class TestRasterLayer(LoadSessionTest): Line 503  class TestRasterLayer(LoadSessionTest):
503                                                       os.pardir, os.pardir,                                                       os.pardir, os.pardir,
504                                                       "Data", "iceland",                                                       "Data", "iceland",
505                                                       "island.tif")))                                                       "island.tif")))
506            self.check_format()
507    
508    
509    class TestJoinedTable(LoadSessionTest):
510    
511        file_contents = '''<?xml version="1.0" encoding="UTF-8"?>
512    <!DOCTYPE session SYSTEM "thuban-0.9.dtd">
513    <session xmlns="http://thuban.intevation.org/dtds/thuban-0.9-dev.dtd" title="A Joined Table session">
514        <fileshapesource filetype="shapefile" id="D137227612"
515            filename="../../Data/iceland/roads-line.shp"/>
516        <filetable filetype="DBF" filename="load_joinedtable.dbf" id="D136171140"
517            title="Some Title"/>
518        <jointable id="D136169900" title="Joined"
519            right="D136171140" left="D137227612"
520            leftcolumn="RDLNTYPE" rightcolumn="RDTYPE"
521            jointype="LEFT OUTER"/>
522        <derivedshapesource table="D136169900" shapesource="D137227612"
523            id="D136170932"/>
524        <map title="Test Map">
525            <layer shapestore="D136170932" visible="true" stroke="#000000"
526                    title="My Layer" stroke_width="1" fill="None"/>
527        </map>
528    </session>
529    '''
530    
531        def setUp(self):
532            """Extend inherited method to create the dbffile for the join"""
533            LoadSessionTest.setUp(self)
534            dbffile = self.temp_file_name("load_joinedtable.dbf")
535            dbf = dbflib.create(dbffile)
536            dbf.add_field("RDTYPE", dbflib.FTInteger, 10, 0)
537            dbf.add_field("TEXT", dbflib.FTString, 10, 0)
538            dbf.write_record(0, {'RDTYPE': 8, "TEXT": "foo"})
539            dbf.write_record(1, {'RDTYPE': 2, "TEXT": "bar"})
540            dbf.write_record(2, {'RDTYPE': 3, "TEXT": "baz"})
541            dbf.close()
542    
543        def test(self):
544            """Test loading a session containing a joined table"""
545            session = load_session(self.filename())
546            self.session = session
547    
548            tables = session.Tables()
549            self.assertEquals(len(tables), 3)
550            # FIXME: The tests shouldn't assume a certain order of the tables
551            self.assertEquals(tables[0].Title(), "Some Title")
552            self.assertEquals(tables[1].Title(), "Joined")
553            self.assertEquals(tables[1].JoinType(), "LEFT OUTER")
554            self.check_format()
555    
556    
557    class TestLoadError(LoadSessionTest):
558    
559        file_contents = '''\
560    <?xml version="1.0" encoding="UTF-8"?>
561    <!DOCTYPE session SYSTEM "thuban-0.9.dtd">
562    <session xmlns="http://thuban.intevation.org/dtds/thuban-0.9-dev.dtd"
563            title="single map&amp;layer">
564        <fileshapesource id="D1" filename="../../Data/iceland/political.shp"/>
565        <map title="Test Map">
566            <projection name="Unknown">
567                <parameter value="zone=26"/>
568                <parameter value="proj=utm"/>
569                <parameter value="ellps=clrk66"/>
570            </projection>
571            <layer shapestore="D1" visible="true"
572                    stroke="#000000" title="My Layer" stroke_width="1"
573                    fill="None"/>
574        </map>
575    </session>
576    '''
577    
578        def test(self):
579            """Test loading a session missing a required attribute"""
580            # Don't use assertRaises to make sure that if a session is
581            # actually returned it gets destroyed properly.
582            try:
583                self.session = load_session(self.filename())
584            except LoadError, value:
585                # Check the actual messge in value to make sure the
586                # LoadError really was about the missing attribute
587                self.assertEquals(str(value),
588                  "Element "
589                  "(u'http://thuban.intevation.org/dtds/thuban-0.9-dev.dtd',"
590                  " u'fileshapesource') requires an attribute 'filetype'")
591            else:
592                self.fail("Missing filetype attribute doesn't raise LoadError")
593    
594  if __name__ == "__main__":  if __name__ == "__main__":
595      unittest.main()      unittest.main()

Legend:
Removed from v.1219  
changed lines
  Added in v.1642

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26