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