/[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 1683 by bh, Thu Aug 28 15:20:57 2003 UTC revision 2654 by jan, Wed Jul 27 21:44:16 2005 UTC
# Line 1  Line 1 
1  # Copyright (c) 2002, 2003 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 16  files created by Thuban 0.2. Line 16  files created by Thuban 0.2.
16  Maintenance of the test cases:  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 time, the tests here should
20  copied to the version specific test file. The round-trip tests which  be 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  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  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  here are actually ones that may have been written by the current thuban
# Line 38  import postgissupport Line 38  import postgissupport
38  from xmlsupport import sax_eventlist  from xmlsupport import sax_eventlist
39    
40  import dbflib  import dbflib
41    import shapelib
42    
43    from Thuban import internal_from_unicode
44  from Thuban.Model.save import save_session  from Thuban.Model.save import save_session
45  from Thuban.Model.load import load_session, parse_color, LoadError, \  from Thuban.Model.load import load_session, parse_color, LoadError, \
46       LoadCancelled       LoadCancelled
# Line 46  from Thuban.Model.color import Transpare Line 48  from Thuban.Model.color import Transpare
48  from Thuban.Model.classification import ClassGroupProperties, ClassGroupRange,\  from Thuban.Model.classification import ClassGroupProperties, ClassGroupRange,\
49      ClassGroupSingleton, ClassGroupDefault      ClassGroupSingleton, ClassGroupDefault
50  from Thuban.Model.postgisdb import ConnectionError  from Thuban.Model.postgisdb import ConnectionError
51    from Thuban.Model.table import DBFTable, MemoryTable, \
52         FIELDTYPE_DOUBLE, FIELDTYPE_INT, FIELDTYPE_STRING, \
53         table_to_dbf
54    from Thuban.Model.label import ALIGN_CENTER, ALIGN_TOP, ALIGN_BOTTOM, \
55         ALIGN_LEFT, ALIGN_RIGHT, ALIGN_BASELINE
56    
57  def filenames_equal(name1, name2):  def filenames_equal(name1, name2):
58      """Return true if the filenames name1 and name2 are equal.      """Return true if the filenames name1 and name2 are equal.
# Line 82  class LoadSessionTest(support.FileLoadTe Line 89  class LoadSessionTest(support.FileLoadTe
89          self.session = None          self.session = None
90    
91    
92      dtd = "http://thuban.intevation.org/dtds/thuban-0.9.dtd"      dtd = "http://thuban.intevation.org/dtds/thuban-1.1-dev.dtd"
93      thubanids = [((dtd, n), (None, "id")) for n in      thubanids = [((dtd, n), (None, "id")) for n in
94                   ["fileshapesource", "filetable", "jointable",                   ["fileshapesource", "filetable", "jointable",
95                    "derivedshapesource"]]                    "derivedshapesource"]]
# Line 92  class LoadSessionTest(support.FileLoadTe Line 99  class LoadSessionTest(support.FileLoadTe
99                       ("jointable", "right"),                       ("jointable", "right"),
100                       ("derivedshapesource", "table"),                       ("derivedshapesource", "table"),
101                       ("derivedshapesource", "shapesource")]]                       ("derivedshapesource", "shapesource")]]
102      filenames = [((dtd, n), (None, m)) for n, m in  
103                   [("fileshapesource", "filename"),      # The filenames in the tests should be understandable on all
104                    ("rasterlayer", "filename"),      # currently supported platforms so filenames is an empty list
105                    ("filetable", "filename")]]      filenames = []
106    
107      del n, m, dtd      del n, m, dtd
108    
109      def check_format(self):      def check_format(self):
# Line 116  class LoadSessionTest(support.FileLoadTe Line 124  class LoadSessionTest(support.FileLoadTe
124                  print a != b and "***************" or ""                  print a != b and "***************" or ""
125                  print a                  print a
126                  print b                  print b
127    
128          self.assertEquals(el1, el2,          self.assertEquals(el1, el2,
129                            "loaded file not equivalent to the saved file")                            "loaded file not equivalent to the saved file")
130    
# Line 154  class ClassificationTest(LoadSessionTest Line 163  class ClassificationTest(LoadSessionTest
163                  props.SetLineWidth(data[CLASSES][i][GROUP_PROPS][1])                  props.SetLineWidth(data[CLASSES][i][GROUP_PROPS][1])
164                  props.SetFill(                  props.SetFill(
165                      parse_color(data[CLASSES][i][GROUP_PROPS][2]))                      parse_color(data[CLASSES][i][GROUP_PROPS][2]))
166                    if len(data[CLASSES][i][GROUP_PROPS]) > 3:
167                        props.SetSize(data[CLASSES][i][GROUP_PROPS][3])
168    
169                  if data[CLASSES][i][GROUP_TYPE] == "default":                  if data[CLASSES][i][GROUP_TYPE] == "default":
170                      g = ClassGroupDefault(props, data[CLASSES][i][GROUP_LABEL])                      g = ClassGroupDefault(props, data[CLASSES][i][GROUP_LABEL])
# Line 173  class ClassificationTest(LoadSessionTest Line 184  class ClassificationTest(LoadSessionTest
184    
185  class TestSingleLayer(LoadSessionTest):  class TestSingleLayer(LoadSessionTest):
186    
187        # Note: The use of &amp; and non-ascii characters is deliberate. We
188        # want to test whether the loading code handles that correctly.
189      file_contents = '''\      file_contents = '''\
190  <?xml version="1.0" encoding="UTF-8"?>  <?xml version="1.0" encoding="UTF-8"?>
191  <!DOCTYPE session SYSTEM "thuban-0.9.dtd">  <!DOCTYPE session SYSTEM "thuban-1.1.dtd">
192  <session xmlns="http://thuban.intevation.org/dtds/thuban-0.9.dtd"  <session xmlns="http://thuban.intevation.org/dtds/thuban-1.1-dev.dtd"
193          title="single map&amp;layer">          title="Stra\xc3\x9fen &amp; Landmarken">
194      <fileshapesource filetype="shapefile" id="D1"      <fileshapesource filetype="shapefile" id="D1"
195          filename="../../Data/iceland/political.shp"/>          filename="../../Data/iceland/political.shp"/>
196      <map title="Test Map">      <map title="\xc3\x9cbersicht">
197          <projection name="Unknown">          <projection epsg="32627" name="WGS 84 / UTM zone 27N">
198              <parameter value="zone=26"/>              <parameter value="datum=WGS84"/>
199                <parameter value="ellps=WGS84"/>
200              <parameter value="proj=utm"/>              <parameter value="proj=utm"/>
201              <parameter value="ellps=clrk66"/>              <parameter value="units=m"/>
202                <parameter value="zone=27"/>
203          </projection>          </projection>
204          <layer shapestore="D1" visible="true"          <layer shapestore="D1" visible="true" title="K\xc3\xbcste">
205                  stroke="#000000" title="My Layer" stroke_width="1"              <classification>
206                  fill="None"/>                  <clnull label="">
207                        <cldata stroke="#000000" stroke_width="1" fill="None"/>
208                    </clnull>
209                </classification>
210            </layer>
211      </map>      </map>
212  </session>  </session>
213  '''  '''
# Line 200  class TestSingleLayer(LoadSessionTest): Line 219  class TestSingleLayer(LoadSessionTest):
219          self.session = session          self.session = session
220    
221          # Check the title          # Check the title
222          eq(session.Title(), "single map&layer")          eq(session.Title(), internal_from_unicode(u"Stra\xdfen & Landmarken"))
223    
224          # the session has one map.          # the session has one map.
225          maps = session.Maps()          maps = session.Maps()
# Line 208  class TestSingleLayer(LoadSessionTest): Line 227  class TestSingleLayer(LoadSessionTest):
227    
228          # Check the map's attributes          # Check the map's attributes
229          map = maps[0]          map = maps[0]
230          eq(map.Title(), "Test Map")          eq(map.Title(), internal_from_unicode(u"\xdcbersicht"))
231            proj = map.GetProjection()
232            eq(proj.GetName(), "WGS 84 / UTM zone 27N")
233            eq(proj.EPSGCode(), "32627")
234            params = proj.GetAllParameters()
235            params.sort()
236            eq(params, ["datum=WGS84", "ellps=WGS84", "proj=utm", "units=m",
237                        "zone=27"])
238    
239          # the map has a single layer          # the map has a single layer
240          layers = map.Layers()          layers = map.Layers()
# Line 216  class TestSingleLayer(LoadSessionTest): Line 242  class TestSingleLayer(LoadSessionTest):
242    
243          # Check the layer attributes          # Check the layer attributes
244          layer = layers[0]          layer = layers[0]
245          eq(layer.Title(), "My Layer")          eq(layer.Title(), internal_from_unicode(u"K\xfcste"))
246          self.failUnless(filenames_equal(layer.ShapeStore().FileName(),          self.failUnless(filenames_equal(layer.ShapeStore().FileName(),
247                                          os.path.join(self.temp_dir(),                                          os.path.join(self.temp_dir(),
248                                                       os.pardir, os.pardir,                                                       os.pardir, os.pardir,
# Line 231  class TestSingleLayer(LoadSessionTest): Line 257  class TestSingleLayer(LoadSessionTest):
257          self.session.Destroy()          self.session.Destroy()
258          self.session = None          self.session = None
259    
260        def test_leak(self):
261            """Test load_session for resource leaks
262    
263            The load_session function had a resource leak in that it created
264            cyclic references. The objects would have been eventually
265            collected by the garbage collector but too late. One symptom is
266            that when layers are removed so that the last normal reference
267            owned indirectly by the session to a shape store goes away, the
268            shape store is not actually removed from the session even though
269            the session only keeps weak references because there are still
270            references owned by the cyclic garbage.
271            """
272            session = load_session(self.filename())
273            self.session = session
274    
275            # sanity check
276            self.assertEquals(len(session.ShapeStores()), 1)
277    
278            # remove the map. The shapestore should go away too
279            session.RemoveMap(session.Maps()[0])
280            self.assertEquals(len(session.ShapeStores()), 0)
281    
282    
283    class TestNonAsciiColumnName(LoadSessionTest):
284    
285        file_contents = '''\
286    <?xml version="1.0" encoding="UTF-8"?>
287    <!DOCTYPE session SYSTEM "thuban-1.1.dtd">
288    <session xmlns="http://thuban.intevation.org/dtds/thuban-1.1-dev.dtd"
289            title="Non ASCII column name test">
290        <fileshapesource filetype="shapefile" id="D1"
291            filename="TestNonAsciiColumnName.shp"/>
292        <map title="map">
293            <projection name="Some Projection">
294                <parameter value="datum=WGS84"/>
295                <parameter value="ellps=WGS84"/>
296                <parameter value="proj=utm"/>
297                <parameter value="units=m"/>
298                <parameter value="zone=27"/>
299            </projection>
300            <layer shapestore="D1" visible="true" title="layer">
301                <classification field="Fl\xc3\xa4che" field_type="double">
302                    <clnull label="">
303                        <cldata stroke="#000000" stroke_width="1" fill="None"/>
304                    </clnull>
305                </classification>
306            </layer>
307        </map>
308    </session>
309    '''
310    
311        def test(self):
312            """Load a session with a single map with a single layer"""
313    
314            # Create a shapefile and a dbffile with a non-ascii column name
315            dbffile = self.temp_file_name("TestNonAsciiColumnName.dbf")
316            shpfile = self.temp_file_name("TestNonAsciiColumnName.shp")
317            dbf = dbflib.create(dbffile)
318            dbf.add_field('Fl\xe4che', dbflib.FTDouble, 10, 5)
319            dbf.write_record(0, (0.0,))
320            dbf.close()
321            shp = shapelib.create(shpfile, shapelib.SHPT_POLYGON)
322            shp.write_object(-1, shapelib.SHPObject(shapelib.SHPT_POLYGON, 1,
323                                                    [[(0,0), (10, 10), (10, 0),
324                                                      (0, 0)]]))
325            shp.close()
326    
327            try:
328                session = load_session(self.filename())
329            except ValueError, v:
330                # Usually if the field name is not decoded properly the
331                # loading fails because the field type mentioned in the file
332                # is not None as returned from the layer for a non-existing
333                # column name so we check for that and report it as failure.
334                # Other exceptions are errors in the test case.
335                if str(v) == "xml field type differs from database!":
336                    self.fail("Cannot load file with non-ascii column names")
337                else:
338                    raise
339            self.session = session
340    
341            # In case Thuban could load the file anyway (i.e. no ValueError
342            # exception in load_session()), check explicitly whether the
343            # field name was decoded properly. The test will probably lead
344            # to a UnicodeError instead of a test failure so we check that
345            # too
346            layer = session.Maps()[0].Layers()[0]
347            try:
348                self.assertEquals(layer.GetClassificationColumn(), 'Fl\xe4che')
349            except UnicodeError:
350                # FIXME: Obviously this will have to change if Thuban ever
351                # supports unicode properly.
352                self.fail("Column name was not converted to a bytestring")
353    
354            # roundtrip check
355            self.check_format()
356    
357    
358  class TestLayerVisibility(LoadSessionTest):  class TestLayerVisibility(LoadSessionTest):
359    
360      file_contents = '''\      file_contents = '''\
361  <?xml version="1.0" encoding="UTF-8"?>  <?xml version="1.0" encoding="UTF-8"?>
362  <!DOCTYPE session SYSTEM "thuban-0.9.dtd">  <!DOCTYPE session SYSTEM "thuban-1.1.dtd">
363  <session xmlns="http://thuban.intevation.org/dtds/thuban-0.9.dtd"  <session xmlns="http://thuban.intevation.org/dtds/thuban-1.1-dev.dtd"
364          title="single map&amp;layer">          title="single map&amp;layer">
365      <fileshapesource filetype="shapefile" id="D1"      <fileshapesource filetype="shapefile" id="D1"
366          filename="../../Data/iceland/political.shp"/>          filename="../../Data/iceland/political.shp"/>
# Line 247  class TestLayerVisibility(LoadSessionTes Line 370  class TestLayerVisibility(LoadSessionTes
370              <parameter value="proj=utm"/>              <parameter value="proj=utm"/>
371              <parameter value="ellps=clrk66"/>              <parameter value="ellps=clrk66"/>
372          </projection>          </projection>
373          <layer shapestore="D1" visible="false" stroke="#000000"          <layer shapestore="D1" visible="false" title="My Layer">
374                  title="My Layer" stroke_width="1" fill="None"/>              <classification>
375                    <clnull label="">
376                        <cldata stroke="#000000" stroke_width="1" fill="None"/>
377                    </clnull>
378                </classification>
379            </layer>
380      </map>      </map>
381  </session>  </session>
382  '''  '''
# Line 270  class TestLayerVisibility(LoadSessionTes Line 398  class TestLayerVisibility(LoadSessionTes
398          self.check_format()          self.check_format()
399    
400    
401    class TestSymbolSize(ClassificationTest):
402    
403        file_contents = '''\
404    <?xml version="1.0" encoding="UTF-8"?>
405    <!DOCTYPE session SYSTEM "thuban-1.1.dtd">
406    <session xmlns="http://thuban.intevation.org/dtds/thuban-1.1-dev.dtd" title="Thuban sample session">
407        <fileshapesource filetype="shapefile" id="D813968480" filename="../../Data/iceland/cultural_landmark-point.shp"/>
408        <map title="Iceland map">
409            <layer title="cultural_landmark-point" shapestore="D813968480" visible="true">
410                <classification field="CLPTLABEL" field_type="string">
411                    <clnull label="">
412                        <cldata stroke="#000000" stroke_width="1" size="3" fill="#000000"/>
413                    </clnull>
414                    <clpoint label="" value="RUINS">
415                        <cldata stroke="#000000" stroke_width="1" size="6" fill="#ffffff"/>
416                    </clpoint>
417                    <clpoint label="" value="FARM">
418                        <cldata stroke="#000000" stroke_width="1" size="9" fill="#ffff00"/>
419                    </clpoint>
420                </classification>
421            </layer>
422        </map>
423    </session>
424    '''
425    
426        def test(self):
427            """Test that the size attribute for point symbols is correctly
428            loaded for a layer."""
429            eq = self.assertEquals
430            session = load_session(self.filename())
431            self.session = session
432    
433            map = session.Maps()[0] # only one map in the sample
434    
435            expected = [("cultural_landmark-point", 2,
436                            [("default", (), "",
437                                ("#000000", 1, "#000000", 3)),
438                             ("single", "RUINS", "",
439                                ("#000000", 1, "#ffffff", 6)),
440                             ("single", "FARM", "",
441                                ("#000000", 1, "#ffff00", 9))])]
442    
443            self.TestLayers(map.Layers(), expected)
444    
445            self.check_format()
446    
447    
448  class TestClassification(ClassificationTest):  class TestClassification(ClassificationTest):
449    
450      file_contents = '''\      file_contents = '''\
451  <?xml version="1.0" encoding="UTF-8"?>  <?xml version="1.0" encoding="UTF-8"?>
452  <!DOCTYPE session SYSTEM "thuban.dtd">  <!DOCTYPE session SYSTEM "thuban-1.1.dtd">
453  <session title="single map&amp;layer">  <session xmlns="http://thuban.intevation.org/dtds/thuban-1.1-dev.dtd"
454          <map title="Test Map">          title="single map&amp;layer">
455                  <projection>      <fileshapesource filetype="shapefile" id="D138389860"
456                          <parameter value="zone=26"/>          filename="../../Data/iceland/political.shp"/>
457                          <parameter value="proj=utm"/>      <fileshapesource filetype="shapefile" id="D138504492"
458                          <parameter value="ellps=clrk66"/>          filename="../../Data/iceland/political.shp"/>
459                  </projection>      <map title="Test Map">
460                  <layer title="My Layer" stroke_width="1" fill="None"          <projection name="">
461                      filename="../../Data/iceland/political.shp"              <parameter value="zone=26"/>
462                      stroke="#000000">              <parameter value="proj=utm"/>
463                <parameter value="ellps=clrk66"/>
464            </projection>
465            <layer shapestore="D138389860" visible="true" title="My Layer">
466              <classification field="POPYREG" field_type="string">              <classification field="POPYREG" field_type="string">
467                  <clnull>                  <clnull label="">
468                      <cldata stroke="#000000" stroke_width="1" fill="None"/>                      <cldata stroke="#000000" stroke_width="1" fill="None"/>
469                  </clnull>                  </clnull>
470                  <clpoint value="1">                  <clpoint label="" value="1">
471                      <cldata stroke="#000000" stroke_width="2" fill="None"/>                      <cldata stroke="#000000" stroke_width="2" fill="None"/>
472                  </clpoint>                  </clpoint>
473                  <clpoint value="1">                  <clpoint label="" value="1">
474                      <cldata stroke="#000000" stroke_width="10" fill="None"/>                      <cldata stroke="#000000" stroke_width="10" fill="None"/>
475                  </clpoint>                  </clpoint>
476                  <clpoint value="\xc3\xa4\xc3\xb6\xc3\xbc"                  <clpoint label="\xc3\x9cml\xc3\xa4uts"
477                           label="\xc3\x9cml\xc3\xa4uts">                          value="\xc3\xa4\xc3\xb6\xc3\xbc">
478                      <cldata fill="None" stroke="#000000" stroke_width="1"/>                      <cldata stroke="#000000" stroke_width="1" fill="None"/>
479                  </clpoint>                  </clpoint>
480              </classification>              </classification>
481          </layer>          </layer>
482                  <layer title="My Layer 2" stroke_width="1" fill="None"          <layer shapestore="D138504492" visible="true" title="My Layer 2">
                     filename="../../Data/iceland/political.shp"  
                     stroke="#000000">  
483              <classification field="AREA" field_type="double">              <classification field="AREA" field_type="double">
484                  <clnull>                  <clnull label="">
485                      <cldata stroke="#000000" stroke_width="2" fill="None"/>                      <cldata stroke="#000000" stroke_width="2" fill="None"/>
486                  </clnull>                  </clnull>
487                  <clrange min="0" max="1">                  <clrange label="" range="[0;1[">
488                      <cldata stroke="#111111" stroke_width="1" fill="None"/>                      <cldata stroke="#111111" stroke_width="1" fill="None"/>
489                  </clrange>                  </clrange>
490                  <clpoint value=".5">                  <clpoint label="" value="0.5">
491                      <cldata stroke="#000000" stroke_width="1" fill="#111111"/>                      <cldata stroke="#000000" stroke_width="1" fill="#111111"/>
492                  </clpoint>                  </clpoint>
493                  <clrange min="-1" max="0">                  <clrange label="" range="[-1;0[">
494                      <cldata stroke="#000000" stroke_width="1" fill="None"/>                      <cldata stroke="#000000" stroke_width="1" fill="None"/>
495                  </clrange>                  </clrange>
496                  <clpoint value="-.5">                  <clpoint label="" value="-0.5">
497                      <cldata stroke="#000000" stroke_width="1" fill="None"/>                      <cldata stroke="#000000" stroke_width="1" fill="None"/>
498                  </clpoint>                  </clpoint>
499              </classification>              </classification>
500          </layer>          </layer>
501          </map>      </map>
502  </session>  </session>
503  '''  '''
504    
# Line 340  class TestClassification(ClassificationT Line 516  class TestClassification(ClassificationT
516                              ("#000000", 2, "None")),                              ("#000000", 2, "None")),
517                           ("single", "1", "",                           ("single", "1", "",
518                              ("#000000", 10, "None")),                              ("#000000", 10, "None")),
519                           ("single", "\xe4\xf6\xfc", "\xdcml\xe4uts",                           ("single", internal_from_unicode(u"\xe4\xf6\xfc"),
520                              internal_from_unicode(u"\xdcml\xe4uts"),
521                              ("#000000", 1, "None"))]),                              ("#000000", 1, "None"))]),
522                       ("My Layer 2", 4,                       ("My Layer 2", 4,
523                           [("default", (), "",                           [("default", (), "",
# Line 356  class TestClassification(ClassificationT Line 533  class TestClassification(ClassificationT
533    
534          self.TestLayers(map.Layers(), expected)          self.TestLayers(map.Layers(), expected)
535    
536            self.check_format()
537    
538    
539  class TestLabels(ClassificationTest):  class TestLabels(ClassificationTest):
540    
541      file_contents = '''\      file_contents = '''\
542  <?xml version="1.0" encoding="UTF-8"?>  <?xml version="1.0" encoding="UTF-8"?>
543  <!DOCTYPE session SYSTEM "thuban-0.9.dtd">  <!DOCTYPE session SYSTEM "thuban-1.1.dtd">
544  <session xmlns="http://thuban.intevation.org/dtds/thuban-0.9.dtd"  <session xmlns="http://thuban.intevation.org/dtds/thuban-1.1-dev.dtd"
545          title="single map&amp;layer">          title="single map&amp;layer">
546      <fileshapesource filetype="shapefile" id="D1"      <fileshapesource filetype="shapefile" id="D1"
547          filename="../../Data/iceland/political.shp"/>          filename="../../Data/iceland/political.shp"/>
# Line 372  class TestLabels(ClassificationTest): Line 551  class TestLabels(ClassificationTest):
551              <parameter value="proj=utm"/>              <parameter value="proj=utm"/>
552              <parameter value="ellps=clrk66"/>              <parameter value="ellps=clrk66"/>
553          </projection>          </projection>
554          <layer shapestore="D1" visible="true" stroke="#000000"          <layer shapestore="D1" visible="true" title="My Layer">
                 title="My Layer" stroke_width="1" fill="None">  
555              <classification field="POPYREG" field_type="string">              <classification field="POPYREG" field_type="string">
556                  <clnull label="hallo">                  <clnull label="hallo">
557                      <cldata stroke="#000000" stroke_width="1" fill="None"/>                      <cldata stroke="#000000" stroke_width="1" fill="None"/>
# Line 409  class TestLayerProjection(LoadSessionTes Line 587  class TestLayerProjection(LoadSessionTes
587    
588      file_contents = '''\      file_contents = '''\
589  <?xml version="1.0" encoding="UTF-8"?>  <?xml version="1.0" encoding="UTF-8"?>
590  <!DOCTYPE session SYSTEM "thuban-0.9.dtd">  <!DOCTYPE session SYSTEM "thuban-1.1.dtd">
591  <session xmlns="http://thuban.intevation.org/dtds/thuban-0.9.dtd"  <session xmlns="http://thuban.intevation.org/dtds/thuban-1.1-dev.dtd"
592          title="single map&amp;layer">          title="single map&amp;layer">
593      <fileshapesource filetype="shapefile" id="D2"      <fileshapesource filetype="shapefile" id="D2"
594          filename="../../Data/iceland/roads-line.shp"/>          filename="../../Data/iceland/roads-line.shp"/>
# Line 422  class TestLayerProjection(LoadSessionTes Line 600  class TestLayerProjection(LoadSessionTes
600              <parameter value="proj=utm"/>              <parameter value="proj=utm"/>
601              <parameter value="ellps=clrk66"/>              <parameter value="ellps=clrk66"/>
602          </projection>          </projection>
603          <layer shapestore="D4" visible="true" stroke="#000000"          <layer shapestore="D4" visible="true" title="My Layer">
                 title="My Layer" stroke_width="1" fill="None">  
604              <projection name="hello">              <projection name="hello">
605                  <parameter value="zone=13"/>                  <parameter value="zone=13"/>
606                  <parameter value="proj=tmerc"/>                  <parameter value="proj=tmerc"/>
# Line 438  class TestLayerProjection(LoadSessionTes Line 615  class TestLayerProjection(LoadSessionTes
615                  </clpoint>                  </clpoint>
616              </classification>              </classification>
617          </layer>          </layer>
618          <layer shapestore="D2" visible="true" stroke="#000000"          <layer shapestore="D2" visible="true" title="My Layer">
                 title="My Layer" stroke_width="1" fill="None">  
619              <projection name="Unknown">              <projection name="Unknown">
620                  <parameter value="proj=lcc"/>                  <parameter value="proj=lcc"/>
621                    <parameter value="lat_1=10"/>
622                    <parameter value="lat_2=20"/>
623                  <parameter value="ellps=clrk66"/>                  <parameter value="ellps=clrk66"/>
624              </projection>              </projection>
625                <classification>
626                    <clnull label="">
627                        <cldata stroke="#000000" stroke_width="1" fill="None"/>
628                    </clnull>
629                </classification>
630          </layer>          </layer>
631      </map>      </map>
632  </session>  </session>
# Line 474  class TestLayerProjection(LoadSessionTes Line 657  class TestLayerProjection(LoadSessionTes
657          neq(proj, None)          neq(proj, None)
658          eq(proj.GetName(), "Unknown")          eq(proj.GetName(), "Unknown")
659          eq(proj.GetParameter("proj"), "lcc")          eq(proj.GetParameter("proj"), "lcc")
660            eq(proj.GetParameter("lat_1"), "10")
661            eq(proj.GetParameter("lat_2"), "20")
662          eq(proj.GetParameter("ellps"), "clrk66")          eq(proj.GetParameter("ellps"), "clrk66")
663    
664          self.check_format()          self.check_format()
# Line 483  class TestRasterLayer(LoadSessionTest): Line 668  class TestRasterLayer(LoadSessionTest):
668    
669      file_contents = '''\      file_contents = '''\
670  <?xml version="1.0" encoding="UTF-8"?>  <?xml version="1.0" encoding="UTF-8"?>
671  <!DOCTYPE session SYSTEM "thuban-0.9.dtd">  <!DOCTYPE session SYSTEM "thuban-1.1.dtd">
672  <session xmlns="http://thuban.intevation.org/dtds/thuban-0.9.dtd"  <session xmlns="http://thuban.intevation.org/dtds/thuban-1.1-dev.dtd"
673          title="single map&amp;layer">          title="single map&amp;layer">
674      <map title="Test Map">      <map title="Test Map">
675          <rasterlayer visible="false" filename="../../Data/iceland/island.tif"          <rasterlayer visible="false" filename="../../Data/iceland/island.tif"
676                  title="My RasterLayer"/>                  title="My RasterLayer" opacity="0.4" masktype="alpha"/>
677      </map>      </map>
678  </session>  </session>
679  '''  '''
# Line 505  class TestRasterLayer(LoadSessionTest): Line 690  class TestRasterLayer(LoadSessionTest):
690          layer = map.Layers()[0] # one layer in the sample          layer = map.Layers()[0] # one layer in the sample
691    
692          eq(layer.Title(), "My RasterLayer")          eq(layer.Title(), "My RasterLayer")
693            eq(layer.Opacity(), 0.4)
694            eq(layer.MaskType(), layer.MASK_ALPHA)
695    
696          self.failIf(layer.Visible())          self.failIf(layer.Visible())
697          self.failUnless(filenames_equal(layer.GetImageFilename(),          self.failUnless(filenames_equal(layer.GetImageFilename(),
698                                          os.path.join(self.temp_dir(),                                          os.path.join(self.temp_dir(),
# Line 517  class TestRasterLayer(LoadSessionTest): Line 705  class TestRasterLayer(LoadSessionTest):
705  class TestJoinedTable(LoadSessionTest):  class TestJoinedTable(LoadSessionTest):
706    
707      file_contents = '''<?xml version="1.0" encoding="UTF-8"?>      file_contents = '''<?xml version="1.0" encoding="UTF-8"?>
708  <!DOCTYPE session SYSTEM "thuban-0.9.dtd">  <!DOCTYPE session SYSTEM "thuban-1.1.dtd">
709  <session xmlns="http://thuban.intevation.org/dtds/thuban-0.9.dtd" title="A Joined Table session">  <session xmlns="http://thuban.intevation.org/dtds/thuban-1.1-dev.dtd" title="A Joined Table session">
710      <fileshapesource filetype="shapefile" id="D137227612"      <fileshapesource filetype="shapefile" id="D137227612"
711          filename="../../Data/iceland/roads-line.shp"/>          filename="../../Data/iceland/roads-line.shp"/>
712      <filetable filetype="DBF" filename="load_joinedtable.dbf" id="D136171140"      <filetable filetype="DBF" filename="load_joinedtable.dbf" id="D136171140"
# Line 530  class TestJoinedTable(LoadSessionTest): Line 718  class TestJoinedTable(LoadSessionTest):
718      <derivedshapesource table="D136169900" shapesource="D137227612"      <derivedshapesource table="D136169900" shapesource="D137227612"
719          id="D136170932"/>          id="D136170932"/>
720      <map title="Test Map">      <map title="Test Map">
721          <layer shapestore="D136170932" visible="true" stroke="#000000"          <layer shapestore="D136170932" visible="true" title="My Layer">
722                  title="My Layer" stroke_width="1" fill="None"/>              <classification>
723                    <clnull label="">
724                        <cldata stroke="#000000" stroke_width="1" fill="None"/>
725                    </clnull>
726                </classification>
727            </layer>
728      </map>      </map>
729  </session>  </session>
730  '''  '''
# Line 562  class TestJoinedTable(LoadSessionTest): Line 755  class TestJoinedTable(LoadSessionTest):
755          self.check_format()          self.check_format()
756    
757    
758    class TestLabelLayer(LoadSessionTest):
759    
760        # Note that the labels deliberately contain non-ascii characters to
761        # test whether they're supported correctly.
762    
763        file_contents = '''<?xml version="1.0" encoding="UTF-8"?>
764    <!DOCTYPE session SYSTEM "thuban-1.1.dtd">
765    <session xmlns="http://thuban.intevation.org/dtds/thuban-1.1-dev.dtd" title="Thuban sample session">
766        <fileshapesource filetype="shapefile" id="D145265052"
767            filename="../../Data/iceland/political.shp"/>
768        <fileshapesource filetype="shapefile" id="D145412868"
769            filename="../../Data/iceland/cultural_landmark-point.shp"/>
770        <map title="Iceland map">
771            <projection name="Unknown">
772                <parameter value="zone=26"/>
773                <parameter value="proj=utm"/>
774                <parameter value="ellps=clrk66"/>
775            </projection>
776            <layer shapestore="D145265052" visible="true" title="political">
777                <projection name="Geographic">
778                    <parameter value="proj=latlong"/>
779                    <parameter value="to_meter=0.017453"/>
780                    <parameter value="ellps=clrk66"/>
781                </projection>
782                <classification>
783                    <clnull label="">
784                        <cldata stroke="#000000" stroke_width="1" fill="#c0c0c0"/>
785                    </clnull>
786                </classification>
787            </layer>
788            <layer shapestore="D145412868" visible="true" title="landmarks">
789                <projection name="Geographic">
790                    <parameter value="proj=latlong"/>
791                    <parameter value="to_meter=0.017453"/>
792                    <parameter value="ellps=clrk66"/>
793                </projection>
794                <classification>
795                    <clnull label="">
796                        <cldata size="5" stroke="#000000" stroke_width="1" fill="#ffff00"/>
797                    </clnull>
798                </classification>
799            </layer>
800            <labellayer>
801                <label x="-21.5" y="64.25" text="RUINS"
802                    halign="left" valign="center"/>
803                <label x="-15.125" y="64.75" text="H\xc3\xbctte"
804                    halign="right" valign="top"/>
805            </labellayer>
806        </map>
807    </session>
808    '''
809    
810        def test(self):
811            """Test loading a session with a label layer"""
812            session = load_session(self.filename())
813            self.session = session
814    
815            label_layer = self.session.Maps()[0].LabelLayer()
816            expected_labels = [(-21.5, 64.25, "RUINS", ALIGN_LEFT, ALIGN_CENTER),
817                               (-15.125, 64.75, internal_from_unicode(u"H\xfctte"),
818                                ALIGN_RIGHT, ALIGN_TOP),
819                               ]
820            for label, values in zip(label_layer.Labels(), expected_labels):
821                self.assertEquals((label.x, label.y, label.text, label.halign,
822                                   label.valign),
823                                  values)
824            self.check_format()
825    
826    
827  class TestPostGISLayer(LoadSessionTest):  class TestPostGISLayer(LoadSessionTest):
828    
829      file_contents = '''<?xml version="1.0" encoding="UTF-8"?>      file_contents = '''<?xml version="1.0" encoding="UTF-8"?>
830  <!DOCTYPE session SYSTEM "thuban-0.9.dtd">  <!DOCTYPE session SYSTEM "thuban-1.1.dtd">
831  <session xmlns="http://thuban.intevation.org/dtds/thuban-0.9.dtd"  <session xmlns="http://thuban.intevation.org/dtds/thuban-1.1-dev.dtd"
832          title="unnamed session">          title="unnamed session">
833      <dbconnection port="%(port)s" host="%(host)s" user="%(user)s"      <dbconnection port="%(port)s" host="%(host)s" user="%(user)s"
834          dbtype="postgis" id="D142684948" dbname="%(dbname)s"/>          dbtype="postgis" id="D142684948" dbname="%(dbname)s"/>
835      <dbshapesource tablename="landmarks" id="D143149420" dbconn="D142684948"/>      <dbshapesource id="D143149420" dbconn="D142684948"
836            tablename="landmarks_point_id" id_column="point_id"
837            geometry_column="the_geom" />
838      <map title="unnamed map">      <map title="unnamed map">
839          <layer shapestore="D143149420" visible="true" stroke="#000000"          <layer shapestore="D143149420" visible="true" stroke="#000000"
840                  title="landmarks" stroke_width="1" fill="None"/>                  title="landmarks" stroke_width="1" fill="None"/>
# Line 615  class TestPostGISLayer(LoadSessionTest): Line 878  class TestPostGISLayer(LoadSessionTest):
878  class TestPostGISLayerPassword(LoadSessionTest):  class TestPostGISLayerPassword(LoadSessionTest):
879    
880      file_contents = '''<?xml version="1.0" encoding="UTF-8"?>      file_contents = '''<?xml version="1.0" encoding="UTF-8"?>
881  <!DOCTYPE session SYSTEM "thuban-0.9.dtd">  <!DOCTYPE session SYSTEM "thuban-1.1.dtd">
882  <session xmlns="http://thuban.intevation.org/dtds/thuban-0.9.dtd"  <session xmlns="http://thuban.intevation.org/dtds/thuban-1.1-dev.dtd"
883          title="unnamed session">          title="unnamed session">
884      <dbconnection port="%(port)s" host="%(host)s" user="%(user)s"      <dbconnection port="%(port)s" host="%(host)s" user="%(user)s"
885          dbtype="postgis" id="D142684948" dbname="%(dbname)s"/>          dbtype="postgis" id="D142684948" dbname="%(dbname)s"/>
# Line 710  class TestLoadError(LoadSessionTest): Line 973  class TestLoadError(LoadSessionTest):
973    
974      file_contents = '''\      file_contents = '''\
975  <?xml version="1.0" encoding="UTF-8"?>  <?xml version="1.0" encoding="UTF-8"?>
976  <!DOCTYPE session SYSTEM "thuban-0.9.dtd">  <!DOCTYPE session SYSTEM "thuban-1.1.dtd">
977  <session xmlns="http://thuban.intevation.org/dtds/thuban-0.9.dtd"  <session xmlns="http://thuban.intevation.org/dtds/thuban-1.1-dev.dtd"
978          title="single map&amp;layer">          title="single map&amp;layer">
979      <fileshapesource id="D1" filename="../../Data/iceland/political.shp"/>      <fileshapesource id="D1" filename="../../Data/iceland/political.shp"/>
980      <map title="Test Map">      <map title="Test Map">
# Line 738  class TestLoadError(LoadSessionTest): Line 1001  class TestLoadError(LoadSessionTest):
1001              # LoadError really was about the missing attribute              # LoadError really was about the missing attribute
1002              self.assertEquals(str(value),              self.assertEquals(str(value),
1003                "Element "                "Element "
1004                "(u'http://thuban.intevation.org/dtds/thuban-0.9.dtd',"                "(u'http://thuban.intevation.org/dtds/thuban-1.1-dev.dtd',"
1005                " u'fileshapesource') requires an attribute 'filetype'")                " u'fileshapesource') requires an attribute 'filetype'")
1006          else:          else:
1007              self.fail("Missing filetype attribute doesn't raise LoadError")              self.fail("Missing filetype attribute doesn't raise LoadError")
1008    
1009    class Shapefile_CallBack:
1010    
1011        def __init__(self, params):
1012            """Initialize the callback return values.
1013              
1014               params must be a dictionary of the potential CB modes (keys),
1015               with lists of tuples of return values as values.
1016               Depending on the test the callback can be called multiple,
1017               each time a return value is poped from the list
1018            """
1019    
1020            self.params = params
1021    
1022    
1023        def s_cb(self, filename, mode = None, second_try= 0):
1024            if self.params.has_key(mode):
1025                return self.params[mode].pop(0)
1026            else:
1027                raise LoadError
1028            
1029    class TestAltPath(LoadSessionTest):
1030    
1031        """Test the various cases in the alternative path feature.
1032    
1033           The test checks the reasonable cases:
1034           - First recognition of a path error, fixed with user interaction.
1035           - First recognition of a path error, load cancelled.
1036           - Path error fixed from list, confirmed by user.
1037           - Path error fixed from list, changed by user.
1038           - Path error fixed from list, cancelled by user.
1039           - Path error wrongly fixed from list, manual fix forced.
1040        """
1041    
1042        file_contents = '''\
1043    <?xml version="1.0" encoding="UTF-8"?>
1044    <!DOCTYPE session SYSTEM "thuban-1.1.dtd">
1045    <session xmlns="http://thuban.intevation.org/dtds/thuban-1.1-dev.dtd" title="AltPath Test session">
1046        <fileshapesource filetype="shapefile" id="D1108450956" filename="../../Data/iceland/political.shp"/>
1047        <fileshapesource filetype="shapefile" id="D1108900076" filename="../Data/iceland/roads-line.shp"/>
1048        <fileshapesource filetype="shapefile" id="D1108947244" filename="../../Data/iceland/cultural_landmark-point.shp"/>
1049        <map title="not the iceland map">
1050            <layer title="political" stroke_width="1" shapestore="D1108450956" visible="true" stroke="#000000" fill="#c0c0c0"/>
1051            <layer title="roads-line" stroke_width="1" shapestore="D1108900076" visible="true" stroke="#000000" fill="None"/>
1052            <layer title="something else" stroke_width="1" shapestore="D1108947244" visible="true" stroke="#000000" fill="None"/>
1053        </map>
1054    </session>
1055    '''
1056    
1057        def checkSession(self, session):
1058            """Check if session has been loaded successfully."""
1059            
1060            eq = self.assertEquals
1061    
1062            map = session.Maps()[0]
1063            layers = map.Layers()
1064    
1065            eq("AltPath Test session", session.Title())
1066            eq("not the iceland map", map.Title())
1067            eq(3,len(layers))
1068            eq("political",layers[0].Title())
1069            eq("roads-line",layers[1].Title())
1070            eq("something else",layers[2].Title())
1071    
1072        def test_01_single_path_error_fix(self):
1073            """Test single file path error fix."""
1074            # The usual initial case
1075            s_cb = Shapefile_CallBack({
1076                        "search": [("../Data/iceland/roads-line.shp",0)],
1077                        "check": [(None, None)]})
1078            self.session = load_session(self.filename(),
1079                                        shapefile_callback =s_cb.s_cb)
1080            self.checkSession(self.session)
1081            
1082        def test_02_path_error_fix_from_list(self):
1083            """Test single file path error fix."""
1084            # This represents the usual case for "from_list"
1085            s_cb = Shapefile_CallBack({
1086                    "search": [("../Data/iceland/roads-line.shp",1)],
1087                    "check": [(os.path.abspath("../Data/iceland/roads-line.shp"),1)]
1088                   })
1089            self.session = load_session(self.filename(),
1090                                        shapefile_callback =s_cb.s_cb)
1091            self.checkSession(self.session)
1092    
1093        def test_03_single_path_error_cancelled(self):
1094            """Test alternative path cancelled."""
1095            s_cb = Shapefile_CallBack({
1096                        "search": [(None,0)],
1097                        "check": [(None, None)]})
1098            self.assertRaises(LoadCancelled,
1099                                load_session, self.filename(), None, s_cb.s_cb)
1100    
1101        def test_04_path_error_fix_from_list_cancelled(self):
1102            """Test alternative path from list cancelled."""
1103            s_cb = Shapefile_CallBack({
1104                    "search": [("../Data/iceland/roads-line.shp",1)],
1105                    "check": [(None,1)]
1106                   })
1107            self.assertRaises(LoadCancelled,
1108                                load_session, self.filename(), None, s_cb.s_cb)
1109    
1110        def test_05_path_error_fix_from_list_changed(self):
1111            """Test alternative path from list changed."""
1112            s_cb = Shapefile_CallBack({
1113                    "search": [("../Data/iceland/roads-line.shp",1)],
1114                    "check": [("../Data/iceland/roads-line.shp",0)]
1115                   })
1116            self.session = load_session(self.filename(),
1117                                        shapefile_callback =s_cb.s_cb)
1118            self.checkSession(self.session)
1119    
1120        def test_06_path_error_fix_from_list_fails(self):
1121            """Test alternative path recovery from list."""
1122            s_cb = Shapefile_CallBack({
1123                    "search": [("../wrong/iceland/roads-line.shp",1),
1124                                ("../Data/iceland/roads-line.shp",0)],
1125                    "check": [(None,None)]
1126                   })
1127            self.session = load_session(self.filename(),
1128                                        shapefile_callback =s_cb.s_cb)
1129            self.assertRaises(IndexError,
1130                                s_cb.s_cb, None, "search")
1131            
1132    
1133    
1134  if __name__ == "__main__":  if __name__ == "__main__":
1135      support.run_tests()      support.run_tests()

Legend:
Removed from v.1683  
changed lines
  Added in v.2654

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26