/[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 774 by jonathan, Tue Apr 29 14:34:45 2003 UTC revision 1247 by bh, Thu Jun 19 19:53:36 2003 UTC
# Line 7  Line 7 
7    
8  """  """
9  Test loading a thuban session from a file  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.
21  """  """
22    
23  __version__ = "$Revision$"  __version__ = "$Revision$"
# Line 20  import support Line 31  import support
31  support.initthuban()  support.initthuban()
32    
33  from Thuban.Model.load import load_session, parse_color  from Thuban.Model.load import load_session, parse_color
 from Thuban.Model.session import Session  
 from Thuban.Model.map import Map  
 from Thuban.Model.layer import Layer  
 from Thuban.Model.proj import Projection  
34  from Thuban.Model.color import Color  from Thuban.Model.color import Color
   
 from Thuban.Model.table import FIELDTYPE_INT, FIELDTYPE_DOUBLE, FIELDTYPE_STRING  
   
35  from Thuban.Model.classification import ClassGroupProperties, ClassGroupRange,\  from Thuban.Model.classification import ClassGroupProperties, ClassGroupRange,\
36      ClassGroupSingleton, ClassGroupDefault      ClassGroupSingleton, ClassGroupDefault
37    
38    
39  def filenames_equal(name1, name2):  def filenames_equal(name1, name2):
40      """Return true if the filenames name1 and name2 are equal.      """Return true if the filenames name1 and name2 are equal.
41    
# Line 43  def filenames_equal(name1, name2): Line 48  def filenames_equal(name1, name2):
48      return os.path.normpath(name1) == os.path.normpath(name2)      return os.path.normpath(name1) == os.path.normpath(name2)
49    
50    
51  contents_single_map = '''\  
52    class LoadSessionTest(support.FileLoadTestCase):
53    
54        """Base class for .thuban file loading tests
55    
56        Basically the same as the FileLoadTestCase, except that all tests
57        use the '.thuban' extension by default and that setUp and tearDown
58        handle sessions.
59        """
60    
61        file_extension = ".thuban"
62    
63        def setUp(self):
64            """Create the test files"""
65            support.FileLoadTestCase.setUp(self)
66            self.session = None
67    
68        def tearDown(self):
69            if self.session is not None:
70                self.session.Destroy()
71            self.session = None
72    
73    
74    class ClassificationTest(LoadSessionTest):
75    
76        """
77        Base class for tests that do some detailed checking of classifications
78        """
79    
80        def TestLayers(self, layers, expected):
81            TITLE = 0
82            NUM_GROUPS = 1
83            CLASSES = 2
84            GROUP_TYPE = 0
85            GROUP_DATA = 1
86            GROUP_LABEL = 2
87            GROUP_PROPS = 3
88    
89            eq = self.assertEquals
90    
91            eq(len(layers), len(expected))
92    
93            for layer, data in zip(layers, expected):
94                eq(layer.Title(), data[TITLE])
95    
96                clazz = layer.GetClassification()
97                eq(clazz.GetNumGroups(), data[NUM_GROUPS])
98                eq(clazz.GetNumGroups() + 1, len(data[CLASSES]))
99    
100                i = 0
101                for group in clazz:
102                    props = ClassGroupProperties()
103                    props.SetLineColor(
104                        parse_color(data[CLASSES][i][GROUP_PROPS][0]))
105                    props.SetLineWidth(data[CLASSES][i][GROUP_PROPS][1])
106                    props.SetFill(
107                        parse_color(data[CLASSES][i][GROUP_PROPS][2]))
108    
109                    if data[CLASSES][i][GROUP_TYPE] == "default":
110                        g = ClassGroupDefault(props, data[CLASSES][i][GROUP_LABEL])
111                    elif data[CLASSES][i][GROUP_TYPE] == "range":
112                        g = ClassGroupRange(data[CLASSES][i][GROUP_DATA][0],
113                                            data[CLASSES][i][GROUP_DATA][1],
114                                            props, data[CLASSES][i][GROUP_LABEL])
115                    elif data[CLASSES][i][GROUP_TYPE] == "single":
116                        g = ClassGroupSingleton(data[CLASSES][i][GROUP_DATA],
117                                              props, data[CLASSES][i][GROUP_LABEL])
118    
119                    eq(group, g)
120    
121                    i += 1
122    
123    
124    
125    class TestSingleLayer(LoadSessionTest):
126    
127        file_contents = '''\
128  <?xml version="1.0" encoding="UTF-8"?>  <?xml version="1.0" encoding="UTF-8"?>
129  <!DOCTYPE session SYSTEM "thuban.dtd">  <!DOCTYPE session SYSTEM "thuban.dtd">
130  <session title="single map&amp;layer">  <session title="single map&amp;layer">
# Line 60  contents_single_map = '''\ Line 141  contents_single_map = '''\
141  </session>  </session>
142  '''  '''
143    
144  contents_classified_map_v0_2 = '''\      def test(self):
145            """Load a session with a single map with a single layer"""
146            eq = self.assertEquals
147            session = load_session(self.filename())
148            self.session = session
149    
150            # Check the title
151            eq(session.Title(), "single map&layer")
152    
153            # the session has one map.
154            maps = session.Maps()
155            eq(len(maps), 1)
156    
157            # Check the map's attributes
158            map = maps[0]
159            eq(map.Title(), "Test Map")
160    
161            # the map has a single layer
162            layers = map.Layers()
163            eq(len(layers), 1)
164    
165            # Check the layer attributes
166            layer = layers[0]
167            eq(layer.Title(), "My Layer")
168            self.failUnless(filenames_equal(layer.ShapeStore().FileName(),
169                                            os.path.join(self.temp_dir(),
170                                                         os.pardir, os.pardir,
171                                                         "Data", "iceland",
172                                                         "political.shp")))
173            eq(layer.GetClassification().GetDefaultFill(), Color.Transparent)
174            eq(layer.GetClassification().GetDefaultLineColor().hex(), "#000000")
175            eq(layer.Visible(), True)
176    
177            self.session.Destroy()
178            self.session = None
179    
180    
181    class TestLayerVisibility(LoadSessionTest):
182    
183        file_contents = '''\
184    <?xml version="1.0" encoding="UTF-8"?>
185    <!DOCTYPE session SYSTEM "thuban.dtd">
186    <session title="single map&amp;layer">
187            <map title="Test Map">
188                    <projection>
189                            <parameter value="zone=26"/>
190                            <parameter value="proj=utm"/>
191                            <parameter value="ellps=clrk66"/>
192                    </projection>
193                    <layer title="My Layer" stroke_width="1" fill="None"
194                        filename="../../Data/iceland/political.shp"
195                        stroke="#000000" visible="false">
196            </layer>
197        </map>
198    </session>
199    '''
200    
201        def test(self):
202            """Test that the visible flag is correctly loaded for a layer."""
203            eq = self.assertEquals
204            session = load_session(self.filename())
205            self.session = session
206            maps = session.Maps()
207            eq(len(maps), 1)
208            map = maps[0]
209            layers = map.Layers()
210            eq(len(layers), 1)
211            layer = layers[0]
212    
213            eq(layer.Visible(), False)
214    
215    
216    
217    
218    class TestClassification(ClassificationTest):
219    
220        file_contents = '''\
221  <?xml version="1.0" encoding="UTF-8"?>  <?xml version="1.0" encoding="UTF-8"?>
222  <!DOCTYPE session SYSTEM "thuban.dtd">  <!DOCTYPE session SYSTEM "thuban.dtd">
223  <session title="single map&amp;layer">  <session title="single map&amp;layer">
# Line 110  contents_classified_map_v0_2 = '''\ Line 267  contents_classified_map_v0_2 = '''\
267  </session>  </session>
268  '''  '''
269    
270  contents_test_labels = '''\      def test(self):
271            """Load a Thuban session with a map and classified layers."""
272            session = load_session(self.filename())
273            self.session = session
274    
275            map = self.session.Maps()[0] # only one map in the sample
276    
277            expected = [("My Layer", 2,
278                            [("default", (), "",
279                                ("#000000", 1, "None")),
280                             ("single", "1", "",
281                                ("#000000", 2, "None")),
282                             ("single", "1", "",
283                                ("#000000", 10, "None"))]),
284                         ("My Layer 2", 4,
285                             [("default", (), "",
286                                ("#000000", 2, "None")),
287                              ("range", (0, 1), "",
288                                ("#111111", 1, "None")),
289                              ("single", .5, "",
290                                ("#000000", 1, "#111111")),
291                              ("range", (-1, 0), "",
292                                ("#000000", 1, "None")),
293                              ("single", -.5, "",
294                                ("#000000", 1, "None"))])]
295    
296            self.TestLayers(map.Layers(), expected)
297    
298    
299    class TestLabels(ClassificationTest):
300    
301        file_contents = '''\
302  <?xml version="1.0" encoding="UTF-8"?>  <?xml version="1.0" encoding="UTF-8"?>
303  <!DOCTYPE session SYSTEM "thuban.dtd">  <!DOCTYPE session SYSTEM "thuban.dtd">
304  <session title="single map&amp;layer">  <session title="single map&amp;layer">
# Line 136  contents_test_labels = '''\ Line 324  contents_test_labels = '''\
324  </session>  </session>
325  '''  '''
326    
327  contents_test_layer_projection = '''\      def test(self):
328            """Load a session and test for reading the group labels."""
329            eq = self.assertEquals
330            session = load_session(self.filename())
331            self.session = session
332    
333            map = self.session.Maps()[0] # only one map in the sample
334    
335            expected = [("My Layer", 1,
336                            [("default", (), "hallo",
337                                ("#000000", 1, "None")),
338                             ("single", "1", "welt",
339                                ("#000000", 2, "None"))])]
340    
341            self.TestLayers(map.Layers(), expected)
342    
343    
344    class TestLayerProjection(LoadSessionTest):
345    
346        file_contents = '''\
347  <?xml version="1.0" encoding="UTF-8"?>  <?xml version="1.0" encoding="UTF-8"?>
348  <!DOCTYPE session SYSTEM "thuban.dtd">  <!DOCTYPE session SYSTEM "thuban.dtd">
349  <session title="single map&amp;layer">  <session title="single map&amp;layer">
# Line 175  contents_test_layer_projection = '''\ Line 382  contents_test_layer_projection = '''\
382  </session>  </session>
383  '''  '''
384    
385  contents_test_visible = '''\      def test(self):
386  <?xml version="1.0" encoding="UTF-8"?>          """Test loading layers with projections"""
 <!DOCTYPE session SYSTEM "thuban.dtd">  
 <session title="single map&amp;layer">  
         <map title="Test Map">  
                 <projection>  
                         <parameter value="zone=26"/>  
                         <parameter value="proj=utm"/>  
                         <parameter value="ellps=clrk66"/>  
                 </projection>  
                 <layer title="My Layer" stroke_width="1" fill="None"  
                     filename="../../Data/iceland/political.shp"  
                     stroke="#000000" visible="false">  
         </layer>  
     </map>  
 </session>  
 '''  
   
 class LoadSessionTest(unittest.TestCase, support.FileTestMixin):  
   
     def setUp(self):  
         """Create the test files"""  
         file = open(self.temp_file_name("load_singlelayer.thuban"), "w")  
         file.write(contents_single_map)  
         file.close()  
   
         file = open(self.temp_file_name("load_classified_v0_2.thuban"), "w")  
         file.write(contents_classified_map_v0_2)  
         file.close()  
   
         file = open(self.temp_file_name("load_labels.thuban"), "w")  
         file.write(contents_test_labels)  
         file.close()  
   
         file = open(self.temp_file_name("load_layerproj.thuban"), "w")  
         file.write(contents_test_layer_projection)  
         file.close()  
   
         file = open(self.temp_file_name("load_visible.thuban"), "w")  
         file.write(contents_test_visible)  
         file.close()  
   
         self.session = None  
   
     def tearDown(self):  
         if self.session is not None:  
             self.session.Destroy()  
         self.session = None  
   
     def testSingleLayer(self):  
         """Load a session with a single map with a single layer"""  
         eq = self.assertEquals  
         session = load_session(self.temp_file_name("load_singlelayer.thuban"))  
         self.session = session  
   
         # Check the title  
         eq(session.Title(), "single map&layer")  
   
         # the session has one map.  
         maps = session.Maps()  
         eq(len(maps), 1)  
   
         # Check the map's attributes  
         map = maps[0]  
         eq(map.Title(), "Test Map")  
   
         # the map has a single layer  
         layers = map.Layers()  
         eq(len(layers), 1)  
   
         # Check the layer attributes  
         layer = layers[0]  
         eq(layer.Title(), "My Layer")  
         self.failUnless(filenames_equal(layer.filename,  
                                         os.path.join(self.temp_dir(),  
                                                      os.pardir, os.pardir,  
                                                      "Data", "iceland",  
                                                      "political.shp")))  
         eq(layer.GetClassification().GetDefaultFill(), Color.Transparent)  
         eq(layer.GetClassification().GetDefaultLineColor().hex(), "#000000")  
         eq(layer.Visible(), True)  
   
         self.session.Destroy()  
         self.session = None  
   
     def testLayerVisibility(self):  
         """Test that the visible flag is correctly loaded for a layer."""  
   
         eq = self.assertEquals  
         session = load_session(self.temp_file_name("load_visible.thuban"))  
         self.session = session  
         maps = session.Maps()  
         eq(len(maps), 1)  
         map = maps[0]  
         layers = map.Layers()  
         eq(len(layers), 1)  
         layer = layers[0]  
   
         eq(layer.Visible(), False)  
   
     def testClassification(self):  
         """Load a session with a map and classified layers."""  
   
         session = load_session(self.temp_file_name("load_classified_v0_2.thuban"))  
         self.session = session  
   
         map = self.session.Maps()[0] # only one map in the sample  
   
         expected = [("My Layer", 2,  
                         [("default", (), "",  
                             ("#000000", 1, "None")),  
                          ("single", "1", "",  
                             ("#000000", 2, "None")),  
                          ("single", "1", "",  
                             ("#000000", 10, "None"))]),  
                      ("My Layer 2", 4,  
                          [("default", (), "",  
                             ("#000000", 2, "None")),  
                           ("range", (0, 1), "",  
                             ("#111111", 1, "None")),  
                           ("single", .5, "",  
                             ("#000000", 1, "#111111")),  
                           ("range", (-1, 0), "",  
                             ("#000000", 1, "None")),  
                           ("single", -.5, "",  
                             ("#000000", 1, "None"))])]  
   
         self.TestLayers(map.Layers(), expected)  
   
     def TestLayers(self, layers, expected):  
   
         TITLE = 0  
         NUM_GROUPS = 1  
         CLASSES = 2  
         GROUP_TYPE = 0  
         GROUP_DATA = 1  
         GROUP_LABEL = 2  
         GROUP_PROPS = 3  
   
         eq = self.assertEquals  
   
         eq(len(layers), len(expected))  
   
         for layer, data in zip(layers, expected):  
             eq(layer.Title(), data[TITLE])  
   
             clazz = layer.GetClassification()  
             eq(clazz.GetNumGroups(), data[NUM_GROUPS])  
             eq(clazz.GetNumGroups() + 1, len(data[CLASSES]))  
   
             i = 0  
             for group in clazz:  
                   
                 props = ClassGroupProperties()  
                 props.SetLineColor(  
                     parse_color(data[CLASSES][i][GROUP_PROPS][0]))  
                 props.SetLineWidth(data[CLASSES][i][GROUP_PROPS][1])  
                 props.SetFill(  
                     parse_color(data[CLASSES][i][GROUP_PROPS][2]))  
   
                 if data[CLASSES][i][GROUP_TYPE] == "default":  
                     g = ClassGroupDefault(props, data[CLASSES][i][GROUP_LABEL])  
                 elif data[CLASSES][i][GROUP_TYPE] == "range":  
                     g = ClassGroupRange(data[CLASSES][i][GROUP_DATA][0],  
                                         data[CLASSES][i][GROUP_DATA][1],  
                                         props, data[CLASSES][i][GROUP_LABEL])  
                 elif data[CLASSES][i][GROUP_TYPE] == "single":  
                     g = ClassGroupSingleton(data[CLASSES][i][GROUP_DATA],  
                                           props, data[CLASSES][i][GROUP_LABEL])  
   
                 eq(group, g)  
   
                 i += 1  
   
     def testLabels(self):  
         """Load a session and test for reading the group labels."""  
   
         eq = self.assertEquals  
         session = load_session(self.temp_file_name("load_labels.thuban"))  
         self.session = session  
   
         map = self.session.Maps()[0] # only one map in the sample  
   
         expected = [("My Layer", 1,  
                         [("default", (), "hallo",  
                             ("#000000", 1, "None")),  
                          ("single", "1", "welt",  
                             ("#000000", 2, "None"))])]  
   
         self.TestLayers(map.Layers(), expected)  
   
     def testLayerProjection(self):  
387          eq = self.assertEquals          eq = self.assertEquals
388          neq = self.assertNotEqual          neq = self.assertNotEqual
389    
390          session = load_session(self.temp_file_name("load_layerproj.thuban"))          session = load_session(self.filename())
391          self.session = session          self.session = session
392    
393          map = self.session.Maps()[0] # only one map in the sample          map = self.session.Maps()[0] # only one map in the sample
# Line 392  class LoadSessionTest(unittest.TestCase, Line 409  class LoadSessionTest(unittest.TestCase,
409          eq(proj.GetParameter("proj"), "lcc")          eq(proj.GetParameter("proj"), "lcc")
410          eq(proj.GetParameter("ellps"), "clrk66")          eq(proj.GetParameter("ellps"), "clrk66")
411    
 if __name__ == "__main__":  
     unittest.main()  
412    
413    class TestRasterLayer(LoadSessionTest):
414    
415        file_contents = '''\
416    <?xml version="1.0" encoding="UTF-8"?>
417    <!DOCTYPE session SYSTEM "thuban.dtd">
418    <session title="single map&amp;layer">
419            <map title="Test Map">
420                    <rasterlayer title="My RasterLayer"
421                         filename="../../Data/iceland/island.tif"
422                         visible="false">
423            </rasterlayer>
424        </map>
425    </session>
426    '''
427    
428        def test(self):
429            eq = self.assertEquals
430            neq = self.assertNotEqual
431    
432            session = load_session(self.filename())
433            self.session = session
434    
435            map = self.session.Maps()[0] # only one map in the sample
436    
437            layer = map.Layers()[0] # one layer in the sample
438    
439            eq(layer.Title(), "My RasterLayer")
440            self.failIf(layer.Visible())
441            self.failUnless(filenames_equal(layer.GetImageFilename(),
442                                            os.path.join(self.temp_dir(),
443                                                         os.pardir, os.pardir,
444                                                         "Data", "iceland",
445                                                         "island.tif")))
446    
447    if __name__ == "__main__":
448        unittest.main()

Legend:
Removed from v.774  
changed lines
  Added in v.1247

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26