/[thuban]/branches/WIP-pyshapelib-bramz/test/test_save.py
ViewVC logotype

Contents of /branches/WIP-pyshapelib-bramz/test/test_save.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 723 - (show annotations)
Thu Apr 24 15:31:53 2003 UTC (21 years, 10 months ago) by bh
Original Path: trunk/thuban/test/test_save.py
File MIME type: text/x-python
File size: 4439 byte(s)
First step towards table management. Introduce a simple data
abstraction so that we replace the data a layer uses more easily
in the next step.

* Thuban/Model/data.py: New file with a simple data abstraction
that bundles shapefile and dbffile into one object.

* Thuban/Model/session.py (Session.OpenShapefile): New method to
open shapefiles and return a shape store object

* Thuban/Model/layer.py (Layer.__init__): Pass the data as a store
object instead of a shapefile filename. This introduces a new
instance variable store holding the datastore. For intermediate
backwards compatibility keep the old instance variables.
(open_shapefile): Removed. No longer needed with the shape store.
(Layer.SetShapeStore, Layer.ShapeStore): New methods to set and
get the shape store used by a layer.
(Layer.Destroy): No need to explicitly destroy the shapefile or
table anymore.

* Thuban/UI/mainwindow.py (MainWindow.AddLayer)
(MainWindow.AddLayer): Use the session's OpenShapefile method to
open shapefiles

* Thuban/Model/load.py (ProcessSession.start_layer): Use the
session's OpenShapefile method to open shapefiles

* test/test_classification.py
(TestClassification.test_classification): Use the session's
OpenShapefile method to open shapefiles and build the filename in
a more platform independed way

* test/test_layer.py (TestLayer.setUp, TestLayer.tearDown):
Implement to have a session to use in the tests
(TestLayer.test_arc_layer, TestLayer.test_polygon_layer)
(TestLayer.test_point_layer, TestLayer.test_empty_layer): Use the
session's OpenShapefile method to open shapefiles
(TestLayerLegend.setUp): Instantiate a session so that we can use
it to open shapefiles.
(TestLayerLegend.tearDown): Make sure that all references to
layers and session are removed otherwise we may get a resource
leak

* test/test_map.py (TestMapAddLayer.test_add_layer)
(TestMapWithContents.setUp): Instantiate a session so that we can
use it to open shapefiles.
(TestMapWithContents.tearDown): Make sure that all references to
layers, maps and sessions are removed otherwise we may get a
resource leak
("__main__"): use support.run_tests() so that more info about
uncollected garbage is printed

* test/test_save.py (SaveSessionTest.testSingleLayer): Use the
session's OpenShapefile method to open shapefiles
("__main__"): use support.run_tests() so that more info about
uncollected garbage is printed

* test/test_selection.py (TestSelection.tearDown): Make sure that
all references to the session and the selection are removed
otherwise we may get a resource leak
(TestSelection.get_layer): Instantiate a session so that we can
use it to open shapefiles.
("__main__"): use support.run_tests() so that more info about
uncollected garbage is printed

* test/test_session.py (TestSessionBase.tearDown)
(TestSessionWithContent.tearDown): Make sure that all references
to the session and layers are removed otherwise we may get a
resource leak
(TestSessionWithContent.setUp): Use the session's OpenShapefile
method to open shapefiles

1 # Copyright (c) 2002, 2003 by Intevation GmbH
2 # Authors:
3 # Bernhard Herzog <[email protected]>
4 #
5 # This program is free software under the GPL (>=v2)
6 # Read the file COPYING coming with Thuban for details.
7
8 """
9 Test saving a thuban session as XML
10 """
11
12 __version__ = "$Revision$"
13 # $Source$
14 # $Id$
15
16 import os
17 import unittest
18 from StringIO import StringIO
19
20 import xml.sax
21 import xml.sax.handler
22 from xml.sax import make_parser, ErrorHandler, SAXNotRecognizedException
23
24 import support
25 support.initthuban()
26
27 from Thuban.Model.save import save_session
28 from Thuban.Model.session import Session
29 from Thuban.Model.map import Map
30 from Thuban.Model.layer import Layer
31 from Thuban.Model.proj import Projection
32
33
34 class SaxEventLister(xml.sax.handler.ContentHandler):
35
36 def __init__(self):
37 self.eventlist = []
38
39 def startElementNS(self, name, qname, attrs):
40 items = attrs.items()
41 items.sort()
42 self.eventlist.append(("start", name, qname, items))
43
44 def endElementNS(self, name, qname):
45 self.eventlist.append(("end", name, qname))
46
47
48 def sax_eventlist(data):
49 """Return a list of SAX event generated for the XML data.
50 """
51 handler = SaxEventLister()
52 parser = make_parser()
53 parser.setContentHandler(handler)
54 parser.setErrorHandler(ErrorHandler())
55 parser.setFeature(xml.sax.handler.feature_namespaces, 1)
56
57 #
58 # see comment at the end of Thuban/Model/load.py
59 #
60 try:
61 parser.setFeature(xml.sax.handler.feature_validation, 0)
62 parser.setFeature(xml.sax.handler.feature_external_ges, 0)
63 parser.setFeature(xml.sax.handler.feature_external_pes, 0)
64 except SAXNotRecognizedException:
65 pass
66
67 inpsrc = xml.sax.InputSource()
68 inpsrc.setByteStream(StringIO(data))
69 parser.parse(inpsrc)
70
71 return handler.eventlist
72
73 class SaveSessionTest(unittest.TestCase, support.FileTestMixin):
74
75 def compare_xml(self, xml1, xml2):
76 self.assertEquals(sax_eventlist(xml1), sax_eventlist(xml2))
77
78 def testEmptySession(self):
79 """Save an empty session"""
80 session = Session("empty session")
81 filename = self.temp_file_name("save_emptysession.thuban")
82 save_session(session, filename)
83 session.Destroy()
84
85 file = open(filename)
86 written_contents = file.read()
87 file.close()
88 self.compare_xml(written_contents,
89 '<?xml version="1.0" encoding="UTF-8"?>\n'
90 '<!DOCTYPE session SYSTEM "thuban.dtd">\n'
91 '<session title="empty session">\n</session>\n')
92
93 def testSingleLayer(self):
94 """Save a session with a single map with a single layer"""
95 # deliberately put an apersand in the title :)
96 session = Session("single map&layer")
97 proj = Projection(["zone=26", "proj=utm", "ellps=clrk66"])
98 map = Map("Test Map", projection = proj)
99 session.AddMap(map)
100 # use shapefile from the example data
101 shpfile = os.path.join(os.path.dirname(__file__),
102 os.pardir, "Data", "iceland", "political.shp")
103 layer = Layer("My Layer", session.OpenShapefile(shpfile))
104 map.AddLayer(layer)
105
106 filename = self.temp_file_name("save_singlemap.thuban")
107 save_session(session, filename)
108 session.Destroy()
109
110 file = open(filename)
111 written_contents = file.read()
112 file.close()
113 expected_contents = '''<?xml version="1.0" encoding="UTF-8"?>
114 <!DOCTYPE session SYSTEM "thuban.dtd">
115 <session title="single map&amp;layer">
116 <map title="Test Map">
117 <projection>
118 <parameter value="zone=26"/>
119 <parameter value="proj=utm"/>
120 <parameter value="ellps=clrk66"/>
121 </projection>
122 <layer title="My Layer" filename="%s"
123 fill="None" stroke="#000000" stroke_width="1"/>
124 </map>
125 </session>''' % os.path.join("..", "..", "Data", "iceland",
126 "political.shp")
127 #print written_contents
128 #print "********************************************"
129 #print expected_contents
130 self.compare_xml(written_contents, expected_contents)
131
132
133
134 if __name__ == "__main__":
135 # Fake the __file__ global because it's needed by a test
136 import sys
137 __file__ = sys.argv[0]
138 support.run_tests()

Properties

Name Value
svn:eol-style native
svn:keywords Author Date Id Revision

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26