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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1903 - (show annotations)
Fri Oct 31 16:52:33 2003 UTC (21 years, 4 months ago) by bh
Original Path: trunk/thuban/test/test_classification.py
File MIME type: text/x-python
File size: 11104 byte(s)
(red, green, blue): New. These
constants were used in several cases. Update the relevant methods.
(TestClassification.test_defaults)
(TestClassification.test_set_default_properties)
(TestClassification.test_add_singleton)
(TestClassification.test_add_range)
(TestClassification.test_multiple_groups)
(TestClassification.test_deepcopy): New. These were formerly all
part of the single method test.
(TestClassification.test_deepcopy): Removed.
(TestClassIterator): Removed. The test case is now a method of
TestClassification since it tests part of the public interface of
Classification
(TestClassification.test_iterator): New. Used to be
TestClassIterator effectively

1 # Copyright (c) 2002, 2003 by Intevation GmbH
2 # 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 from __future__ import nested_scopes
17
18 import unittest
19
20 import support
21 support.initthuban()
22
23 import os
24 import copy
25
26 from Thuban.Model.color import Color, Transparent, Black
27 from Thuban.Model.table import FIELDTYPE_INT, FIELDTYPE_STRING, FIELDTYPE_DOUBLE
28 from Thuban.Model.classification import \
29 Classification, ClassGroup, \
30 ClassGroupDefault, ClassGroupSingleton, ClassGroupRange,\
31 ClassGroupProperties
32
33 from Thuban.Model.session import Session
34 from Thuban.Model.layer import Layer
35 from Thuban.Model.range import Range
36
37
38
39 # A few colors for use by the test cases
40 red = Color(1, 0, 0)
41 green = Color(0, 1, 0)
42 blue = Color(0, 0, 1)
43
44
45 class TestClassGroupProperties(unittest.TestCase):
46
47 def test(self):
48 """Test ClassGroupProperties"""
49
50 props = ClassGroupProperties()
51 self.assertEqual(props.GetLineColor(), Black)
52 self.assertEqual(props.GetLineWidth(), 1)
53 self.assertEqual(props.GetFill(), Transparent)
54
55 props.SetLineColor(red)
56 self.assertEqual(props.GetLineColor(), red)
57
58 props.SetLineColor(blue)
59 self.assertEqual(props.GetLineColor(), blue)
60
61 props.SetLineWidth(10)
62 self.assertEqual(props.GetLineWidth(), 10)
63
64 self.assertRaises(ValueError, props.SetLineWidth, -10)
65 self.assertEqual(props.GetLineWidth(), 10)
66
67 newProps1 = ClassGroupProperties()
68 newProps2 = ClassGroupProperties()
69 self.assertNotEqual(newProps1, props)
70 self.assertEqual(newProps1, newProps2)
71
72
73 class TestClassGroup(unittest.TestCase):
74
75 def test(self):
76 """Test ClassGroup"""
77
78 # test constructor with no label
79 group = ClassGroup()
80 self.assertEqual(group.GetLabel(), "")
81
82 # test constructor with label
83 group = ClassGroup("hallo")
84 self.assertEqual(group.GetLabel(), "hallo")
85
86 # test SetLabel()/GetLabel()
87 group = ClassGroup("welt")
88 group.SetLabel("hallo")
89 self.assertEqual(group.GetLabel(), "hallo")
90
91 group.SetLabel("")
92 self.assertEqual(group.GetLabel(), "")
93
94 # test Matches
95 # Matches() is a virtual function...can't test it here
96 #
97 #self.assertEqual(group.Matches(None), False)
98 #self.assertEqual(group.Matches(1), False)
99 #self.assertEqual(group.Matches("hallo"), False)
100 #self.assertEqual(group.Matches([]), False)
101
102 # test GetProperties...also a virtual function
103 #self.assertEqual(group.GetProperties(), None)
104
105
106 class TestClassGroupDefault(unittest.TestCase):
107
108 def test(self):
109 """Test ClassGroupDefault"""
110
111 defProps = ClassGroupProperties()
112
113 newProps = ClassGroupProperties()
114 newProps.SetLineColor(Color(.25, .5, .75))
115 newProps.SetLineWidth(5)
116 newProps.SetFill(Color(.12, .24, .36))
117
118 # test constructor
119
120 group = ClassGroupDefault(newProps)
121 self.assertEqual(group.GetProperties(), newProps)
122
123 group = ClassGroupDefault(newProps, "hallo")
124 self.assertEqual(group.GetProperties(), newProps)
125 self.assertEqual(group.GetLabel(), "hallo")
126
127 # test empty constructor
128 group = ClassGroupDefault()
129 props = group.GetProperties()
130
131 self.assertEqual(group.GetLabel(), "")
132 self.assertEqual(defProps, props)
133
134 # test Matches()
135 self.assertEqual(group.Matches(None), True)
136 self.assertEqual(group.Matches(1), True)
137 self.assertEqual(group.Matches("hallo"), True)
138 self.assertEqual(group.Matches([]), True)
139
140 # test SetProperties()/GetProperties()
141 group.SetProperties(newProps)
142 self.assertEqual(group.GetProperties(), newProps)
143
144 # test copy
145 groupCopy = copy.copy(group)
146 self.assertEqual(group, groupCopy)
147
148
149 class TestClassGroupRange(unittest.TestCase):
150
151 def test(self):
152 """Test ClassGroupRange"""
153
154 defProps = ClassGroupProperties()
155 newProps = ClassGroupProperties()
156 newProps.SetLineColor(Color(.25, .5, .75))
157 newProps.SetLineWidth(5)
158 newProps.SetFill(Color(.12, .24, .36))
159
160 # test empty constructor
161 group = ClassGroupRange()
162
163 self.assertEqual(group.GetMin(), 0)
164 self.assertEqual(group.GetMax(), 1)
165 self.assertEqual(group.GetProperties(), defProps)
166 self.assertEqual(group.GetLabel(), "")
167
168 # test SetMax()
169 self.assertRaises(ValueError, group.SetMax, 0)
170 self.assertRaises(ValueError, group.SetMax, -1)
171 self.assertEquals(group.GetMax(), 1)
172 group.SetMax(2)
173 self.assertEquals(group.GetMax(), 2)
174
175 # test SetMin()
176 self.assertRaises(ValueError, group.SetMin, 2)
177 self.assertRaises(ValueError, group.SetMin, 3)
178 self.assertEquals(group.GetMin(), 0)
179 group.SetMin(-5)
180 self.assertEquals(group.GetMin(), -5)
181
182 # test SetProperties()/GetProperties()
183 group.SetProperties(newProps)
184 self.assertEqual(group.GetProperties(), newProps)
185
186 # test SetRange()
187 self.assertRaises(ValueError, group.SetRange, (1, 0))
188 group.SetRange(Range("]-oo;6]"))
189 self.assertEqual(group.GetRange(), "]-oo;6]")
190 group.SetRange((-5, 5))
191 self.assertEqual(group.GetRange(), "[-5;5[")
192
193 # test Matches()
194 self.assertEqual(group.Matches(-6), False)
195 self.assertEqual(group.Matches(-5), True)
196 self.assertEqual(group.Matches(0), True)
197 self.assertEqual(group.Matches(4), True)
198 self.assertEqual(group.Matches(5), False)
199
200 # test copy
201 groupCopy = copy.copy(group)
202 self.assertEqual(group, groupCopy)
203
204
205 class TestClassGroupSingleton(unittest.TestCase):
206
207 def test(self):
208 """Test ClassGroupSingleton"""
209
210 defProps = ClassGroupProperties()
211 newProps = ClassGroupProperties()
212 newProps.SetLineColor(Color(.25, .5, .75))
213 newProps.SetLineWidth(5)
214 newProps.SetFill(Color(.12, .24, .36))
215
216 # test empty constructor
217 group = ClassGroupSingleton()
218
219 self.assertEqual(group.GetValue(), 0)
220 self.assertEqual(group.GetProperties(), defProps)
221 self.assertEqual(group.GetLabel(), "")
222
223 # test SetProperties()/GetProperties()
224 group.SetProperties(newProps)
225 self.assertEqual(group.GetProperties(), newProps)
226
227 # test SetValue()
228 group.SetValue(5)
229 self.assertEqual(group.GetValue(), 5)
230
231 # test Matches()
232 self.assertEqual(group.Matches(0), False)
233 self.assertEqual(group.Matches(5), True)
234
235 group.SetValue("5")
236 self.assertNotEqual(group.GetValue(), 5)
237
238 # test Matches()
239 self.assertEqual(group.Matches(5), False)
240 self.assertEqual(group.Matches("5"), True)
241
242 group.SetValue("hallo")
243 self.assertEqual(group.GetValue(), "hallo")
244
245 # test Matches()
246 self.assertEqual(group.Matches("HALLO"), False)
247 self.assertEqual(group.Matches("hallo"), True)
248
249 # test copy
250 groupCopy = copy.copy(group)
251 self.assertEqual(group, groupCopy)
252
253
254 class TestClassification(unittest.TestCase):
255
256 """Test cases for Classification"""
257
258 def test_defaults(self):
259 """Test Classification default settings"""
260 c = Classification()
261 self.assertEqual(c.FindGroup(-1), c.GetDefaultGroup())
262 self.assertEqual(c.GetDefaultLineColor(), Black)
263 self.assertEqual(c.GetDefaultFill(), Transparent)
264
265 # The default group is not counted, hence 0 groups
266 self.assertEqual(c.GetNumGroups(), 0)
267
268 def test_set_default_properties(self):
269 """Test Classification.SetDefaultLineColor and SetDefaultFill"""
270 c = Classification()
271
272 c.SetDefaultLineColor(red)
273 self.assertEqual(c.GetDefaultLineColor(), red)
274 self.assertEqual(c.GetDefaultFill(), Transparent)
275
276 c.SetDefaultFill(green)
277 self.assertEqual(c.GetDefaultFill(), green)
278 self.assertEqual(c.GetDefaultLineColor(), red)
279
280 def test_add_singleton(self):
281 """Test Classification.AppendGroup(ClassGroupSingleton())"""
282 c = Classification()
283 self.assertEquals(c.FindGroup(5), c.GetDefaultGroup())
284
285 s = ClassGroupSingleton(5)
286 c.AppendGroup(s)
287 self.assertEquals(c.FindGroup(5), s)
288 self.assertEquals(c.FindGroup(0), c.GetDefaultGroup())
289
290 def test_add_range(self):
291 """Test Classification.AppendGroup(ClassGroupRange())"""
292 c = Classification()
293 self.assertEquals(c.FindGroup(0), c.GetDefaultGroup())
294
295 r = ClassGroupRange((-10, 10))
296 c.AppendGroup(r)
297 self.assertEquals(c.FindGroup(-11), c.GetDefaultGroup())
298 self.assertEquals(c.FindGroup(-10), r)
299 self.assertEquals(c.FindGroup(9), r)
300 self.assertEquals(c.FindGroup(5), r)
301 self.assertEquals(c.FindGroup(10), c.GetDefaultGroup())
302
303 def test_multiple_groups(self):
304 """Test Classification with multiple groups"""
305 c = Classification()
306 s1 = ClassGroupSingleton(1)
307 s1a = ClassGroupSingleton(1)
308 s2 = ClassGroupSingleton(2)
309 r = ClassGroupRange((-10, 10))
310
311 c.AppendGroup(s1)
312 c.AppendGroup(s2)
313 c.AppendGroup(s1a)
314 c.AppendGroup(r)
315
316 self.assertEquals(c.FindGroup(-11), c.GetDefaultGroup())
317 self.assertEquals(c.FindGroup(-10), r)
318 self.assertEquals(c.FindGroup(1), s1)
319 self.assertEquals(c.FindGroup(2), s2)
320 self.assertEquals(c.FindGroup(3), r)
321 self.assertEquals(c.FindGroup(9), r)
322 self.assertEquals(c.FindGroup(10), c.GetDefaultGroup())
323
324 def test_deepcopy(self):
325 """Test deepcopy(Classification())"""
326 c = Classification()
327 c.AppendGroup(ClassGroupSingleton(5))
328 c.AppendGroup(ClassGroupRange((-10, 10)))
329
330 clazz = copy.deepcopy(c)
331
332 self.assertEquals(clazz.GetNumGroups(), c.GetNumGroups())
333
334 for i in range(clazz.GetNumGroups()):
335 self.assertEquals(clazz.GetGroup(i), c.GetGroup(i))
336
337
338 def test_iterator(self):
339 """Test Classification iteration"""
340 groups = [ClassGroupSingleton(5), ClassGroupSingleton(5),
341 ClassGroupRange((-3, 3)), ClassGroupSingleton(-5),
342 ClassGroupDefault()]
343
344 clazz = Classification()
345
346 for g in groups:
347 clazz.AppendGroup(g)
348
349 def convert(clazz):
350 if isinstance(clazz, ClassGroupDefault): return 0
351 if isinstance(clazz, ClassGroupSingleton): return 1
352 if isinstance(clazz, ClassGroupRange): return 2
353
354 list = []
355 for g in clazz:
356 list.append(convert(g))
357
358 self.assertEquals(list, [0, 1, 1, 2, 1, 0])
359
360
361 if __name__ == "__main__":
362 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