17 |
import unittest |
import unittest |
18 |
from StringIO import StringIO |
from StringIO import StringIO |
19 |
|
|
|
import xml.sax |
|
|
import xml.sax.handler |
|
|
from xml.sax import make_parser, ErrorHandler, SAXNotRecognizedException |
|
|
|
|
20 |
import xmlsupport |
import xmlsupport |
21 |
|
import postgissupport |
22 |
|
|
23 |
import support |
import support |
24 |
support.initthuban() |
support.initthuban() |
40 |
|
|
41 |
from Thuban.Model.range import Range |
from Thuban.Model.range import Range |
42 |
|
|
43 |
|
from Thuban.Model.postgisdb import PostGISConnection, PostGISShapeStore |
44 |
|
|
45 |
|
|
46 |
class XMLWriterTest(unittest.TestCase): |
class XMLWriterTest(unittest.TestCase): |
47 |
|
|
65 |
class SaveSessionTest(unittest.TestCase, support.FileTestMixin, |
class SaveSessionTest(unittest.TestCase, support.FileTestMixin, |
66 |
xmlsupport.ValidationTest): |
xmlsupport.ValidationTest): |
67 |
|
|
68 |
dtd = "http://thuban.intevation.org/dtds/thuban-0.9-dev.dtd" |
dtd = "http://thuban.intevation.org/dtds/thuban-0.9.dtd" |
69 |
thubanids = [((dtd, n), (None, "id")) for n in |
thubanids = [((dtd, n), (None, "id")) for n in |
70 |
["fileshapesource", "filetable", "jointable", |
["fileshapesource", "filetable", "jointable", |
71 |
"derivedshapesource"]] |
"derivedshapesource", "dbshapesource", "dbconnection"]] |
72 |
thubanidrefs = [((dtd, n), (None, m)) for n, m in |
thubanidrefs = [((dtd, n), (None, m)) for n, m in |
73 |
[("layer", "shapestore"), |
[("layer", "shapestore"), |
74 |
("jointable", "left"), |
("jointable", "left"), |
75 |
("jointable", "right"), |
("jointable", "right"), |
76 |
("derivedshapesource", "table"), |
("derivedshapesource", "table"), |
77 |
("derivedshapesource", "shapesource")]] |
("derivedshapesource", "shapesource"), |
78 |
|
("dbshapesource", "dbconn")]] |
79 |
del n, m, dtd |
del n, m, dtd |
80 |
|
|
81 |
def compare_xml(self, xml1, xml2): |
def compare_xml(self, xml1, xml2): |
103 |
'<?xml version="1.0" encoding="UTF-8"?>\n' |
'<?xml version="1.0" encoding="UTF-8"?>\n' |
104 |
'<!DOCTYPE session SYSTEM "thuban-0.9.dtd">\n' |
'<!DOCTYPE session SYSTEM "thuban-0.9.dtd">\n' |
105 |
'<session title="empty session" ' |
'<session title="empty session" ' |
106 |
'xmlns="http://thuban.intevation.org/dtds/thuban-0.9-dev.dtd">' |
'xmlns="http://thuban.intevation.org/dtds/thuban-0.9.dtd">' |
107 |
'\n</session>\n') |
'\n</session>\n') |
108 |
|
|
109 |
self.validate_data(written_contents) |
self.validate_data(written_contents) |
130 |
expected_template = '''<?xml version="1.0" encoding="UTF-8"?> |
expected_template = '''<?xml version="1.0" encoding="UTF-8"?> |
131 |
<!DOCTYPE session SYSTEM "thuban-0.9.dtd"> |
<!DOCTYPE session SYSTEM "thuban-0.9.dtd"> |
132 |
<session title="single map&layer" |
<session title="single map&layer" |
133 |
xmlns="http://thuban.intevation.org/dtds/thuban-0.9-dev.dtd"> |
xmlns="http://thuban.intevation.org/dtds/thuban-0.9.dtd"> |
134 |
<fileshapesource id="D1" filename="%s" filetype="shapefile"/> |
<fileshapesource id="D1" filename="%s" filetype="shapefile"/> |
135 |
<map title="Test Map"> |
<map title="Test Map"> |
136 |
<projection name="Unknown"> |
<projection name="Unknown"> |
190 |
expected_contents = '''<?xml version="1.0" encoding="UTF-8"?> |
expected_contents = '''<?xml version="1.0" encoding="UTF-8"?> |
191 |
<!DOCTYPE session SYSTEM "thuban-0.9.dtd"> |
<!DOCTYPE session SYSTEM "thuban-0.9.dtd"> |
192 |
<session title="single map&layer" |
<session title="single map&layer" |
193 |
xmlns="http://thuban.intevation.org/dtds/thuban-0.9-dev.dtd"> |
xmlns="http://thuban.intevation.org/dtds/thuban-0.9.dtd"> |
194 |
<fileshapesource id="D1" filename="%s" filetype="shapefile"/> |
<fileshapesource id="D1" filename="%s" filetype="shapefile"/> |
195 |
<map title="Test Map"> |
<map title="Test Map"> |
196 |
<projection name="Unknown"> |
<projection name="Unknown"> |
236 |
expected_contents = '''<?xml version="1.0" encoding="UTF-8"?> |
expected_contents = '''<?xml version="1.0" encoding="UTF-8"?> |
237 |
<!DOCTYPE session SYSTEM "thuban-0.9.dtd"> |
<!DOCTYPE session SYSTEM "thuban-0.9.dtd"> |
238 |
<session title="single map&layer" |
<session title="single map&layer" |
239 |
xmlns="http://thuban.intevation.org/dtds/thuban-0.9-dev.dtd"> |
xmlns="http://thuban.intevation.org/dtds/thuban-0.9.dtd"> |
240 |
<map title="Test Map"> |
<map title="Test Map"> |
241 |
<rasterlayer title="My RasterLayer" filename="%s" |
<rasterlayer title="My RasterLayer" filename="%s" |
242 |
visible="true"> |
visible="true"> |
303 |
expected_template = '''<?xml version="1.0" encoding="UTF-8"?> |
expected_template = '''<?xml version="1.0" encoding="UTF-8"?> |
304 |
<!DOCTYPE session SYSTEM "thuban-0.9.dtd"> |
<!DOCTYPE session SYSTEM "thuban-0.9.dtd"> |
305 |
<session title="Map with Classifications" |
<session title="Map with Classifications" |
306 |
xmlns="http://thuban.intevation.org/dtds/thuban-0.9-dev.dtd"> |
xmlns="http://thuban.intevation.org/dtds/thuban-0.9.dtd"> |
307 |
<fileshapesource id="D1" filename="%s" filetype="shapefile"/> |
<fileshapesource id="D1" filename="%s" filetype="shapefile"/> |
308 |
<map title="Test Map"> |
<map title="Test Map"> |
309 |
<projection name="Unknown"> |
<projection name="Unknown"> |
376 |
expected_template = '''<?xml version="1.0" encoding="UTF-8"?> |
expected_template = '''<?xml version="1.0" encoding="UTF-8"?> |
377 |
<!DOCTYPE session SYSTEM "thuban-0.9.dtd"> |
<!DOCTYPE session SYSTEM "thuban-0.9.dtd"> |
378 |
<session title="a DBF Table session" |
<session title="a DBF Table session" |
379 |
xmlns="http://thuban.intevation.org/dtds/thuban-0.9-dev.dtd"> |
xmlns="http://thuban.intevation.org/dtds/thuban-0.9.dtd"> |
380 |
<filetable id="D1" filename="%s" filetype="DBF" title="political"/> |
<filetable id="D1" filename="%s" filetype="DBF" title="political"/> |
381 |
</session>''' |
</session>''' |
382 |
|
|
384 |
% os.path.join(os.pardir, os.pardir, "Data", |
% os.path.join(os.pardir, os.pardir, "Data", |
385 |
"iceland", "political.dbf")) |
"iceland", "political.dbf")) |
386 |
self.compare_xml(written_contents, expected_contents) |
self.compare_xml(written_contents, expected_contents) |
387 |
|
self.validate_data(written_contents) |
388 |
|
|
389 |
def test_joined_table(self): |
def test_joined_table(self): |
390 |
"""Test saving a session with joined table""" |
"""Test saving a session with joined table""" |
436 |
expected_template = '''<?xml version="1.0" encoding="UTF-8"?> |
expected_template = '''<?xml version="1.0" encoding="UTF-8"?> |
437 |
<!DOCTYPE session SYSTEM "thuban-0.9.dtd"> |
<!DOCTYPE session SYSTEM "thuban-0.9.dtd"> |
438 |
<session title="A Joined Table session" |
<session title="A Joined Table session" |
439 |
xmlns="http://thuban.intevation.org/dtds/thuban-0.9-dev.dtd"> |
xmlns="http://thuban.intevation.org/dtds/thuban-0.9.dtd"> |
440 |
<fileshapesource filename="%(shpfile)s" |
<fileshapesource filename="%(shpfile)s" |
441 |
filetype="shapefile" id="D142197204"/> |
filetype="shapefile" id="D142197204"/> |
442 |
<filetable filename="%(dbffile)s" |
<filetable filename="%(dbffile)s" |
462 |
"shpfile": relative_filename(self.temp_dir(), shpfile) |
"shpfile": relative_filename(self.temp_dir(), shpfile) |
463 |
} |
} |
464 |
self.compare_xml(written_contents, expected_contents) |
self.compare_xml(written_contents, expected_contents) |
465 |
|
self.validate_data(written_contents) |
466 |
finally: |
finally: |
467 |
session.Destroy() |
session.Destroy() |
468 |
session = None |
session = None |
469 |
|
|
470 |
|
|
471 |
|
def test_save_postgis(self): |
472 |
|
"""Test saving a session with a postgis connection""" |
473 |
|
|
474 |
|
class NonConnection(PostGISConnection): |
475 |
|
"""connection class that doesn't actually connect """ |
476 |
|
def connect(self): |
477 |
|
pass |
478 |
|
|
479 |
|
class NonConnectionStore(PostGISShapeStore): |
480 |
|
"""Shapestore that doesn't try to access the server""" |
481 |
|
def _fetch_table_information(self): |
482 |
|
pass |
483 |
|
|
484 |
|
session = Session("A PostGIS Session") |
485 |
|
try: |
486 |
|
dbconn = NonConnection(dbname="plugh", host="xyzzy", port="42", |
487 |
|
user="grue") |
488 |
|
session.AddDBConnection(dbconn) |
489 |
|
map = Map("Test Map") |
490 |
|
session.AddMap(map) |
491 |
|
store = NonConnectionStore(dbconn, "roads") |
492 |
|
session.AddShapeStore(store) |
493 |
|
layer = Layer("Roads to Nowhere", store) |
494 |
|
map.AddLayer(layer) |
495 |
|
|
496 |
|
# Save the session |
497 |
|
filename = self.temp_file_name(self.id() + ".thuban") |
498 |
|
save_session(session, filename) |
499 |
|
|
500 |
|
# Read it back and compare |
501 |
|
file = open(filename) |
502 |
|
written = file.read() |
503 |
|
file.close() |
504 |
|
expected = '''<?xml version="1.0" encoding="UTF-8"?> |
505 |
|
<!DOCTYPE session SYSTEM "thuban-0.9.dtd"> |
506 |
|
<session title="A PostGIS Session" |
507 |
|
xmlns="http://thuban.intevation.org/dtds/thuban-0.9.dtd"> |
508 |
|
<dbconnection id="DB" |
509 |
|
dbtype="postgis" dbname="plugh" |
510 |
|
host="xyzzy" port="42" |
511 |
|
user="grue"/> |
512 |
|
<dbshapesource id="roads" dbconn="DB" tablename="roads"/> |
513 |
|
<map title="Test Map"> |
514 |
|
<layer title="Roads to Nowhere" |
515 |
|
shapestore="roads" visible="true" |
516 |
|
stroke="#000000" stroke_width="1" fill="None"/> |
517 |
|
</map> |
518 |
|
</session>''' |
519 |
|
self.compare_xml(written, expected) |
520 |
|
self.validate_data(written) |
521 |
|
finally: |
522 |
|
session.Destroy() |
523 |
|
|
524 |
|
|
525 |
class MockDataStore: |
class MockDataStore: |
526 |
|
|
527 |
"""A very simple data store that only has dependencies""" |
"""A very simple data store that only has dependencies""" |