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

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

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 2104 by bh, Fri Mar 12 12:19:15 2004 UTC revision 2551 by jonathan, Thu Jan 27 14:19:41 2005 UTC
# Line 53  from Thuban.Model.table import DBFTable, Line 53  from Thuban.Model.table import DBFTable,
53  from Thuban.Model.label import ALIGN_CENTER, ALIGN_TOP, ALIGN_BOTTOM, \  from Thuban.Model.label import ALIGN_CENTER, ALIGN_TOP, ALIGN_BOTTOM, \
54       ALIGN_LEFT, ALIGN_RIGHT, ALIGN_BASELINE       ALIGN_LEFT, ALIGN_RIGHT, ALIGN_BASELINE
55    
   
56  def filenames_equal(name1, name2):  def filenames_equal(name1, name2):
57      """Return true if the filenames name1 and name2 are equal.      """Return true if the filenames name1 and name2 are equal.
58    
# Line 124  class LoadSessionTest(support.FileLoadTe Line 123  class LoadSessionTest(support.FileLoadTe
123                  print a != b and "***************" or ""                  print a != b and "***************" or ""
124                  print a                  print a
125                  print b                  print b
126    
127          self.assertEquals(el1, el2,          self.assertEquals(el1, el2,
128                            "loaded file not equivalent to the saved file")                            "loaded file not equivalent to the saved file")
129    
# Line 162  class ClassificationTest(LoadSessionTest Line 162  class ClassificationTest(LoadSessionTest
162                  props.SetLineWidth(data[CLASSES][i][GROUP_PROPS][1])                  props.SetLineWidth(data[CLASSES][i][GROUP_PROPS][1])
163                  props.SetFill(                  props.SetFill(
164                      parse_color(data[CLASSES][i][GROUP_PROPS][2]))                      parse_color(data[CLASSES][i][GROUP_PROPS][2]))
165                    if len(data[CLASSES][i][GROUP_PROPS]) > 3:
166                        props.SetSize(data[CLASSES][i][GROUP_PROPS][3])
167    
168                  if data[CLASSES][i][GROUP_TYPE] == "default":                  if data[CLASSES][i][GROUP_TYPE] == "default":
169                      g = ClassGroupDefault(props, data[CLASSES][i][GROUP_LABEL])                      g = ClassGroupDefault(props, data[CLASSES][i][GROUP_LABEL])
# Line 388  class TestLayerVisibility(LoadSessionTes Line 390  class TestLayerVisibility(LoadSessionTes
390          self.check_format()          self.check_format()
391    
392    
393    class TestSymbolSize(ClassificationTest):
394    
395        file_contents = '''\
396    <?xml version="1.0" encoding="UTF-8"?>
397    <!DOCTYPE session SYSTEM "thuban-1.1.dtd">
398    <session xmlns="http://thuban.intevation.org/dtds/thuban-1.1-dev.dtd" title="Thuban sample session">
399        <fileshapesource filetype="shapefile" id="D813968480" filename="../../Data/iceland/cultural_landmark-point.shp"/>
400        <map title="Iceland map">
401            <layer title="cultural_landmark-point" stroke_width="1" shapestore="D813968480" visible="true" stroke="#000000" fill="#000000">
402                <classification field="CLPTLABEL" field_type="string">
403                    <clnull label="">
404                        <cldata stroke="#000000" stroke_width="1" size="3" fill="#000000"/>
405                    </clnull>
406                    <clpoint label="" value="RUINS">
407                        <cldata stroke="#000000" stroke_width="1" size="6" fill="#ffffff"/>
408                    </clpoint>
409                    <clpoint label="" value="FARM">
410                        <cldata stroke="#000000" stroke_width="1" size="9" fill="#ffff00"/>
411                    </clpoint>
412                </classification>
413            </layer>
414        </map>
415    </session>
416    '''
417    
418        def test(self):
419            """Test that the size attribute for point symbols is correctly
420            loaded for a layer."""
421            eq = self.assertEquals
422            session = load_session(self.filename())
423            self.session = session
424    
425            map = session.Maps()[0] # only one map in the sample
426    
427            expected = [("cultural_landmark-point", 2,
428                            [("default", (), "",
429                                ("#000000", 1, "#000000", 3)),
430                             ("single", "RUINS", "",
431                                ("#000000", 1, "#ffffff", 6)),
432                             ("single", "FARM", "",
433                                ("#000000", 1, "#ffff00", 9))])]
434    
435            self.TestLayers(map.Layers(), expected)
436    
437            self.check_format()
438    
439    
440  class TestClassification(ClassificationTest):  class TestClassification(ClassificationTest):
441    
442      file_contents = '''\      file_contents = '''\
# Line 614  class TestRasterLayer(LoadSessionTest): Line 663  class TestRasterLayer(LoadSessionTest):
663  <session xmlns="http://thuban.intevation.org/dtds/thuban-1.1-dev.dtd"  <session xmlns="http://thuban.intevation.org/dtds/thuban-1.1-dev.dtd"
664          title="single map&amp;layer">          title="single map&amp;layer">
665      <map title="Test Map">      <map title="Test Map">
666          <rasterlayer visible="false" filename="../../Data/iceland/island.tif"          <rasterlayer visible="false" use_mask="true" filename="../../Data/iceland/island.tif"
667                  title="My RasterLayer"/>                  title="My RasterLayer"/>
668      </map>      </map>
669  </session>  </session>
# Line 631  class TestRasterLayer(LoadSessionTest): Line 680  class TestRasterLayer(LoadSessionTest):
680    
681          layer = map.Layers()[0] # one layer in the sample          layer = map.Layers()[0] # one layer in the sample
682    
683            layer.SetUseMask(True)
684    
685          eq(layer.Title(), "My RasterLayer")          eq(layer.Title(), "My RasterLayer")
686          self.failIf(layer.Visible())          self.failIf(layer.Visible())
687          self.failUnless(filenames_equal(layer.GetImageFilename(),          self.failUnless(filenames_equal(layer.GetImageFilename(),
# Line 931  class TestLoadError(LoadSessionTest): Line 982  class TestLoadError(LoadSessionTest):
982          else:          else:
983              self.fail("Missing filetype attribute doesn't raise LoadError")              self.fail("Missing filetype attribute doesn't raise LoadError")
984    
985    class Shapefile_CallBack:
986    
987        def __init__(self, params):
988            """Initialize the callback return values.
989              
990               params must be a dictionary of the potential CB modes (keys),
991               with lists of tuples of return values as values.
992               Depending on the test the callback can be called multiple,
993               each time a return value is poped from the list
994            """
995    
996            self.params = params
997    
998    
999        def s_cb(self, filename, mode = None, second_try= 0):
1000            if self.params.has_key(mode):
1001                return self.params[mode].pop(0)
1002            else:
1003                raise LoadError
1004            
1005    class TestAltPath(LoadSessionTest):
1006    
1007        """Test the various cases in the alternative path feature.
1008    
1009           The test checks the reasonable cases:
1010           - First recognition of a path error, fixed with user interaction.
1011           - First recognition of a path error, load cancelled.
1012           - Path error fixed from list, confirmed by user.
1013           - Path error fixed from list, changed by user.
1014           - Path error fixed from list, cancelled by user.
1015           - Path error wrongly fixed from list, manual fix forced.
1016        """
1017    
1018        file_contents = '''\
1019    <?xml version="1.0" encoding="UTF-8"?>
1020    <!DOCTYPE session SYSTEM "thuban-1.1.dtd">
1021    <session xmlns="http://thuban.intevation.org/dtds/thuban-1.1-dev.dtd" title="AltPath Test session">
1022        <fileshapesource filetype="shapefile" id="D1108450956" filename="../../Data/iceland/political.shp"/>
1023        <fileshapesource filetype="shapefile" id="D1108900076" filename="../Data/iceland/roads-line.shp"/>
1024        <fileshapesource filetype="shapefile" id="D1108947244" filename="../../Data/iceland/cultural_landmark-point.shp"/>
1025        <map title="not the iceland map">
1026            <layer title="political" stroke_width="1" shapestore="D1108450956" visible="true" stroke="#000000" fill="#c0c0c0"/>
1027            <layer title="roads-line" stroke_width="1" shapestore="D1108900076" visible="true" stroke="#000000" fill="None"/>
1028            <layer title="something else" stroke_width="1" shapestore="D1108947244" visible="true" stroke="#000000" fill="None"/>
1029        </map>
1030    </session>
1031    '''
1032    
1033        def checkSession(self, session):
1034            """Check if session has been loaded successfully."""
1035            
1036            eq = self.assertEquals
1037    
1038            map = session.Maps()[0]
1039            layers = map.Layers()
1040    
1041            eq("AltPath Test session", session.Title())
1042            eq("not the iceland map", map.Title())
1043            eq(3,len(layers))
1044            eq("political",layers[0].Title())
1045            eq("roads-line",layers[1].Title())
1046            eq("something else",layers[2].Title())
1047    
1048        def test_01_single_path_error_fix(self):
1049            """Test single file path error fix."""
1050            # The usual initial case
1051            s_cb = Shapefile_CallBack({
1052                        "search": [("../Data/iceland/roads-line.shp",0)],
1053                        "check": [(None, None)]})
1054            self.session = load_session(self.filename(),
1055                                        shapefile_callback =s_cb.s_cb)
1056            self.checkSession(self.session)
1057            
1058        def test_02_path_error_fix_from_list(self):
1059            """Test single file path error fix."""
1060            # This represents the usual case for "from_list"
1061            s_cb = Shapefile_CallBack({
1062                    "search": [("../Data/iceland/roads-line.shp",1)],
1063                    "check": [(os.path.abspath("../Data/iceland/roads-line.shp"),1)]
1064                   })
1065            self.session = load_session(self.filename(),
1066                                        shapefile_callback =s_cb.s_cb)
1067            self.checkSession(self.session)
1068    
1069        def test_03_single_path_error_cancelled(self):
1070            """Test alternative path cancelled."""
1071            s_cb = Shapefile_CallBack({
1072                        "search": [(None,0)],
1073                        "check": [(None, None)]})
1074            self.assertRaises(LoadCancelled,
1075                                load_session, self.filename(), None, s_cb.s_cb)
1076    
1077        def test_04_path_error_fix_from_list_cancelled(self):
1078            """Test alternative path from list cancelled."""
1079            s_cb = Shapefile_CallBack({
1080                    "search": [("../Data/iceland/roads-line.shp",1)],
1081                    "check": [(None,1)]
1082                   })
1083            self.assertRaises(LoadCancelled,
1084                                load_session, self.filename(), None, s_cb.s_cb)
1085    
1086        def test_05_path_error_fix_from_list_changed(self):
1087            """Test alternative path from list changed."""
1088            s_cb = Shapefile_CallBack({
1089                    "search": [("../Data/iceland/roads-line.shp",1)],
1090                    "check": [("../Data/iceland/roads-line.shp",0)]
1091                   })
1092            self.session = load_session(self.filename(),
1093                                        shapefile_callback =s_cb.s_cb)
1094            self.checkSession(self.session)
1095    
1096        def test_06_path_error_fix_from_list_fails(self):
1097            """Test alternative path recovery from list."""
1098            s_cb = Shapefile_CallBack({
1099                    "search": [("../wrong/iceland/roads-line.shp",1),
1100                                ("../Data/iceland/roads-line.shp",0)],
1101                    "check": [(None,None)]
1102                   })
1103            self.session = load_session(self.filename(),
1104                                        shapefile_callback =s_cb.s_cb)
1105            self.assertRaises(IndexError,
1106                                s_cb.s_cb, None, "search")
1107            
1108    
1109    
1110  if __name__ == "__main__":  if __name__ == "__main__":
1111      support.run_tests()      support.run_tests()

Legend:
Removed from v.2104  
changed lines
  Added in v.2551

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26