/[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 690 by jonathan, Wed Apr 16 13:47:22 2003 UTC revision 1683 by bh, Thu Aug 28 15:20:57 2003 UTC
# Line 1  Line 1 
1  # Copyright (c) 2002 by Intevation GmbH  # Copyright (c) 2002, 2003 by Intevation GmbH
2  # Authors:  # Authors:
3  # Bernhard Herzog <[email protected]>  # Bernhard Herzog <[email protected]>
4  #  #
# 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  import postgissupport
38  from Thuban.Model.session import Session  from xmlsupport import sax_eventlist
 from Thuban.Model.map import Map  
 from Thuban.Model.layer import Layer  
 from Thuban.Model.proj import Projection  
 from Thuban.Model.color import Color  
39    
40  from Thuban.Model.table import FIELDTYPE_INT, FIELDTYPE_DOUBLE, FIELDTYPE_STRING  import dbflib
41    
42    from Thuban.Model.save import save_session
43    from Thuban.Model.load import load_session, parse_color, LoadError, \
44         LoadCancelled
45    from Thuban.Model.color import Transparent
46  from Thuban.Model.classification import ClassGroupProperties, ClassGroupRange,\  from Thuban.Model.classification import ClassGroupProperties, ClassGroupRange,\
47      ClassGroupSingleton, ClassGroupDefault      ClassGroupSingleton, ClassGroupDefault
48    from Thuban.Model.postgisdb import ConnectionError
49    
50  def filenames_equal(name1, name2):  def filenames_equal(name1, name2):
51      """Return true if the filenames name1 and name2 are equal.      """Return true if the filenames name1 and name2 are equal.
# Line 43  def filenames_equal(name1, name2): Line 59  def filenames_equal(name1, name2):
59      return os.path.normpath(name1) == os.path.normpath(name2)      return os.path.normpath(name1) == os.path.normpath(name2)
60    
61    
62  contents_single_map = '''\  
63    class LoadSessionTest(support.FileLoadTestCase):
64    
65        """Base class for .thuban file loading tests
66    
67        Basically the same as the FileLoadTestCase, except that all tests
68        use the '.thuban' extension by default and that setUp and tearDown
69        handle sessions.
70        """
71    
72        file_extension = ".thuban"
73    
74        def setUp(self):
75            """Create the test files"""
76            support.FileLoadTestCase.setUp(self)
77            self.session = None
78    
79        def tearDown(self):
80            if self.session is not None:
81                self.session.Destroy()
82            self.session = None
83    
84    
85        dtd = "http://thuban.intevation.org/dtds/thuban-0.9.dtd"
86        thubanids = [((dtd, n), (None, "id")) for n in
87                     ["fileshapesource", "filetable", "jointable",
88                      "derivedshapesource"]]
89        thubanidrefs = [((dtd, n), (None, m)) for n, m in
90                        [("layer", "shapestore"),
91                         ("jointable", "left"),
92                         ("jointable", "right"),
93                         ("derivedshapesource", "table"),
94                         ("derivedshapesource", "shapesource")]]
95        filenames = [((dtd, n), (None, m)) for n, m in
96                     [("fileshapesource", "filename"),
97                      ("rasterlayer", "filename"),
98                      ("filetable", "filename")]]
99        del n, m, dtd
100    
101        def check_format(self):
102            """Check whether the file we loaded from matches the one that
103            would be written. Call this from each test case after loading
104            the session
105            """
106            filename = self.temp_file_name(self.id() + ".roundtrip.thuban")
107            save_session(self.session, filename)
108            el1 = sax_eventlist(filename = filename, ids = self.thubanids,
109                                idrefs = self.thubanidrefs,
110                                filenames = self.filenames)
111            el2 = sax_eventlist(filename = self.filename(), ids = self.thubanids,
112                                idrefs = self.thubanidrefs,
113                                filenames = self.filenames)
114            if 0:
115                for a, b in zip(el1, el2):
116                    print a != b and "***************" or ""
117                    print a
118                    print b
119            self.assertEquals(el1, el2,
120                              "loaded file not equivalent to the saved file")
121    
122    
123    class ClassificationTest(LoadSessionTest):
124    
125        """
126        Base class for tests that do some detailed checking of classifications
127        """
128    
129        def TestLayers(self, layers, expected):
130            TITLE = 0
131            NUM_GROUPS = 1
132            CLASSES = 2
133            GROUP_TYPE = 0
134            GROUP_DATA = 1
135            GROUP_LABEL = 2
136            GROUP_PROPS = 3
137    
138            eq = self.assertEquals
139    
140            eq(len(layers), len(expected))
141    
142            for layer, data in zip(layers, expected):
143                eq(layer.Title(), data[TITLE])
144    
145                clazz = layer.GetClassification()
146                eq(clazz.GetNumGroups(), data[NUM_GROUPS])
147                eq(clazz.GetNumGroups() + 1, len(data[CLASSES]))
148    
149                i = 0
150                for group in clazz:
151                    props = ClassGroupProperties()
152                    props.SetLineColor(
153                        parse_color(data[CLASSES][i][GROUP_PROPS][0]))
154                    props.SetLineWidth(data[CLASSES][i][GROUP_PROPS][1])
155                    props.SetFill(
156                        parse_color(data[CLASSES][i][GROUP_PROPS][2]))
157    
158                    if data[CLASSES][i][GROUP_TYPE] == "default":
159                        g = ClassGroupDefault(props, data[CLASSES][i][GROUP_LABEL])
160                    elif data[CLASSES][i][GROUP_TYPE] == "range":
161                        g = ClassGroupRange((data[CLASSES][i][GROUP_DATA][0],
162                                             data[CLASSES][i][GROUP_DATA][1]),
163                                            props, data[CLASSES][i][GROUP_LABEL])
164                    elif data[CLASSES][i][GROUP_TYPE] == "single":
165                        g = ClassGroupSingleton(data[CLASSES][i][GROUP_DATA],
166                                              props, data[CLASSES][i][GROUP_LABEL])
167    
168                    eq(group, g)
169    
170                    i += 1
171    
172    
173    
174    class TestSingleLayer(LoadSessionTest):
175    
176        file_contents = '''\
177  <?xml version="1.0" encoding="UTF-8"?>  <?xml version="1.0" encoding="UTF-8"?>
178  <!DOCTYPE session SYSTEM "thuban.dtd">  <!DOCTYPE session SYSTEM "thuban-0.9.dtd">
179  <session title="single map&amp;layer">  <session xmlns="http://thuban.intevation.org/dtds/thuban-0.9.dtd"
180          <map title="Test Map">          title="single map&amp;layer">
181                  <projection>      <fileshapesource filetype="shapefile" id="D1"
182                          <parameter value="zone=26"/>          filename="../../Data/iceland/political.shp"/>
183                          <parameter value="proj=utm"/>      <map title="Test Map">
184                          <parameter value="ellps=clrk66"/>          <projection name="Unknown">
185                  </projection>              <parameter value="zone=26"/>
186                  <layer title="My Layer" stroke_width="1" fill="None"              <parameter value="proj=utm"/>
187                      filename="../../Data/iceland/political.shp"              <parameter value="ellps=clrk66"/>
188                      stroke="#000000"/>          </projection>
189          </map>          <layer shapestore="D1" visible="true"
190                    stroke="#000000" title="My Layer" stroke_width="1"
191                    fill="None"/>
192        </map>
193    </session>
194    '''
195    
196        def test(self):
197            """Load a session with a single map with a single layer"""
198            eq = self.assertEquals
199            session = load_session(self.filename())
200            self.session = session
201    
202            # Check the title
203            eq(session.Title(), "single map&layer")
204    
205            # the session has one map.
206            maps = session.Maps()
207            eq(len(maps), 1)
208    
209            # Check the map's attributes
210            map = maps[0]
211            eq(map.Title(), "Test Map")
212    
213            # the map has a single layer
214            layers = map.Layers()
215            eq(len(layers), 1)
216    
217            # Check the layer attributes
218            layer = layers[0]
219            eq(layer.Title(), "My Layer")
220            self.failUnless(filenames_equal(layer.ShapeStore().FileName(),
221                                            os.path.join(self.temp_dir(),
222                                                         os.pardir, os.pardir,
223                                                         "Data", "iceland",
224                                                         "political.shp")))
225            eq(layer.GetClassification().GetDefaultFill(), Transparent)
226            eq(layer.GetClassification().GetDefaultLineColor().hex(), "#000000")
227            eq(layer.Visible(), True)
228    
229            self.check_format()
230    
231            self.session.Destroy()
232            self.session = None
233    
234    
235    class TestLayerVisibility(LoadSessionTest):
236    
237        file_contents = '''\
238    <?xml version="1.0" encoding="UTF-8"?>
239    <!DOCTYPE session SYSTEM "thuban-0.9.dtd">
240    <session xmlns="http://thuban.intevation.org/dtds/thuban-0.9.dtd"
241            title="single map&amp;layer">
242        <fileshapesource filetype="shapefile" id="D1"
243            filename="../../Data/iceland/political.shp"/>
244        <map title="Test Map">
245            <projection name="Unknown">
246                <parameter value="zone=26"/>
247                <parameter value="proj=utm"/>
248                <parameter value="ellps=clrk66"/>
249            </projection>
250            <layer shapestore="D1" visible="false" stroke="#000000"
251                    title="My Layer" stroke_width="1" fill="None"/>
252        </map>
253  </session>  </session>
254  '''  '''
255    
256  contents_classified_map_v0_2 = '''\      def test(self):
257            """Test that the visible flag is correctly loaded for a layer."""
258            eq = self.assertEquals
259            session = load_session(self.filename())
260            self.session = session
261            maps = session.Maps()
262            eq(len(maps), 1)
263            map = maps[0]
264            layers = map.Layers()
265            eq(len(layers), 1)
266            layer = layers[0]
267    
268            eq(layer.Visible(), False)
269    
270            self.check_format()
271    
272    
273    class TestClassification(ClassificationTest):
274    
275        file_contents = '''\
276  <?xml version="1.0" encoding="UTF-8"?>  <?xml version="1.0" encoding="UTF-8"?>
277  <!DOCTYPE session SYSTEM "thuban.dtd">  <!DOCTYPE session SYSTEM "thuban.dtd">
278  <session title="single map&amp;layer">  <session title="single map&amp;layer">
# Line 83  contents_classified_map_v0_2 = '''\ Line 295  contents_classified_map_v0_2 = '''\
295                  <clpoint value="1">                  <clpoint value="1">
296                      <cldata stroke="#000000" stroke_width="10" fill="None"/>                      <cldata stroke="#000000" stroke_width="10" fill="None"/>
297                  </clpoint>                  </clpoint>
298                    <clpoint value="\xc3\xa4\xc3\xb6\xc3\xbc"
299                             label="\xc3\x9cml\xc3\xa4uts">
300                        <cldata fill="None" stroke="#000000" stroke_width="1"/>
301                    </clpoint>
302              </classification>              </classification>
303          </layer>          </layer>
304                  <layer title="My Layer 2" stroke_width="1" fill="None"                  <layer title="My Layer 2" stroke_width="1" fill="None"
# Line 90  contents_classified_map_v0_2 = '''\ Line 306  contents_classified_map_v0_2 = '''\
306                      stroke="#000000">                      stroke="#000000">
307              <classification field="AREA" field_type="double">              <classification field="AREA" field_type="double">
308                  <clnull>                  <clnull>
                     <cldata stroke="#000000" stroke_width="1" fill="None"/>  
                 </clnull>  
                 <clnull>  
309                      <cldata stroke="#000000" stroke_width="2" fill="None"/>                      <cldata stroke="#000000" stroke_width="2" fill="None"/>
310                  </clnull>                  </clnull>
311                  <clrange min="0" max="1">                  <clrange min="0" max="1">
312                      <cldata stroke="#000000" stroke_width="1" fill="None"/>                      <cldata stroke="#111111" stroke_width="1" fill="None"/>
313                  </clrange>                  </clrange>
314                  <clpoint value=".5">                  <clpoint value=".5">
315                      <cldata stroke="#000000" stroke_width="1" fill="None"/>                      <cldata stroke="#000000" stroke_width="1" fill="#111111"/>
316                  </clpoint>                  </clpoint>
317                  <clrange min="-1" max="0">                  <clrange min="-1" max="0">
318                      <cldata stroke="#000000" stroke_width="1" fill="None"/>                      <cldata stroke="#000000" stroke_width="1" fill="None"/>
# Line 113  contents_classified_map_v0_2 = '''\ Line 326  contents_classified_map_v0_2 = '''\
326  </session>  </session>
327  '''  '''
328    
329  contents_test_labels = '''\      def test(self):
330            """Load a Thuban session with a map and classified layers."""
331            session = load_session(self.filename())
332            self.session = session
333    
334            map = self.session.Maps()[0] # only one map in the sample
335    
336            expected = [("My Layer", 3,
337                            [("default", (), "",
338                                ("#000000", 1, "None")),
339                             ("single", "1", "",
340                                ("#000000", 2, "None")),
341                             ("single", "1", "",
342                                ("#000000", 10, "None")),
343                             ("single", "\xe4\xf6\xfc", "\xdcml\xe4uts",
344                                ("#000000", 1, "None"))]),
345                         ("My Layer 2", 4,
346                             [("default", (), "",
347                                ("#000000", 2, "None")),
348                              ("range", (0, 1), "",
349                                ("#111111", 1, "None")),
350                              ("single", .5, "",
351                                ("#000000", 1, "#111111")),
352                              ("range", (-1, 0), "",
353                                ("#000000", 1, "None")),
354                              ("single", -.5, "",
355                                ("#000000", 1, "None"))])]
356    
357            self.TestLayers(map.Layers(), expected)
358    
359    
360    class TestLabels(ClassificationTest):
361    
362        file_contents = '''\
363  <?xml version="1.0" encoding="UTF-8"?>  <?xml version="1.0" encoding="UTF-8"?>
364  <!DOCTYPE session SYSTEM "thuban.dtd">  <!DOCTYPE session SYSTEM "thuban-0.9.dtd">
365  <session title="single map&amp;layer">  <session xmlns="http://thuban.intevation.org/dtds/thuban-0.9.dtd"
366          <map title="Test Map">          title="single map&amp;layer">
367                  <projection>      <fileshapesource filetype="shapefile" id="D1"
368                          <parameter value="zone=26"/>          filename="../../Data/iceland/political.shp"/>
369                          <parameter value="proj=utm"/>      <map title="Test Map">
370                          <parameter value="ellps=clrk66"/>          <projection name="Unknown">
371                  </projection>              <parameter value="zone=26"/>
372                  <layer title="My Layer" stroke_width="1" fill="None"              <parameter value="proj=utm"/>
373                      filename="../../Data/iceland/political.shp"              <parameter value="ellps=clrk66"/>
374                      stroke="#000000">          </projection>
375            <layer shapestore="D1" visible="true" stroke="#000000"
376                    title="My Layer" stroke_width="1" fill="None">
377              <classification field="POPYREG" field_type="string">              <classification field="POPYREG" field_type="string">
378                  <clnull label="hallo">                  <clnull label="hallo">
379                      <cldata stroke="#000000" stroke_width="1" fill="None"/>                      <cldata stroke="#000000" stroke_width="1" fill="None"/>
# Line 139  contents_test_labels = '''\ Line 387  contents_test_labels = '''\
387  </session>  </session>
388  '''  '''
389    
390  class LoadSessionTest(unittest.TestCase, support.FileTestMixin):      def test(self):
391            """Load a session and test for reading the group labels."""
392            eq = self.assertEquals
393            session = load_session(self.filename())
394            self.session = session
395    
396      def setUp(self):          map = self.session.Maps()[0] # only one map in the sample
         """Create the test files"""  
         file = open(self.temp_file_name("load_singlelayer.thuban"), "w")  
         file.write(contents_single_map)  
         file.close()  
   
         file = open(self.temp_file_name("load_classified_v0_2.thuban"), "w")  
         file.write(contents_classified_map_v0_2)  
         file.close()  
   
         file = open(self.temp_file_name("load_labels.thuban"), "w")  
         file.write(contents_test_labels)  
         file.close()  
         self.session = None  
397    
398      def tearDown(self):          expected = [("My Layer", 1,
399          if self.session is not None:                          [("default", (), "hallo",
400              self.session.Destroy()                              ("#000000", 1, "None")),
401                             ("single", "1", "welt",
402                                ("#000000", 2, "None"))])]
403    
404      def testSingleLayer(self):          self.TestLayers(map.Layers(), expected)
405          """Load a session with a single map with a single layer"""          self.check_format()
406    
407    
408    class TestLayerProjection(LoadSessionTest):
409    
410        file_contents = '''\
411    <?xml version="1.0" encoding="UTF-8"?>
412    <!DOCTYPE session SYSTEM "thuban-0.9.dtd">
413    <session xmlns="http://thuban.intevation.org/dtds/thuban-0.9.dtd"
414            title="single map&amp;layer">
415        <fileshapesource filetype="shapefile" id="D2"
416            filename="../../Data/iceland/roads-line.shp"/>
417        <fileshapesource filetype="shapefile" id="D4"
418            filename="../../Data/iceland/political.shp"/>
419        <map title="Test Map">
420            <projection name="Unknown">
421                <parameter value="zone=26"/>
422                <parameter value="proj=utm"/>
423                <parameter value="ellps=clrk66"/>
424            </projection>
425            <layer shapestore="D4" visible="true" stroke="#000000"
426                    title="My Layer" stroke_width="1" fill="None">
427                <projection name="hello">
428                    <parameter value="zone=13"/>
429                    <parameter value="proj=tmerc"/>
430                    <parameter value="ellps=clrk66"/>
431                </projection>
432                <classification field="POPYREG" field_type="string">
433                    <clnull label="hallo">
434                        <cldata stroke="#000000" stroke_width="1" fill="None"/>
435                    </clnull>
436                    <clpoint label="welt" value="1">
437                        <cldata stroke="#000000" stroke_width="2" fill="None"/>
438                    </clpoint>
439                </classification>
440            </layer>
441            <layer shapestore="D2" visible="true" stroke="#000000"
442                    title="My Layer" stroke_width="1" fill="None">
443                <projection name="Unknown">
444                    <parameter value="proj=lcc"/>
445                    <parameter value="ellps=clrk66"/>
446                </projection>
447            </layer>
448        </map>
449    </session>
450    '''
451    
452        def test(self):
453            """Test loading layers with projections"""
454          eq = self.assertEquals          eq = self.assertEquals
455          session = load_session(self.temp_file_name("load_singlelayer.thuban"))          neq = self.assertNotEqual
456    
457            session = load_session(self.filename())
458          self.session = session          self.session = session
459    
460          # Check the title          map = self.session.Maps()[0] # only one map in the sample
         eq(session.Title(), "single map&layer")  
461    
462          # the session has one map.          layers = map.Layers() # two layers in the sample
         maps = session.Maps()  
         eq(len(maps), 1)  
463    
464          # Check the map's attributes          # test layer with a named projection
465          map = maps[0]          proj = layers[0].GetProjection()
466          eq(map.Title(), "Test Map")          neq(proj, None)
467            eq(proj.GetName(), "hello")
468            eq(proj.GetParameter("proj"), "tmerc")
469            eq(proj.GetParameter("zone"), "13")
470            eq(proj.GetParameter("ellps"), "clrk66")
471    
472          # the map has a single layer          # test layer with an unnamed projection
473          layers = map.Layers()          proj = layers[1].GetProjection()
474          eq(len(layers), 1)          neq(proj, None)
475            eq(proj.GetName(), "Unknown")
476            eq(proj.GetParameter("proj"), "lcc")
477            eq(proj.GetParameter("ellps"), "clrk66")
478    
479          # Check the layer attributes          self.check_format()
         layer = layers[0]  
         eq(layer.Title(), "My Layer")  
         self.failUnless(filenames_equal(layer.filename,  
                                         os.path.join(self.temp_dir(),  
                                                      os.pardir, os.pardir,  
                                                      "Data", "iceland",  
                                                      "political.shp")))  
         eq(layer.GetClassification().GetDefaultFill(), Color.Transparent)  
         eq(layer.GetClassification().GetDefaultLineColor().hex(), "#000000")  
480    
         self.session.Destroy()  
         self.session = None  
481    
482      def testClassification(self):  class TestRasterLayer(LoadSessionTest):
483          """Load a session with a map and classified layers."""  
484        file_contents = '''\
485    <?xml version="1.0" encoding="UTF-8"?>
486    <!DOCTYPE session SYSTEM "thuban-0.9.dtd">
487    <session xmlns="http://thuban.intevation.org/dtds/thuban-0.9.dtd"
488            title="single map&amp;layer">
489        <map title="Test Map">
490            <rasterlayer visible="false" filename="../../Data/iceland/island.tif"
491                    title="My RasterLayer"/>
492        </map>
493    </session>
494    '''
495    
496        def test(self):
497            eq = self.assertEquals
498            neq = self.assertNotEqual
499    
500          session = load_session(self.temp_file_name("load_classified_v0_2.thuban"))          session = load_session(self.filename())
501          self.session = session          self.session = session
502    
503          map = self.session.Maps()[0] # only one map in the sample          map = self.session.Maps()[0] # only one map in the sample
504    
505          expected = [("My Layer", 2,          layer = map.Layers()[0] # one layer in the sample
                         [("default", (), "",  
                             ("#000000", 1, "None")),  
                          ("single", "1", "",  
                             ("#000000", 2, "None")),  
                          ("single", "1", "",  
                             ("#000000", 10, "None"))]),  
                      ("My Layer 2", 4,  
                          [("default", (), "",  
                             ("#000000", 2, "None")),  
                           ("range", (0, 1), "",  
                             ("#000000", 1, "None")),  
                           ("single", .5, "",  
                             ("#000000", 1, "None")),  
                           ("range", (-1, 0), "",  
                             ("#000000", 1, "None")),  
                           ("single", -.5, "",  
                             ("#000000", 1, "None"))])]  
506    
507          self.TestLayers(map.Layers(), expected)          eq(layer.Title(), "My RasterLayer")
508            self.failIf(layer.Visible())
509            self.failUnless(filenames_equal(layer.GetImageFilename(),
510                                            os.path.join(self.temp_dir(),
511                                                         os.pardir, os.pardir,
512                                                         "Data", "iceland",
513                                                         "island.tif")))
514            self.check_format()
515    
     def TestLayers(self, layers, expected):  
516    
517          TITLE = 0  class TestJoinedTable(LoadSessionTest):
         NUM_GROUPS = 1  
         CLASSES = 2  
         GROUP_TYPE = 0  
         GROUP_DATA = 1  
         GROUP_LABEL = 2  
         GROUP_PROPS = 3  
518    
519          eq = self.assertEquals      file_contents = '''<?xml version="1.0" encoding="UTF-8"?>
520    <!DOCTYPE session SYSTEM "thuban-0.9.dtd">
521    <session xmlns="http://thuban.intevation.org/dtds/thuban-0.9.dtd" title="A Joined Table session">
522        <fileshapesource filetype="shapefile" id="D137227612"
523            filename="../../Data/iceland/roads-line.shp"/>
524        <filetable filetype="DBF" filename="load_joinedtable.dbf" id="D136171140"
525            title="Some Title"/>
526        <jointable id="D136169900" title="Joined"
527            right="D136171140" left="D137227612"
528            leftcolumn="RDLNTYPE" rightcolumn="RDTYPE"
529            jointype="LEFT OUTER"/>
530        <derivedshapesource table="D136169900" shapesource="D137227612"
531            id="D136170932"/>
532        <map title="Test Map">
533            <layer shapestore="D136170932" visible="true" stroke="#000000"
534                    title="My Layer" stroke_width="1" fill="None"/>
535        </map>
536    </session>
537    '''
538    
539          eq(len(layers), len(expected))      def setUp(self):
540            """Extend inherited method to create the dbffile for the join"""
541            LoadSessionTest.setUp(self)
542            dbffile = self.temp_file_name("load_joinedtable.dbf")
543            dbf = dbflib.create(dbffile)
544            dbf.add_field("RDTYPE", dbflib.FTInteger, 10, 0)
545            dbf.add_field("TEXT", dbflib.FTString, 10, 0)
546            dbf.write_record(0, {'RDTYPE': 8, "TEXT": "foo"})
547            dbf.write_record(1, {'RDTYPE': 2, "TEXT": "bar"})
548            dbf.write_record(2, {'RDTYPE': 3, "TEXT": "baz"})
549            dbf.close()
550    
551        def test(self):
552            """Test loading a session containing a joined table"""
553            session = load_session(self.filename())
554            self.session = session
555    
556          for layer, data in zip(layers, expected):          tables = session.Tables()
557              eq(layer.Title(), data[TITLE])          self.assertEquals(len(tables), 3)
558            # FIXME: The tests shouldn't assume a certain order of the tables
559            self.assertEquals(tables[0].Title(), "Some Title")
560            self.assertEquals(tables[1].Title(), "Joined")
561            self.assertEquals(tables[1].JoinType(), "LEFT OUTER")
562            self.check_format()
563    
             clazz = layer.GetClassification()  
             eq(clazz.GetNumGroups(), data[NUM_GROUPS])  
             eq(clazz.GetNumGroups() + 1, len(data[CLASSES]))  
564    
             i = 0  
             for group in clazz:  
                   
                 props = ClassGroupProperties()  
                 props.SetLineColor(  
                     parse_color(data[CLASSES][i][GROUP_PROPS][0]))  
                 props.SetLineWidth(data[CLASSES][i][GROUP_PROPS][1])  
                 props.SetFill(  
                     parse_color(data[CLASSES][i][GROUP_PROPS][2]))  
565    
566                  if data[CLASSES][i][GROUP_TYPE] == "default":  class TestPostGISLayer(LoadSessionTest):
567                      g = ClassGroupDefault(props, data[CLASSES][i][GROUP_LABEL])  
568                  elif data[CLASSES][i][GROUP_TYPE] == "range":      file_contents = '''<?xml version="1.0" encoding="UTF-8"?>
569                      g = ClassGroupRange(data[CLASSES][i][GROUP_DATA][0],  <!DOCTYPE session SYSTEM "thuban-0.9.dtd">
570                                          data[CLASSES][i][GROUP_DATA][1],  <session xmlns="http://thuban.intevation.org/dtds/thuban-0.9.dtd"
571                                          props, data[CLASSES][i][GROUP_LABEL])          title="unnamed session">
572                  elif data[CLASSES][i][GROUP_TYPE] == "single":      <dbconnection port="%(port)s" host="%(host)s" user="%(user)s"
573                      g = ClassGroupSingleton(data[CLASSES][i][GROUP_DATA],          dbtype="postgis" id="D142684948" dbname="%(dbname)s"/>
574                                            props, data[CLASSES][i][GROUP_LABEL])      <dbshapesource tablename="landmarks" id="D143149420" dbconn="D142684948"/>
575        <map title="unnamed map">
576            <layer shapestore="D143149420" visible="true" stroke="#000000"
577                    title="landmarks" stroke_width="1" fill="None"/>
578        </map>
579    </session>
580    '''
581    
582                  eq(group, g)      def setUp(self):
583            """Extend the inherited method to start the postgis server
584    
585                  i += 1          Furthermore, patch the file contents with the real postgis db
586            information
587            """
588            postgissupport.skip_if_no_postgis()
589            self.server = postgissupport.get_test_server()
590            self.postgisdb = self.server.get_default_static_data_db()
591    
592            self.file_contents = self.__class__.file_contents % {
593                "dbname": self.postgisdb.dbname,
594                "user": self.server.user_name,
595                "port": self.server.port,
596                "host": self.server.host}
597            LoadSessionTest.setUp(self)
598    
599        def test(self):
600            """Test loading a session containing a postgis shapestore"""
601            session = load_session(self.filename())
602            self.session = session
603            connections = session.DBConnections()
604            self.assertEquals(len(connections), 1)
605            conn = connections[0]
606            for attr, value in [("host", self.server.host),
607                                ("port", str(self.server.port)),
608                                ("user", self.server.user_name),
609                                ("dbname", self.postgisdb.dbname)]:
610                self.assertEquals(getattr(conn, attr), value)
611            layer = session.Maps()[0].Layers()[0]
612            self.failUnless(layer.ShapeStore().DBConnection() is conn)
613    
614    
615    class TestPostGISLayerPassword(LoadSessionTest):
616    
617        file_contents = '''<?xml version="1.0" encoding="UTF-8"?>
618    <!DOCTYPE session SYSTEM "thuban-0.9.dtd">
619    <session xmlns="http://thuban.intevation.org/dtds/thuban-0.9.dtd"
620            title="unnamed session">
621        <dbconnection port="%(port)s" host="%(host)s" user="%(user)s"
622            dbtype="postgis" id="D142684948" dbname="%(dbname)s"/>
623        <dbshapesource tablename="landmarks" id="D143149420" dbconn="D142684948"/>
624        <map title="unnamed map">
625            <layer shapestore="D143149420" visible="true" stroke="#000000"
626                    title="landmarks" stroke_width="1" fill="None"/>
627        </map>
628    </session>
629    '''
630    
631      def testLabels(self):      def setUp(self):
632          """Load a session and test for reading the group labels."""          """Extend the inherited method to start the postgis server
633    
634          eq = self.assertEquals          Furthermore, patch the file contents with the real postgis db
635          session = load_session(self.temp_file_name("load_labels.thuban"))          information
636          self.session = session          """
637            postgissupport.skip_if_no_postgis()
638            self.server = postgissupport.get_test_server()
639            self.postgisdb = self.server.get_default_static_data_db()
640    
641            self.file_contents = self.__class__.file_contents % {
642                "dbname": self.postgisdb.dbname,
643                "user": self.server.user_name,
644                "port": self.server.port,
645                "host": self.server.host}
646            LoadSessionTest.setUp(self)
647    
648          map = self.session.Maps()[0] # only one map in the sample          self.db_connection_callback_called = False
649            self.server.require_authentication(True)
650    
651          expected = [("My Layer", 1,      def tearDown(self):
652                          [("default", (), "hallo",          """Extend the inherited method to switch off postgresql authentication
653                              ("#000000", 1, "None")),          """
654                           ("single", "1", "welt",          self.server.require_authentication(False)
655                              ("#000000", 2, "None"))])]          LoadSessionTest.tearDown(self)
656    
657        def db_connection_callback(self, params, message):
658            """Implementation of Thuban.Model.hooks.query_db_connection_parameters
659            """
660            self.assertEquals(params,
661                              {"dbname": self.postgisdb.dbname,
662                               "user": self.server.user_name,
663                               "port": str(self.server.port),
664                               "host": self.server.host})
665            self.db_connection_callback_called = True
666            params = params.copy()
667            params["password"] = self.server.user_password
668            return params
669    
670        def test_with_callback(self):
671            """Test loading a session with postgis, authentication and a callback
672            """
673            session = load_session(self.filename(),
674                          db_connection_callback = self.db_connection_callback)
675            self.session = session
676            connections = session.DBConnections()
677            self.assertEquals(len(connections), 1)
678            conn = connections[0]
679            for attr, value in [("host", self.server.host),
680                                ("port", str(self.server.port)),
681                                ("user", self.server.user_name),
682                                ("dbname", self.postgisdb.dbname)]:
683                self.assertEquals(getattr(conn, attr), value)
684            layer = session.Maps()[0].Layers()[0]
685            self.failUnless(layer.ShapeStore().DBConnection() is conn)
686            self.failUnless(self.db_connection_callback_called)
687    
688        def test_without_callback(self):
689            """Test loading a session with postgis, authentication and no callback
690            """
691            # A password is required and there's no callback, so we should
692            # get a ConnectionError
693            self.assertRaises(ConnectionError, load_session, self.filename())
694    
695        def test_cancel(self):
696            """Test loading a session with postgis and cancelling authentication
697            """
698            def cancel(*args):
699                self.db_connection_callback_called = True
700                return None
701    
702            # If the user cancels, i.e. if the callbakc returns None, a
703            # LoadCancelled exception is raised.
704            self.assertRaises(LoadCancelled,
705                              load_session, self.filename(), cancel)
706            self.failUnless(self.db_connection_callback_called)
707    
         self.TestLayers(map.Layers(), expected)  
708    
709  if __name__ == "__main__":  class TestLoadError(LoadSessionTest):
     unittest.main()  
710    
711        file_contents = '''\
712    <?xml version="1.0" encoding="UTF-8"?>
713    <!DOCTYPE session SYSTEM "thuban-0.9.dtd">
714    <session xmlns="http://thuban.intevation.org/dtds/thuban-0.9.dtd"
715            title="single map&amp;layer">
716        <fileshapesource id="D1" filename="../../Data/iceland/political.shp"/>
717        <map title="Test Map">
718            <projection name="Unknown">
719                <parameter value="zone=26"/>
720                <parameter value="proj=utm"/>
721                <parameter value="ellps=clrk66"/>
722            </projection>
723            <layer shapestore="D1" visible="true"
724                    stroke="#000000" title="My Layer" stroke_width="1"
725                    fill="None"/>
726        </map>
727    </session>
728    '''
729    
730        def test(self):
731            """Test loading a session missing a required attribute"""
732            # Don't use assertRaises to make sure that if a session is
733            # actually returned it gets destroyed properly.
734            try:
735                self.session = load_session(self.filename())
736            except LoadError, value:
737                # Check the actual messge in value to make sure the
738                # LoadError really was about the missing attribute
739                self.assertEquals(str(value),
740                  "Element "
741                  "(u'http://thuban.intevation.org/dtds/thuban-0.9.dtd',"
742                  " u'fileshapesource') requires an attribute 'filetype'")
743            else:
744                self.fail("Missing filetype attribute doesn't raise LoadError")
745    
746    if __name__ == "__main__":
747        support.run_tests()

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26