20 |
import support |
import support |
21 |
support.initthuban() |
support.initthuban() |
22 |
|
|
23 |
from Thuban.Model.table import * |
import os |
24 |
from Thuban.Model.classification import * |
from Thuban.Model.color import Color, Transparent, Black |
25 |
|
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 |
|
from Thuban.Model.session import Session |
32 |
from Thuban.Model.layer import Layer |
from Thuban.Model.layer import Layer |
33 |
|
from Thuban.Model.range import Range |
34 |
|
|
35 |
import copy |
import copy |
36 |
|
|
37 |
|
|
38 |
class TestClassification(unittest.TestCase): |
class TestClassGroupProperties(unittest.TestCase): |
39 |
|
|
40 |
def test_ClassGroupProperties(self): |
def test(self): |
41 |
"""Test ClassGroupProperties""" |
"""Test ClassGroupProperties""" |
42 |
|
|
43 |
props = ClassGroupProperties() |
props = ClassGroupProperties() |
44 |
self.assertEqual(props.GetLineColor(), Color.Black) |
self.assertEqual(props.GetLineColor(), Black) |
45 |
self.assertEqual(props.GetLineWidth(), 1) |
self.assertEqual(props.GetLineWidth(), 1) |
46 |
self.assertEqual(props.GetFill(), Color.Transparent) |
self.assertEqual(props.GetFill(), Transparent) |
47 |
|
|
48 |
red = Color(1, 0, 0) |
red = Color(1, 0, 0) |
49 |
props.SetLineColor(red) |
props.SetLineColor(red) |
64 |
self.assertNotEqual(newProps1, props) |
self.assertNotEqual(newProps1, props) |
65 |
self.assertEqual(newProps1, newProps2) |
self.assertEqual(newProps1, newProps2) |
66 |
|
|
67 |
def test_ClassGroup(self): |
|
68 |
|
class TestClassGroup(unittest.TestCase): |
69 |
|
|
70 |
|
def test(self): |
71 |
"""Test ClassGroup""" |
"""Test ClassGroup""" |
72 |
|
|
73 |
# test constructor with no label |
# test constructor with no label |
97 |
# test GetProperties...also a virtual function |
# test GetProperties...also a virtual function |
98 |
#self.assertEqual(group.GetProperties(), None) |
#self.assertEqual(group.GetProperties(), None) |
99 |
|
|
100 |
def test_ClassGroupDefault(self): |
|
101 |
|
class TestClassGroupDefault(unittest.TestCase): |
102 |
|
|
103 |
|
def test(self): |
104 |
"""Test ClassGroupDefault""" |
"""Test ClassGroupDefault""" |
105 |
|
|
106 |
defProps = ClassGroupProperties() |
defProps = ClassGroupProperties() |
140 |
groupCopy = copy.copy(group) |
groupCopy = copy.copy(group) |
141 |
self.assertEqual(group, groupCopy) |
self.assertEqual(group, groupCopy) |
142 |
|
|
143 |
def test_ClassGroupRange(self): |
|
144 |
|
class TestClassGroupRange(unittest.TestCase): |
145 |
|
|
146 |
|
def test(self): |
147 |
"""Test ClassGroupRange""" |
"""Test ClassGroupRange""" |
148 |
|
|
149 |
defProps = ClassGroupProperties() |
defProps = ClassGroupProperties() |
159 |
self.assertEqual(group.GetMax(), 1) |
self.assertEqual(group.GetMax(), 1) |
160 |
self.assertEqual(group.GetProperties(), defProps) |
self.assertEqual(group.GetProperties(), defProps) |
161 |
self.assertEqual(group.GetLabel(), "") |
self.assertEqual(group.GetLabel(), "") |
162 |
|
|
163 |
# test SetMax() |
# test SetMax() |
164 |
self.assertRaises(ValueError, group.SetMax, 0) |
self.assertRaises(ValueError, group.SetMax, 0) |
165 |
self.assertRaises(ValueError, group.SetMax, -1) |
self.assertRaises(ValueError, group.SetMax, -1) |
179 |
self.assertEqual(group.GetProperties(), newProps) |
self.assertEqual(group.GetProperties(), newProps) |
180 |
|
|
181 |
# test SetRange() |
# test SetRange() |
182 |
self.assertRaises(ValueError, group.SetRange, 1, 0) |
self.assertRaises(ValueError, group.SetRange, (1, 0)) |
183 |
group.SetRange(-5, 5) |
group.SetRange(Range("]-oo;6]")) |
184 |
self.assertEqual(group.GetRange(), (-5, 5)) |
self.assertEqual(group.GetRange(), "]-oo;6]") |
185 |
|
group.SetRange((-5, 5)) |
186 |
|
self.assertEqual(group.GetRange(), "[-5;5[") |
187 |
|
|
188 |
# test Matches() |
# test Matches() |
189 |
self.assertEqual(group.Matches(-6), False) |
self.assertEqual(group.Matches(-6), False) |
196 |
groupCopy = copy.copy(group) |
groupCopy = copy.copy(group) |
197 |
self.assertEqual(group, groupCopy) |
self.assertEqual(group, groupCopy) |
198 |
|
|
199 |
def test_ClassGroupSingleton(self): |
|
200 |
|
class TestClassGroupSingleton(unittest.TestCase): |
201 |
|
|
202 |
|
def test(self): |
203 |
"""Test ClassGroupSingleton""" |
"""Test ClassGroupSingleton""" |
204 |
|
|
205 |
defProps = ClassGroupProperties() |
defProps = ClassGroupProperties() |
244 |
# test copy |
# test copy |
245 |
groupCopy = copy.copy(group) |
groupCopy = copy.copy(group) |
246 |
self.assertEqual(group, groupCopy) |
self.assertEqual(group, groupCopy) |
|
|
|
247 |
|
|
248 |
def test_ClassIterator(self): |
|
249 |
|
class TestClassIterator(unittest.TestCase): |
250 |
|
|
251 |
|
def test(self): |
252 |
"""Test ClassIterator""" |
"""Test ClassIterator""" |
253 |
|
|
254 |
groups = [ClassGroupSingleton(5), ClassGroupSingleton(5), |
groups = [ClassGroupSingleton(5), ClassGroupSingleton(5), |
255 |
ClassGroupRange(-3, 3), ClassGroupSingleton(-5), |
ClassGroupRange((-3, 3)), ClassGroupSingleton(-5), |
256 |
ClassGroupDefault()] |
ClassGroupDefault()] |
257 |
|
|
258 |
clazz = Classification() |
clazz = Classification() |
271 |
|
|
272 |
self.assertEquals(list, [0, 1, 1, 2, 1, 0]) |
self.assertEquals(list, [0, 1, 1, 2, 1, 0]) |
273 |
|
|
274 |
def test_classification(self): |
|
275 |
|
class TestClassification(unittest.TestCase): |
276 |
|
|
277 |
|
def test(self): |
278 |
"""Test Classification""" |
"""Test Classification""" |
279 |
|
|
280 |
defProps = ClassGroupProperties() |
defProps = ClassGroupProperties() |
282 |
green = Color(0, 1, 0) |
green = Color(0, 1, 0) |
283 |
blue = Color(0, 0, 1) |
blue = Color(0, 0, 1) |
284 |
|
|
285 |
layer = Layer("asdf", "../Data/iceland/political.dbf") |
session = Session("Test session") |
286 |
|
filename = os.path.join("..", "Data", "iceland", "political.dbf") |
287 |
|
layer = Layer("asdf", session.OpenShapefile(filename)) |
288 |
|
|
289 |
# |
# |
290 |
# init with no params |
# init with no params |
291 |
# |
# |
292 |
c = Classification() |
c = Classification() |
|
self.assertEqual(c.GetField(), None) |
|
|
self.assertEqual(c.GetFieldType(), None) |
|
293 |
self.assertEqual(c.FindGroup(-1), c.GetDefaultGroup()) |
self.assertEqual(c.FindGroup(-1), c.GetDefaultGroup()) |
294 |
|
|
295 |
c.SetDefaultLineColor(red) |
c.SetDefaultLineColor(red) |
296 |
self.assertEqual(c.GetDefaultLineColor(), red) |
self.assertEqual(c.GetDefaultLineColor(), red) |
297 |
self.assertEqual(c.GetDefaultFill(), Color.Transparent) |
self.assertEqual(c.GetDefaultFill(), Transparent) |
298 |
|
|
299 |
c.SetDefaultFill(green) |
c.SetDefaultFill(green) |
300 |
self.assertEqual(c.GetDefaultFill(), green) |
self.assertEqual(c.GetDefaultFill(), green) |
301 |
self.assertEqual(c.GetDefaultLineColor(), red) |
self.assertEqual(c.GetDefaultLineColor(), red) |
302 |
|
|
303 |
c.SetField("hallo") |
layer.SetClassification(c) |
|
self.assertEqual(c.GetField(), "hallo") |
|
|
|
|
|
c.SetFieldType(FIELDTYPE_STRING) |
|
|
self.assertEqual(c.GetFieldType(), FIELDTYPE_STRING) |
|
304 |
|
|
|
# should raise an exception because 'hallo' doesn't |
|
|
# exist in the table |
|
|
self.assertRaises(ValueError, c.SetLayer, layer) |
|
|
|
|
|
c.SetField("AREA") |
|
|
c.SetLayer(layer) |
|
|
self.assertEqual(c.GetLayer(), layer) |
|
|
self.assertEqual(c.GetField(), "AREA") |
|
|
self.assertEqual(c.GetFieldType(), FIELDTYPE_DOUBLE) |
|
|
|
|
|
c.SetField(None) |
|
|
self.assertEquals(c.GetFieldType(), None) |
|
305 |
self.assertEquals(c.FindGroup(5), c.GetDefaultGroup()) |
self.assertEquals(c.FindGroup(5), c.GetDefaultGroup()) |
306 |
|
|
|
c.SetField("AREA") |
|
307 |
s = ClassGroupSingleton(5) |
s = ClassGroupSingleton(5) |
308 |
c.AppendGroup(s) |
c.AppendGroup(s) |
309 |
self.assertEquals(c.FindGroup(5), s) |
self.assertEquals(c.FindGroup(5), s) |
310 |
self.assertEquals(c.FindGroup(0), c.GetDefaultGroup()) |
self.assertEquals(c.FindGroup(0), c.GetDefaultGroup()) |
311 |
|
|
312 |
r = ClassGroupRange(-10, 10) |
r = ClassGroupRange((-10, 10)) |
313 |
c.AppendGroup(r) |
c.AppendGroup(r) |
314 |
self.assertEquals(c.FindGroup(-11), c.GetDefaultGroup()) |
self.assertEquals(c.FindGroup(-11), c.GetDefaultGroup()) |
315 |
self.assertEquals(c.FindGroup(-10), r) |
self.assertEquals(c.FindGroup(-10), r) |
324 |
for i in range(clazz.GetNumGroups()): |
for i in range(clazz.GetNumGroups()): |
325 |
self.assertEquals(clazz.GetGroup(i), c.GetGroup(i)) |
self.assertEquals(clazz.GetGroup(i), c.GetGroup(i)) |
326 |
|
|
327 |
|
session.Destroy() |
328 |
layer.Destroy() |
layer.Destroy() |
329 |
|
|
330 |
|
|
331 |
if __name__ == "__main__": |
if __name__ == "__main__": |
332 |
support.run_tests() |
support.run_tests() |