/[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 1687 by bh, Fri Aug 29 10:02:16 2003 UTC revision 1970 by bh, Mon Nov 24 18:36:00 2003 UTC
# 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.Model.save import save_session  from Thuban.Model.save import save_session
44  from Thuban.Model.load import load_session, parse_color, LoadError, \  from Thuban.Model.load import load_session, parse_color, LoadError, \
# Line 46  from Thuban.Model.color import Transpare Line 47  from Thuban.Model.color import Transpare
47  from Thuban.Model.classification import ClassGroupProperties, ClassGroupRange,\  from Thuban.Model.classification import ClassGroupProperties, ClassGroupRange,\
48      ClassGroupSingleton, ClassGroupDefault      ClassGroupSingleton, ClassGroupDefault
49  from Thuban.Model.postgisdb import ConnectionError  from Thuban.Model.postgisdb import ConnectionError
50    from Thuban.Model.table import DBFTable, MemoryTable, \
51         FIELDTYPE_DOUBLE, FIELDTYPE_INT, FIELDTYPE_STRING, \
52         table_to_dbf
53    
54    
55  def filenames_equal(name1, name2):  def filenames_equal(name1, name2):
56      """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 87  class LoadSessionTest(support.FileLoadTe
87          self.session = None          self.session = None
88    
89    
90      dtd = "http://thuban.intevation.org/dtds/thuban-0.9.dtd"      dtd = "http://thuban.intevation.org/dtds/thuban-1.0-dev.dtd"
91      thubanids = [((dtd, n), (None, "id")) for n in      thubanids = [((dtd, n), (None, "id")) for n in
92                   ["fileshapesource", "filetable", "jointable",                   ["fileshapesource", "filetable", "jointable",
93                    "derivedshapesource"]]                    "derivedshapesource"]]
# Line 173  class ClassificationTest(LoadSessionTest Line 178  class ClassificationTest(LoadSessionTest
178    
179  class TestSingleLayer(LoadSessionTest):  class TestSingleLayer(LoadSessionTest):
180    
181        # Note: The use of & and non-ascii characters is deliberate. We
182        # want to test whether the loading code handles that correctly.
183      file_contents = '''\      file_contents = '''\
184  <?xml version="1.0" encoding="UTF-8"?>  <?xml version="1.0" encoding="UTF-8"?>
185  <!DOCTYPE session SYSTEM "thuban-0.9.dtd">  <!DOCTYPE session SYSTEM "thuban-1.0.dtd">
186  <session xmlns="http://thuban.intevation.org/dtds/thuban-0.9.dtd"  <session xmlns="http://thuban.intevation.org/dtds/thuban-1.0-dev.dtd"
187          title="single map&amp;layer">          title="Stra\xc3\x9fen &amp; Landmarken">
188      <fileshapesource filetype="shapefile" id="D1"      <fileshapesource filetype="shapefile" id="D1"
189          filename="../../Data/iceland/political.shp"/>          filename="../../Data/iceland/political.shp"/>
190      <map title="Test Map">      <map title="\xc3\x9cbersicht">
191          <projection name="Unknown">          <projection epsg="32627" name="WGS 84 / UTM zone 27N">
192              <parameter value="zone=26"/>              <parameter value="datum=WGS84"/>
193                <parameter value="ellps=WGS84"/>
194              <parameter value="proj=utm"/>              <parameter value="proj=utm"/>
195              <parameter value="ellps=clrk66"/>              <parameter value="units=m"/>
196                <parameter value="zone=27"/>
197          </projection>          </projection>
198          <layer shapestore="D1" visible="true"          <layer shapestore="D1" visible="true"
199                  stroke="#000000" title="My Layer" stroke_width="1"                  stroke="#000000" title="K\xc3\xbcste" stroke_width="1"
200                  fill="None"/>                  fill="None"/>
201      </map>      </map>
202  </session>  </session>
# Line 200  class TestSingleLayer(LoadSessionTest): Line 209  class TestSingleLayer(LoadSessionTest):
209          self.session = session          self.session = session
210    
211          # Check the title          # Check the title
212          eq(session.Title(), "single map&layer")          eq(session.Title(), "Stra\xdfen & Landmarken")
213    
214          # the session has one map.          # the session has one map.
215          maps = session.Maps()          maps = session.Maps()
# Line 208  class TestSingleLayer(LoadSessionTest): Line 217  class TestSingleLayer(LoadSessionTest):
217    
218          # Check the map's attributes          # Check the map's attributes
219          map = maps[0]          map = maps[0]
220          eq(map.Title(), "Test Map")          eq(map.Title(), "\xdcbersicht")
221            proj = map.GetProjection()
222            eq(proj.GetName(), "WGS 84 / UTM zone 27N")
223            eq(proj.EPSGCode(), "32627")
224            params = proj.GetAllParameters()
225            params.sort()
226            eq(params, ["datum=WGS84", "ellps=WGS84", "proj=utm", "units=m",
227                        "zone=27"])
228    
229          # the map has a single layer          # the map has a single layer
230          layers = map.Layers()          layers = map.Layers()
# Line 216  class TestSingleLayer(LoadSessionTest): Line 232  class TestSingleLayer(LoadSessionTest):
232    
233          # Check the layer attributes          # Check the layer attributes
234          layer = layers[0]          layer = layers[0]
235          eq(layer.Title(), "My Layer")          eq(layer.Title(), "K\xfcste")
236          self.failUnless(filenames_equal(layer.ShapeStore().FileName(),          self.failUnless(filenames_equal(layer.ShapeStore().FileName(),
237                                          os.path.join(self.temp_dir(),                                          os.path.join(self.temp_dir(),
238                                                       os.pardir, os.pardir,                                                       os.pardir, os.pardir,
# Line 231  class TestSingleLayer(LoadSessionTest): Line 247  class TestSingleLayer(LoadSessionTest):
247          self.session.Destroy()          self.session.Destroy()
248          self.session = None          self.session = None
249    
250        def test_leak(self):
251            """Test load_session for resource leaks
252    
253            The load_session function had a resource leak in that it created
254            cyclic references. The objects would have been eventually
255            collected by the garbage collector but too late. One symptom is
256            that when layers are removed so that the last normal reference
257            owned indirectly by the session to a shape store goes away, the
258            shape store is not actually removed from the session even though
259            the session only keeps weak references because there are still
260            references owned by the cyclic garbage.
261            """
262            session = load_session(self.filename())
263            self.session = session
264    
265            # sanity check
266            self.assertEquals(len(session.ShapeStores()), 1)
267    
268            # remove the map. The shapestore should go away too
269            session.RemoveMap(session.Maps()[0])
270            self.assertEquals(len(session.ShapeStores()), 0)
271    
272    
273    class TestNonAsciiColumnName(LoadSessionTest):
274    
275        file_contents = '''\
276    <?xml version="1.0" encoding="UTF-8"?>
277    <!DOCTYPE session SYSTEM "thuban-1.0.dtd">
278    <session xmlns="http://thuban.intevation.org/dtds/thuban-1.0-dev.dtd"
279            title="Non ASCII column name test">
280        <fileshapesource filetype="shapefile" id="D1"
281            filename="TestNonAsciiColumnName.shp"/>
282        <map title="map">
283            <projection name="Some Projection">
284                <parameter value="datum=WGS84"/>
285                <parameter value="ellps=WGS84"/>
286                <parameter value="proj=utm"/>
287                <parameter value="units=m"/>
288                <parameter value="zone=27"/>
289            </projection>
290            <layer shapestore="D1" visible="true"
291                    stroke="#000000" title="layer" stroke_width="1"
292                    fill="None">
293                <classification field="Fl\xc3\xa4che" field_type="double">
294                    <clnull label="">
295                        <cldata stroke="#000000" stroke_width="1" fill="None"/>
296                    </clnull>
297                </classification>
298            </layer>
299        </map>
300    </session>
301    '''
302    
303        def test(self):
304            """Load a session with a single map with a single layer"""
305    
306            # Create a shapefile and a dbffile with a non-ascii column name
307            dbffile = self.temp_file_name("TestNonAsciiColumnName.dbf")
308            shpfile = self.temp_file_name("TestNonAsciiColumnName.shp")
309            dbf = dbflib.create(dbffile)
310            dbf.add_field('Fl\xe4che', dbflib.FTDouble, 10, 5)
311            dbf.write_record(0, (0.0,))
312            dbf.close()
313            shp = shapelib.create(shpfile, shapelib.SHPT_POLYGON)
314            shp.write_object(-1, shapelib.SHPObject(shapelib.SHPT_POLYGON, 1,
315                                                    [[(0,0), (10, 10), (10, 0),
316                                                      (0, 0)]]))
317            shp.close()
318    
319            try:
320                session = load_session(self.filename())
321            except ValueError, v:
322                # Usually if the field name is not decoded properly the
323                # loading fails because the field type mentioned in the file
324                # is not None as returned from the layer for a non-existing
325                # column name so we check for that and report it as failure.
326                # Other exceptions are errors in the test case.
327                if str(v) == "xml field type differs from database!":
328                    self.fail("Cannot load file with non-ascii column names")
329                else:
330                    raise
331            self.session = session
332    
333            # In case Thuban could load the file anyway (i.e. no ValueError
334            # exception in load_session()), check explicitly whether the
335            # field name was decoded properly. The test will probably lead
336            # to a UnicodeError instead of a test failure so we check that
337            # too
338            layer = session.Maps()[0].Layers()[0]
339            try:
340                self.assertEquals(layer.GetClassificationColumn(), 'Fl\xe4che')
341            except UnicodeError:
342                # FIXME: Obviously this will have to change if Thuban ever
343                # supports unicode properly.
344                self.fail("Column name was not converted to a bytestring")
345    
346            # roundtrip check
347            self.check_format()
348    
349    
350  class TestLayerVisibility(LoadSessionTest):  class TestLayerVisibility(LoadSessionTest):
351    
352      file_contents = '''\      file_contents = '''\
353  <?xml version="1.0" encoding="UTF-8"?>  <?xml version="1.0" encoding="UTF-8"?>
354  <!DOCTYPE session SYSTEM "thuban-0.9.dtd">  <!DOCTYPE session SYSTEM "thuban-1.0.dtd">
355  <session xmlns="http://thuban.intevation.org/dtds/thuban-0.9.dtd"  <session xmlns="http://thuban.intevation.org/dtds/thuban-1.0-dev.dtd"
356          title="single map&amp;layer">          title="single map&amp;layer">
357      <fileshapesource filetype="shapefile" id="D1"      <fileshapesource filetype="shapefile" id="D1"
358          filename="../../Data/iceland/political.shp"/>          filename="../../Data/iceland/political.shp"/>
# Line 361  class TestLabels(ClassificationTest): Line 476  class TestLabels(ClassificationTest):
476    
477      file_contents = '''\      file_contents = '''\
478  <?xml version="1.0" encoding="UTF-8"?>  <?xml version="1.0" encoding="UTF-8"?>
479  <!DOCTYPE session SYSTEM "thuban-0.9.dtd">  <!DOCTYPE session SYSTEM "thuban-1.0.dtd">
480  <session xmlns="http://thuban.intevation.org/dtds/thuban-0.9.dtd"  <session xmlns="http://thuban.intevation.org/dtds/thuban-1.0-dev.dtd"
481          title="single map&amp;layer">          title="single map&amp;layer">
482      <fileshapesource filetype="shapefile" id="D1"      <fileshapesource filetype="shapefile" id="D1"
483          filename="../../Data/iceland/political.shp"/>          filename="../../Data/iceland/political.shp"/>
# Line 409  class TestLayerProjection(LoadSessionTes Line 524  class TestLayerProjection(LoadSessionTes
524    
525      file_contents = '''\      file_contents = '''\
526  <?xml version="1.0" encoding="UTF-8"?>  <?xml version="1.0" encoding="UTF-8"?>
527  <!DOCTYPE session SYSTEM "thuban-0.9.dtd">  <!DOCTYPE session SYSTEM "thuban-1.0.dtd">
528  <session xmlns="http://thuban.intevation.org/dtds/thuban-0.9.dtd"  <session xmlns="http://thuban.intevation.org/dtds/thuban-1.0-dev.dtd"
529          title="single map&amp;layer">          title="single map&amp;layer">
530      <fileshapesource filetype="shapefile" id="D2"      <fileshapesource filetype="shapefile" id="D2"
531          filename="../../Data/iceland/roads-line.shp"/>          filename="../../Data/iceland/roads-line.shp"/>
# Line 487  class TestRasterLayer(LoadSessionTest): Line 602  class TestRasterLayer(LoadSessionTest):
602    
603      file_contents = '''\      file_contents = '''\
604  <?xml version="1.0" encoding="UTF-8"?>  <?xml version="1.0" encoding="UTF-8"?>
605  <!DOCTYPE session SYSTEM "thuban-0.9.dtd">  <!DOCTYPE session SYSTEM "thuban-1.0.dtd">
606  <session xmlns="http://thuban.intevation.org/dtds/thuban-0.9.dtd"  <session xmlns="http://thuban.intevation.org/dtds/thuban-1.0-dev.dtd"
607          title="single map&amp;layer">          title="single map&amp;layer">
608      <map title="Test Map">      <map title="Test Map">
609          <rasterlayer visible="false" filename="../../Data/iceland/island.tif"          <rasterlayer visible="false" filename="../../Data/iceland/island.tif"
# Line 521  class TestRasterLayer(LoadSessionTest): Line 636  class TestRasterLayer(LoadSessionTest):
636  class TestJoinedTable(LoadSessionTest):  class TestJoinedTable(LoadSessionTest):
637    
638      file_contents = '''<?xml version="1.0" encoding="UTF-8"?>      file_contents = '''<?xml version="1.0" encoding="UTF-8"?>
639  <!DOCTYPE session SYSTEM "thuban-0.9.dtd">  <!DOCTYPE session SYSTEM "thuban-1.0.dtd">
640  <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.0-dev.dtd" title="A Joined Table session">
641      <fileshapesource filetype="shapefile" id="D137227612"      <fileshapesource filetype="shapefile" id="D137227612"
642          filename="../../Data/iceland/roads-line.shp"/>          filename="../../Data/iceland/roads-line.shp"/>
643      <filetable filetype="DBF" filename="load_joinedtable.dbf" id="D136171140"      <filetable filetype="DBF" filename="load_joinedtable.dbf" id="D136171140"
# Line 570  class TestJoinedTable(LoadSessionTest): Line 685  class TestJoinedTable(LoadSessionTest):
685  class TestPostGISLayer(LoadSessionTest):  class TestPostGISLayer(LoadSessionTest):
686    
687      file_contents = '''<?xml version="1.0" encoding="UTF-8"?>      file_contents = '''<?xml version="1.0" encoding="UTF-8"?>
688  <!DOCTYPE session SYSTEM "thuban-0.9.dtd">  <!DOCTYPE session SYSTEM "thuban-1.0.dtd">
689  <session xmlns="http://thuban.intevation.org/dtds/thuban-0.9.dtd"  <session xmlns="http://thuban.intevation.org/dtds/thuban-1.0-dev.dtd"
690          title="unnamed session">          title="unnamed session">
691      <dbconnection port="%(port)s" host="%(host)s" user="%(user)s"      <dbconnection port="%(port)s" host="%(host)s" user="%(user)s"
692          dbtype="postgis" id="D142684948" dbname="%(dbname)s"/>          dbtype="postgis" id="D142684948" dbname="%(dbname)s"/>
# Line 619  class TestPostGISLayer(LoadSessionTest): Line 734  class TestPostGISLayer(LoadSessionTest):
734  class TestPostGISLayerPassword(LoadSessionTest):  class TestPostGISLayerPassword(LoadSessionTest):
735    
736      file_contents = '''<?xml version="1.0" encoding="UTF-8"?>      file_contents = '''<?xml version="1.0" encoding="UTF-8"?>
737  <!DOCTYPE session SYSTEM "thuban-0.9.dtd">  <!DOCTYPE session SYSTEM "thuban-1.0.dtd">
738  <session xmlns="http://thuban.intevation.org/dtds/thuban-0.9.dtd"  <session xmlns="http://thuban.intevation.org/dtds/thuban-1.0-dev.dtd"
739          title="unnamed session">          title="unnamed session">
740      <dbconnection port="%(port)s" host="%(host)s" user="%(user)s"      <dbconnection port="%(port)s" host="%(host)s" user="%(user)s"
741          dbtype="postgis" id="D142684948" dbname="%(dbname)s"/>          dbtype="postgis" id="D142684948" dbname="%(dbname)s"/>
# Line 714  class TestLoadError(LoadSessionTest): Line 829  class TestLoadError(LoadSessionTest):
829    
830      file_contents = '''\      file_contents = '''\
831  <?xml version="1.0" encoding="UTF-8"?>  <?xml version="1.0" encoding="UTF-8"?>
832  <!DOCTYPE session SYSTEM "thuban-0.9.dtd">  <!DOCTYPE session SYSTEM "thuban-1.0.dtd">
833  <session xmlns="http://thuban.intevation.org/dtds/thuban-0.9.dtd"  <session xmlns="http://thuban.intevation.org/dtds/thuban-1.0-dev.dtd"
834          title="single map&amp;layer">          title="single map&amp;layer">
835      <fileshapesource id="D1" filename="../../Data/iceland/political.shp"/>      <fileshapesource id="D1" filename="../../Data/iceland/political.shp"/>
836      <map title="Test Map">      <map title="Test Map">
# Line 742  class TestLoadError(LoadSessionTest): Line 857  class TestLoadError(LoadSessionTest):
857              # LoadError really was about the missing attribute              # LoadError really was about the missing attribute
858              self.assertEquals(str(value),              self.assertEquals(str(value),
859                "Element "                "Element "
860                "(u'http://thuban.intevation.org/dtds/thuban-0.9.dtd',"                "(u'http://thuban.intevation.org/dtds/thuban-1.0-dev.dtd',"
861                " u'fileshapesource') requires an attribute 'filetype'")                " u'fileshapesource') requires an attribute 'filetype'")
862          else:          else:
863              self.fail("Missing filetype attribute doesn't raise LoadError")              self.fail("Missing filetype attribute doesn't raise LoadError")

Legend:
Removed from v.1687  
changed lines
  Added in v.1970

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26