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 the Transient DB classes |
10 |
""" |
11 |
|
12 |
__version__ = "$Revision$" |
13 |
# $Source$ |
14 |
# $Id$ |
15 |
|
16 |
import os |
17 |
import unittest |
18 |
|
19 |
import support |
20 |
support.initthuban() |
21 |
|
22 |
from Thuban.Model.table import DBFTable, MemoryTable, FIELDTYPE_STRING, \ |
23 |
FIELDTYPE_INT, FIELDTYPE_DOUBLE |
24 |
from Thuban.Model.transientdb import TransientDatabase, TransientTable, \ |
25 |
TransientJoinedTable, AutoTransientTable |
26 |
|
27 |
|
28 |
class TestTransientTable(unittest.TestCase, support.FileTestMixin): |
29 |
|
30 |
def setUp(self): |
31 |
"""Create a transient database as self.transientdb""" |
32 |
filename = self.temp_file_name("transient_table.sqlite") |
33 |
if os.path.exists(filename): |
34 |
os.remove(filename) |
35 |
journal = filename + "-journal" |
36 |
if os.path.exists(journal): |
37 |
print "removing journal", journal |
38 |
os.remove(journal) |
39 |
self.transientdb = TransientDatabase(filename) |
40 |
|
41 |
def tearDown(self): |
42 |
self.transientdb.close() |
43 |
|
44 |
def run_iceland_political_tests(self, table): |
45 |
"""Run some tests on tablte |
46 |
|
47 |
Assume that table holds the data of the file |
48 |
../Data/iceland/political.dbf sample file. |
49 |
""" |
50 |
self.assertEquals(table.NumRows(), 156) |
51 |
self.assertEquals(table.NumColumns(), 8) |
52 |
|
53 |
# Check one each of the possible field types. The width and |
54 |
# decimal precision is always 0. |
55 |
columns = table.Columns() |
56 |
self.assertEquals(columns[0].name, 'AREA') |
57 |
self.assertEquals(columns[0].type, FIELDTYPE_DOUBLE) |
58 |
self.assertEquals(columns[3].name, 'PONET_ID') |
59 |
self.assertEquals(columns[3].type, FIELDTYPE_INT) |
60 |
self.assertEquals(columns[6].name, 'POPYCOUN') |
61 |
self.assertEquals(columns[6].type, FIELDTYPE_STRING) |
62 |
|
63 |
# HasColumn |
64 |
self.failUnless(table.HasColumn("AREA")) |
65 |
self.failUnless(table.HasColumn(1)) |
66 |
# HasColumn for non-exisiting columns |
67 |
self.failIf(table.HasColumn("non_existing_name")) |
68 |
self.failIf(table.HasColumn(100)) |
69 |
|
70 |
# Read an `interesting' record |
71 |
self.assertEquals(table.ReadRowAsDict(144), |
72 |
{'POPYCOUN': 'IC', 'POPYADMIN': '', 'PONET_': 146, |
73 |
'AREA': 19.462, |
74 |
'POPYTYPE': 1, 'PERIMETER': 88.518000000000001, |
75 |
'POPYREG': '1', |
76 |
'PONET_ID': 145}) |
77 |
|
78 |
# ValueRange may induce a copy to the transient database. |
79 |
# Therefore we put it last so that we can execute this method |
80 |
# twice to check whether the other methods still work after the |
81 |
# copy |
82 |
self.assertEquals(table.ValueRange("AREA"), (0.0, 19.462)) |
83 |
|
84 |
unique = table.UniqueValues("PONET_ID") |
85 |
unique.sort() |
86 |
self.assertEquals(unique, range(1, 157)) |
87 |
|
88 |
def test_transient_table(self): |
89 |
"""Test TransientTable(dbftable) |
90 |
|
91 |
The TransientTable should copy the data to the |
92 |
TransientDatabase. |
93 |
""" |
94 |
orig_table = DBFTable(os.path.join("..", "Data", "iceland", |
95 |
"political.dbf")) |
96 |
table = TransientTable(self.transientdb, orig_table) |
97 |
self.run_iceland_political_tests(table) |
98 |
|
99 |
# The transient_table method should return the table itself |
100 |
self.assert_(table is table.transient_table()) |
101 |
|
102 |
|
103 |
def test_auto_transient_table(self): |
104 |
"""Test AutoTransientTable(dbftable) |
105 |
|
106 |
The AutoTransientTable should copy the data to the |
107 |
TransientDatabase on demand. |
108 |
""" |
109 |
orig_table = DBFTable(os.path.join("..", "Data", "iceland", |
110 |
"political.dbf")) |
111 |
table = AutoTransientTable(self.transientdb, orig_table) |
112 |
|
113 |
# Run the tests twice so that we execute them once when the data |
114 |
# has not been copied to the transient db yet and once when it |
115 |
# has. This assumes that run_iceland_political_tests does at |
116 |
# least one call to a method that copies to the transient db at |
117 |
# its end. |
118 |
self.run_iceland_political_tests(table) |
119 |
self.run_iceland_political_tests(table) |
120 |
|
121 |
|
122 |
def test_transient_joined_table(self): |
123 |
"""Test TransientJoinedTable""" |
124 |
simple = MemoryTable([("type", FIELDTYPE_STRING), |
125 |
("code", FIELDTYPE_INT)], |
126 |
[("OTHER/UNKNOWN", 0), |
127 |
("RUINS", 1), |
128 |
("FARM", 2), |
129 |
("BUILDING", 3), |
130 |
("HUT", 4), |
131 |
("LIGHTHOUSE", 5)]) |
132 |
auto = AutoTransientTable(self.transientdb, simple) |
133 |
filename = os.path.join("..", "Data", "iceland", |
134 |
"cultural_landmark-point.dbf") |
135 |
landmarks = AutoTransientTable(self.transientdb, DBFTable(filename)) |
136 |
|
137 |
table = TransientJoinedTable(self.transientdb, landmarks, "CLPTLABEL", |
138 |
auto, "type") |
139 |
|
140 |
self.assertEquals(table.NumRows(), 34) |
141 |
self.assertEquals(table.NumColumns(), 8) |
142 |
self.assertEquals(table.Column(0).type, FIELDTYPE_DOUBLE) |
143 |
self.assertEquals(table.Column(0).name, 'AREA') |
144 |
self.assertEquals(table.Column(7).type, FIELDTYPE_INT) |
145 |
self.assertEquals(table.Column(7).name, 'code') |
146 |
self.assertEquals(table.Column(4).type, FIELDTYPE_STRING) |
147 |
self.assertEquals(table.Column(4).name, 'CLPTLABEL') |
148 |
# HasColumn |
149 |
self.failUnless(table.HasColumn("AREA")) |
150 |
self.failUnless(table.HasColumn(1)) |
151 |
# HasColumn for non-exisiting columns |
152 |
self.failIf(table.HasColumn("non_existing_name")) |
153 |
self.failIf(table.HasColumn(100)) |
154 |
|
155 |
# Read an `interesting' record |
156 |
self.assertEquals(table.ReadRowAsDict(22), |
157 |
{'PERIMETER': 0.0, 'CLPOINT_': 23, |
158 |
'AREA': 0.0, 'CLPTLABEL': 'RUINS', |
159 |
'CLPOINT_ID': 38, 'CLPTFLAG': 0, |
160 |
'code': 1, 'type': 'RUINS'}) |
161 |
|
162 |
# The transient_table method should return the table itself |
163 |
self.assert_(table is table.transient_table()) |
164 |
|
165 |
|
166 |
def test_transient_table_read_twice(self): |
167 |
"""Test TransientTable.ReadRowAsDict() reading the same record twice""" |
168 |
simple = MemoryTable([("type", FIELDTYPE_STRING), |
169 |
("code", FIELDTYPE_INT)], |
170 |
[("OTHER/UNKNOWN", 0), |
171 |
("RUINS", 1), |
172 |
("FARM", 2), |
173 |
("BUILDING", 3), |
174 |
("HUT", 4), |
175 |
("LIGHTHOUSE", 5)]) |
176 |
table = TransientTable(self.transientdb, simple) |
177 |
|
178 |
# There was a bug where reading the same record twice would |
179 |
# raise an exception in the second call because of an |
180 |
# unitialized local variable, so for passing the test it's |
181 |
# enough if reading simply succeeds. OTOH, while we're at it we |
182 |
# might as well check whether the results are equal anyway :) |
183 |
result1 = table.ReadRowAsDict(3) |
184 |
result2 = table.ReadRowAsDict(3) |
185 |
self.assertEquals(result1, result2) |
186 |
|
187 |
|
188 |
def test_transient_table_query(self): |
189 |
"""Test TransientTable.SimpleQuery()""" |
190 |
simple = MemoryTable([("type", FIELDTYPE_STRING), |
191 |
("value", FIELDTYPE_DOUBLE), |
192 |
("code", FIELDTYPE_INT)], |
193 |
[("OTHER/UNKNOWN", -1.5, 11), |
194 |
("RUINS", 0.0, 1), |
195 |
("FARM", 3.141, 2), |
196 |
("BUILDING", 2.5, 3), |
197 |
("HUT", 1e6, 4), |
198 |
("LIGHTHOUSE", -0.01, 5)]) |
199 |
table = TransientTable(self.transientdb, simple) |
200 |
|
201 |
# A column and a value |
202 |
self.assertEquals(table.SimpleQuery(table.Column(0), "==", "RUINS"), |
203 |
[1]) |
204 |
self.assertEquals(table.SimpleQuery(table.Column(2), "!=", 2), |
205 |
[0, 1, 3, 4, 5]) |
206 |
self.assertEquals(table.SimpleQuery(table.Column(1), "<", 1.0), |
207 |
[0, 1, 5]) |
208 |
self.assertEquals(table.SimpleQuery(table.Column(1), "<=", -1.5), |
209 |
[0]) |
210 |
self.assertEquals(table.SimpleQuery(table.Column(2), ">", 3), |
211 |
[0, 4, 5]) |
212 |
self.assertEquals(table.SimpleQuery(table.Column(2), ">=", 3), |
213 |
[0, 3, 4, 5]) |
214 |
|
215 |
# Two columns as operands |
216 |
self.assertEquals(table.SimpleQuery(table.Column(1), |
217 |
"<=", table.Column(2)), |
218 |
[0, 1, 3, 5]) |
219 |
|
220 |
# Test whether invalid operators raise a ValueError |
221 |
self.assertRaises(ValueError, |
222 |
table.SimpleQuery, |
223 |
table.Column(1), "<<", table.Column(2)) |
224 |
|
225 |
|
226 |
if __name__ == "__main__": |
227 |
support.run_tests() |