/[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 1848 - (hide annotations)
Tue Oct 21 11:06:56 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: 26693 byte(s)
* test/test_load.py (TestSingleLayer.file_contents)
(TestSingleLayer.test): Add non-ascii characters to the titles of
session, map and layer. This is effectively a port of the
TestUnicodeStrings test in test_load_0_8.py which for some reason
was only added there.

* test/test_load_0_9.py (TestSingleLayer.file_contents)
(TestSingleLayer.test): Same as in test_load.py: add non-ascii
characters to the titles of session, map and layer,.

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