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

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

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

revision 1818 by bh, Mon Oct 13 15:54:14 2003 UTC revision 1831 by bh, Thu Oct 16 16:36:11 2003 UTC
# Line 23  support.initthuban() Line 23  support.initthuban()
23  from Thuban import _  from Thuban import _
24  from Thuban.Model.proj import Projection, ProjFile, \  from Thuban.Model.proj import Projection, ProjFile, \
25       PROJ_UNITS_METERS, PROJ_UNITS_DEGREES       PROJ_UNITS_METERS, PROJ_UNITS_DEGREES
26    from Thuban.Model.messages import PROJECTION_ADDED, PROJECTION_REMOVED, \
27         PROJECTION_REPLACED
28  import Thuban.Model.resource as resource  import Thuban.Model.resource as resource
29    
30  from xmlsupport import sax_eventlist  from xmlsupport import sax_eventlist
# Line 114  class TestProjection(unittest.TestCase, Line 115  class TestProjection(unittest.TestCase,
115          self.assertEquals(proj.EPSGCode(), "42")          self.assertEquals(proj.EPSGCode(), "42")
116    
117    
 class ProjFileTest(unittest.TestCase, support.FileTestMixin):  
   
     """Base class for the proj file tests"""  
118    
119      def filename(self):  class TestProjFileSimple:
         """Return the filename for the test"""  
         return self.temp_file_name(self.id() + ".proj")  
120    
121        def test_init(self):
122            """Test ProjFile coinstructor"""
123            proj_file = ProjFile("some_filename")
124            self.assertEquals(proj_file.GetFilename(), "some_filename")
125            self.assertEquals(len(proj_file.GetProjections()), 0)
126    
127  class TestProjFile(ProjFileTest, xmlsupport.ValidationTest):      def test_set_filename(self):
128            """Test ProjFile.SetFilename()"""
129            proj_file = ProjFile("some_filename")
130            proj.SetFilename("other_name")
131            self.assertEquals(proj_file.GetFilename(), "other_name")
132    
     """Test cases for reading and writing projection files.  
     """  
133    
134      def test(self):  class TestProjFile(unittest.TestCase, support.SubscriberMixin):
         """Test ProjFile"""  
135    
136          proj0 = Projection(["proj=tmerc", "ellps=clrk66"])      """Test cases for ProjFile objects"""
         proj1 = Projection(["proj=utm", "ellps=clrk66"])  
         proj2 = Projection(["proj=lcc", "ellps=clrk66",  
                             "lat_1=0", "lat_2=20"])  
   
         eq = self.assertEquals  
   
   
         #  
         # __init__()  
         # GetFilename()  
         # SetFilename()  
         #  
         for name in ["", "hello_world"]:  
             projFile = ProjFile(name)  
             eq(projFile.GetFilename(), name)  
   
             projFile.SetFilename("XXX")  
             projFile.SetFilename(name)  
             eq(projFile.GetFilename(), name)  
   
         # initial number of projections should be 0  
         eq(len(projFile.GetProjections()), 0)  
   
         #  
         # Add()  
         # Remove()  
         #  
         projFile.Add(proj0)  
         eq(len(projFile.GetProjections()), 1)  
         projFile.Remove(proj0)  
         eq(len(projFile.GetProjections()), 0)  
   
         # try to remove something that doesn't exist  
         self.assertRaises(ValueError, projFile.Remove, None)  
         self.assertRaises(ValueError, projFile.Remove, proj0)  
   
         projFile.Add(proj0)  
         projFile.Add(proj1)  
         projFile.Add(proj2)  
         eq(len(projFile.GetProjections()), 3)  
   
         # GetProjections() -- tests order  
         projs = projFile.GetProjections()  
         eq(projs[0], proj0)  
         eq(projs[1], proj1)  
         eq(projs[2], proj2)  
137    
138          projFile.Remove(proj2)      def setUp(self):
139          projFile.Remove(proj1)          self.clear_messages()
140            self.proj0 = Projection(["proj=tmerc", "ellps=clrk66"])
141            self.proj1 = Projection(["proj=utm", "ellps=clrk66"])
142            self.proj2 = Projection(["proj=lcc", "ellps=clrk66",
143                                     "lat_1=0", "lat_2=20"])
144            self.proj_file = ProjFile("some_filename")
145            for msg in [PROJECTION_ADDED, PROJECTION_REMOVED, PROJECTION_REPLACED]:
146                self.proj_file.Subscribe(msg, self.subscribe_with_params, msg)
147    
148        def tearDown(self):
149            self.clear_messages()
150            self.proj_file.Destroy()
151    
152        def test_add_remove(self):
153            """Test ProjFile.Add() and ProjFile.Remove()"""
154            self.proj_file.Add(self.proj0)
155            self.proj_file.Add(self.proj1)
156            self.assertEquals(self.proj_file.GetProjections(),
157                              [self.proj0, self.proj1])
158            self.check_messages([(self.proj0, PROJECTION_ADDED),
159                                 (self.proj1, PROJECTION_ADDED)])
160            self.clear_messages()
161    
162            self.proj_file.Remove(self.proj0)
163            self.assertEquals(self.proj_file.GetProjections(), [self.proj1])
164            self.check_messages([(self.proj0, PROJECTION_REMOVED)])
165    
166        def test_remove_non_existing(self):
167            """Test ProjFile.Remove(<proj not in projfile>)"""
168            self.assertRaises(ValueError, self.proj_file.Remove, self.proj0)
169            # Nothing happened, so no messages should have been sent
170            self.check_messages([])
171    
172        def test_replace(self):
173            """Test ProjFile.Replace()"""
174            self.proj_file.Add(self.proj0)
175            self.proj_file.Add(self.proj1)
176            self.clear_messages()
177    
178          # Replace()          # Replace()
179          projFile.Replace(proj0, proj1)          self.proj_file.Replace(self.proj0, self.proj2)
180          projs = projFile.GetProjections()          self.assertEquals(self.proj_file.GetProjections(),
181          eq(projs[0], proj1)                            [self.proj2, self.proj1])
182            self.check_messages([(self.proj0, self.proj2, PROJECTION_REPLACED)])
183          # replace a non-existent projection  
184          self.assertRaises(ValueError, projFile.Replace, None, proj2)      def test_replace_non_existing(self):
185          self.assertRaises(ValueError, projFile.Replace, proj0, proj2)          """Test ProjFile.Replace(<proj not in projfile>, <some proj>)"""
186            self.proj_file.Add(self.proj0)
187            self.proj_file.Add(self.proj1)
188            self.clear_messages()
189            self.assertRaises(ValueError,
190                              self.proj_file.Replace, self.proj2, self.proj0)
191            # All projections should still be there
192            self.assertEquals(self.proj_file.GetProjections(),
193                              [self.proj0, self.proj1])
194            # Nothing happened, so no messages should have been sent
195            self.check_messages([])
196    
197    
198    class ProjFileTest(unittest.TestCase, support.FileTestMixin,
199                       xmlsupport.ValidationTest):
200    
201        """Base class for the proj file tests that read or write files"""
202    
203        def filename(self):
204            """Return the filename for the test"""
205            return self.temp_file_name(self.id() + ".proj")
206    
207    
208    class ProjFileReadTests(ProjFileTest):
209    
210        """Test read ProjFile objects from files
211    
212        The files only cover error handling and the system projection file.
213        """
214    
215      def test_read_non_existing_file(self):      def test_read_non_existing_file(self):
216          """Test read_proj_file with non-existing file"""          """Test read_proj_file with non-existing file"""
# Line 228  class TestProjFile(ProjFileTest, xmlsupp Line 249  class TestProjFile(ProjFileTest, xmlsupp
249          self.assertEquals(warnings, [])          self.assertEquals(warnings, [])
250          self.assert_(len(projfile.GetProjections()) > 0)          self.assert_(len(projfile.GetProjections()) > 0)
251    
252            # see whether it got cached and we get the same projfile object
253            # when we read the file again
254            projfile2, warnings = resource.get_system_proj_file()
255            self.assert_(projfile is projfile2)
256    
257  class WriteProjFileTests(ProjFileTest, xmlsupport.ValidationTest):  
258    class WriteProjFileTests(ProjFileTest):
259    
260      """Test cases for writing proj files"""      """Test cases for writing proj files"""
261    
# Line 296  class WriteProjFileTests(ProjFileTest, x Line 322  class WriteProjFileTests(ProjFileTest, x
322          self.doTestWrite(pf, file_contents)          self.doTestWrite(pf, file_contents)
323    
324    
325  class TestLoadingProjFile(support.FileLoadTestCase):  class ProjFileLoadTestCase(support.FileLoadTestCase):
326    
327        """Base class for the test cases that read specific test files"""
328    
329      file_extension = ".proj"      file_extension = ".proj"
330    
331        def tearDown(self):
332            """Clear the cache explicitly"""
333            resource.clear_proj_file_cache()
334    
335    
336    class TestLoadingProjFile(ProjFileLoadTestCase):
337    
338      file_contents = '''\      file_contents = '''\
339  <?xml version="1.0" encoding="UTF-8"?>  <?xml version="1.0" encoding="UTF-8"?>
340  <!DOCTYPE projectionlist SYSTEM "projfile.dtd">  <!DOCTYPE projectionlist SYSTEM "projfile.dtd">
# Line 350  class TestLoadingProjFile(support.FileLo Line 386  class TestLoadingProjFile(support.FileLo
386                                 "proj=tmerc", "units=m",                                 "proj=tmerc", "units=m",
387                                 "x_0=400000.000", "y_0=0.000"])                                 "x_0=400000.000", "y_0=0.000"])
388    
389        def test_caching(self):
390            # test whether the projfile cache works
391            projfile, warnings = resource.read_proj_file(self.filename())
392            projfile2, warnings = resource.read_proj_file(self.filename())
393    
394  class TestLoadingProjFileWithEmptyProjectionlist(support.FileLoadTestCase):          # Both projfiles should be the same object
395            self.assert_(projfile2 is projfile)
396    
397            # If we clear the cache we should get a new one.
398            resource.clear_proj_file_cache()
399            projfile3, warnings = resource.read_proj_file(self.filename())
400            self.assert_(projfile3 is not projfile)
401    
402    
403    class TestLoadingProjFileWithEmptyProjectionlist(ProjFileLoadTestCase):
404    
     file_extension = ".proj"  
405      file_contents = '''\      file_contents = '''\
406  <?xml version="1.0" encoding="UTF-8"?>  <?xml version="1.0" encoding="UTF-8"?>
407  <!DOCTYPE projectionlist SYSTEM "projfile.dtd">  <!DOCTYPE projectionlist SYSTEM "projfile.dtd">
# Line 370  class TestLoadingProjFileWithEmptyProjec Line 418  class TestLoadingProjFileWithEmptyProjec
418          self.assertEquals(len(projfile.GetProjections()), 0)          self.assertEquals(len(projfile.GetProjections()), 0)
419    
420    
421  class TestProjFileWithInvalidParameters(unittest.TestCase,  class TestProjFileWithInvalidParameters(ProjFileLoadTestCase):
                                         support.FileLoadTestCase):  
422    
     file_extension = ".proj"  
423      file_contents = '''\      file_contents = '''\
424  <?xml version="1.0" encoding="UTF-8"?>  <?xml version="1.0" encoding="UTF-8"?>
425  <!DOCTYPE projectionlist SYSTEM "projfile.dtd">  <!DOCTYPE projectionlist SYSTEM "projfile.dtd">
# Line 413  class TestProjFileWithInvalidParameters( Line 459  class TestProjFileWithInvalidParameters(
459    
460    
461  if __name__ == "__main__":  if __name__ == "__main__":
462      unittest.main()      support.run_tests()

Legend:
Removed from v.1818  
changed lines
  Added in v.1831

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26