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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1683 - (hide annotations)
Thu Aug 28 15:20:57 2003 UTC (21 years, 6 months ago) by bh
Original Path: trunk/thuban/test/test_load.py
File MIME type: text/x-python
File size: 27465 byte(s)
* test/test_load.py (LoadSessionTest.filenames): New class
variable with the filename attributes to normalize
(LoadSessionTest.check_format): Pass self.filenames to
sax_eventlist to normalize the filename attributes

* test/xmlsupport.py: Add cvs keywords
(SaxEventLister.__init__): New parameter filenames which indicates
attributes that contain filenames
(SaxEventLister.startElementNS): Normalize the filename attributes
with os.path.normpath
(sax_eventlist): New parameter filenames to pass through to
SaxEventLister

1 bh 765 # Copyright (c) 2002, 2003 by Intevation GmbH
2 bh 292 # 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 bh 1247
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 bh 1257 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 bh 292 """
26    
27     __version__ = "$Revision$"
28     # $Source$
29     # $Id$
30    
31     import os
32     import unittest
33    
34     import support
35     support.initthuban()
36    
37 bh 1646 import postgissupport
38 bh 1257 from xmlsupport import sax_eventlist
39 bh 1268
40     import dbflib
41    
42 bh 1257 from Thuban.Model.save import save_session
43 bh 1646 from Thuban.Model.load import load_session, parse_color, LoadError, \
44     LoadCancelled
45 jonathan 1350 from Thuban.Model.color import Transparent
46 jonathan 684 from Thuban.Model.classification import ClassGroupProperties, ClassGroupRange,\
47     ClassGroupSingleton, ClassGroupDefault
48 bh 1646 from Thuban.Model.postgisdb import ConnectionError
49 jonathan 684
50 bh 292 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 bh 957
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 bh 1268
85 bh 1664 dtd = "http://thuban.intevation.org/dtds/thuban-0.9.dtd"
86 bh 1268 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 bh 1683 filenames = [((dtd, n), (None, m)) for n, m in
96     [("fileshapesource", "filename"),
97     ("rasterlayer", "filename"),
98     ("filetable", "filename")]]
99 bh 1268 del n, m, dtd
100    
101 bh 1257 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 bh 1268 el1 = sax_eventlist(filename = filename, ids = self.thubanids,
109 bh 1683 idrefs = self.thubanidrefs,
110     filenames = self.filenames)
111 bh 1268 el2 = sax_eventlist(filename = self.filename(), ids = self.thubanids,
112 bh 1683 idrefs = self.thubanidrefs,
113     filenames = self.filenames)
114 bh 1268 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 bh 957
122 bh 1257
123 bh 957 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 jonathan 1357 g = ClassGroupRange((data[CLASSES][i][GROUP_DATA][0],
162     data[CLASSES][i][GROUP_DATA][1]),
163 bh 957 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 bh 292 <?xml version="1.0" encoding="UTF-8"?>
178 bh 1375 <!DOCTYPE session SYSTEM "thuban-0.9.dtd">
179 bh 1664 <session xmlns="http://thuban.intevation.org/dtds/thuban-0.9.dtd"
180 bh 1268 title="single map&amp;layer">
181     <fileshapesource filetype="shapefile" id="D1"
182     filename="../../Data/iceland/political.shp"/>
183     <map title="Test Map">
184     <projection name="Unknown">
185     <parameter value="zone=26"/>
186     <parameter value="proj=utm"/>
187     <parameter value="ellps=clrk66"/>
188     </projection>
189     <layer shapestore="D1" visible="true"
190     stroke="#000000" title="My Layer" stroke_width="1"
191     fill="None"/>
192     </map>
193 bh 292 </session>
194     '''
195    
196 bh 957 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 bh 1219 self.failUnless(filenames_equal(layer.ShapeStore().FileName(),
221 bh 957 os.path.join(self.temp_dir(),
222     os.pardir, os.pardir,
223     "Data", "iceland",
224     "political.shp")))
225 jonathan 1347 eq(layer.GetClassification().GetDefaultFill(), Transparent)
226 bh 957 eq(layer.GetClassification().GetDefaultLineColor().hex(), "#000000")
227     eq(layer.Visible(), True)
228    
229 bh 1257 self.check_format()
230    
231 bh 957 self.session.Destroy()
232     self.session = None
233    
234    
235     class TestLayerVisibility(LoadSessionTest):
236    
237     file_contents = '''\
238 jonathan 690 <?xml version="1.0" encoding="UTF-8"?>
239 bh 1375 <!DOCTYPE session SYSTEM "thuban-0.9.dtd">
240 bh 1664 <session xmlns="http://thuban.intevation.org/dtds/thuban-0.9.dtd"
241 bh 1268 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 bh 957 </map>
253     </session>
254     '''
255    
256     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 bh 1257 self.check_format()
271 bh 957
272    
273     class TestClassification(ClassificationTest):
274    
275     file_contents = '''\
276     <?xml version="1.0" encoding="UTF-8"?>
277     <!DOCTYPE session SYSTEM "thuban.dtd">
278     <session title="single map&amp;layer">
279     <map title="Test Map">
280     <projection>
281     <parameter value="zone=26"/>
282     <parameter value="proj=utm"/>
283     <parameter value="ellps=clrk66"/>
284     </projection>
285     <layer title="My Layer" stroke_width="1" fill="None"
286     filename="../../Data/iceland/political.shp"
287 jonathan 690 stroke="#000000">
288     <classification field="POPYREG" field_type="string">
289     <clnull>
290     <cldata stroke="#000000" stroke_width="1" fill="None"/>
291     </clnull>
292     <clpoint value="1">
293     <cldata stroke="#000000" stroke_width="2" fill="None"/>
294     </clpoint>
295     <clpoint value="1">
296     <cldata stroke="#000000" stroke_width="10" fill="None"/>
297     </clpoint>
298 bh 1417 <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 jonathan 690 </classification>
303     </layer>
304     <layer title="My Layer 2" stroke_width="1" fill="None"
305     filename="../../Data/iceland/political.shp"
306     stroke="#000000">
307     <classification field="AREA" field_type="double">
308     <clnull>
309     <cldata stroke="#000000" stroke_width="2" fill="None"/>
310     </clnull>
311     <clrange min="0" max="1">
312 jonathan 692 <cldata stroke="#111111" stroke_width="1" fill="None"/>
313 jonathan 690 </clrange>
314     <clpoint value=".5">
315 jonathan 692 <cldata stroke="#000000" stroke_width="1" fill="#111111"/>
316 jonathan 690 </clpoint>
317     <clrange min="-1" max="0">
318     <cldata stroke="#000000" stroke_width="1" fill="None"/>
319     </clrange>
320     <clpoint value="-.5">
321     <cldata stroke="#000000" stroke_width="1" fill="None"/>
322     </clpoint>
323     </classification>
324     </layer>
325     </map>
326     </session>
327     '''
328 bh 292
329 bh 957 def test(self):
330 bh 1247 """Load a Thuban session with a map and classified layers."""
331 bh 957 session = load_session(self.filename())
332     self.session = session
333    
334     map = self.session.Maps()[0] # only one map in the sample
335    
336 bh 1417 expected = [("My Layer", 3,
337 bh 957 [("default", (), "",
338     ("#000000", 1, "None")),
339     ("single", "1", "",
340     ("#000000", 2, "None")),
341     ("single", "1", "",
342 bh 1417 ("#000000", 10, "None")),
343     ("single", "\xe4\xf6\xfc", "\xdcml\xe4uts",
344     ("#000000", 1, "None"))]),
345 bh 957 ("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 jonathan 690 <?xml version="1.0" encoding="UTF-8"?>
364 bh 1375 <!DOCTYPE session SYSTEM "thuban-0.9.dtd">
365 bh 1664 <session xmlns="http://thuban.intevation.org/dtds/thuban-0.9.dtd"
366 bh 1268 title="single map&amp;layer">
367     <fileshapesource filetype="shapefile" id="D1"
368     filename="../../Data/iceland/political.shp"/>
369     <map title="Test Map">
370     <projection name="Unknown">
371     <parameter value="zone=26"/>
372     <parameter value="proj=utm"/>
373     <parameter value="ellps=clrk66"/>
374     </projection>
375     <layer shapestore="D1" visible="true" stroke="#000000"
376     title="My Layer" stroke_width="1" fill="None">
377 jonathan 690 <classification field="POPYREG" field_type="string">
378     <clnull label="hallo">
379     <cldata stroke="#000000" stroke_width="1" fill="None"/>
380     </clnull>
381     <clpoint label="welt" value="1">
382     <cldata stroke="#000000" stroke_width="2" fill="None"/>
383     </clpoint>
384     </classification>
385     </layer>
386     </map>
387     </session>
388     '''
389    
390 bh 957 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     map = self.session.Maps()[0] # only one map in the sample
397    
398     expected = [("My Layer", 1,
399     [("default", (), "hallo",
400     ("#000000", 1, "None")),
401     ("single", "1", "welt",
402     ("#000000", 2, "None"))])]
403    
404     self.TestLayers(map.Layers(), expected)
405 bh 1257 self.check_format()
406 bh 957
407    
408     class TestLayerProjection(LoadSessionTest):
409    
410     file_contents = '''\
411 jonathan 746 <?xml version="1.0" encoding="UTF-8"?>
412 bh 1375 <!DOCTYPE session SYSTEM "thuban-0.9.dtd">
413 bh 1664 <session xmlns="http://thuban.intevation.org/dtds/thuban-0.9.dtd"
414 bh 1268 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 jonathan 746 <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 bh 1268 <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 jonathan 746 </layer>
448     </map>
449     </session>
450     '''
451    
452 bh 957 def test(self):
453     """Test loading layers with projections"""
454 bh 292 eq = self.assertEquals
455 jonathan 746 neq = self.assertNotEqual
456    
457 bh 957 session = load_session(self.filename())
458 jonathan 746 self.session = session
459    
460     map = self.session.Maps()[0] # only one map in the sample
461    
462     layers = map.Layers() # two layers in the sample
463    
464     # test layer with a named projection
465     proj = layers[0].GetProjection()
466     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     # test layer with an unnamed projection
473     proj = layers[1].GetProjection()
474     neq(proj, None)
475     eq(proj.GetName(), "Unknown")
476     eq(proj.GetParameter("proj"), "lcc")
477     eq(proj.GetParameter("ellps"), "clrk66")
478    
479 bh 1257 self.check_format()
480 bh 957
481 bh 1257
482 bh 957 class TestRasterLayer(LoadSessionTest):
483    
484     file_contents = '''\
485     <?xml version="1.0" encoding="UTF-8"?>
486 bh 1375 <!DOCTYPE session SYSTEM "thuban-0.9.dtd">
487 bh 1664 <session xmlns="http://thuban.intevation.org/dtds/thuban-0.9.dtd"
488 bh 1268 title="single map&amp;layer">
489     <map title="Test Map">
490     <rasterlayer visible="false" filename="../../Data/iceland/island.tif"
491     title="My RasterLayer"/>
492 bh 957 </map>
493     </session>
494     '''
495    
496     def test(self):
497 jonathan 947 eq = self.assertEquals
498     neq = self.assertNotEqual
499    
500 bh 957 session = load_session(self.filename())
501 jonathan 947 self.session = session
502    
503     map = self.session.Maps()[0] # only one map in the sample
504    
505     layer = map.Layers()[0] # one layer in the sample
506    
507     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 bh 1257 self.check_format()
515 jonathan 947
516 bh 1268
517     class TestJoinedTable(LoadSessionTest):
518    
519     file_contents = '''<?xml version="1.0" encoding="UTF-8"?>
520 bh 1375 <!DOCTYPE session SYSTEM "thuban-0.9.dtd">
521 bh 1664 <session xmlns="http://thuban.intevation.org/dtds/thuban-0.9.dtd" title="A Joined Table session">
522 bh 1282 <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 bh 1375 <jointable id="D136169900" title="Joined"
527     right="D136171140" left="D137227612"
528     leftcolumn="RDLNTYPE" rightcolumn="RDTYPE"
529     jointype="LEFT OUTER"/>
530 bh 1282 <derivedshapesource table="D136169900" shapesource="D137227612"
531     id="D136170932"/>
532 bh 1268 <map title="Test Map">
533 bh 1282 <layer shapestore="D136170932" visible="true" stroke="#000000"
534     title="My Layer" stroke_width="1" fill="None"/>
535 bh 1268 </map>
536 bh 1282 </session>
537     '''
538 bh 1268
539     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     tables = session.Tables()
557     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 bh 1375 self.assertEquals(tables[1].JoinType(), "LEFT OUTER")
562 bh 1282 self.check_format()
563 bh 1268
564    
565 bh 1646
566     class TestPostGISLayer(LoadSessionTest):
567    
568     file_contents = '''<?xml version="1.0" encoding="UTF-8"?>
569     <!DOCTYPE session SYSTEM "thuban-0.9.dtd">
570 bh 1664 <session xmlns="http://thuban.intevation.org/dtds/thuban-0.9.dtd"
571 bh 1646 title="unnamed session">
572     <dbconnection port="%(port)s" host="%(host)s" user="%(user)s"
573     dbtype="postgis" id="D142684948" dbname="%(dbname)s"/>
574     <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     def setUp(self):
583     """Extend the inherited method to start the postgis server
584    
585     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 bh 1664 <session xmlns="http://thuban.intevation.org/dtds/thuban-0.9.dtd"
620 bh 1646 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 setUp(self):
632     """Extend the inherited method to start the postgis server
633    
634     Furthermore, patch the file contents with the real postgis db
635     information
636     """
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     self.db_connection_callback_called = False
649     self.server.require_authentication(True)
650    
651     def tearDown(self):
652     """Extend the inherited method to switch off postgresql authentication
653     """
654     self.server.require_authentication(False)
655     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    
708    
709 bh 1268 class TestLoadError(LoadSessionTest):
710    
711     file_contents = '''\
712     <?xml version="1.0" encoding="UTF-8"?>
713 bh 1375 <!DOCTYPE session SYSTEM "thuban-0.9.dtd">
714 bh 1664 <session xmlns="http://thuban.intevation.org/dtds/thuban-0.9.dtd"
715 bh 1268 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 bh 1642 # 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 bh 1664 "(u'http://thuban.intevation.org/dtds/thuban-0.9.dtd',"
742 bh 1642 " u'fileshapesource') requires an attribute 'filetype'")
743 bh 1268 else:
744     self.fail("Missing filetype attribute doesn't raise LoadError")
745    
746 bh 292 if __name__ == "__main__":
747 bh 1683 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