/[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 698 by jonathan, Wed Apr 16 16:40:18 2003 UTC revision 1687 by bh, Fri Aug 29 10:02:16 2003 UTC
# Line 1  Line 1 
1  # Copyright (c) 2002 by Intevation GmbH  # Copyright (c) 2002, 2003 by Intevation GmbH
2  # Authors:  # Authors:
3  # Bernhard Herzog <[email protected]>  # Bernhard Herzog <[email protected]>
4  #  #
# Line 14  __version__ = "$Revision$" Line 14  __version__ = "$Revision$"
14  # $Id$  # $Id$
15    
16  import unittest  import unittest
17    import os
18    
19  import support  import support
20  support.initthuban()  support.initthuban()
21    
22    from Thuban import _
23  from Thuban.Model.proj import Projection, ProjFile  from Thuban.Model.proj import Projection, ProjFile
24    
25  import Thuban.Model.resource as resource  import Thuban.Model.resource as resource
26    
27  from test_save import sax_eventlist  from xmlsupport import sax_eventlist
28    
29    from xml.sax import SAXParseException
30    
31    
32  class TestProjection(unittest.TestCase, support.FloatComparisonMixin):  class TestProjection(unittest.TestCase, support.FloatComparisonMixin):
# Line 32  class TestProjection(unittest.TestCase, Line 36  class TestProjection(unittest.TestCase,
36    
37      def test(self):      def test(self):
38          """Test Projection"""          """Test Projection"""
39          proj = Projection(["zone=26", "proj=utm", "ellps=clrk66"])          params = ["zone=26", "proj=utm", "ellps=clrk66"]
40          self.assertEquals(proj.params, ["zone=26", "proj=utm", "ellps=clrk66"])          proj = Projection(params)
41            self.assertEquals(proj.params, params)
42    
43          # It's not clear whether this value is really the correct one          # It's not clear whether this value is really the correct one
44          # but a test failure here probably still means a bug somewhere          # but a test failure here probably still means a bug somewhere
# Line 48  class TestProjection(unittest.TestCase, Line 53  class TestProjection(unittest.TestCase,
53                                    3875381.8535437919, 252962.10480170773),                                    3875381.8535437919, 252962.10480170773),
54                                   epsilon = 1e-5)                                   epsilon = 1e-5)
55    
56            # GetName()
57            self.assertEquals(proj.GetName(), _("Unknown"))
58    
59            # GetParameter()
60            self.assertEquals(proj.GetParameter("zone"), "26")
61            self.assertEquals(proj.GetParameter("proj"), "utm")
62            self.assertEquals(proj.GetParameter("ellps"), "clrk66")
63            self.assertEquals(proj.GetParameter("hallo"), "")
64    
65            # GetAllParameters()
66            self.assertEquals(proj.GetAllParameters(), params)
67    
68            # GetName()
69            proj = Projection(params, "MyName")
70            self.assertEquals(proj.GetName(), "MyName")
71    
72    
73  sample_projfile = '''\  sample_projfile = '''\
# Line 107  class TestProjFile(unittest.TestCase, su Line 127  class TestProjFile(unittest.TestCase, su
127      def compare_xml(self, xml1, xml2):      def compare_xml(self, xml1, xml2):
128          self.assertEquals(sax_eventlist(xml1), sax_eventlist(xml2))          self.assertEquals(sax_eventlist(xml1), sax_eventlist(xml2))
129    
130        def test(self):
131            """Test ProjFile"""
132    
133            proj0 = Projection(["proj=tmerc", "ellps=clrk66"])
134            proj1 = Projection(["proj=utm", "ellps=clrk66"])
135            proj2 = Projection(["proj=lcc", "ellps=clrk66",
136                                "lat_1=0", "lat_2=20"])
137    
138            eq = self.assertEquals
139    
140    
141            #
142            # __init__()
143            # GetFilename()
144            # SetFilename()
145            #
146            for name in ["", "hello_world"]:
147                projFile = ProjFile(name)
148                eq(projFile.GetFilename(), name)
149    
150                projFile.SetFilename("XXX")
151                projFile.SetFilename(name)
152                eq(projFile.GetFilename(), name)
153    
154            # initial number of projections should be 0
155            eq(len(projFile.GetProjections()), 0)
156    
157            #
158            # Add()
159            # Remove()
160            #
161            projFile.Add(proj0)
162            eq(len(projFile.GetProjections()), 1)
163            projFile.Remove(proj0)
164            eq(len(projFile.GetProjections()), 0)
165    
166            # try to remove something that doesn't exist
167            self.assertRaises(ValueError, projFile.Remove, None)
168            self.assertRaises(ValueError, projFile.Remove, proj0)
169    
170            projFile.Add(proj0)
171            projFile.Add(proj1)
172            projFile.Add(proj2)
173            eq(len(projFile.GetProjections()), 3)
174    
175            # GetProjections() -- tests order
176            projs = projFile.GetProjections()
177            eq(projs[0], proj0)
178            eq(projs[1], proj1)
179            eq(projs[2], proj2)
180    
181            projFile.Remove(proj2)
182            projFile.Remove(proj1)
183    
184            # Replace()
185            projFile.Replace(proj0, proj1)
186            projs = projFile.GetProjections()
187            eq(projs[0], proj1)
188    
189            # replace a non-existent projection
190            self.assertRaises(ValueError, projFile.Replace, None, proj2)
191            self.assertRaises(ValueError, projFile.Replace, proj0, proj2)
192    
193      def testRead(self):      def testRead(self):
194          """Test ReadProjFile"""          """Test read_proj_file"""
195    
196          self.doTestRead(sample_projfile_data, sample_projfile)          self.doTestRead(sample_projfile_data, sample_projfile)
197          self.doTestRead(sample_projfile_data2, sample_projfile2)          self.doTestRead(sample_projfile_data2, sample_projfile2)
198    
199            #
200            # file doesn't exist
201            #
202            self.assertRaises(IOError,
203                resource.read_proj_file, self.temp_file_name("nonexistent.proj"))
204    
205            #
206            # file isn't readable
207            #
208            filename = self.temp_file_name("projfile.proj")
209            file = open(filename, "w")
210            file.close()
211            os.chmod(filename, 0200) # write-only
212            self.assertRaises(IOError, resource.read_proj_file, filename)
213            os.chmod(filename, 0600) # read/write so we reuse the file
214    
215            #
216            # file has invalid XML (or none at all)
217            #
218            filename = self.temp_file_name("projfile.proj")
219            file = open(filename, "w")
220            file.close()
221    
222            self.assertRaises(SAXParseException, resource.read_proj_file, filename)
223    
224      def testWrite(self):      def testWrite(self):
225          """Test WriteProjFile"""          """Test write_proj_file"""
226    
227          self.doTestWrite(sample_projfile_data, sample_projfile)          self.doTestWrite(sample_projfile_data, sample_projfile)
228          self.doTestWrite(sample_projfile_data2, sample_projfile2)          self.doTestWrite(sample_projfile_data2, sample_projfile2)
# Line 127  class TestProjFile(unittest.TestCase, su Line 235  class TestProjFile(unittest.TestCase, su
235          for proj in data:          for proj in data:
236              pf.Add(Projection(proj[1], proj[0]))              pf.Add(Projection(proj[1], proj[0]))
237    
238          resource.WriteProjFile(pf)          resource.write_proj_file(pf)
239    
240          file = open(filename)          file = open(filename)
241          written_contents = file.read()          written_contents = file.read()
# Line 141  class TestProjFile(unittest.TestCase, su Line 249  class TestProjFile(unittest.TestCase, su
249          file.write(input)          file.write(input)
250          file.close()          file.close()
251    
252          pf = resource.ReadProjFile(filename)          pf = resource.read_proj_file(filename)
253    
254          eq = self.assertEquals          eq = self.assertEquals
255    
256          eq(pf.GetFileName(), filename)          eq(pf.GetFilename(), filename)
257    
258          for proj, d in zip(pf.GetProjections(), data):          for proj, d in zip(pf.GetProjections(), data):
259              eq(proj.GetName(), d[0])              eq(proj.GetName(), d[0])
260              for param in proj.GetParameters():              for param in proj.GetAllParameters():
261                  self.assert_(param in d[1])                  self.assert_(param in d[1])
262    
263    

Legend:
Removed from v.698  
changed lines
  Added in v.1687

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26