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"]] |
152 |
if data[CLASSES][i][GROUP_TYPE] == "default": |
if data[CLASSES][i][GROUP_TYPE] == "default": |
153 |
g = ClassGroupDefault(props, data[CLASSES][i][GROUP_LABEL]) |
g = ClassGroupDefault(props, data[CLASSES][i][GROUP_LABEL]) |
154 |
elif data[CLASSES][i][GROUP_TYPE] == "range": |
elif data[CLASSES][i][GROUP_TYPE] == "range": |
155 |
g = ClassGroupRange(data[CLASSES][i][GROUP_DATA][0], |
g = ClassGroupRange((data[CLASSES][i][GROUP_DATA][0], |
156 |
data[CLASSES][i][GROUP_DATA][1], |
data[CLASSES][i][GROUP_DATA][1]), |
157 |
props, data[CLASSES][i][GROUP_LABEL]) |
props, data[CLASSES][i][GROUP_LABEL]) |
158 |
elif data[CLASSES][i][GROUP_TYPE] == "single": |
elif data[CLASSES][i][GROUP_TYPE] == "single": |
159 |
g = ClassGroupSingleton(data[CLASSES][i][GROUP_DATA], |
g = ClassGroupSingleton(data[CLASSES][i][GROUP_DATA], |
169 |
|
|
170 |
file_contents = '''\ |
file_contents = '''\ |
171 |
<?xml version="1.0" encoding="UTF-8"?> |
<?xml version="1.0" encoding="UTF-8"?> |
172 |
<!DOCTYPE session SYSTEM "thuban-0.8.dtd"> |
<!DOCTYPE session SYSTEM "thuban-0.9.dtd"> |
173 |
<session xmlns="http://thuban.intevation.org/dtds/thuban-0.8.dtd" |
<session xmlns="http://thuban.intevation.org/dtds/thuban-0.9.dtd" |
174 |
title="single map&layer"> |
title="single map&layer"> |
175 |
<fileshapesource filetype="shapefile" id="D1" |
<fileshapesource filetype="shapefile" id="D1" |
176 |
filename="../../Data/iceland/political.shp"/> |
filename="../../Data/iceland/political.shp"/> |
230 |
|
|
231 |
file_contents = '''\ |
file_contents = '''\ |
232 |
<?xml version="1.0" encoding="UTF-8"?> |
<?xml version="1.0" encoding="UTF-8"?> |
233 |
<!DOCTYPE session SYSTEM "thuban-0.8.dtd"> |
<!DOCTYPE session SYSTEM "thuban-0.9.dtd"> |
234 |
<session xmlns="http://thuban.intevation.org/dtds/thuban-0.8.dtd" |
<session xmlns="http://thuban.intevation.org/dtds/thuban-0.9.dtd" |
235 |
title="single map&layer"> |
title="single map&layer"> |
236 |
<fileshapesource filetype="shapefile" id="D1" |
<fileshapesource filetype="shapefile" id="D1" |
237 |
filename="../../Data/iceland/political.shp"/> |
filename="../../Data/iceland/political.shp"/> |
289 |
<clpoint value="1"> |
<clpoint value="1"> |
290 |
<cldata stroke="#000000" stroke_width="10" fill="None"/> |
<cldata stroke="#000000" stroke_width="10" fill="None"/> |
291 |
</clpoint> |
</clpoint> |
292 |
|
<clpoint value="\xc3\xa4\xc3\xb6\xc3\xbc" |
293 |
|
label="\xc3\x9cml\xc3\xa4uts"> |
294 |
|
<cldata fill="None" stroke="#000000" stroke_width="1"/> |
295 |
|
</clpoint> |
296 |
</classification> |
</classification> |
297 |
</layer> |
</layer> |
298 |
<layer title="My Layer 2" stroke_width="1" fill="None" |
<layer title="My Layer 2" stroke_width="1" fill="None" |
327 |
|
|
328 |
map = self.session.Maps()[0] # only one map in the sample |
map = self.session.Maps()[0] # only one map in the sample |
329 |
|
|
330 |
expected = [("My Layer", 2, |
expected = [("My Layer", 3, |
331 |
[("default", (), "", |
[("default", (), "", |
332 |
("#000000", 1, "None")), |
("#000000", 1, "None")), |
333 |
("single", "1", "", |
("single", "1", "", |
334 |
("#000000", 2, "None")), |
("#000000", 2, "None")), |
335 |
("single", "1", "", |
("single", "1", "", |
336 |
("#000000", 10, "None"))]), |
("#000000", 10, "None")), |
337 |
|
("single", "\xe4\xf6\xfc", "\xdcml\xe4uts", |
338 |
|
("#000000", 1, "None"))]), |
339 |
("My Layer 2", 4, |
("My Layer 2", 4, |
340 |
[("default", (), "", |
[("default", (), "", |
341 |
("#000000", 2, "None")), |
("#000000", 2, "None")), |
355 |
|
|
356 |
file_contents = '''\ |
file_contents = '''\ |
357 |
<?xml version="1.0" encoding="UTF-8"?> |
<?xml version="1.0" encoding="UTF-8"?> |
358 |
<!DOCTYPE session SYSTEM "thuban-0.8.dtd"> |
<!DOCTYPE session SYSTEM "thuban-0.9.dtd"> |
359 |
<session xmlns="http://thuban.intevation.org/dtds/thuban-0.8.dtd" |
<session xmlns="http://thuban.intevation.org/dtds/thuban-0.9.dtd" |
360 |
title="single map&layer"> |
title="single map&layer"> |
361 |
<fileshapesource filetype="shapefile" id="D1" |
<fileshapesource filetype="shapefile" id="D1" |
362 |
filename="../../Data/iceland/political.shp"/> |
filename="../../Data/iceland/political.shp"/> |
403 |
|
|
404 |
file_contents = '''\ |
file_contents = '''\ |
405 |
<?xml version="1.0" encoding="UTF-8"?> |
<?xml version="1.0" encoding="UTF-8"?> |
406 |
<!DOCTYPE session SYSTEM "thuban-0.8.dtd"> |
<!DOCTYPE session SYSTEM "thuban-0.9.dtd"> |
407 |
<session xmlns="http://thuban.intevation.org/dtds/thuban-0.8.dtd" |
<session xmlns="http://thuban.intevation.org/dtds/thuban-0.9.dtd" |
408 |
title="single map&layer"> |
title="single map&layer"> |
409 |
<fileshapesource filetype="shapefile" id="D2" |
<fileshapesource filetype="shapefile" id="D2" |
410 |
filename="../../Data/iceland/roads-line.shp"/> |
filename="../../Data/iceland/roads-line.shp"/> |
477 |
|
|
478 |
file_contents = '''\ |
file_contents = '''\ |
479 |
<?xml version="1.0" encoding="UTF-8"?> |
<?xml version="1.0" encoding="UTF-8"?> |
480 |
<!DOCTYPE session SYSTEM "thuban-0.8.dtd"> |
<!DOCTYPE session SYSTEM "thuban-0.9.dtd"> |
481 |
<session xmlns="http://thuban.intevation.org/dtds/thuban-0.8.dtd" |
<session xmlns="http://thuban.intevation.org/dtds/thuban-0.9.dtd" |
482 |
title="single map&layer"> |
title="single map&layer"> |
483 |
<map title="Test Map"> |
<map title="Test Map"> |
484 |
<rasterlayer visible="false" filename="../../Data/iceland/island.tif" |
<rasterlayer visible="false" filename="../../Data/iceland/island.tif" |
511 |
class TestJoinedTable(LoadSessionTest): |
class TestJoinedTable(LoadSessionTest): |
512 |
|
|
513 |
file_contents = '''<?xml version="1.0" encoding="UTF-8"?> |
file_contents = '''<?xml version="1.0" encoding="UTF-8"?> |
514 |
<!DOCTYPE session SYSTEM "thuban-0.8.dtd"> |
<!DOCTYPE session SYSTEM "thuban-0.9.dtd"> |
515 |
<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"> |
516 |
<fileshapesource filetype="shapefile" id="D137227612" |
<fileshapesource filetype="shapefile" id="D137227612" |
517 |
filename="../../Data/iceland/roads-line.shp"/> |
filename="../../Data/iceland/roads-line.shp"/> |
518 |
<filetable filetype="DBF" filename="load_joinedtable.dbf" id="D136171140" |
<filetable filetype="DBF" filename="load_joinedtable.dbf" id="D136171140" |
519 |
title="Some Title"/> |
title="Some Title"/> |
520 |
<jointable leftcolumn="RDLNTYPE" right="D136171140" |
<jointable id="D136169900" title="Joined" |
521 |
title="Joined" rightcolumn="RDTYPE" id="D136169900" left="D137227612"/> |
right="D136171140" left="D137227612" |
522 |
|
leftcolumn="RDLNTYPE" rightcolumn="RDTYPE" |
523 |
|
jointype="LEFT OUTER"/> |
524 |
<derivedshapesource table="D136169900" shapesource="D137227612" |
<derivedshapesource table="D136169900" shapesource="D137227612" |
525 |
id="D136170932"/> |
id="D136170932"/> |
526 |
<map title="Test Map"> |
<map title="Test Map"> |
552 |
# FIXME: The tests shouldn't assume a certain order of the tables |
# FIXME: The tests shouldn't assume a certain order of the tables |
553 |
self.assertEquals(tables[0].Title(), "Some Title") |
self.assertEquals(tables[0].Title(), "Some Title") |
554 |
self.assertEquals(tables[1].Title(), "Joined") |
self.assertEquals(tables[1].Title(), "Joined") |
555 |
|
self.assertEquals(tables[1].JoinType(), "LEFT OUTER") |
556 |
self.check_format() |
self.check_format() |
557 |
|
|
558 |
|
|
559 |
|
|
560 |
|
class TestPostGISLayer(LoadSessionTest): |
561 |
|
|
562 |
|
file_contents = '''<?xml version="1.0" encoding="UTF-8"?> |
563 |
|
<!DOCTYPE session SYSTEM "thuban-0.9.dtd"> |
564 |
|
<session xmlns="http://thuban.intevation.org/dtds/thuban-0.9.dtd" |
565 |
|
title="unnamed session"> |
566 |
|
<dbconnection port="%(port)s" host="%(host)s" user="%(user)s" |
567 |
|
dbtype="postgis" id="D142684948" dbname="%(dbname)s"/> |
568 |
|
<dbshapesource tablename="landmarks" id="D143149420" dbconn="D142684948"/> |
569 |
|
<map title="unnamed map"> |
570 |
|
<layer shapestore="D143149420" visible="true" stroke="#000000" |
571 |
|
title="landmarks" stroke_width="1" fill="None"/> |
572 |
|
</map> |
573 |
|
</session> |
574 |
|
''' |
575 |
|
|
576 |
|
def setUp(self): |
577 |
|
"""Extend the inherited method to start the postgis server |
578 |
|
|
579 |
|
Furthermore, patch the file contents with the real postgis db |
580 |
|
information |
581 |
|
""" |
582 |
|
postgissupport.skip_if_no_postgis() |
583 |
|
self.server = postgissupport.get_test_server() |
584 |
|
self.postgisdb = self.server.get_default_static_data_db() |
585 |
|
|
586 |
|
self.file_contents = self.__class__.file_contents % { |
587 |
|
"dbname": self.postgisdb.dbname, |
588 |
|
"user": self.server.user_name, |
589 |
|
"port": self.server.port, |
590 |
|
"host": self.server.host} |
591 |
|
LoadSessionTest.setUp(self) |
592 |
|
|
593 |
|
def test(self): |
594 |
|
"""Test loading a session containing a postgis shapestore""" |
595 |
|
session = load_session(self.filename()) |
596 |
|
self.session = session |
597 |
|
connections = session.DBConnections() |
598 |
|
self.assertEquals(len(connections), 1) |
599 |
|
conn = connections[0] |
600 |
|
for attr, value in [("host", self.server.host), |
601 |
|
("port", str(self.server.port)), |
602 |
|
("user", self.server.user_name), |
603 |
|
("dbname", self.postgisdb.dbname)]: |
604 |
|
self.assertEquals(getattr(conn, attr), value) |
605 |
|
layer = session.Maps()[0].Layers()[0] |
606 |
|
self.failUnless(layer.ShapeStore().DBConnection() is conn) |
607 |
|
|
608 |
|
|
609 |
|
class TestPostGISLayerPassword(LoadSessionTest): |
610 |
|
|
611 |
|
file_contents = '''<?xml version="1.0" encoding="UTF-8"?> |
612 |
|
<!DOCTYPE session SYSTEM "thuban-0.9.dtd"> |
613 |
|
<session xmlns="http://thuban.intevation.org/dtds/thuban-0.9.dtd" |
614 |
|
title="unnamed session"> |
615 |
|
<dbconnection port="%(port)s" host="%(host)s" user="%(user)s" |
616 |
|
dbtype="postgis" id="D142684948" dbname="%(dbname)s"/> |
617 |
|
<dbshapesource tablename="landmarks" id="D143149420" dbconn="D142684948"/> |
618 |
|
<map title="unnamed map"> |
619 |
|
<layer shapestore="D143149420" visible="true" stroke="#000000" |
620 |
|
title="landmarks" stroke_width="1" fill="None"/> |
621 |
|
</map> |
622 |
|
</session> |
623 |
|
''' |
624 |
|
|
625 |
|
def setUp(self): |
626 |
|
"""Extend the inherited method to start the postgis server |
627 |
|
|
628 |
|
Furthermore, patch the file contents with the real postgis db |
629 |
|
information |
630 |
|
""" |
631 |
|
postgissupport.skip_if_no_postgis() |
632 |
|
self.server = postgissupport.get_test_server() |
633 |
|
self.postgisdb = self.server.get_default_static_data_db() |
634 |
|
|
635 |
|
self.file_contents = self.__class__.file_contents % { |
636 |
|
"dbname": self.postgisdb.dbname, |
637 |
|
"user": self.server.user_name, |
638 |
|
"port": self.server.port, |
639 |
|
"host": self.server.host} |
640 |
|
LoadSessionTest.setUp(self) |
641 |
|
|
642 |
|
self.db_connection_callback_called = False |
643 |
|
self.server.require_authentication(True) |
644 |
|
|
645 |
|
def tearDown(self): |
646 |
|
"""Extend the inherited method to switch off postgresql authentication |
647 |
|
""" |
648 |
|
self.server.require_authentication(False) |
649 |
|
LoadSessionTest.tearDown(self) |
650 |
|
|
651 |
|
def db_connection_callback(self, params, message): |
652 |
|
"""Implementation of Thuban.Model.hooks.query_db_connection_parameters |
653 |
|
""" |
654 |
|
self.assertEquals(params, |
655 |
|
{"dbname": self.postgisdb.dbname, |
656 |
|
"user": self.server.user_name, |
657 |
|
"port": str(self.server.port), |
658 |
|
"host": self.server.host}) |
659 |
|
self.db_connection_callback_called = True |
660 |
|
params = params.copy() |
661 |
|
params["password"] = self.server.user_password |
662 |
|
return params |
663 |
|
|
664 |
|
def test_with_callback(self): |
665 |
|
"""Test loading a session with postgis, authentication and a callback |
666 |
|
""" |
667 |
|
session = load_session(self.filename(), |
668 |
|
db_connection_callback = self.db_connection_callback) |
669 |
|
self.session = session |
670 |
|
connections = session.DBConnections() |
671 |
|
self.assertEquals(len(connections), 1) |
672 |
|
conn = connections[0] |
673 |
|
for attr, value in [("host", self.server.host), |
674 |
|
("port", str(self.server.port)), |
675 |
|
("user", self.server.user_name), |
676 |
|
("dbname", self.postgisdb.dbname)]: |
677 |
|
self.assertEquals(getattr(conn, attr), value) |
678 |
|
layer = session.Maps()[0].Layers()[0] |
679 |
|
self.failUnless(layer.ShapeStore().DBConnection() is conn) |
680 |
|
self.failUnless(self.db_connection_callback_called) |
681 |
|
|
682 |
|
def test_without_callback(self): |
683 |
|
"""Test loading a session with postgis, authentication and no callback |
684 |
|
""" |
685 |
|
# A password is required and there's no callback, so we should |
686 |
|
# get a ConnectionError |
687 |
|
self.assertRaises(ConnectionError, load_session, self.filename()) |
688 |
|
|
689 |
|
def test_cancel(self): |
690 |
|
"""Test loading a session with postgis and cancelling authentication |
691 |
|
""" |
692 |
|
def cancel(*args): |
693 |
|
self.db_connection_callback_called = True |
694 |
|
return None |
695 |
|
|
696 |
|
# If the user cancels, i.e. if the callbakc returns None, a |
697 |
|
# LoadCancelled exception is raised. |
698 |
|
self.assertRaises(LoadCancelled, |
699 |
|
load_session, self.filename(), cancel) |
700 |
|
self.failUnless(self.db_connection_callback_called) |
701 |
|
|
702 |
|
|
703 |
class TestLoadError(LoadSessionTest): |
class TestLoadError(LoadSessionTest): |
704 |
|
|
705 |
file_contents = '''\ |
file_contents = '''\ |
706 |
<?xml version="1.0" encoding="UTF-8"?> |
<?xml version="1.0" encoding="UTF-8"?> |
707 |
<!DOCTYPE session SYSTEM "thuban-0.8.dtd"> |
<!DOCTYPE session SYSTEM "thuban-0.9.dtd"> |
708 |
<session xmlns="http://thuban.intevation.org/dtds/thuban-0.8.dtd" |
<session xmlns="http://thuban.intevation.org/dtds/thuban-0.9.dtd" |
709 |
title="single map&layer"> |
title="single map&layer"> |
710 |
<fileshapesource id="D1" filename="../../Data/iceland/political.shp"/> |
<fileshapesource id="D1" filename="../../Data/iceland/political.shp"/> |
711 |
<map title="Test Map"> |
<map title="Test Map"> |
728 |
try: |
try: |
729 |
self.session = load_session(self.filename()) |
self.session = load_session(self.filename()) |
730 |
except LoadError, value: |
except LoadError, value: |
731 |
pass |
# Check the actual messge in value to make sure the |
732 |
|
# LoadError really was about the missing attribute |
733 |
|
self.assertEquals(str(value), |
734 |
|
"Element " |
735 |
|
"(u'http://thuban.intevation.org/dtds/thuban-0.9.dtd'," |
736 |
|
" u'fileshapesource') requires an attribute 'filetype'") |
737 |
else: |
else: |
738 |
self.fail("Missing filetype attribute doesn't raise LoadError") |
self.fail("Missing filetype attribute doesn't raise LoadError") |
739 |
|
|