/[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 1437 - (hide annotations)
Wed Jul 16 13:25:16 2003 UTC (21 years, 7 months ago) by jonathan
Original Path: trunk/thuban/test/test_classification.py
File MIME type: text/x-python
File size: 9684 byte(s)
(TestClassification.test_classification): Remove tests for methods
        that no longer exist.

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