1 |
# Copyright (c) 2002, 2003 by Intevation GmbH |
# Copyright (c) 2002, 2003, 2004, 2005 by Intevation GmbH |
2 |
# Authors: |
# Authors: |
3 |
# Bernhard Herzog <[email protected]> |
# Bernhard Herzog <[email protected]> |
4 |
# |
# |
16 |
Maintenance of the test cases: |
Maintenance of the test cases: |
17 |
|
|
18 |
When during a development period the file format is changed with respect |
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 |
to the last released version for the first time, the tests here should |
20 |
copied to the version specific test file. The round-trip tests which |
be 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 |
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 |
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 |
here are actually ones that may have been written by the current thuban |
40 |
import dbflib |
import dbflib |
41 |
import shapelib |
import shapelib |
42 |
|
|
43 |
|
from Thuban import internal_from_unicode |
44 |
from Thuban.Model.save import save_session |
from Thuban.Model.save import save_session |
45 |
from Thuban.Model.load import load_session, parse_color, LoadError, \ |
from Thuban.Model.load import load_session, parse_color, LoadError, \ |
46 |
LoadCancelled |
LoadCancelled |
51 |
from Thuban.Model.table import DBFTable, MemoryTable, \ |
from Thuban.Model.table import DBFTable, MemoryTable, \ |
52 |
FIELDTYPE_DOUBLE, FIELDTYPE_INT, FIELDTYPE_STRING, \ |
FIELDTYPE_DOUBLE, FIELDTYPE_INT, FIELDTYPE_STRING, \ |
53 |
table_to_dbf |
table_to_dbf |
54 |
|
from Thuban.Model.label import ALIGN_CENTER, ALIGN_TOP, ALIGN_BOTTOM, \ |
55 |
|
ALIGN_LEFT, ALIGN_RIGHT, ALIGN_BASELINE |
56 |
|
|
57 |
def filenames_equal(name1, name2): |
def filenames_equal(name1, name2): |
58 |
"""Return true if the filenames name1 and name2 are equal. |
"""Return true if the filenames name1 and name2 are equal. |
89 |
self.session = None |
self.session = None |
90 |
|
|
91 |
|
|
92 |
dtd = "http://thuban.intevation.org/dtds/thuban-1.0rc1.dtd" |
dtd = "http://thuban.intevation.org/dtds/thuban-1.1-dev.dtd" |
93 |
thubanids = [((dtd, n), (None, "id")) for n in |
thubanids = [((dtd, n), (None, "id")) for n in |
94 |
["fileshapesource", "filetable", "jointable", |
["fileshapesource", "filetable", "jointable", |
95 |
"derivedshapesource"]] |
"derivedshapesource"]] |
124 |
print a != b and "***************" or "" |
print a != b and "***************" or "" |
125 |
print a |
print a |
126 |
print b |
print b |
127 |
|
|
128 |
self.assertEquals(el1, el2, |
self.assertEquals(el1, el2, |
129 |
"loaded file not equivalent to the saved file") |
"loaded file not equivalent to the saved file") |
130 |
|
|
163 |
props.SetLineWidth(data[CLASSES][i][GROUP_PROPS][1]) |
props.SetLineWidth(data[CLASSES][i][GROUP_PROPS][1]) |
164 |
props.SetFill( |
props.SetFill( |
165 |
parse_color(data[CLASSES][i][GROUP_PROPS][2])) |
parse_color(data[CLASSES][i][GROUP_PROPS][2])) |
166 |
|
if len(data[CLASSES][i][GROUP_PROPS]) > 3: |
167 |
|
props.SetSize(data[CLASSES][i][GROUP_PROPS][3]) |
168 |
|
|
169 |
if data[CLASSES][i][GROUP_TYPE] == "default": |
if data[CLASSES][i][GROUP_TYPE] == "default": |
170 |
g = ClassGroupDefault(props, data[CLASSES][i][GROUP_LABEL]) |
g = ClassGroupDefault(props, data[CLASSES][i][GROUP_LABEL]) |
188 |
# want to test whether the loading code handles that correctly. |
# want to test whether the loading code handles that correctly. |
189 |
file_contents = '''\ |
file_contents = '''\ |
190 |
<?xml version="1.0" encoding="UTF-8"?> |
<?xml version="1.0" encoding="UTF-8"?> |
191 |
<!DOCTYPE session SYSTEM "thuban-1.0.dtd"> |
<!DOCTYPE session SYSTEM "thuban-1.1.dtd"> |
192 |
<session xmlns="http://thuban.intevation.org/dtds/thuban-1.0rc1.dtd" |
<session xmlns="http://thuban.intevation.org/dtds/thuban-1.1-dev.dtd" |
193 |
title="Stra\xc3\x9fen & Landmarken"> |
title="Stra\xc3\x9fen & Landmarken"> |
194 |
<fileshapesource filetype="shapefile" id="D1" |
<fileshapesource filetype="shapefile" id="D1" |
195 |
filename="../../Data/iceland/political.shp"/> |
filename="../../Data/iceland/political.shp"/> |
215 |
self.session = session |
self.session = session |
216 |
|
|
217 |
# Check the title |
# Check the title |
218 |
eq(session.Title(), "Stra\xdfen & Landmarken") |
eq(session.Title(), internal_from_unicode(u"Stra\xdfen & Landmarken")) |
219 |
|
|
220 |
# the session has one map. |
# the session has one map. |
221 |
maps = session.Maps() |
maps = session.Maps() |
223 |
|
|
224 |
# Check the map's attributes |
# Check the map's attributes |
225 |
map = maps[0] |
map = maps[0] |
226 |
eq(map.Title(), "\xdcbersicht") |
eq(map.Title(), internal_from_unicode(u"\xdcbersicht")) |
227 |
proj = map.GetProjection() |
proj = map.GetProjection() |
228 |
eq(proj.GetName(), "WGS 84 / UTM zone 27N") |
eq(proj.GetName(), "WGS 84 / UTM zone 27N") |
229 |
eq(proj.EPSGCode(), "32627") |
eq(proj.EPSGCode(), "32627") |
238 |
|
|
239 |
# Check the layer attributes |
# Check the layer attributes |
240 |
layer = layers[0] |
layer = layers[0] |
241 |
eq(layer.Title(), "K\xfcste") |
eq(layer.Title(), internal_from_unicode(u"K\xfcste")) |
242 |
self.failUnless(filenames_equal(layer.ShapeStore().FileName(), |
self.failUnless(filenames_equal(layer.ShapeStore().FileName(), |
243 |
os.path.join(self.temp_dir(), |
os.path.join(self.temp_dir(), |
244 |
os.pardir, os.pardir, |
os.pardir, os.pardir, |
280 |
|
|
281 |
file_contents = '''\ |
file_contents = '''\ |
282 |
<?xml version="1.0" encoding="UTF-8"?> |
<?xml version="1.0" encoding="UTF-8"?> |
283 |
<!DOCTYPE session SYSTEM "thuban-1.0.dtd"> |
<!DOCTYPE session SYSTEM "thuban-1.1.dtd"> |
284 |
<session xmlns="http://thuban.intevation.org/dtds/thuban-1.0rc1.dtd" |
<session xmlns="http://thuban.intevation.org/dtds/thuban-1.1-dev.dtd" |
285 |
title="Non ASCII column name test"> |
title="Non ASCII column name test"> |
286 |
<fileshapesource filetype="shapefile" id="D1" |
<fileshapesource filetype="shapefile" id="D1" |
287 |
filename="TestNonAsciiColumnName.shp"/> |
filename="TestNonAsciiColumnName.shp"/> |
357 |
|
|
358 |
file_contents = '''\ |
file_contents = '''\ |
359 |
<?xml version="1.0" encoding="UTF-8"?> |
<?xml version="1.0" encoding="UTF-8"?> |
360 |
<!DOCTYPE session SYSTEM "thuban-1.0.dtd"> |
<!DOCTYPE session SYSTEM "thuban-1.1.dtd"> |
361 |
<session xmlns="http://thuban.intevation.org/dtds/thuban-1.0rc1.dtd" |
<session xmlns="http://thuban.intevation.org/dtds/thuban-1.1-dev.dtd" |
362 |
title="single map&layer"> |
title="single map&layer"> |
363 |
<fileshapesource filetype="shapefile" id="D1" |
<fileshapesource filetype="shapefile" id="D1" |
364 |
filename="../../Data/iceland/political.shp"/> |
filename="../../Data/iceland/political.shp"/> |
391 |
self.check_format() |
self.check_format() |
392 |
|
|
393 |
|
|
394 |
|
class TestSymbolSize(ClassificationTest): |
395 |
|
|
396 |
|
file_contents = '''\ |
397 |
|
<?xml version="1.0" encoding="UTF-8"?> |
398 |
|
<!DOCTYPE session SYSTEM "thuban-1.1.dtd"> |
399 |
|
<session xmlns="http://thuban.intevation.org/dtds/thuban-1.1-dev.dtd" title="Thuban sample session"> |
400 |
|
<fileshapesource filetype="shapefile" id="D813968480" filename="../../Data/iceland/cultural_landmark-point.shp"/> |
401 |
|
<map title="Iceland map"> |
402 |
|
<layer title="cultural_landmark-point" stroke_width="1" shapestore="D813968480" visible="true" stroke="#000000" fill="#000000"> |
403 |
|
<classification field="CLPTLABEL" field_type="string"> |
404 |
|
<clnull label=""> |
405 |
|
<cldata stroke="#000000" stroke_width="1" size="3" fill="#000000"/> |
406 |
|
</clnull> |
407 |
|
<clpoint label="" value="RUINS"> |
408 |
|
<cldata stroke="#000000" stroke_width="1" size="6" fill="#ffffff"/> |
409 |
|
</clpoint> |
410 |
|
<clpoint label="" value="FARM"> |
411 |
|
<cldata stroke="#000000" stroke_width="1" size="9" fill="#ffff00"/> |
412 |
|
</clpoint> |
413 |
|
</classification> |
414 |
|
</layer> |
415 |
|
</map> |
416 |
|
</session> |
417 |
|
''' |
418 |
|
|
419 |
|
def test(self): |
420 |
|
"""Test that the size attribute for point symbols is correctly |
421 |
|
loaded for a layer.""" |
422 |
|
eq = self.assertEquals |
423 |
|
session = load_session(self.filename()) |
424 |
|
self.session = session |
425 |
|
|
426 |
|
map = session.Maps()[0] # only one map in the sample |
427 |
|
|
428 |
|
expected = [("cultural_landmark-point", 2, |
429 |
|
[("default", (), "", |
430 |
|
("#000000", 1, "#000000", 3)), |
431 |
|
("single", "RUINS", "", |
432 |
|
("#000000", 1, "#ffffff", 6)), |
433 |
|
("single", "FARM", "", |
434 |
|
("#000000", 1, "#ffff00", 9))])] |
435 |
|
|
436 |
|
self.TestLayers(map.Layers(), expected) |
437 |
|
|
438 |
|
self.check_format() |
439 |
|
|
440 |
|
|
441 |
class TestClassification(ClassificationTest): |
class TestClassification(ClassificationTest): |
442 |
|
|
443 |
file_contents = '''\ |
file_contents = '''\ |
444 |
<?xml version="1.0" encoding="UTF-8"?> |
<?xml version="1.0" encoding="UTF-8"?> |
445 |
<!DOCTYPE session SYSTEM "thuban-1.0.dtd"> |
<!DOCTYPE session SYSTEM "thuban-1.1.dtd"> |
446 |
<session xmlns="http://thuban.intevation.org/dtds/thuban-1.0rc1.dtd" |
<session xmlns="http://thuban.intevation.org/dtds/thuban-1.1-dev.dtd" |
447 |
title="single map&layer"> |
title="single map&layer"> |
448 |
<fileshapesource filetype="shapefile" id="D138389860" |
<fileshapesource filetype="shapefile" id="D138389860" |
449 |
filename="../../Data/iceland/political.shp"/> |
filename="../../Data/iceland/political.shp"/> |
511 |
("#000000", 2, "None")), |
("#000000", 2, "None")), |
512 |
("single", "1", "", |
("single", "1", "", |
513 |
("#000000", 10, "None")), |
("#000000", 10, "None")), |
514 |
("single", "\xe4\xf6\xfc", "\xdcml\xe4uts", |
("single", internal_from_unicode(u"\xe4\xf6\xfc"), |
515 |
|
internal_from_unicode(u"\xdcml\xe4uts"), |
516 |
("#000000", 1, "None"))]), |
("#000000", 1, "None"))]), |
517 |
("My Layer 2", 4, |
("My Layer 2", 4, |
518 |
[("default", (), "", |
[("default", (), "", |
535 |
|
|
536 |
file_contents = '''\ |
file_contents = '''\ |
537 |
<?xml version="1.0" encoding="UTF-8"?> |
<?xml version="1.0" encoding="UTF-8"?> |
538 |
<!DOCTYPE session SYSTEM "thuban-1.0.dtd"> |
<!DOCTYPE session SYSTEM "thuban-1.1.dtd"> |
539 |
<session xmlns="http://thuban.intevation.org/dtds/thuban-1.0rc1.dtd" |
<session xmlns="http://thuban.intevation.org/dtds/thuban-1.1-dev.dtd" |
540 |
title="single map&layer"> |
title="single map&layer"> |
541 |
<fileshapesource filetype="shapefile" id="D1" |
<fileshapesource filetype="shapefile" id="D1" |
542 |
filename="../../Data/iceland/political.shp"/> |
filename="../../Data/iceland/political.shp"/> |
583 |
|
|
584 |
file_contents = '''\ |
file_contents = '''\ |
585 |
<?xml version="1.0" encoding="UTF-8"?> |
<?xml version="1.0" encoding="UTF-8"?> |
586 |
<!DOCTYPE session SYSTEM "thuban-1.0.dtd"> |
<!DOCTYPE session SYSTEM "thuban-1.1.dtd"> |
587 |
<session xmlns="http://thuban.intevation.org/dtds/thuban-1.0rc1.dtd" |
<session xmlns="http://thuban.intevation.org/dtds/thuban-1.1-dev.dtd" |
588 |
title="single map&layer"> |
title="single map&layer"> |
589 |
<fileshapesource filetype="shapefile" id="D2" |
<fileshapesource filetype="shapefile" id="D2" |
590 |
filename="../../Data/iceland/roads-line.shp"/> |
filename="../../Data/iceland/roads-line.shp"/> |
661 |
|
|
662 |
file_contents = '''\ |
file_contents = '''\ |
663 |
<?xml version="1.0" encoding="UTF-8"?> |
<?xml version="1.0" encoding="UTF-8"?> |
664 |
<!DOCTYPE session SYSTEM "thuban-1.0.dtd"> |
<!DOCTYPE session SYSTEM "thuban-1.1.dtd"> |
665 |
<session xmlns="http://thuban.intevation.org/dtds/thuban-1.0rc1.dtd" |
<session xmlns="http://thuban.intevation.org/dtds/thuban-1.1-dev.dtd" |
666 |
title="single map&layer"> |
title="single map&layer"> |
667 |
<map title="Test Map"> |
<map title="Test Map"> |
668 |
<rasterlayer visible="false" filename="../../Data/iceland/island.tif" |
<rasterlayer visible="false" filename="../../Data/iceland/island.tif" |
669 |
title="My RasterLayer"/> |
title="My RasterLayer" opacity="0.4" masktype="alpha"/> |
670 |
</map> |
</map> |
671 |
</session> |
</session> |
672 |
''' |
''' |
683 |
layer = map.Layers()[0] # one layer in the sample |
layer = map.Layers()[0] # one layer in the sample |
684 |
|
|
685 |
eq(layer.Title(), "My RasterLayer") |
eq(layer.Title(), "My RasterLayer") |
686 |
|
eq(layer.Opacity(), 0.4) |
687 |
|
eq(layer.MaskType(), layer.MASK_ALPHA) |
688 |
|
|
689 |
self.failIf(layer.Visible()) |
self.failIf(layer.Visible()) |
690 |
self.failUnless(filenames_equal(layer.GetImageFilename(), |
self.failUnless(filenames_equal(layer.GetImageFilename(), |
691 |
os.path.join(self.temp_dir(), |
os.path.join(self.temp_dir(), |
698 |
class TestJoinedTable(LoadSessionTest): |
class TestJoinedTable(LoadSessionTest): |
699 |
|
|
700 |
file_contents = '''<?xml version="1.0" encoding="UTF-8"?> |
file_contents = '''<?xml version="1.0" encoding="UTF-8"?> |
701 |
<!DOCTYPE session SYSTEM "thuban-1.0.dtd"> |
<!DOCTYPE session SYSTEM "thuban-1.1.dtd"> |
702 |
<session xmlns="http://thuban.intevation.org/dtds/thuban-1.0rc1.dtd" title="A Joined Table session"> |
<session xmlns="http://thuban.intevation.org/dtds/thuban-1.1-dev.dtd" title="A Joined Table session"> |
703 |
<fileshapesource filetype="shapefile" id="D137227612" |
<fileshapesource filetype="shapefile" id="D137227612" |
704 |
filename="../../Data/iceland/roads-line.shp"/> |
filename="../../Data/iceland/roads-line.shp"/> |
705 |
<filetable filetype="DBF" filename="load_joinedtable.dbf" id="D136171140" |
<filetable filetype="DBF" filename="load_joinedtable.dbf" id="D136171140" |
743 |
self.check_format() |
self.check_format() |
744 |
|
|
745 |
|
|
746 |
|
class TestLabelLayer(LoadSessionTest): |
747 |
|
|
748 |
|
# Note that the labels deliberately contain non-ascii characters to |
749 |
|
# test whether they're supported correctly. |
750 |
|
|
751 |
|
file_contents = '''<?xml version="1.0" encoding="UTF-8"?> |
752 |
|
<!DOCTYPE session SYSTEM "thuban-1.1.dtd"> |
753 |
|
<session xmlns="http://thuban.intevation.org/dtds/thuban-1.1-dev.dtd" title="Thuban sample session"> |
754 |
|
<fileshapesource filetype="shapefile" id="D145265052" |
755 |
|
filename="../../Data/iceland/political.shp"/> |
756 |
|
<fileshapesource filetype="shapefile" id="D145412868" |
757 |
|
filename="../../Data/iceland/cultural_landmark-point.shp"/> |
758 |
|
<map title="Iceland map"> |
759 |
|
<projection name="Unknown"> |
760 |
|
<parameter value="zone=26"/> |
761 |
|
<parameter value="proj=utm"/> |
762 |
|
<parameter value="ellps=clrk66"/> |
763 |
|
</projection> |
764 |
|
<layer shapestore="D145265052" visible="true" stroke="#000000" |
765 |
|
title="political" stroke_width="1" fill="#c0c0c0"> |
766 |
|
<projection name="Geographic"> |
767 |
|
<parameter value="proj=latlong"/> |
768 |
|
<parameter value="to_meter=0.017453"/> |
769 |
|
<parameter value="ellps=clrk66"/> |
770 |
|
</projection> |
771 |
|
</layer> |
772 |
|
<layer shapestore="D145412868" visible="true" stroke="#000000" |
773 |
|
title="landmarks" stroke_width="1" fill="#ffff00"> |
774 |
|
<projection name="Geographic"> |
775 |
|
<parameter value="proj=latlong"/> |
776 |
|
<parameter value="to_meter=0.017453"/> |
777 |
|
<parameter value="ellps=clrk66"/> |
778 |
|
</projection> |
779 |
|
</layer> |
780 |
|
<labellayer> |
781 |
|
<label x="-21.5" y="64.25" text="RUINS" |
782 |
|
halign="left" valign="center"/> |
783 |
|
<label x="-15.125" y="64.75" text="H\xc3\xbctte" |
784 |
|
halign="right" valign="top"/> |
785 |
|
</labellayer> |
786 |
|
</map> |
787 |
|
</session> |
788 |
|
''' |
789 |
|
|
790 |
|
def test(self): |
791 |
|
"""Test loading a session with a label layer""" |
792 |
|
session = load_session(self.filename()) |
793 |
|
self.session = session |
794 |
|
|
795 |
|
label_layer = self.session.Maps()[0].LabelLayer() |
796 |
|
expected_labels = [(-21.5, 64.25, "RUINS", ALIGN_LEFT, ALIGN_CENTER), |
797 |
|
(-15.125, 64.75, internal_from_unicode(u"H\xfctte"), |
798 |
|
ALIGN_RIGHT, ALIGN_TOP), |
799 |
|
] |
800 |
|
for label, values in zip(label_layer.Labels(), expected_labels): |
801 |
|
self.assertEquals((label.x, label.y, label.text, label.halign, |
802 |
|
label.valign), |
803 |
|
values) |
804 |
|
self.check_format() |
805 |
|
|
806 |
|
|
807 |
class TestPostGISLayer(LoadSessionTest): |
class TestPostGISLayer(LoadSessionTest): |
808 |
|
|
809 |
file_contents = '''<?xml version="1.0" encoding="UTF-8"?> |
file_contents = '''<?xml version="1.0" encoding="UTF-8"?> |
810 |
<!DOCTYPE session SYSTEM "thuban-1.0.dtd"> |
<!DOCTYPE session SYSTEM "thuban-1.1.dtd"> |
811 |
<session xmlns="http://thuban.intevation.org/dtds/thuban-1.0rc1.dtd" |
<session xmlns="http://thuban.intevation.org/dtds/thuban-1.1-dev.dtd" |
812 |
title="unnamed session"> |
title="unnamed session"> |
813 |
<dbconnection port="%(port)s" host="%(host)s" user="%(user)s" |
<dbconnection port="%(port)s" host="%(host)s" user="%(user)s" |
814 |
dbtype="postgis" id="D142684948" dbname="%(dbname)s"/> |
dbtype="postgis" id="D142684948" dbname="%(dbname)s"/> |
815 |
<dbshapesource tablename="landmarks" id="D143149420" dbconn="D142684948"/> |
<dbshapesource id="D143149420" dbconn="D142684948" |
816 |
|
tablename="landmarks_point_id" id_column="point_id" |
817 |
|
geometry_column="the_geom" /> |
818 |
<map title="unnamed map"> |
<map title="unnamed map"> |
819 |
<layer shapestore="D143149420" visible="true" stroke="#000000" |
<layer shapestore="D143149420" visible="true" stroke="#000000" |
820 |
title="landmarks" stroke_width="1" fill="None"/> |
title="landmarks" stroke_width="1" fill="None"/> |
858 |
class TestPostGISLayerPassword(LoadSessionTest): |
class TestPostGISLayerPassword(LoadSessionTest): |
859 |
|
|
860 |
file_contents = '''<?xml version="1.0" encoding="UTF-8"?> |
file_contents = '''<?xml version="1.0" encoding="UTF-8"?> |
861 |
<!DOCTYPE session SYSTEM "thuban-1.0.dtd"> |
<!DOCTYPE session SYSTEM "thuban-1.1.dtd"> |
862 |
<session xmlns="http://thuban.intevation.org/dtds/thuban-1.0rc1.dtd" |
<session xmlns="http://thuban.intevation.org/dtds/thuban-1.1-dev.dtd" |
863 |
title="unnamed session"> |
title="unnamed session"> |
864 |
<dbconnection port="%(port)s" host="%(host)s" user="%(user)s" |
<dbconnection port="%(port)s" host="%(host)s" user="%(user)s" |
865 |
dbtype="postgis" id="D142684948" dbname="%(dbname)s"/> |
dbtype="postgis" id="D142684948" dbname="%(dbname)s"/> |
953 |
|
|
954 |
file_contents = '''\ |
file_contents = '''\ |
955 |
<?xml version="1.0" encoding="UTF-8"?> |
<?xml version="1.0" encoding="UTF-8"?> |
956 |
<!DOCTYPE session SYSTEM "thuban-1.0.dtd"> |
<!DOCTYPE session SYSTEM "thuban-1.1.dtd"> |
957 |
<session xmlns="http://thuban.intevation.org/dtds/thuban-1.0rc1.dtd" |
<session xmlns="http://thuban.intevation.org/dtds/thuban-1.1-dev.dtd" |
958 |
title="single map&layer"> |
title="single map&layer"> |
959 |
<fileshapesource id="D1" filename="../../Data/iceland/political.shp"/> |
<fileshapesource id="D1" filename="../../Data/iceland/political.shp"/> |
960 |
<map title="Test Map"> |
<map title="Test Map"> |
981 |
# LoadError really was about the missing attribute |
# LoadError really was about the missing attribute |
982 |
self.assertEquals(str(value), |
self.assertEquals(str(value), |
983 |
"Element " |
"Element " |
984 |
"(u'http://thuban.intevation.org/dtds/thuban-1.0rc1.dtd'," |
"(u'http://thuban.intevation.org/dtds/thuban-1.1-dev.dtd'," |
985 |
" u'fileshapesource') requires an attribute 'filetype'") |
" u'fileshapesource') requires an attribute 'filetype'") |
986 |
else: |
else: |
987 |
self.fail("Missing filetype attribute doesn't raise LoadError") |
self.fail("Missing filetype attribute doesn't raise LoadError") |
988 |
|
|
989 |
|
class Shapefile_CallBack: |
990 |
|
|
991 |
|
def __init__(self, params): |
992 |
|
"""Initialize the callback return values. |
993 |
|
|
994 |
|
params must be a dictionary of the potential CB modes (keys), |
995 |
|
with lists of tuples of return values as values. |
996 |
|
Depending on the test the callback can be called multiple, |
997 |
|
each time a return value is poped from the list |
998 |
|
""" |
999 |
|
|
1000 |
|
self.params = params |
1001 |
|
|
1002 |
|
|
1003 |
|
def s_cb(self, filename, mode = None, second_try= 0): |
1004 |
|
if self.params.has_key(mode): |
1005 |
|
return self.params[mode].pop(0) |
1006 |
|
else: |
1007 |
|
raise LoadError |
1008 |
|
|
1009 |
|
class TestAltPath(LoadSessionTest): |
1010 |
|
|
1011 |
|
"""Test the various cases in the alternative path feature. |
1012 |
|
|
1013 |
|
The test checks the reasonable cases: |
1014 |
|
- First recognition of a path error, fixed with user interaction. |
1015 |
|
- First recognition of a path error, load cancelled. |
1016 |
|
- Path error fixed from list, confirmed by user. |
1017 |
|
- Path error fixed from list, changed by user. |
1018 |
|
- Path error fixed from list, cancelled by user. |
1019 |
|
- Path error wrongly fixed from list, manual fix forced. |
1020 |
|
""" |
1021 |
|
|
1022 |
|
file_contents = '''\ |
1023 |
|
<?xml version="1.0" encoding="UTF-8"?> |
1024 |
|
<!DOCTYPE session SYSTEM "thuban-1.1.dtd"> |
1025 |
|
<session xmlns="http://thuban.intevation.org/dtds/thuban-1.1-dev.dtd" title="AltPath Test session"> |
1026 |
|
<fileshapesource filetype="shapefile" id="D1108450956" filename="../../Data/iceland/political.shp"/> |
1027 |
|
<fileshapesource filetype="shapefile" id="D1108900076" filename="../Data/iceland/roads-line.shp"/> |
1028 |
|
<fileshapesource filetype="shapefile" id="D1108947244" filename="../../Data/iceland/cultural_landmark-point.shp"/> |
1029 |
|
<map title="not the iceland map"> |
1030 |
|
<layer title="political" stroke_width="1" shapestore="D1108450956" visible="true" stroke="#000000" fill="#c0c0c0"/> |
1031 |
|
<layer title="roads-line" stroke_width="1" shapestore="D1108900076" visible="true" stroke="#000000" fill="None"/> |
1032 |
|
<layer title="something else" stroke_width="1" shapestore="D1108947244" visible="true" stroke="#000000" fill="None"/> |
1033 |
|
</map> |
1034 |
|
</session> |
1035 |
|
''' |
1036 |
|
|
1037 |
|
def checkSession(self, session): |
1038 |
|
"""Check if session has been loaded successfully.""" |
1039 |
|
|
1040 |
|
eq = self.assertEquals |
1041 |
|
|
1042 |
|
map = session.Maps()[0] |
1043 |
|
layers = map.Layers() |
1044 |
|
|
1045 |
|
eq("AltPath Test session", session.Title()) |
1046 |
|
eq("not the iceland map", map.Title()) |
1047 |
|
eq(3,len(layers)) |
1048 |
|
eq("political",layers[0].Title()) |
1049 |
|
eq("roads-line",layers[1].Title()) |
1050 |
|
eq("something else",layers[2].Title()) |
1051 |
|
|
1052 |
|
def test_01_single_path_error_fix(self): |
1053 |
|
"""Test single file path error fix.""" |
1054 |
|
# The usual initial case |
1055 |
|
s_cb = Shapefile_CallBack({ |
1056 |
|
"search": [("../Data/iceland/roads-line.shp",0)], |
1057 |
|
"check": [(None, None)]}) |
1058 |
|
self.session = load_session(self.filename(), |
1059 |
|
shapefile_callback =s_cb.s_cb) |
1060 |
|
self.checkSession(self.session) |
1061 |
|
|
1062 |
|
def test_02_path_error_fix_from_list(self): |
1063 |
|
"""Test single file path error fix.""" |
1064 |
|
# This represents the usual case for "from_list" |
1065 |
|
s_cb = Shapefile_CallBack({ |
1066 |
|
"search": [("../Data/iceland/roads-line.shp",1)], |
1067 |
|
"check": [(os.path.abspath("../Data/iceland/roads-line.shp"),1)] |
1068 |
|
}) |
1069 |
|
self.session = load_session(self.filename(), |
1070 |
|
shapefile_callback =s_cb.s_cb) |
1071 |
|
self.checkSession(self.session) |
1072 |
|
|
1073 |
|
def test_03_single_path_error_cancelled(self): |
1074 |
|
"""Test alternative path cancelled.""" |
1075 |
|
s_cb = Shapefile_CallBack({ |
1076 |
|
"search": [(None,0)], |
1077 |
|
"check": [(None, None)]}) |
1078 |
|
self.assertRaises(LoadCancelled, |
1079 |
|
load_session, self.filename(), None, s_cb.s_cb) |
1080 |
|
|
1081 |
|
def test_04_path_error_fix_from_list_cancelled(self): |
1082 |
|
"""Test alternative path from list cancelled.""" |
1083 |
|
s_cb = Shapefile_CallBack({ |
1084 |
|
"search": [("../Data/iceland/roads-line.shp",1)], |
1085 |
|
"check": [(None,1)] |
1086 |
|
}) |
1087 |
|
self.assertRaises(LoadCancelled, |
1088 |
|
load_session, self.filename(), None, s_cb.s_cb) |
1089 |
|
|
1090 |
|
def test_05_path_error_fix_from_list_changed(self): |
1091 |
|
"""Test alternative path from list changed.""" |
1092 |
|
s_cb = Shapefile_CallBack({ |
1093 |
|
"search": [("../Data/iceland/roads-line.shp",1)], |
1094 |
|
"check": [("../Data/iceland/roads-line.shp",0)] |
1095 |
|
}) |
1096 |
|
self.session = load_session(self.filename(), |
1097 |
|
shapefile_callback =s_cb.s_cb) |
1098 |
|
self.checkSession(self.session) |
1099 |
|
|
1100 |
|
def test_06_path_error_fix_from_list_fails(self): |
1101 |
|
"""Test alternative path recovery from list.""" |
1102 |
|
s_cb = Shapefile_CallBack({ |
1103 |
|
"search": [("../wrong/iceland/roads-line.shp",1), |
1104 |
|
("../Data/iceland/roads-line.shp",0)], |
1105 |
|
"check": [(None,None)] |
1106 |
|
}) |
1107 |
|
self.session = load_session(self.filename(), |
1108 |
|
shapefile_callback =s_cb.s_cb) |
1109 |
|
self.assertRaises(IndexError, |
1110 |
|
s_cb.s_cb, None, "search") |
1111 |
|
|
1112 |
|
|
1113 |
|
|
1114 |
if __name__ == "__main__": |
if __name__ == "__main__": |
1115 |
support.run_tests() |
support.run_tests() |