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 |
|
LoadCancelled |
45 |
from Thuban.Model.color import Transparent |
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 "" |
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"/> |
483 |
|
|
484 |
file_contents = '''\ |
file_contents = '''\ |
485 |
<?xml version="1.0" encoding="UTF-8"?> |
<?xml version="1.0" encoding="UTF-8"?> |
486 |
<!DOCTYPE session SYSTEM "thuban-0.8.dtd"> |
<!DOCTYPE session SYSTEM "thuban-0.9.dtd"> |
487 |
<session xmlns="http://thuban.intevation.org/dtds/thuban-0.8.dtd" |
<session xmlns="http://thuban.intevation.org/dtds/thuban-0.9.dtd" |
488 |
title="single map&layer"> |
title="single map&layer"> |
489 |
<map title="Test Map"> |
<map title="Test Map"> |
490 |
<rasterlayer visible="false" filename="../../Data/iceland/island.tif" |
<rasterlayer visible="false" filename="../../Data/iceland/island.tif" |
517 |
class TestJoinedTable(LoadSessionTest): |
class TestJoinedTable(LoadSessionTest): |
518 |
|
|
519 |
file_contents = '''<?xml version="1.0" encoding="UTF-8"?> |
file_contents = '''<?xml version="1.0" encoding="UTF-8"?> |
520 |
<!DOCTYPE session SYSTEM "thuban-0.8.dtd"> |
<!DOCTYPE session SYSTEM "thuban-0.9.dtd"> |
521 |
<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"> |
522 |
<fileshapesource filetype="shapefile" id="D137227612" |
<fileshapesource filetype="shapefile" id="D137227612" |
523 |
filename="../../Data/iceland/roads-line.shp"/> |
filename="../../Data/iceland/roads-line.shp"/> |
524 |
<filetable filetype="DBF" filename="load_joinedtable.dbf" id="D136171140" |
<filetable filetype="DBF" filename="load_joinedtable.dbf" id="D136171140" |
525 |
title="Some Title"/> |
title="Some Title"/> |
526 |
<jointable leftcolumn="RDLNTYPE" right="D136171140" |
<jointable id="D136169900" title="Joined" |
527 |
title="Joined" rightcolumn="RDTYPE" id="D136169900" left="D137227612"/> |
right="D136171140" left="D137227612" |
528 |
|
leftcolumn="RDLNTYPE" rightcolumn="RDTYPE" |
529 |
|
jointype="LEFT OUTER"/> |
530 |
<derivedshapesource table="D136169900" shapesource="D137227612" |
<derivedshapesource table="D136169900" shapesource="D137227612" |
531 |
id="D136170932"/> |
id="D136170932"/> |
532 |
<map title="Test Map"> |
<map title="Test Map"> |
558 |
# FIXME: The tests shouldn't assume a certain order of the tables |
# FIXME: The tests shouldn't assume a certain order of the tables |
559 |
self.assertEquals(tables[0].Title(), "Some Title") |
self.assertEquals(tables[0].Title(), "Some Title") |
560 |
self.assertEquals(tables[1].Title(), "Joined") |
self.assertEquals(tables[1].Title(), "Joined") |
561 |
|
self.assertEquals(tables[1].JoinType(), "LEFT OUTER") |
562 |
self.check_format() |
self.check_format() |
563 |
|
|
564 |
|
|
565 |
|
|
566 |
|
class TestPostGISLayer(LoadSessionTest): |
567 |
|
|
568 |
|
file_contents = '''<?xml version="1.0" encoding="UTF-8"?> |
569 |
|
<!DOCTYPE session SYSTEM "thuban-0.9.dtd"> |
570 |
|
<session xmlns="http://thuban.intevation.org/dtds/thuban-0.9.dtd" |
571 |
|
title="unnamed session"> |
572 |
|
<dbconnection port="%(port)s" host="%(host)s" user="%(user)s" |
573 |
|
dbtype="postgis" id="D142684948" dbname="%(dbname)s"/> |
574 |
|
<dbshapesource tablename="landmarks" id="D143149420" dbconn="D142684948"/> |
575 |
|
<map title="unnamed map"> |
576 |
|
<layer shapestore="D143149420" visible="true" stroke="#000000" |
577 |
|
title="landmarks" stroke_width="1" fill="None"/> |
578 |
|
</map> |
579 |
|
</session> |
580 |
|
''' |
581 |
|
|
582 |
|
def setUp(self): |
583 |
|
"""Extend the inherited method to start the postgis server |
584 |
|
|
585 |
|
Furthermore, patch the file contents with the real postgis db |
586 |
|
information |
587 |
|
""" |
588 |
|
postgissupport.skip_if_no_postgis() |
589 |
|
self.server = postgissupport.get_test_server() |
590 |
|
self.postgisdb = self.server.get_default_static_data_db() |
591 |
|
|
592 |
|
self.file_contents = self.__class__.file_contents % { |
593 |
|
"dbname": self.postgisdb.dbname, |
594 |
|
"user": self.server.user_name, |
595 |
|
"port": self.server.port, |
596 |
|
"host": self.server.host} |
597 |
|
LoadSessionTest.setUp(self) |
598 |
|
|
599 |
|
def test(self): |
600 |
|
"""Test loading a session containing a postgis shapestore""" |
601 |
|
session = load_session(self.filename()) |
602 |
|
self.session = session |
603 |
|
connections = session.DBConnections() |
604 |
|
self.assertEquals(len(connections), 1) |
605 |
|
conn = connections[0] |
606 |
|
for attr, value in [("host", self.server.host), |
607 |
|
("port", str(self.server.port)), |
608 |
|
("user", self.server.user_name), |
609 |
|
("dbname", self.postgisdb.dbname)]: |
610 |
|
self.assertEquals(getattr(conn, attr), value) |
611 |
|
layer = session.Maps()[0].Layers()[0] |
612 |
|
self.failUnless(layer.ShapeStore().DBConnection() is conn) |
613 |
|
|
614 |
|
|
615 |
|
class TestPostGISLayerPassword(LoadSessionTest): |
616 |
|
|
617 |
|
file_contents = '''<?xml version="1.0" encoding="UTF-8"?> |
618 |
|
<!DOCTYPE session SYSTEM "thuban-0.9.dtd"> |
619 |
|
<session xmlns="http://thuban.intevation.org/dtds/thuban-0.9.dtd" |
620 |
|
title="unnamed session"> |
621 |
|
<dbconnection port="%(port)s" host="%(host)s" user="%(user)s" |
622 |
|
dbtype="postgis" id="D142684948" dbname="%(dbname)s"/> |
623 |
|
<dbshapesource tablename="landmarks" id="D143149420" dbconn="D142684948"/> |
624 |
|
<map title="unnamed map"> |
625 |
|
<layer shapestore="D143149420" visible="true" stroke="#000000" |
626 |
|
title="landmarks" stroke_width="1" fill="None"/> |
627 |
|
</map> |
628 |
|
</session> |
629 |
|
''' |
630 |
|
|
631 |
|
def setUp(self): |
632 |
|
"""Extend the inherited method to start the postgis server |
633 |
|
|
634 |
|
Furthermore, patch the file contents with the real postgis db |
635 |
|
information |
636 |
|
""" |
637 |
|
postgissupport.skip_if_no_postgis() |
638 |
|
self.server = postgissupport.get_test_server() |
639 |
|
self.postgisdb = self.server.get_default_static_data_db() |
640 |
|
|
641 |
|
self.file_contents = self.__class__.file_contents % { |
642 |
|
"dbname": self.postgisdb.dbname, |
643 |
|
"user": self.server.user_name, |
644 |
|
"port": self.server.port, |
645 |
|
"host": self.server.host} |
646 |
|
LoadSessionTest.setUp(self) |
647 |
|
|
648 |
|
self.db_connection_callback_called = False |
649 |
|
self.server.require_authentication(True) |
650 |
|
|
651 |
|
def tearDown(self): |
652 |
|
"""Extend the inherited method to switch off postgresql authentication |
653 |
|
""" |
654 |
|
self.server.require_authentication(False) |
655 |
|
LoadSessionTest.tearDown(self) |
656 |
|
|
657 |
|
def db_connection_callback(self, params, message): |
658 |
|
"""Implementation of Thuban.Model.hooks.query_db_connection_parameters |
659 |
|
""" |
660 |
|
self.assertEquals(params, |
661 |
|
{"dbname": self.postgisdb.dbname, |
662 |
|
"user": self.server.user_name, |
663 |
|
"port": str(self.server.port), |
664 |
|
"host": self.server.host}) |
665 |
|
self.db_connection_callback_called = True |
666 |
|
params = params.copy() |
667 |
|
params["password"] = self.server.user_password |
668 |
|
return params |
669 |
|
|
670 |
|
def test_with_callback(self): |
671 |
|
"""Test loading a session with postgis, authentication and a callback |
672 |
|
""" |
673 |
|
session = load_session(self.filename(), |
674 |
|
db_connection_callback = self.db_connection_callback) |
675 |
|
self.session = session |
676 |
|
connections = session.DBConnections() |
677 |
|
self.assertEquals(len(connections), 1) |
678 |
|
conn = connections[0] |
679 |
|
for attr, value in [("host", self.server.host), |
680 |
|
("port", str(self.server.port)), |
681 |
|
("user", self.server.user_name), |
682 |
|
("dbname", self.postgisdb.dbname)]: |
683 |
|
self.assertEquals(getattr(conn, attr), value) |
684 |
|
layer = session.Maps()[0].Layers()[0] |
685 |
|
self.failUnless(layer.ShapeStore().DBConnection() is conn) |
686 |
|
self.failUnless(self.db_connection_callback_called) |
687 |
|
|
688 |
|
def test_without_callback(self): |
689 |
|
"""Test loading a session with postgis, authentication and no callback |
690 |
|
""" |
691 |
|
# A password is required and there's no callback, so we should |
692 |
|
# get a ConnectionError |
693 |
|
self.assertRaises(ConnectionError, load_session, self.filename()) |
694 |
|
|
695 |
|
def test_cancel(self): |
696 |
|
"""Test loading a session with postgis and cancelling authentication |
697 |
|
""" |
698 |
|
def cancel(*args): |
699 |
|
self.db_connection_callback_called = True |
700 |
|
return None |
701 |
|
|
702 |
|
# If the user cancels, i.e. if the callbakc returns None, a |
703 |
|
# LoadCancelled exception is raised. |
704 |
|
self.assertRaises(LoadCancelled, |
705 |
|
load_session, self.filename(), cancel) |
706 |
|
self.failUnless(self.db_connection_callback_called) |
707 |
|
|
708 |
|
|
709 |
class TestLoadError(LoadSessionTest): |
class TestLoadError(LoadSessionTest): |
710 |
|
|
711 |
file_contents = '''\ |
file_contents = '''\ |
712 |
<?xml version="1.0" encoding="UTF-8"?> |
<?xml version="1.0" encoding="UTF-8"?> |
713 |
<!DOCTYPE session SYSTEM "thuban-0.8.dtd"> |
<!DOCTYPE session SYSTEM "thuban-0.9.dtd"> |
714 |
<session xmlns="http://thuban.intevation.org/dtds/thuban-0.8.dtd" |
<session xmlns="http://thuban.intevation.org/dtds/thuban-0.9.dtd" |
715 |
title="single map&layer"> |
title="single map&layer"> |
716 |
<fileshapesource id="D1" filename="../../Data/iceland/political.shp"/> |
<fileshapesource id="D1" filename="../../Data/iceland/political.shp"/> |
717 |
<map title="Test Map"> |
<map title="Test Map"> |
734 |
try: |
try: |
735 |
self.session = load_session(self.filename()) |
self.session = load_session(self.filename()) |
736 |
except LoadError, value: |
except LoadError, value: |
737 |
pass |
# Check the actual messge in value to make sure the |
738 |
|
# LoadError really was about the missing attribute |
739 |
|
self.assertEquals(str(value), |
740 |
|
"Element " |
741 |
|
"(u'http://thuban.intevation.org/dtds/thuban-0.9.dtd'," |
742 |
|
" u'fileshapesource') requires an attribute 'filetype'") |
743 |
else: |
else: |
744 |
self.fail("Missing filetype attribute doesn't raise LoadError") |
self.fail("Missing filetype attribute doesn't raise LoadError") |
745 |
|
|
746 |
if __name__ == "__main__": |
if __name__ == "__main__": |
747 |
unittest.main() |
support.run_tests() |