34 |
import support |
import support |
35 |
support.initthuban() |
support.initthuban() |
36 |
|
|
37 |
|
import postgissupport |
38 |
from xmlsupport import sax_eventlist |
from xmlsupport import sax_eventlist |
39 |
|
|
40 |
import dbflib |
import dbflib |
41 |
|
|
42 |
from Thuban.Model.save import save_session |
from Thuban.Model.save import save_session |
43 |
from Thuban.Model.load import load_session, parse_color, LoadError |
from Thuban.Model.load import load_session, parse_color, LoadError, \ |
44 |
from Thuban.Model.color Transparent |
LoadCancelled |
45 |
|
from Thuban.Model.color import Transparent |
46 |
from Thuban.Model.classification import ClassGroupProperties, ClassGroupRange,\ |
from Thuban.Model.classification import ClassGroupProperties, ClassGroupRange,\ |
47 |
ClassGroupSingleton, ClassGroupDefault |
ClassGroupSingleton, ClassGroupDefault |
48 |
|
from Thuban.Model.postgisdb import ConnectionError |
49 |
|
|
50 |
def filenames_equal(name1, name2): |
def filenames_equal(name1, name2): |
51 |
"""Return true if the filenames name1 and name2 are equal. |
"""Return true if the filenames name1 and name2 are equal. |
82 |
self.session = None |
self.session = None |
83 |
|
|
84 |
|
|
85 |
dtd = "http://thuban.intevation.org/dtds/thuban-0.8.dtd" |
dtd = "http://thuban.intevation.org/dtds/thuban-0.9.dtd" |
86 |
thubanids = [((dtd, n), (None, "id")) for n in |
thubanids = [((dtd, n), (None, "id")) for n in |
87 |
["fileshapesource", "filetable", "jointable", |
["fileshapesource", "filetable", "jointable", |
88 |
"derivedshapesource"]] |
"derivedshapesource"]] |
92 |
("jointable", "right"), |
("jointable", "right"), |
93 |
("derivedshapesource", "table"), |
("derivedshapesource", "table"), |
94 |
("derivedshapesource", "shapesource")]] |
("derivedshapesource", "shapesource")]] |
95 |
|
filenames = [((dtd, n), (None, m)) for n, m in |
96 |
|
[("fileshapesource", "filename"), |
97 |
|
("rasterlayer", "filename"), |
98 |
|
("filetable", "filename")]] |
99 |
del n, m, dtd |
del n, m, dtd |
100 |
|
|
101 |
def check_format(self): |
def check_format(self): |
106 |
filename = self.temp_file_name(self.id() + ".roundtrip.thuban") |
filename = self.temp_file_name(self.id() + ".roundtrip.thuban") |
107 |
save_session(self.session, filename) |
save_session(self.session, filename) |
108 |
el1 = sax_eventlist(filename = filename, ids = self.thubanids, |
el1 = sax_eventlist(filename = filename, ids = self.thubanids, |
109 |
idrefs = self.thubanidrefs) |
idrefs = self.thubanidrefs, |
110 |
|
filenames = self.filenames) |
111 |
el2 = sax_eventlist(filename = self.filename(), ids = self.thubanids, |
el2 = sax_eventlist(filename = self.filename(), ids = self.thubanids, |
112 |
idrefs = self.thubanidrefs) |
idrefs = self.thubanidrefs, |
113 |
|
filenames = self.filenames) |
114 |
if 0: |
if 0: |
115 |
for a, b in zip(el1, el2): |
for a, b in zip(el1, el2): |
116 |
print a != b and "***************" or "" |
print a != b and "***************" or "" |
158 |
if data[CLASSES][i][GROUP_TYPE] == "default": |
if data[CLASSES][i][GROUP_TYPE] == "default": |
159 |
g = ClassGroupDefault(props, data[CLASSES][i][GROUP_LABEL]) |
g = ClassGroupDefault(props, data[CLASSES][i][GROUP_LABEL]) |
160 |
elif data[CLASSES][i][GROUP_TYPE] == "range": |
elif data[CLASSES][i][GROUP_TYPE] == "range": |
161 |
g = ClassGroupRange(data[CLASSES][i][GROUP_DATA][0], |
g = ClassGroupRange((data[CLASSES][i][GROUP_DATA][0], |
162 |
data[CLASSES][i][GROUP_DATA][1], |
data[CLASSES][i][GROUP_DATA][1]), |
163 |
props, data[CLASSES][i][GROUP_LABEL]) |
props, data[CLASSES][i][GROUP_LABEL]) |
164 |
elif data[CLASSES][i][GROUP_TYPE] == "single": |
elif data[CLASSES][i][GROUP_TYPE] == "single": |
165 |
g = ClassGroupSingleton(data[CLASSES][i][GROUP_DATA], |
g = ClassGroupSingleton(data[CLASSES][i][GROUP_DATA], |
175 |
|
|
176 |
file_contents = '''\ |
file_contents = '''\ |
177 |
<?xml version="1.0" encoding="UTF-8"?> |
<?xml version="1.0" encoding="UTF-8"?> |
178 |
<!DOCTYPE session SYSTEM "thuban-0.8.dtd"> |
<!DOCTYPE session SYSTEM "thuban-0.9.dtd"> |
179 |
<session xmlns="http://thuban.intevation.org/dtds/thuban-0.8.dtd" |
<session xmlns="http://thuban.intevation.org/dtds/thuban-0.9.dtd" |
180 |
title="single map&layer"> |
title="single map&layer"> |
181 |
<fileshapesource filetype="shapefile" id="D1" |
<fileshapesource filetype="shapefile" id="D1" |
182 |
filename="../../Data/iceland/political.shp"/> |
filename="../../Data/iceland/political.shp"/> |
236 |
|
|
237 |
file_contents = '''\ |
file_contents = '''\ |
238 |
<?xml version="1.0" encoding="UTF-8"?> |
<?xml version="1.0" encoding="UTF-8"?> |
239 |
<!DOCTYPE session SYSTEM "thuban-0.8.dtd"> |
<!DOCTYPE session SYSTEM "thuban-0.9.dtd"> |
240 |
<session xmlns="http://thuban.intevation.org/dtds/thuban-0.8.dtd" |
<session xmlns="http://thuban.intevation.org/dtds/thuban-0.9.dtd" |
241 |
title="single map&layer"> |
title="single map&layer"> |
242 |
<fileshapesource filetype="shapefile" id="D1" |
<fileshapesource filetype="shapefile" id="D1" |
243 |
filename="../../Data/iceland/political.shp"/> |
filename="../../Data/iceland/political.shp"/> |
295 |
<clpoint value="1"> |
<clpoint value="1"> |
296 |
<cldata stroke="#000000" stroke_width="10" fill="None"/> |
<cldata stroke="#000000" stroke_width="10" fill="None"/> |
297 |
</clpoint> |
</clpoint> |
298 |
|
<clpoint value="\xc3\xa4\xc3\xb6\xc3\xbc" |
299 |
|
label="\xc3\x9cml\xc3\xa4uts"> |
300 |
|
<cldata fill="None" stroke="#000000" stroke_width="1"/> |
301 |
|
</clpoint> |
302 |
</classification> |
</classification> |
303 |
</layer> |
</layer> |
304 |
<layer title="My Layer 2" stroke_width="1" fill="None" |
<layer title="My Layer 2" stroke_width="1" fill="None" |
333 |
|
|
334 |
map = self.session.Maps()[0] # only one map in the sample |
map = self.session.Maps()[0] # only one map in the sample |
335 |
|
|
336 |
expected = [("My Layer", 2, |
expected = [("My Layer", 3, |
337 |
[("default", (), "", |
[("default", (), "", |
338 |
("#000000", 1, "None")), |
("#000000", 1, "None")), |
339 |
("single", "1", "", |
("single", "1", "", |
340 |
("#000000", 2, "None")), |
("#000000", 2, "None")), |
341 |
("single", "1", "", |
("single", "1", "", |
342 |
("#000000", 10, "None"))]), |
("#000000", 10, "None")), |
343 |
|
("single", "\xe4\xf6\xfc", "\xdcml\xe4uts", |
344 |
|
("#000000", 1, "None"))]), |
345 |
("My Layer 2", 4, |
("My Layer 2", 4, |
346 |
[("default", (), "", |
[("default", (), "", |
347 |
("#000000", 2, "None")), |
("#000000", 2, "None")), |
361 |
|
|
362 |
file_contents = '''\ |
file_contents = '''\ |
363 |
<?xml version="1.0" encoding="UTF-8"?> |
<?xml version="1.0" encoding="UTF-8"?> |
364 |
<!DOCTYPE session SYSTEM "thuban-0.8.dtd"> |
<!DOCTYPE session SYSTEM "thuban-0.9.dtd"> |
365 |
<session xmlns="http://thuban.intevation.org/dtds/thuban-0.8.dtd" |
<session xmlns="http://thuban.intevation.org/dtds/thuban-0.9.dtd" |
366 |
title="single map&layer"> |
title="single map&layer"> |
367 |
<fileshapesource filetype="shapefile" id="D1" |
<fileshapesource filetype="shapefile" id="D1" |
368 |
filename="../../Data/iceland/political.shp"/> |
filename="../../Data/iceland/political.shp"/> |
409 |
|
|
410 |
file_contents = '''\ |
file_contents = '''\ |
411 |
<?xml version="1.0" encoding="UTF-8"?> |
<?xml version="1.0" encoding="UTF-8"?> |
412 |
<!DOCTYPE session SYSTEM "thuban-0.8.dtd"> |
<!DOCTYPE session SYSTEM "thuban-0.9.dtd"> |
413 |
<session xmlns="http://thuban.intevation.org/dtds/thuban-0.8.dtd" |
<session xmlns="http://thuban.intevation.org/dtds/thuban-0.9.dtd" |
414 |
title="single map&layer"> |
title="single map&layer"> |
415 |
<fileshapesource filetype="shapefile" id="D2" |
<fileshapesource filetype="shapefile" id="D2" |
416 |
filename="../../Data/iceland/roads-line.shp"/> |
filename="../../Data/iceland/roads-line.shp"/> |
442 |
title="My Layer" stroke_width="1" fill="None"> |
title="My Layer" stroke_width="1" fill="None"> |
443 |
<projection name="Unknown"> |
<projection name="Unknown"> |
444 |
<parameter value="proj=lcc"/> |
<parameter value="proj=lcc"/> |
445 |
|
<parameter value="lat_1=10"/> |
446 |
|
<parameter value="lat_2=20"/> |
447 |
<parameter value="ellps=clrk66"/> |
<parameter value="ellps=clrk66"/> |
448 |
</projection> |
</projection> |
449 |
</layer> |
</layer> |
476 |
neq(proj, None) |
neq(proj, None) |
477 |
eq(proj.GetName(), "Unknown") |
eq(proj.GetName(), "Unknown") |
478 |
eq(proj.GetParameter("proj"), "lcc") |
eq(proj.GetParameter("proj"), "lcc") |
479 |
|
eq(proj.GetParameter("lat_1"), "10") |
480 |
|
eq(proj.GetParameter("lat_2"), "20") |
481 |
eq(proj.GetParameter("ellps"), "clrk66") |
eq(proj.GetParameter("ellps"), "clrk66") |
482 |
|
|
483 |
self.check_format() |
self.check_format() |
487 |
|
|
488 |
file_contents = '''\ |
file_contents = '''\ |
489 |
<?xml version="1.0" encoding="UTF-8"?> |
<?xml version="1.0" encoding="UTF-8"?> |
490 |
<!DOCTYPE session SYSTEM "thuban-0.8.dtd"> |
<!DOCTYPE session SYSTEM "thuban-0.9.dtd"> |
491 |
<session xmlns="http://thuban.intevation.org/dtds/thuban-0.8.dtd" |
<session xmlns="http://thuban.intevation.org/dtds/thuban-0.9.dtd" |
492 |
title="single map&layer"> |
title="single map&layer"> |
493 |
<map title="Test Map"> |
<map title="Test Map"> |
494 |
<rasterlayer visible="false" filename="../../Data/iceland/island.tif" |
<rasterlayer visible="false" filename="../../Data/iceland/island.tif" |
521 |
class TestJoinedTable(LoadSessionTest): |
class TestJoinedTable(LoadSessionTest): |
522 |
|
|
523 |
file_contents = '''<?xml version="1.0" encoding="UTF-8"?> |
file_contents = '''<?xml version="1.0" encoding="UTF-8"?> |
524 |
<!DOCTYPE session SYSTEM "thuban-0.8.dtd"> |
<!DOCTYPE session SYSTEM "thuban-0.9.dtd"> |
525 |
<session xmlns="http://thuban.intevation.org/dtds/thuban-0.8.dtd" title="A Joined Table session"> |
<session xmlns="http://thuban.intevation.org/dtds/thuban-0.9.dtd" title="A Joined Table session"> |
526 |
<fileshapesource filetype="shapefile" id="D137227612" |
<fileshapesource filetype="shapefile" id="D137227612" |
527 |
filename="../../Data/iceland/roads-line.shp"/> |
filename="../../Data/iceland/roads-line.shp"/> |
528 |
<filetable filetype="DBF" filename="load_joinedtable.dbf" id="D136171140" |
<filetable filetype="DBF" filename="load_joinedtable.dbf" id="D136171140" |
529 |
title="Some Title"/> |
title="Some Title"/> |
530 |
<jointable leftcolumn="RDLNTYPE" right="D136171140" |
<jointable id="D136169900" title="Joined" |
531 |
title="Joined" rightcolumn="RDTYPE" id="D136169900" left="D137227612"/> |
right="D136171140" left="D137227612" |
532 |
|
leftcolumn="RDLNTYPE" rightcolumn="RDTYPE" |
533 |
|
jointype="LEFT OUTER"/> |
534 |
<derivedshapesource table="D136169900" shapesource="D137227612" |
<derivedshapesource table="D136169900" shapesource="D137227612" |
535 |
id="D136170932"/> |
id="D136170932"/> |
536 |
<map title="Test Map"> |
<map title="Test Map"> |
562 |
# FIXME: The tests shouldn't assume a certain order of the tables |
# FIXME: The tests shouldn't assume a certain order of the tables |
563 |
self.assertEquals(tables[0].Title(), "Some Title") |
self.assertEquals(tables[0].Title(), "Some Title") |
564 |
self.assertEquals(tables[1].Title(), "Joined") |
self.assertEquals(tables[1].Title(), "Joined") |
565 |
|
self.assertEquals(tables[1].JoinType(), "LEFT OUTER") |
566 |
self.check_format() |
self.check_format() |
567 |
|
|
568 |
|
|
569 |
|
|
570 |
|
class TestPostGISLayer(LoadSessionTest): |
571 |
|
|
572 |
|
file_contents = '''<?xml version="1.0" encoding="UTF-8"?> |
573 |
|
<!DOCTYPE session SYSTEM "thuban-0.9.dtd"> |
574 |
|
<session xmlns="http://thuban.intevation.org/dtds/thuban-0.9.dtd" |
575 |
|
title="unnamed session"> |
576 |
|
<dbconnection port="%(port)s" host="%(host)s" user="%(user)s" |
577 |
|
dbtype="postgis" id="D142684948" dbname="%(dbname)s"/> |
578 |
|
<dbshapesource tablename="landmarks" id="D143149420" dbconn="D142684948"/> |
579 |
|
<map title="unnamed map"> |
580 |
|
<layer shapestore="D143149420" visible="true" stroke="#000000" |
581 |
|
title="landmarks" stroke_width="1" fill="None"/> |
582 |
|
</map> |
583 |
|
</session> |
584 |
|
''' |
585 |
|
|
586 |
|
def setUp(self): |
587 |
|
"""Extend the inherited method to start the postgis server |
588 |
|
|
589 |
|
Furthermore, patch the file contents with the real postgis db |
590 |
|
information |
591 |
|
""" |
592 |
|
postgissupport.skip_if_no_postgis() |
593 |
|
self.server = postgissupport.get_test_server() |
594 |
|
self.postgisdb = self.server.get_default_static_data_db() |
595 |
|
|
596 |
|
self.file_contents = self.__class__.file_contents % { |
597 |
|
"dbname": self.postgisdb.dbname, |
598 |
|
"user": self.server.user_name, |
599 |
|
"port": self.server.port, |
600 |
|
"host": self.server.host} |
601 |
|
LoadSessionTest.setUp(self) |
602 |
|
|
603 |
|
def test(self): |
604 |
|
"""Test loading a session containing a postgis shapestore""" |
605 |
|
session = load_session(self.filename()) |
606 |
|
self.session = session |
607 |
|
connections = session.DBConnections() |
608 |
|
self.assertEquals(len(connections), 1) |
609 |
|
conn = connections[0] |
610 |
|
for attr, value in [("host", self.server.host), |
611 |
|
("port", str(self.server.port)), |
612 |
|
("user", self.server.user_name), |
613 |
|
("dbname", self.postgisdb.dbname)]: |
614 |
|
self.assertEquals(getattr(conn, attr), value) |
615 |
|
layer = session.Maps()[0].Layers()[0] |
616 |
|
self.failUnless(layer.ShapeStore().DBConnection() is conn) |
617 |
|
|
618 |
|
|
619 |
|
class TestPostGISLayerPassword(LoadSessionTest): |
620 |
|
|
621 |
|
file_contents = '''<?xml version="1.0" encoding="UTF-8"?> |
622 |
|
<!DOCTYPE session SYSTEM "thuban-0.9.dtd"> |
623 |
|
<session xmlns="http://thuban.intevation.org/dtds/thuban-0.9.dtd" |
624 |
|
title="unnamed session"> |
625 |
|
<dbconnection port="%(port)s" host="%(host)s" user="%(user)s" |
626 |
|
dbtype="postgis" id="D142684948" dbname="%(dbname)s"/> |
627 |
|
<dbshapesource tablename="landmarks" id="D143149420" dbconn="D142684948"/> |
628 |
|
<map title="unnamed map"> |
629 |
|
<layer shapestore="D143149420" visible="true" stroke="#000000" |
630 |
|
title="landmarks" stroke_width="1" fill="None"/> |
631 |
|
</map> |
632 |
|
</session> |
633 |
|
''' |
634 |
|
|
635 |
|
def setUp(self): |
636 |
|
"""Extend the inherited method to start the postgis server |
637 |
|
|
638 |
|
Furthermore, patch the file contents with the real postgis db |
639 |
|
information |
640 |
|
""" |
641 |
|
postgissupport.skip_if_no_postgis() |
642 |
|
self.server = postgissupport.get_test_server() |
643 |
|
self.postgisdb = self.server.get_default_static_data_db() |
644 |
|
|
645 |
|
self.file_contents = self.__class__.file_contents % { |
646 |
|
"dbname": self.postgisdb.dbname, |
647 |
|
"user": self.server.user_name, |
648 |
|
"port": self.server.port, |
649 |
|
"host": self.server.host} |
650 |
|
LoadSessionTest.setUp(self) |
651 |
|
|
652 |
|
self.db_connection_callback_called = False |
653 |
|
self.server.require_authentication(True) |
654 |
|
|
655 |
|
def tearDown(self): |
656 |
|
"""Extend the inherited method to switch off postgresql authentication |
657 |
|
""" |
658 |
|
self.server.require_authentication(False) |
659 |
|
LoadSessionTest.tearDown(self) |
660 |
|
|
661 |
|
def db_connection_callback(self, params, message): |
662 |
|
"""Implementation of Thuban.Model.hooks.query_db_connection_parameters |
663 |
|
""" |
664 |
|
self.assertEquals(params, |
665 |
|
{"dbname": self.postgisdb.dbname, |
666 |
|
"user": self.server.user_name, |
667 |
|
"port": str(self.server.port), |
668 |
|
"host": self.server.host}) |
669 |
|
self.db_connection_callback_called = True |
670 |
|
params = params.copy() |
671 |
|
params["password"] = self.server.user_password |
672 |
|
return params |
673 |
|
|
674 |
|
def test_with_callback(self): |
675 |
|
"""Test loading a session with postgis, authentication and a callback |
676 |
|
""" |
677 |
|
session = load_session(self.filename(), |
678 |
|
db_connection_callback = self.db_connection_callback) |
679 |
|
self.session = session |
680 |
|
connections = session.DBConnections() |
681 |
|
self.assertEquals(len(connections), 1) |
682 |
|
conn = connections[0] |
683 |
|
for attr, value in [("host", self.server.host), |
684 |
|
("port", str(self.server.port)), |
685 |
|
("user", self.server.user_name), |
686 |
|
("dbname", self.postgisdb.dbname)]: |
687 |
|
self.assertEquals(getattr(conn, attr), value) |
688 |
|
layer = session.Maps()[0].Layers()[0] |
689 |
|
self.failUnless(layer.ShapeStore().DBConnection() is conn) |
690 |
|
self.failUnless(self.db_connection_callback_called) |
691 |
|
|
692 |
|
def test_without_callback(self): |
693 |
|
"""Test loading a session with postgis, authentication and no callback |
694 |
|
""" |
695 |
|
# A password is required and there's no callback, so we should |
696 |
|
# get a ConnectionError |
697 |
|
self.assertRaises(ConnectionError, load_session, self.filename()) |
698 |
|
|
699 |
|
def test_cancel(self): |
700 |
|
"""Test loading a session with postgis and cancelling authentication |
701 |
|
""" |
702 |
|
def cancel(*args): |
703 |
|
self.db_connection_callback_called = True |
704 |
|
return None |
705 |
|
|
706 |
|
# If the user cancels, i.e. if the callbakc returns None, a |
707 |
|
# LoadCancelled exception is raised. |
708 |
|
self.assertRaises(LoadCancelled, |
709 |
|
load_session, self.filename(), cancel) |
710 |
|
self.failUnless(self.db_connection_callback_called) |
711 |
|
|
712 |
|
|
713 |
class TestLoadError(LoadSessionTest): |
class TestLoadError(LoadSessionTest): |
714 |
|
|
715 |
file_contents = '''\ |
file_contents = '''\ |
716 |
<?xml version="1.0" encoding="UTF-8"?> |
<?xml version="1.0" encoding="UTF-8"?> |
717 |
<!DOCTYPE session SYSTEM "thuban-0.8.dtd"> |
<!DOCTYPE session SYSTEM "thuban-0.9.dtd"> |
718 |
<session xmlns="http://thuban.intevation.org/dtds/thuban-0.8.dtd" |
<session xmlns="http://thuban.intevation.org/dtds/thuban-0.9.dtd" |
719 |
title="single map&layer"> |
title="single map&layer"> |
720 |
<fileshapesource id="D1" filename="../../Data/iceland/political.shp"/> |
<fileshapesource id="D1" filename="../../Data/iceland/political.shp"/> |
721 |
<map title="Test Map"> |
<map title="Test Map"> |
738 |
try: |
try: |
739 |
self.session = load_session(self.filename()) |
self.session = load_session(self.filename()) |
740 |
except LoadError, value: |
except LoadError, value: |
741 |
pass |
# Check the actual messge in value to make sure the |
742 |
|
# LoadError really was about the missing attribute |
743 |
|
self.assertEquals(str(value), |
744 |
|
"Element " |
745 |
|
"(u'http://thuban.intevation.org/dtds/thuban-0.9.dtd'," |
746 |
|
" u'fileshapesource') requires an attribute 'filetype'") |
747 |
else: |
else: |
748 |
self.fail("Missing filetype attribute doesn't raise LoadError") |
self.fail("Missing filetype attribute doesn't raise LoadError") |
749 |
|
|
750 |
if __name__ == "__main__": |
if __name__ == "__main__": |
751 |
unittest.main() |
support.run_tests() |