/[thuban]/branches/WIP-pyshapelib-bramz/test/test_load_0_9.py
ViewVC logotype

Annotation of /branches/WIP-pyshapelib-bramz/test/test_load_0_9.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1842 - (hide annotations)
Tue Oct 21 10:49:20 2003 UTC (21 years, 4 months ago) by bh
Original Path: trunk/thuban/test/test_load_0_9.py
File MIME type: text/x-python
File size: 26519 byte(s)
New. Effectively a copy of test_load.py
as of Thuban 0.9. These are now tests to determine whether Thuban
can still read files generated by Thuban 0.9

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

Properties

Name Value
svn:eol-style native
svn:keywords Author Date Id Revision

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26