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 |
131 |
self.assertEquals(proj_file.GetFilename(), "other_name") |
self.assertEquals(proj_file.GetFilename(), "other_name") |
132 |
|
|
133 |
|
|
134 |
class TestProjFile(unittest.TestCase): |
class TestProjFile(unittest.TestCase, support.SubscriberMixin): |
135 |
|
|
136 |
"""Test cases for reading and writing projection files. |
"""Test cases for ProjFile objects""" |
|
""" |
|
137 |
|
|
138 |
def setUp(self): |
def setUp(self): |
139 |
|
self.clear_messages() |
140 |
self.proj0 = Projection(["proj=tmerc", "ellps=clrk66"]) |
self.proj0 = Projection(["proj=tmerc", "ellps=clrk66"]) |
141 |
self.proj1 = Projection(["proj=utm", "ellps=clrk66"]) |
self.proj1 = Projection(["proj=utm", "ellps=clrk66"]) |
142 |
self.proj2 = Projection(["proj=lcc", "ellps=clrk66", |
self.proj2 = Projection(["proj=lcc", "ellps=clrk66", |
143 |
"lat_1=0", "lat_2=20"]) |
"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): |
def test_add_remove(self): |
153 |
"""Test ProjFile.Add() and ProjFile.Remove()""" |
"""Test ProjFile.Add() and ProjFile.Remove()""" |
154 |
proj_file = ProjFile("some_filename") |
self.proj_file.Add(self.proj0) |
155 |
proj_file.Add(self.proj0) |
self.proj_file.Add(self.proj1) |
156 |
proj_file.Add(self.proj1) |
self.assertEquals(self.proj_file.GetProjections(), |
157 |
self.assertEquals(proj_file.GetProjections(), [self.proj0, self.proj1]) |
[self.proj0, self.proj1]) |
158 |
proj_file.Remove(self.proj0) |
self.check_messages([(self.proj0, PROJECTION_ADDED), |
159 |
self.assertEquals(proj_file.GetProjections(), [self.proj1]) |
(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): |
def test_remove_non_existing(self): |
167 |
"""Test ProjFile.Remove(<proj not in projfile>)""" |
"""Test ProjFile.Remove(<proj not in projfile>)""" |
168 |
proj_file = ProjFile("some_filename") |
self.assertRaises(ValueError, self.proj_file.Remove, self.proj0) |
169 |
self.assertRaises(ValueError, proj_file.Remove, self.proj0) |
# Nothing happened, so no messages should have been sent |
170 |
|
self.check_messages([]) |
171 |
|
|
172 |
def test_replace(self): |
def test_replace(self): |
173 |
"""Test ProjFile.Replace()""" |
"""Test ProjFile.Replace()""" |
174 |
proj_file = ProjFile("some_filename") |
self.proj_file.Add(self.proj0) |
175 |
proj_file.Add(self.proj0) |
self.proj_file.Add(self.proj1) |
176 |
proj_file.Add(self.proj1) |
self.clear_messages() |
177 |
|
|
178 |
# Replace() |
# Replace() |
179 |
proj_file.Replace(self.proj0, self.proj2) |
self.proj_file.Replace(self.proj0, self.proj2) |
180 |
self.assertEquals(proj_file.GetProjections(), [self.proj2, self.proj1]) |
self.assertEquals(self.proj_file.GetProjections(), |
181 |
|
[self.proj2, self.proj1]) |
182 |
|
self.check_messages([(self.proj0, self.proj2, PROJECTION_REPLACED)]) |
183 |
|
|
184 |
def test_replace_non_existing(self): |
def test_replace_non_existing(self): |
185 |
"""Test ProjFile.Replace(<proj not in projfile>, <some proj>)""" |
"""Test ProjFile.Replace(<proj not in projfile>, <some proj>)""" |
186 |
proj_file = ProjFile("some_filename") |
self.proj_file.Add(self.proj0) |
187 |
proj_file.Add(self.proj0) |
self.proj_file.Add(self.proj1) |
188 |
proj_file.Add(self.proj1) |
self.clear_messages() |
189 |
self.assertRaises(ValueError, |
self.assertRaises(ValueError, |
190 |
proj_file.Replace, self.proj2, self.proj0) |
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, |
class ProjFileTest(unittest.TestCase, support.FileTestMixin, |
207 |
|
|
208 |
class ProjFileReadTests(ProjFileTest): |
class ProjFileReadTests(ProjFileTest): |
209 |
|
|
210 |
"""Test read ProjFile objects from files""" |
"""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""" |
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 |
|
|
258 |
class WriteProjFileTests(ProjFileTest): |
class WriteProjFileTests(ProjFileTest): |
259 |
|
|
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"> |
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 |
|
# Both projfiles should be the same object |
395 |
|
self.assert_(projfile2 is projfile) |
396 |
|
|
397 |
class TestLoadingProjFileWithEmptyProjectionlist(support.FileLoadTestCase): |
# 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"> |
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"> |
459 |
|
|
460 |
|
|
461 |
if __name__ == "__main__": |
if __name__ == "__main__": |
462 |
unittest.main() |
support.run_tests() |