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

Annotation of /branches/WIP-pyshapelib-bramz/test/test_classification.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 723 - (hide annotations)
Thu Apr 24 15:31:53 2003 UTC (21 years, 10 months ago) by bh
Original Path: trunk/thuban/test/test_classification.py
File MIME type: text/x-python
File size: 10012 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 bh 598 # Copyright (c) 2002, 2003 by Intevation GmbH
2 jonathan 369 # Authors:
3     # Jonathan Coles <[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 the Classification class
10     """
11    
12     __version__ = "$Revision$"
13     # $Source$
14     # $Id$
15    
16 jonathan 482 from __future__ import nested_scopes
17    
18 jonathan 369 import unittest
19    
20     import support
21     support.initthuban()
22    
23 bh 723 import os
24 jonathan 482 from Thuban.Model.table import *
25     from Thuban.Model.classification import *
26 bh 723 from Thuban.Model.session import Session
27 jonathan 395 from Thuban.Model.layer import Layer
28 jonathan 369
29 jonathan 482 import copy
30 jonathan 369
31 jonathan 482
32 jonathan 369 class TestClassification(unittest.TestCase):
33    
34 jonathan 482 def test_ClassGroupProperties(self):
35     """Test ClassGroupProperties"""
36    
37     props = ClassGroupProperties()
38     self.assertEqual(props.GetLineColor(), Color.Black)
39     self.assertEqual(props.GetLineWidth(), 1)
40 jonathan 610 self.assertEqual(props.GetFill(), Color.Transparent)
41 jonathan 482
42     red = Color(1, 0, 0)
43     props.SetLineColor(red)
44     self.assertEqual(props.GetLineColor(), red)
45    
46     blue = Color(0, 0, 1)
47     props.SetLineColor(blue)
48     self.assertEqual(props.GetLineColor(), blue)
49    
50     props.SetLineWidth(10)
51     self.assertEqual(props.GetLineWidth(), 10)
52    
53     self.assertRaises(ValueError, props.SetLineWidth, -10)
54     self.assertEqual(props.GetLineWidth(), 10)
55    
56     newProps1 = ClassGroupProperties()
57     newProps2 = ClassGroupProperties()
58     self.assertNotEqual(newProps1, props)
59     self.assertEqual(newProps1, newProps2)
60    
61     def test_ClassGroup(self):
62     """Test ClassGroup"""
63    
64     # test constructor with no label
65     group = ClassGroup()
66     self.assertEqual(group.GetLabel(), "")
67    
68     # test constructor with label
69     group = ClassGroup("hallo")
70     self.assertEqual(group.GetLabel(), "hallo")
71    
72     # test SetLabel()/GetLabel()
73     group = ClassGroup("welt")
74     group.SetLabel("hallo")
75     self.assertEqual(group.GetLabel(), "hallo")
76    
77     group.SetLabel("")
78     self.assertEqual(group.GetLabel(), "")
79    
80     # test Matches
81 jonathan 610 # Matches() is a virtual function...can't test it here
82     #
83     #self.assertEqual(group.Matches(None), False)
84     #self.assertEqual(group.Matches(1), False)
85     #self.assertEqual(group.Matches("hallo"), False)
86     #self.assertEqual(group.Matches([]), False)
87 jonathan 482
88 jonathan 610 # test GetProperties...also a virtual function
89     #self.assertEqual(group.GetProperties(), None)
90 jonathan 482
91     def test_ClassGroupDefault(self):
92     """Test ClassGroupDefault"""
93    
94     defProps = ClassGroupProperties()
95    
96     newProps = ClassGroupProperties()
97     newProps.SetLineColor(Color(.25, .5, .75))
98     newProps.SetLineWidth(5)
99     newProps.SetFill(Color(.12, .24, .36))
100    
101     # test constructor
102    
103     group = ClassGroupDefault(newProps)
104     self.assertEqual(group.GetProperties(), newProps)
105    
106     group = ClassGroupDefault(newProps, "hallo")
107     self.assertEqual(group.GetProperties(), newProps)
108     self.assertEqual(group.GetLabel(), "hallo")
109    
110     # test empty constructor
111     group = ClassGroupDefault()
112     props = group.GetProperties()
113    
114     self.assertEqual(group.GetLabel(), "")
115     self.assertEqual(defProps, props)
116    
117     # test Matches()
118     self.assertEqual(group.Matches(None), True)
119     self.assertEqual(group.Matches(1), True)
120     self.assertEqual(group.Matches("hallo"), True)
121     self.assertEqual(group.Matches([]), True)
122    
123     # test SetProperties()/GetProperties()
124     group.SetProperties(newProps)
125     self.assertEqual(group.GetProperties(), newProps)
126    
127     # test copy
128     groupCopy = copy.copy(group)
129     self.assertEqual(group, groupCopy)
130    
131     def test_ClassGroupRange(self):
132     """Test ClassGroupRange"""
133    
134     defProps = ClassGroupProperties()
135     newProps = ClassGroupProperties()
136     newProps.SetLineColor(Color(.25, .5, .75))
137     newProps.SetLineWidth(5)
138     newProps.SetFill(Color(.12, .24, .36))
139    
140     # test empty constructor
141     group = ClassGroupRange()
142    
143     self.assertEqual(group.GetMin(), 0)
144     self.assertEqual(group.GetMax(), 1)
145     self.assertEqual(group.GetProperties(), defProps)
146     self.assertEqual(group.GetLabel(), "")
147    
148     # test SetMax()
149     self.assertRaises(ValueError, group.SetMax, 0)
150     self.assertRaises(ValueError, group.SetMax, -1)
151     self.assertEquals(group.GetMax(), 1)
152     group.SetMax(2)
153     self.assertEquals(group.GetMax(), 2)
154    
155     # test SetMin()
156     self.assertRaises(ValueError, group.SetMin, 2)
157     self.assertRaises(ValueError, group.SetMin, 3)
158     self.assertEquals(group.GetMin(), 0)
159     group.SetMin(-5)
160     self.assertEquals(group.GetMin(), -5)
161    
162     # test SetProperties()/GetProperties()
163     group.SetProperties(newProps)
164     self.assertEqual(group.GetProperties(), newProps)
165    
166     # test SetRange()
167     self.assertRaises(ValueError, group.SetRange, 1, 0)
168     group.SetRange(-5, 5)
169     self.assertEqual(group.GetRange(), (-5, 5))
170    
171     # test Matches()
172     self.assertEqual(group.Matches(-6), False)
173     self.assertEqual(group.Matches(-5), True)
174     self.assertEqual(group.Matches(0), True)
175     self.assertEqual(group.Matches(4), True)
176     self.assertEqual(group.Matches(5), False)
177    
178     # test copy
179     groupCopy = copy.copy(group)
180     self.assertEqual(group, groupCopy)
181    
182     def test_ClassGroupSingleton(self):
183     """Test ClassGroupSingleton"""
184    
185     defProps = ClassGroupProperties()
186     newProps = ClassGroupProperties()
187     newProps.SetLineColor(Color(.25, .5, .75))
188     newProps.SetLineWidth(5)
189     newProps.SetFill(Color(.12, .24, .36))
190    
191     # test empty constructor
192     group = ClassGroupSingleton()
193    
194     self.assertEqual(group.GetValue(), 0)
195     self.assertEqual(group.GetProperties(), defProps)
196     self.assertEqual(group.GetLabel(), "")
197    
198     # test SetProperties()/GetProperties()
199     group.SetProperties(newProps)
200     self.assertEqual(group.GetProperties(), newProps)
201    
202     # test SetValue()
203     group.SetValue(5)
204     self.assertEqual(group.GetValue(), 5)
205    
206     # test Matches()
207     self.assertEqual(group.Matches(0), False)
208     self.assertEqual(group.Matches(5), True)
209    
210     group.SetValue("5")
211     self.assertNotEqual(group.GetValue(), 5)
212    
213     # test Matches()
214     self.assertEqual(group.Matches(5), False)
215     self.assertEqual(group.Matches("5"), True)
216    
217     group.SetValue("hallo")
218     self.assertEqual(group.GetValue(), "hallo")
219    
220     # test Matches()
221     self.assertEqual(group.Matches("HALLO"), False)
222     self.assertEqual(group.Matches("hallo"), True)
223    
224     # test copy
225     groupCopy = copy.copy(group)
226     self.assertEqual(group, groupCopy)
227    
228    
229     def test_ClassIterator(self):
230     """Test ClassIterator"""
231    
232     groups = [ClassGroupSingleton(5), ClassGroupSingleton(5),
233     ClassGroupRange(-3, 3), ClassGroupSingleton(-5),
234     ClassGroupDefault()]
235    
236     clazz = Classification()
237    
238     for g in groups:
239 jonathan 618 clazz.AppendGroup(g)
240 jonathan 482
241     def convert(clazz):
242     if isinstance(clazz, ClassGroupDefault): return 0
243     if isinstance(clazz, ClassGroupSingleton): return 1
244     if isinstance(clazz, ClassGroupRange): return 2
245    
246     list = []
247     for g in clazz:
248     list.append(convert(g))
249    
250 jonathan 618 self.assertEquals(list, [0, 1, 1, 2, 1, 0])
251 jonathan 482
252 jonathan 369 def test_classification(self):
253     """Test Classification"""
254    
255 jonathan 482 defProps = ClassGroupProperties()
256     red = Color(1, 0, 0)
257     green = Color(0, 1, 0)
258     blue = Color(0, 0, 1)
259    
260 bh 723 session = Session("Test session")
261     filename = os.path.join("..", "Data", "iceland", "political.dbf")
262     layer = Layer("asdf", session.OpenShapefile(filename))
263 jonathan 395
264 jonathan 369 #
265     # init with no params
266     #
267 jonathan 482 c = Classification()
268 jonathan 446 self.assertEqual(c.GetField(), None)
269 jonathan 482 self.assertEqual(c.GetFieldType(), None)
270 jonathan 618 self.assertEqual(c.FindGroup(-1), c.GetDefaultGroup())
271 jonathan 369
272 jonathan 482 c.SetDefaultLineColor(red)
273     self.assertEqual(c.GetDefaultLineColor(), red)
274 jonathan 610 self.assertEqual(c.GetDefaultFill(), Color.Transparent)
275 jonathan 369
276 jonathan 482 c.SetDefaultFill(green)
277     self.assertEqual(c.GetDefaultFill(), green)
278     self.assertEqual(c.GetDefaultLineColor(), red)
279 jonathan 369
280 jonathan 482 c.SetField("hallo")
281     self.assertEqual(c.GetField(), "hallo")
282 jonathan 369
283 jonathan 482 c.SetFieldType(FIELDTYPE_STRING)
284     self.assertEqual(c.GetFieldType(), FIELDTYPE_STRING)
285 jonathan 369
286 jonathan 494 # should raise an exception because 'hallo' doesn't
287     # exist in the table
288     self.assertRaises(ValueError, c.SetLayer, layer)
289    
290     c.SetField("AREA")
291 jonathan 482 c.SetLayer(layer)
292     self.assertEqual(c.GetLayer(), layer)
293 jonathan 494 self.assertEqual(c.GetField(), "AREA")
294     self.assertEqual(c.GetFieldType(), FIELDTYPE_DOUBLE)
295 jonathan 369
296 jonathan 482 c.SetField(None)
297 jonathan 494 self.assertEquals(c.GetFieldType(), None)
298 jonathan 618 self.assertEquals(c.FindGroup(5), c.GetDefaultGroup())
299 jonathan 369
300 jonathan 494 c.SetField("AREA")
301 jonathan 482 s = ClassGroupSingleton(5)
302 jonathan 618 c.AppendGroup(s)
303     self.assertEquals(c.FindGroup(5), s)
304     self.assertEquals(c.FindGroup(0), c.GetDefaultGroup())
305 jonathan 369
306 jonathan 482 r = ClassGroupRange(-10, 10)
307 jonathan 618 c.AppendGroup(r)
308     self.assertEquals(c.FindGroup(-11), c.GetDefaultGroup())
309     self.assertEquals(c.FindGroup(-10), r)
310     self.assertEquals(c.FindGroup(9), r)
311     self.assertEquals(c.FindGroup(5), s)
312     self.assertEquals(c.FindGroup(10), c.GetDefaultGroup())
313 jonathan 369
314 jonathan 656 clazz = copy.deepcopy(c)
315    
316     self.assertEquals(clazz.GetNumGroups(), c.GetNumGroups())
317    
318     for i in range(clazz.GetNumGroups()):
319     self.assertEquals(clazz.GetGroup(i), c.GetGroup(i))
320    
321 bh 598 layer.Destroy()
322    
323 jonathan 369 if __name__ == "__main__":
324 bh 598 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