/[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 1821 by bh, Tue Oct 14 13:54:03 2003 UTC revision 1966 by bh, Thu Nov 20 18:15:57 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 79  class TestProjection(unittest.TestCase, Line 80  class TestProjection(unittest.TestCase,
80          self.assertEquals(proj.GetParameter("south"), "south")          self.assertEquals(proj.GetParameter("south"), "south")
81    
82      def test_get_projection_units_geo(self):      def test_get_projection_units_geo(self):
83          """Test Projection.GetProjectedUnits() for geographic projection"""          """Test Projection.GetProjectedUnits() for geographic projection.
84            Test for the alias 'longlat' as well.
85            """
86          proj = Projection(["proj=latlong", "to_meter=0.017453292519943295",          proj = Projection(["proj=latlong", "to_meter=0.017453292519943295",
87                             "ellps=clrk66"])                             "ellps=clrk66"])
88          self.assertEquals(proj.GetProjectedUnits(), PROJ_UNITS_DEGREES)          self.assertEquals(proj.GetProjectedUnits(), PROJ_UNITS_DEGREES)
89            proj = Projection(["proj=longlat", "to_meter=0.017453292519943295",
90                               "ellps=clrk66"])
91            self.assertEquals(proj.GetProjectedUnits(), PROJ_UNITS_DEGREES)
92    
93      def test_get_projection_units_normal(self):      def test_get_projection_units_normal(self):
94          """Test Projection.GetProjectedUnits() for normal projection"""          """Test Projection.GetProjectedUnits() for normal projection"""
# Line 130  class TestProjFileSimple: Line 136  class TestProjFileSimple:
136          self.assertEquals(proj_file.GetFilename(), "other_name")          self.assertEquals(proj_file.GetFilename(), "other_name")
137    
138    
139  class TestProjFile(unittest.TestCase):  class TestProjFile(unittest.TestCase, support.SubscriberMixin):
140    
141      """Test cases for reading and writing projection files.      """Test cases for ProjFile objects"""
     """  
142    
143      def setUp(self):      def setUp(self):
144            self.clear_messages()
145          self.proj0 = Projection(["proj=tmerc", "ellps=clrk66"])          self.proj0 = Projection(["proj=tmerc", "ellps=clrk66"])
146          self.proj1 = Projection(["proj=utm", "ellps=clrk66"])          self.proj1 = Projection(["proj=utm", "ellps=clrk66"])
147          self.proj2 = Projection(["proj=lcc", "ellps=clrk66",          self.proj2 = Projection(["proj=lcc", "ellps=clrk66",
148                                   "lat_1=0", "lat_2=20"])                                   "lat_1=0", "lat_2=20"])
149            self.proj_file = ProjFile("some_filename")
150            for msg in [PROJECTION_ADDED, PROJECTION_REMOVED, PROJECTION_REPLACED]:
151                self.proj_file.Subscribe(msg, self.subscribe_with_params, msg)
152    
153        def tearDown(self):
154            self.clear_messages()
155            self.proj_file.Destroy()
156    
157      def test_add_remove(self):      def test_add_remove(self):
158          """Test ProjFile.Add() and ProjFile.Remove()"""          """Test ProjFile.Add() and ProjFile.Remove()"""
159          proj_file = ProjFile("some_filename")          self.proj_file.Add(self.proj0)
160          proj_file.Add(self.proj0)          self.proj_file.Add(self.proj1)
161          proj_file.Add(self.proj1)          self.assertEquals(self.proj_file.GetProjections(),
162          self.assertEquals(proj_file.GetProjections(), [self.proj0, self.proj1])                            [self.proj0, self.proj1])
163          proj_file.Remove(self.proj0)          self.check_messages([(self.proj0, PROJECTION_ADDED),
164          self.assertEquals(proj_file.GetProjections(), [self.proj1])                               (self.proj1, PROJECTION_ADDED)])
165            self.clear_messages()
166    
167            self.proj_file.Remove(self.proj0)
168            self.assertEquals(self.proj_file.GetProjections(), [self.proj1])
169            self.check_messages([(self.proj0, PROJECTION_REMOVED)])
170    
171      def test_remove_non_existing(self):      def test_remove_non_existing(self):
172          """Test ProjFile.Remove(<proj not in projfile>)"""          """Test ProjFile.Remove(<proj not in projfile>)"""
173          proj_file = ProjFile("some_filename")          self.assertRaises(ValueError, self.proj_file.Remove, self.proj0)
174          self.assertRaises(ValueError, proj_file.Remove, self.proj0)          # Nothing happened, so no messages should have been sent
175            self.check_messages([])
176    
177      def test_replace(self):      def test_replace(self):
178          """Test ProjFile.Replace()"""          """Test ProjFile.Replace()"""
179          proj_file = ProjFile("some_filename")          self.proj_file.Add(self.proj0)
180          proj_file.Add(self.proj0)          self.proj_file.Add(self.proj1)
181          proj_file.Add(self.proj1)          self.clear_messages()
182    
183          # Replace()          # Replace()
184          proj_file.Replace(self.proj0, self.proj2)          self.proj_file.Replace(self.proj0, self.proj2)
185          self.assertEquals(proj_file.GetProjections(), [self.proj2, self.proj1])          self.assertEquals(self.proj_file.GetProjections(),
186                              [self.proj2, self.proj1])
187            self.check_messages([(self.proj0, self.proj2, PROJECTION_REPLACED)])
188    
189      def test_replace_non_existing(self):      def test_replace_non_existing(self):
190          """Test ProjFile.Replace(<proj not in projfile>, <some proj>)"""          """Test ProjFile.Replace(<proj not in projfile>, <some proj>)"""
191          proj_file = ProjFile("some_filename")          self.proj_file.Add(self.proj0)
192          proj_file.Add(self.proj0)          self.proj_file.Add(self.proj1)
193          proj_file.Add(self.proj1)          self.clear_messages()
194          self.assertRaises(ValueError,          self.assertRaises(ValueError,
195                            proj_file.Replace, self.proj2, self.proj0)                            self.proj_file.Replace, self.proj2, self.proj0)
196            # All projections should still be there
197            self.assertEquals(self.proj_file.GetProjections(),
198                              [self.proj0, self.proj1])
199            # Nothing happened, so no messages should have been sent
200            self.check_messages([])
201    
202    
203  class ProjFileTest(unittest.TestCase, support.FileTestMixin,  class ProjFileTest(unittest.TestCase, support.FileTestMixin,
# Line 186  class ProjFileTest(unittest.TestCase, su Line 212  class ProjFileTest(unittest.TestCase, su
212    
213  class ProjFileReadTests(ProjFileTest):  class ProjFileReadTests(ProjFileTest):
214    
215      """Test read ProjFile objects from files"""      """Test read ProjFile objects from files
216    
217        The files only cover error handling and the system projection file.
218        """
219    
220      def test_read_non_existing_file(self):      def test_read_non_existing_file(self):
221          """Test read_proj_file with non-existing file"""          """Test read_proj_file with non-existing file"""
# Line 200  class ProjFileReadTests(ProjFileTest): Line 229  class ProjFileReadTests(ProjFileTest):
229          As currently written this only works on unix-like systems and          As currently written this only works on unix-like systems and
230          not e.g. on MS Windows.          not e.g. on MS Windows.
231          """          """
232            if os.name != "posix":
233                raise support.SkipTest("Test only works on posix systems")
234          filename = self.filename()          filename = self.filename()
235          file = open(filename, "w")          file = open(filename, "w")
236          file.close()          file.close()
# Line 215  class ProjFileReadTests(ProjFileTest): Line 246  class ProjFileReadTests(ProjFileTest):
246          self.assertRaises(SAXParseException, resource.read_proj_file, filename)          self.assertRaises(SAXParseException, resource.read_proj_file, filename)
247    
248      def test_get_system_proj_file(self):      def test_get_system_proj_file(self):
249          """Test resource.get_system_proj_file()          """Test resource.get_system_proj_file(DEFAULT_PROJ_FILE)
250    
251          This is primarily to test whether the system proj file contains          This is primarily to test whether the system proj file contains
252          invalid projection paramers and whether the proj file is not          invalid projection paramers and whether the proj file is not
253          empty          empty
254          """          """
255          projfile, warnings = resource.get_system_proj_file()          projfile, warnings\
256                      = resource.get_system_proj_file(resource.DEFAULT_PROJ_FILE)
257          self.assertEquals(warnings, [])          self.assertEquals(warnings, [])
258          self.assert_(len(projfile.GetProjections()) > 0)          self.assert_(len(projfile.GetProjections()) > 0)
259    
260            # see whether it got cached and we get the same projfile object
261            # when we read the file again
262            projfile2, warnings \
263                       = resource.get_system_proj_file(resource.DEFAULT_PROJ_FILE)
264            self.assert_(projfile is projfile2)
265    
266    
267  class WriteProjFileTests(ProjFileTest):  class WriteProjFileTests(ProjFileTest):
268    
# Line 293  class WriteProjFileTests(ProjFileTest): Line 331  class WriteProjFileTests(ProjFileTest):
331          self.doTestWrite(pf, file_contents)          self.doTestWrite(pf, file_contents)
332    
333    
334  class TestLoadingProjFile(support.FileLoadTestCase):  class ProjFileLoadTestCase(support.FileLoadTestCase):
335    
336        """Base class for the test cases that read specific test files"""
337    
338      file_extension = ".proj"      file_extension = ".proj"
339    
340        def tearDown(self):
341            """Clear the cache explicitly"""
342            resource.clear_proj_file_cache()
343    
344    
345    class TestLoadingProjFile(ProjFileLoadTestCase):
346    
347      file_contents = '''\      file_contents = '''\
348  <?xml version="1.0" encoding="UTF-8"?>  <?xml version="1.0" encoding="UTF-8"?>
349  <!DOCTYPE projectionlist SYSTEM "projfile.dtd">  <!DOCTYPE projectionlist SYSTEM "projfile.dtd">
# Line 347  class TestLoadingProjFile(support.FileLo Line 395  class TestLoadingProjFile(support.FileLo
395                                 "proj=tmerc", "units=m",                                 "proj=tmerc", "units=m",
396                                 "x_0=400000.000", "y_0=0.000"])                                 "x_0=400000.000", "y_0=0.000"])
397    
398        def test_caching(self):
399            # test whether the projfile cache works
400            projfile, warnings = resource.read_proj_file(self.filename())
401            projfile2, warnings = resource.read_proj_file(self.filename())
402    
403  class TestLoadingProjFileWithEmptyProjectionlist(support.FileLoadTestCase):          # Both projfiles should be the same object
404            self.assert_(projfile2 is projfile)
405    
406            # If we clear the cache we should get a new one.
407            resource.clear_proj_file_cache()
408            projfile3, warnings = resource.read_proj_file(self.filename())
409            self.assert_(projfile3 is not projfile)
410    
411    
412    class TestLoadingProjFileWithEmptyProjectionlist(ProjFileLoadTestCase):
413    
     file_extension = ".proj"  
414      file_contents = '''\      file_contents = '''\
415  <?xml version="1.0" encoding="UTF-8"?>  <?xml version="1.0" encoding="UTF-8"?>
416  <!DOCTYPE projectionlist SYSTEM "projfile.dtd">  <!DOCTYPE projectionlist SYSTEM "projfile.dtd">
# Line 367  class TestLoadingProjFileWithEmptyProjec Line 427  class TestLoadingProjFileWithEmptyProjec
427          self.assertEquals(len(projfile.GetProjections()), 0)          self.assertEquals(len(projfile.GetProjections()), 0)
428    
429    
430  class TestProjFileWithInvalidParameters(unittest.TestCase,  class TestProjFileWithInvalidParameters(ProjFileLoadTestCase):
                                         support.FileLoadTestCase):  
431    
     file_extension = ".proj"  
432      file_contents = '''\      file_contents = '''\
433  <?xml version="1.0" encoding="UTF-8"?>  <?xml version="1.0" encoding="UTF-8"?>
434  <!DOCTYPE projectionlist SYSTEM "projfile.dtd">  <!DOCTYPE projectionlist SYSTEM "projfile.dtd">
# Line 410  class TestProjFileWithInvalidParameters( Line 468  class TestProjFileWithInvalidParameters(
468    
469    
470  if __name__ == "__main__":  if __name__ == "__main__":
471      unittest.main()      support.run_tests()

Legend:
Removed from v.1821  
changed lines
  Added in v.1966

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26