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.8.dtd" |
dtd = "http://thuban.intevation.org/dtds/thuban-1.0rc1.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 tearDown(self): |
82 |
|
"""Call self.session.Destroy |
83 |
|
|
84 |
|
Test cases that create session should bind it to self.session so |
85 |
|
that it gets destroyed properly |
86 |
|
""" |
87 |
|
if hasattr(self, "session"): |
88 |
|
self.session.Destroy() |
89 |
|
self.session = None |
90 |
|
|
91 |
def compare_xml(self, xml1, xml2): |
def compare_xml(self, xml1, xml2): |
92 |
if 0: |
list1 = xmlsupport.sax_eventlist(xml1, ids = self.thubanids, |
93 |
for a, b in zip(sax_eventlist(xml1, self.thubanids, |
idrefs = self.thubanidrefs) |
94 |
self.thubanidrefs), |
list2 = xmlsupport.sax_eventlist(xml2, ids = self.thubanids, |
95 |
sax_eventlist(xml2, self.thubanids, |
idrefs = self.thubanidrefs) |
96 |
self.thubanidrefs)): |
if list1 != list2: |
97 |
print a == b and ' ' or '*****' |
for a, b in zip(list1, list2): |
98 |
print a |
if a != b: |
99 |
print b |
self.fail("%r != %r" % (a, b)) |
100 |
self.assertEquals(xmlsupport.sax_eventlist(xml1, ids = self.thubanids, |
|
|
idrefs = self.thubanidrefs), |
|
|
xmlsupport.sax_eventlist(xml2, ids = self.thubanids, |
|
|
idrefs = self.thubanidrefs)) |
|
101 |
|
|
102 |
def testEmptySession(self): |
def testEmptySession(self): |
103 |
"""Save an empty session""" |
"""Save an empty session""" |
111 |
file.close() |
file.close() |
112 |
self.compare_xml(written_contents, |
self.compare_xml(written_contents, |
113 |
'<?xml version="1.0" encoding="UTF-8"?>\n' |
'<?xml version="1.0" encoding="UTF-8"?>\n' |
114 |
'<!DOCTYPE session SYSTEM "thuban-0.8.dtd">\n' |
'<!DOCTYPE session SYSTEM "thuban-1.0.dtd">\n' |
115 |
'<session title="empty session" ' |
'<session title="empty session" ' |
116 |
'xmlns="http://thuban.intevation.org/dtds/thuban-0.8.dtd">' |
'xmlns="http://thuban.intevation.org/dtds/thuban-1.0rc1.dtd">' |
117 |
'\n</session>\n') |
'\n</session>\n') |
118 |
|
|
119 |
self.validate_data(written_contents) |
self.validate_data(written_contents) |
122 |
"""Save a session with a single map with a single layer""" |
"""Save a session with a single map with a single layer""" |
123 |
# deliberately put an apersand in the title :) |
# deliberately put an apersand in the title :) |
124 |
session = Session("single map&layer") |
session = Session("single map&layer") |
125 |
proj = Projection(["zone=26", "proj=utm", "ellps=clrk66"]) |
proj = Projection(["proj=utm", "zone=27", "ellps=WGS84", |
126 |
|
"datum=WGS84", "units=m"], |
127 |
|
name = "WGS 84 / UTM zone 27N", |
128 |
|
epsg = "32627") |
129 |
map = Map("Test Map", projection = proj) |
map = Map("Test Map", projection = proj) |
130 |
session.AddMap(map) |
session.AddMap(map) |
131 |
# use shapefile from the example data |
# use shapefile from the example data |
141 |
written_contents = file.read() |
written_contents = file.read() |
142 |
file.close() |
file.close() |
143 |
expected_template = '''<?xml version="1.0" encoding="UTF-8"?> |
expected_template = '''<?xml version="1.0" encoding="UTF-8"?> |
144 |
<!DOCTYPE session SYSTEM "thuban-0.8.dtd"> |
<!DOCTYPE session SYSTEM "thuban-1.0.dtd"> |
145 |
<session title="single map&layer" |
<session title="single map&layer" |
146 |
xmlns="http://thuban.intevation.org/dtds/thuban-0.8.dtd"> |
xmlns="http://thuban.intevation.org/dtds/thuban-1.0rc1.dtd"> |
147 |
<fileshapesource id="D1" filename="%s" filetype="shapefile"/> |
<fileshapesource id="D1" |
148 |
|
filename="../../Data/iceland/political.shp" |
149 |
|
filetype="shapefile"/> |
150 |
<map title="Test Map"> |
<map title="Test Map"> |
151 |
<projection name="Unknown"> |
<projection epsg="32627" name="WGS 84 / UTM zone 27N"> |
|
<parameter value="zone=26"/> |
|
152 |
<parameter value="proj=utm"/> |
<parameter value="proj=utm"/> |
153 |
<parameter value="ellps=clrk66"/> |
<parameter value="zone=27"/> |
154 |
|
<parameter value="ellps=WGS84"/> |
155 |
|
<parameter value="datum=WGS84"/> |
156 |
|
<parameter value="units=m"/> |
157 |
</projection> |
</projection> |
158 |
<layer title="My Layer" shapestore="D1" |
<layer title="My Layer" shapestore="D1" |
159 |
fill="None" stroke="#000000" stroke_width="1" visible="%s"/> |
fill="None" stroke="#000000" stroke_width="1" visible="%s"/> |
160 |
</map> |
</map> |
161 |
</session>''' |
</session>''' |
162 |
|
|
163 |
expected_contents = expected_template % \ |
expected_contents = expected_template % "true" |
|
(os.path.join("..", "..", "Data", "iceland", "political.shp"), |
|
|
"true") |
|
164 |
|
|
165 |
self.compare_xml(written_contents, expected_contents) |
self.compare_xml(written_contents, expected_contents) |
166 |
|
|
167 |
self.validate_data(written_contents) |
self.validate_data(written_contents) |
168 |
|
|
169 |
|
# Repeat with an invisible layer |
170 |
layer.SetVisible(False) |
layer.SetVisible(False) |
171 |
save_session(session, filename) |
save_session(session, filename) |
172 |
|
|
173 |
file = open(filename) |
file = open(filename) |
174 |
written_contents = file.read() |
written_contents = file.read() |
175 |
file.close() |
file.close() |
176 |
expected_contents = expected_template % \ |
expected_contents = expected_template % "false" |
|
(os.path.join("..", "..", "Data", "iceland", "political.shp"), |
|
|
"false") |
|
177 |
self.compare_xml(written_contents, expected_contents) |
self.compare_xml(written_contents, expected_contents) |
178 |
self.validate_data(written_contents) |
self.validate_data(written_contents) |
179 |
|
|
182 |
def testLayerProjection(self): |
def testLayerProjection(self): |
183 |
"""Test saving layers with projections""" |
"""Test saving layers with projections""" |
184 |
# deliberately put an apersand in the title :) |
# deliberately put an apersand in the title :) |
185 |
session = Session("single map&layer") |
session = self.session = Session("single map&layer") |
186 |
proj = Projection(["zone=26", "proj=utm", "ellps=clrk66"]) |
proj = Projection(["zone=26", "proj=utm", "ellps=clrk66"]) |
187 |
map = Map("Test Map", projection = proj) |
map = Map("Test Map", projection = proj) |
188 |
session.AddMap(map) |
session.AddMap(map) |
190 |
shpfile = os.path.join(os.path.dirname(__file__), |
shpfile = os.path.join(os.path.dirname(__file__), |
191 |
os.pardir, "Data", "iceland", "political.shp") |
os.pardir, "Data", "iceland", "political.shp") |
192 |
layer = Layer("My Layer", session.OpenShapefile(shpfile)) |
layer = Layer("My Layer", session.OpenShapefile(shpfile)) |
193 |
proj = Projection(["proj=lcc", "ellps=clrk66"], "Layer Projection") |
proj = Projection(["proj=lcc", "ellps=clrk66", |
194 |
|
"lat_1=0", "lat_2=20"], |
195 |
|
"Layer Projection") |
196 |
layer.SetProjection(proj) |
layer.SetProjection(proj) |
197 |
map.AddLayer(layer) |
map.AddLayer(layer) |
198 |
|
|
199 |
filename = self.temp_file_name("save_layerproj.thuban") |
filename = self.temp_file_name("save_layerproj.thuban") |
200 |
save_session(session, filename) |
save_session(session, filename) |
|
session.Destroy() |
|
201 |
|
|
202 |
file = open(filename) |
file = open(filename) |
203 |
written_contents = file.read() |
written_contents = file.read() |
204 |
file.close() |
file.close() |
205 |
expected_contents = '''<?xml version="1.0" encoding="UTF-8"?> |
expected_contents = '''<?xml version="1.0" encoding="UTF-8"?> |
206 |
<!DOCTYPE session SYSTEM "thuban-0.8.dtd"> |
<!DOCTYPE session SYSTEM "thuban-1.0.dtd"> |
207 |
<session title="single map&layer" |
<session title="single map&layer" |
208 |
xmlns="http://thuban.intevation.org/dtds/thuban-0.8.dtd"> |
xmlns="http://thuban.intevation.org/dtds/thuban-1.0rc1.dtd"> |
209 |
<fileshapesource id="D1" filename="%s" filetype="shapefile"/> |
<fileshapesource id="D1" |
210 |
|
filename="../../Data/iceland/political.shp" |
211 |
|
filetype="shapefile"/> |
212 |
<map title="Test Map"> |
<map title="Test Map"> |
213 |
<projection name="Unknown"> |
<projection name="Unknown"> |
214 |
<parameter value="zone=26"/> |
<parameter value="zone=26"/> |
220 |
<projection name="Layer Projection"> |
<projection name="Layer Projection"> |
221 |
<parameter value="proj=lcc"/> |
<parameter value="proj=lcc"/> |
222 |
<parameter value="ellps=clrk66"/> |
<parameter value="ellps=clrk66"/> |
223 |
|
<parameter value="lat_1=0"/> |
224 |
|
<parameter value="lat_2=20"/> |
225 |
</projection> |
</projection> |
226 |
</layer> |
</layer> |
227 |
</map> |
</map> |
228 |
</session>''' % os.path.join("..", "..", "Data", "iceland", |
</session>''' |
|
"political.shp") |
|
229 |
#print written_contents |
#print written_contents |
230 |
#print "********************************************" |
#print "********************************************" |
231 |
#print expected_contents |
#print expected_contents |
244 |
layer = RasterLayer("My RasterLayer", imgfile) |
layer = RasterLayer("My RasterLayer", imgfile) |
245 |
map.AddLayer(layer) |
map.AddLayer(layer) |
246 |
|
|
247 |
filename = self.temp_file_name("save_singlemap.thuban") |
filename = self.temp_file_name("%s.thuban" % self.id()) |
248 |
save_session(session, filename) |
save_session(session, filename) |
249 |
session.Destroy() |
session.Destroy() |
250 |
|
|
252 |
written_contents = file.read() |
written_contents = file.read() |
253 |
file.close() |
file.close() |
254 |
expected_contents = '''<?xml version="1.0" encoding="UTF-8"?> |
expected_contents = '''<?xml version="1.0" encoding="UTF-8"?> |
255 |
<!DOCTYPE session SYSTEM "thuban-0.8.dtd"> |
<!DOCTYPE session SYSTEM "thuban-1.0.dtd"> |
256 |
<session title="single map&layer" |
<session title="single map&layer" |
257 |
xmlns="http://thuban.intevation.org/dtds/thuban-0.8.dtd"> |
xmlns="http://thuban.intevation.org/dtds/thuban-1.0rc1.dtd"> |
258 |
<map title="Test Map"> |
<map title="Test Map"> |
259 |
<rasterlayer title="My RasterLayer" filename="%s" |
<rasterlayer title="My RasterLayer" |
260 |
visible="true"> |
filename="../../Data/iceland/island.tif" |
261 |
|
visible="true"> |
262 |
</rasterlayer> |
</rasterlayer> |
263 |
</map> |
</map> |
264 |
</session>''' % os.path.join(os.path.dirname(__file__), |
</session>''' |
|
os.pardir, "Data", "iceland", |
|
|
"island.tif") |
|
265 |
#print written_contents |
#print written_contents |
266 |
#print "********************************************" |
#print "********************************************" |
267 |
#print expected_contents |
#print expected_contents |
270 |
self.validate_data(written_contents) |
self.validate_data(written_contents) |
271 |
|
|
272 |
def testClassifiedLayer(self): |
def testClassifiedLayer(self): |
273 |
"""Save a session with a single map with a single layer |
"""Save a session with a single map with classifications""" |
|
with a classificaton. |
|
|
""" |
|
274 |
# deliberately put an apersand in the title :) |
# deliberately put an apersand in the title :) |
275 |
session = Session("single map&layer") |
session = Session("Map with Classifications") |
276 |
proj = Projection(["zone=26", "proj=utm", "ellps=clrk66"]) |
proj = Projection(["zone=26", "proj=utm", "ellps=clrk66"]) |
277 |
map = Map("Test Map", projection = proj) |
map = Map("Test Map", projection = proj) |
278 |
session.AddMap(map) |
session.AddMap(map) |
281 |
os.pardir, "Data", "iceland", "political.shp") |
os.pardir, "Data", "iceland", "political.shp") |
282 |
layer = Layer("My Layer", session.OpenShapefile(shpfile)) |
layer = Layer("My Layer", session.OpenShapefile(shpfile)) |
283 |
map.AddLayer(layer) |
map.AddLayer(layer) |
284 |
|
layer2 = Layer("My Layer", layer.ShapeStore()) |
285 |
|
map.AddLayer(layer2) |
286 |
|
|
287 |
clazz = layer.GetClassification() |
clazz = layer.GetClassification() |
288 |
|
|
289 |
clazz.SetFieldInfo("AREA", None) |
layer.SetClassificationColumn("AREA") |
290 |
|
|
291 |
clazz.AppendGroup(ClassGroupSingleton(42, |
clazz.AppendGroup(ClassGroupSingleton(42, ClassGroupProperties(), |
292 |
ClassGroupProperties(), |
"single")) |
293 |
"single")) |
clazz.AppendGroup(ClassGroupSingleton("text", ClassGroupProperties(), |
294 |
clazz.AppendGroup(ClassGroupSingleton("text", |
"single-text")) |
|
ClassGroupProperties(), |
|
|
"single-text")) |
|
295 |
|
|
296 |
clazz.AppendGroup(ClassGroupRange(0, 42, |
clazz.AppendGroup(ClassGroupRange((0, 42), |
297 |
ClassGroupProperties(), |
ClassGroupProperties(), |
298 |
"range")) |
"range")) |
299 |
|
|
302 |
range.SetLabel("new-range") |
range.SetLabel("new-range") |
303 |
clazz.AppendGroup(range) |
clazz.AppendGroup(range) |
304 |
|
|
305 |
filename = self.temp_file_name("save_singlemap.thuban") |
|
306 |
|
clazz = layer2.GetClassification() |
307 |
|
layer2.SetClassificationColumn("POPYCOUN") |
308 |
|
|
309 |
|
# Classification with Latin 1 text |
310 |
|
clazz.AppendGroup(ClassGroupSingleton('\xe4\xf6\xfc', # ae, oe, ue |
311 |
|
ClassGroupProperties(), |
312 |
|
'\xdcml\xe4uts')) # Uemlaeuts |
313 |
|
|
314 |
|
|
315 |
|
filename = self.temp_file_name("%s.thuban" % self.id()) |
316 |
save_session(session, filename) |
save_session(session, filename) |
317 |
|
|
318 |
file = open(filename) |
file = open(filename) |
319 |
written_contents = file.read() |
written_contents = file.read() |
320 |
file.close() |
file.close() |
321 |
expected_template = '''<?xml version="1.0" encoding="UTF-8"?> |
expected_contents = '''<?xml version="1.0" encoding="UTF-8"?> |
322 |
<!DOCTYPE session SYSTEM "thuban-0.8.dtd"> |
<!DOCTYPE session SYSTEM "thuban-1.0.dtd"> |
323 |
<session title="single map&layer" |
<session title="Map with Classifications" |
324 |
xmlns="http://thuban.intevation.org/dtds/thuban-0.8.dtd"> |
xmlns="http://thuban.intevation.org/dtds/thuban-1.0rc1.dtd"> |
325 |
<fileshapesource id="D1" filename="%s" filetype="shapefile"/> |
<fileshapesource id="D1" |
326 |
|
filename="../../Data/iceland/political.shp" |
327 |
|
filetype="shapefile"/> |
328 |
<map title="Test Map"> |
<map title="Test Map"> |
329 |
<projection name="Unknown"> |
<projection name="Unknown"> |
330 |
<parameter value="zone=26"/> |
<parameter value="zone=26"/> |
332 |
<parameter value="ellps=clrk66"/> |
<parameter value="ellps=clrk66"/> |
333 |
</projection> |
</projection> |
334 |
<layer title="My Layer" shapestore="D1" |
<layer title="My Layer" shapestore="D1" |
335 |
fill="None" stroke="#000000" stroke_width="1" visible="%s"> |
fill="None" stroke="#000000" stroke_width="1" visible="true"> |
336 |
<classification field="AREA" field_type="double"> |
<classification field="AREA" field_type="double"> |
337 |
<clnull label=""> |
<clnull label=""> |
338 |
<cldata fill="None" stroke="#000000" stroke_width="1"/> |
<cldata fill="None" stroke="#000000" stroke_width="1"/> |
351 |
</clrange> |
</clrange> |
352 |
</classification> |
</classification> |
353 |
</layer> |
</layer> |
354 |
|
<layer title="My Layer" shapestore="D1" |
355 |
|
fill="None" stroke="#000000" stroke_width="1" visible="true"> |
356 |
|
<classification field="POPYCOUN" field_type="string"> |
357 |
|
<clnull label=""> |
358 |
|
<cldata fill="None" stroke="#000000" stroke_width="1"/> |
359 |
|
</clnull> |
360 |
|
<clpoint value="\xc3\xa4\xc3\xb6\xc3\xbc" |
361 |
|
label="\xc3\x9cml\xc3\xa4uts"> |
362 |
|
<cldata fill="None" stroke="#000000" stroke_width="1"/> |
363 |
|
</clpoint> |
364 |
|
</classification> |
365 |
|
</layer> |
366 |
</map> |
</map> |
367 |
</session>''' |
</session>''' |
368 |
|
|
|
expected_contents = expected_template % \ |
|
|
(os.path.join("..", "..", "Data", "iceland", "political.shp"), |
|
|
"true") |
|
|
|
|
369 |
#print written_contents |
#print written_contents |
370 |
#print "********************************************" |
#print "********************************************" |
371 |
#print expected_contents |
#print expected_contents |
377 |
|
|
378 |
def test_dbf_table(self): |
def test_dbf_table(self): |
379 |
"""Test saving a session with a dbf table link""" |
"""Test saving a session with a dbf table link""" |
380 |
session = Session("a DBF Table session") |
session = self.session = Session("a DBF Table session") |
381 |
# use shapefile from the example data |
# use shapefile from the example data |
382 |
dbffile = os.path.join(os.path.dirname(__file__), |
dbffile = os.path.join(os.path.dirname(__file__), |
383 |
os.pardir, "Data", "iceland", "political.dbf") |
os.pardir, "Data", "iceland", "political.dbf") |
389 |
file = open(filename) |
file = open(filename) |
390 |
written_contents = file.read() |
written_contents = file.read() |
391 |
file.close() |
file.close() |
392 |
expected_template = '''<?xml version="1.0" encoding="UTF-8"?> |
expected_contents = '''<?xml version="1.0" encoding="UTF-8"?> |
393 |
<!DOCTYPE session SYSTEM "thuban-0.8.dtd"> |
<!DOCTYPE session SYSTEM "thuban-1.0.dtd"> |
394 |
<session title="a DBF Table session" |
<session title="a DBF Table session" |
395 |
xmlns="http://thuban.intevation.org/dtds/thuban-0.8.dtd"> |
xmlns="http://thuban.intevation.org/dtds/thuban-1.0rc1.dtd"> |
396 |
<filetable id="D1" filename="%s" filetype="DBF" title="political"/> |
<filetable id="D1" filename="../../Data/iceland/political.dbf" |
397 |
|
filetype="DBF" title="political"/> |
398 |
</session>''' |
</session>''' |
399 |
|
|
|
expected_contents = expected_template % dbffile |
|
400 |
self.compare_xml(written_contents, expected_contents) |
self.compare_xml(written_contents, expected_contents) |
401 |
|
self.validate_data(written_contents) |
402 |
|
|
403 |
def test_joined_table(self): |
def test_joined_table(self): |
404 |
"""Test saving a session with joined table""" |
"""Test saving a session with joined table""" |
434 |
# print col.name |
# print col.name |
435 |
joined = TransientJoinedTable(session.TransientDB(), |
joined = TransientJoinedTable(session.TransientDB(), |
436 |
store.Table(), "RDLNTYPE", |
store.Table(), "RDLNTYPE", |
437 |
dbftable, "RDTYPE") |
dbftable, "RDTYPE", |
438 |
|
outer_join = True) |
439 |
store = session.AddShapeStore(DerivedShapeStore(store, joined)) |
store = session.AddShapeStore(DerivedShapeStore(store, joined)) |
440 |
layer.SetShapeStore(store) |
layer.SetShapeStore(store) |
441 |
|
|
447 |
file = open(filename) |
file = open(filename) |
448 |
written_contents = file.read() |
written_contents = file.read() |
449 |
file.close() |
file.close() |
450 |
expected_template = '''<?xml version="1.0" encoding="UTF-8"?> |
expected_contents = '''<?xml version="1.0" encoding="UTF-8"?> |
451 |
<!DOCTYPE session SYSTEM "thuban-0.8.dtd"> |
<!DOCTYPE session SYSTEM "thuban-1.0.dtd"> |
452 |
<session title="A Joined Table session" |
<session title="A Joined Table session" |
453 |
xmlns="http://thuban.intevation.org/dtds/thuban-0.8.dtd"> |
xmlns="http://thuban.intevation.org/dtds/thuban-1.0rc1.dtd"> |
454 |
<fileshapesource filename="%(shpfile)s" |
<fileshapesource filename="../../Data/iceland/roads-line.shp" |
455 |
filetype="shapefile" id="D142197204"/> |
filetype="shapefile" id="D142197204"/> |
456 |
<filetable filename="%(dbffile)s" |
<filetable filename="save_joinedtable.dbf" |
457 |
title="save_joinedtable" |
title="save_joinedtable" |
458 |
filetype="DBF" id="D141881756"/> |
filetype="DBF" id="D141881756"/> |
459 |
<jointable id="D142180284" |
<jointable id="D142180284" |
460 |
title="Join of roads-line and save_joinedtable" |
title="Join of roads-line and save_joinedtable" |
461 |
leftcolumn="RDLNTYPE" left="D142197204" |
leftcolumn="RDLNTYPE" left="D142197204" |
462 |
rightcolumn="RDTYPE" right="D141881756"/> |
rightcolumn="RDTYPE" right="D141881756" |
463 |
|
jointype="LEFT OUTER" /> |
464 |
<derivedshapesource id="D141915644" |
<derivedshapesource id="D141915644" |
465 |
table="D142180284" |
table="D142180284" |
466 |
shapesource="D142197204"/> |
shapesource="D142197204"/> |
471 |
</map> |
</map> |
472 |
</session>''' |
</session>''' |
473 |
|
|
|
expected_contents = expected_template % { |
|
|
"dbffile": relative_filename(self.temp_dir(), dbffile), |
|
|
"shpfile": relative_filename(self.temp_dir(), shpfile) |
|
|
} |
|
474 |
self.compare_xml(written_contents, expected_contents) |
self.compare_xml(written_contents, expected_contents) |
475 |
|
self.validate_data(written_contents) |
476 |
finally: |
finally: |
477 |
session.Destroy() |
session.Destroy() |
478 |
session = None |
session = None |
479 |
|
|
480 |
|
|
481 |
|
def test_save_postgis(self): |
482 |
|
"""Test saving a session with a postgis connection""" |
483 |
|
|
484 |
|
class NonConnection(PostGISConnection): |
485 |
|
"""connection class that doesn't actually connect """ |
486 |
|
def connect(self): |
487 |
|
pass |
488 |
|
|
489 |
|
class NonConnectionStore(PostGISShapeStore): |
490 |
|
"""Shapestore that doesn't try to access the server""" |
491 |
|
def _fetch_table_information(self): |
492 |
|
pass |
493 |
|
|
494 |
|
session = Session("A PostGIS Session") |
495 |
|
try: |
496 |
|
dbconn = NonConnection(dbname="plugh", host="xyzzy", port="42", |
497 |
|
user="grue") |
498 |
|
session.AddDBConnection(dbconn) |
499 |
|
map = Map("Test Map") |
500 |
|
session.AddMap(map) |
501 |
|
store = NonConnectionStore(dbconn, "roads") |
502 |
|
session.AddShapeStore(store) |
503 |
|
layer = Layer("Roads to Nowhere", store) |
504 |
|
map.AddLayer(layer) |
505 |
|
|
506 |
|
# Save the session |
507 |
|
filename = self.temp_file_name(self.id() + ".thuban") |
508 |
|
save_session(session, filename) |
509 |
|
|
510 |
|
# Read it back and compare |
511 |
|
file = open(filename) |
512 |
|
written = file.read() |
513 |
|
file.close() |
514 |
|
expected = '''<?xml version="1.0" encoding="UTF-8"?> |
515 |
|
<!DOCTYPE session SYSTEM "thuban-1.0.dtd"> |
516 |
|
<session title="A PostGIS Session" |
517 |
|
xmlns="http://thuban.intevation.org/dtds/thuban-1.0rc1.dtd"> |
518 |
|
<dbconnection id="DB" |
519 |
|
dbtype="postgis" dbname="plugh" |
520 |
|
host="xyzzy" port="42" |
521 |
|
user="grue"/> |
522 |
|
<dbshapesource id="roads" dbconn="DB" tablename="roads"/> |
523 |
|
<map title="Test Map"> |
524 |
|
<layer title="Roads to Nowhere" |
525 |
|
shapestore="roads" visible="true" |
526 |
|
stroke="#000000" stroke_width="1" fill="None"/> |
527 |
|
</map> |
528 |
|
</session>''' |
529 |
|
self.compare_xml(written, expected) |
530 |
|
self.validate_data(written) |
531 |
|
finally: |
532 |
|
session.Destroy() |
533 |
|
|
534 |
|
|
535 |
class MockDataStore: |
class MockDataStore: |
536 |
|
|
537 |
"""A very simple data store that only has dependencies""" |
"""A very simple data store that only has dependencies""" |