19 |
import support |
import support |
20 |
support.initthuban() |
support.initthuban() |
21 |
|
|
22 |
from Thuban.Model.table import DBFTable, FIELDTYPE_STRING, FIELDTYPE_INT |
from Thuban.Model.table import DBFTable, MemoryTable, FIELDTYPE_STRING, \ |
23 |
|
FIELDTYPE_INT, FIELDTYPE_DOUBLE |
24 |
from Thuban.Model.transientdb import TransientDatabase, TransientTable, \ |
from Thuban.Model.transientdb import TransientDatabase, TransientTable, \ |
25 |
TransientJoinedTable, AutoTransientTable |
TransientJoinedTable, AutoTransientTable |
26 |
|
|
27 |
|
|
|
class SimpleTable: |
|
|
|
|
|
"""Very simple table implementation that operates on a list of tuples""" |
|
|
|
|
|
def __init__(self, fields, data): |
|
|
"""Initialize the SimpleTable |
|
|
|
|
|
Parameters: |
|
|
fields -- List of (name, field_type) pairs |
|
|
data -- List of tuples, one for each row of data |
|
|
""" |
|
|
self.fields = fields |
|
|
self.data = data |
|
|
|
|
|
def field_count(self): |
|
|
return len(self.fields) |
|
|
|
|
|
def field_info(self, index): |
|
|
name, type = self.fields[index] |
|
|
return (type, name, 0, 0) |
|
|
|
|
|
def record_count(self): |
|
|
return len(self.data) |
|
|
|
|
|
def read_record(self, index): |
|
|
return dict([(self.fields[i][0], self.data[index][i]) |
|
|
for i in range(len(self.fields))]) |
|
|
|
|
|
|
|
28 |
class TestTransientTable(unittest.TestCase, support.FileTestMixin): |
class TestTransientTable(unittest.TestCase, support.FileTestMixin): |
29 |
|
|
30 |
def setUp(self): |
def setUp(self): |
47 |
Assume that table holds the data of the file |
Assume that table holds the data of the file |
48 |
../Data/iceland/political.dbf sample file. |
../Data/iceland/political.dbf sample file. |
49 |
""" |
""" |
50 |
self.assertEquals(table.record_count(), 156) |
self.assertEquals(table.NumRows(), 156) |
51 |
self.assertEquals(table.field_count(), 8) |
self.assertEquals(table.NumColumns(), 8) |
52 |
|
|
53 |
# Check one each of the possible field types. The width and |
# Check one each of the possible field types. The width and |
54 |
# decimal precision is always 0. |
# decimal precision is always 0. |
55 |
self.assertEquals(table.field_info(0), ('double', 'AREA', 0, 0)) |
columns = table.Columns() |
56 |
self.assertEquals(table.field_info(3), ('int', 'PONET_ID', 0, 0)) |
self.assertEquals(columns[0].name, 'AREA') |
57 |
self.assertEquals(table.field_info(6), ('string', 'POPYCOUN', 0, 0)) |
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 |
# Reading rows and values. |
71 |
self.assertEquals(table.read_record(144), |
self.assertEquals(table.ReadRowAsDict(144), |
72 |
{'POPYCOUN': 'IC', 'POPYADMIN': '', 'PONET_': 146, |
{'POPYCOUN': 'IC', 'POPYADMIN': '', 'PONET_': 146, |
73 |
'AREA': 19.462, |
'AREA': 19.462, |
74 |
'POPYTYPE': 1, 'PERIMETER': 88.518000000000001, |
'POPYTYPE': 1, 'PERIMETER': 88.518000000000001, |
75 |
'POPYREG': '1', |
'POPYREG': '1', |
76 |
'PONET_ID': 145}) |
'PONET_ID': 145}) |
77 |
|
self.assertEquals(table.ReadValue(144, "AREA"), 19.462) |
78 |
|
self.assertEquals(table.ReadValue(144, 3), 145) |
79 |
|
|
80 |
# field_range may induce a copy to the transient database. |
# ValueRange may induce a copy to the transient database. |
81 |
# Therefore we put it last so that we can execute this method |
# Therefore we put it last so that we can execute this method |
82 |
# twice to check whether the other methods still work after the |
# twice to check whether the other methods still work after the |
83 |
# copy |
# copy |
84 |
self.assertEquals(table.field_range("AREA"), |
self.assertEquals(table.ValueRange("AREA"), (0.0, 19.462)) |
|
((0.0, None), (19.462, None))) |
|
85 |
|
|
86 |
unique = table.GetUniqueValues("PONET_ID") |
unique = table.UniqueValues("PONET_ID") |
87 |
unique.sort() |
unique.sort() |
88 |
self.assertEquals(unique, range(1, 157)) |
self.assertEquals(unique, range(1, 157)) |
89 |
|
|
120 |
self.run_iceland_political_tests(table) |
self.run_iceland_political_tests(table) |
121 |
self.run_iceland_political_tests(table) |
self.run_iceland_political_tests(table) |
122 |
|
|
123 |
|
def test_auto_transient_table_query(self): |
124 |
|
"""Test AutoTransientTable.SimpleQuery()""" |
125 |
|
orig_table = DBFTable(os.path.join("..", "Data", "iceland", |
126 |
|
"political.dbf")) |
127 |
|
table = AutoTransientTable(self.transientdb, orig_table) |
128 |
|
# Only a simple test here. The AutoTransientTable siply |
129 |
|
# delegates to its transient table so it should be OK that the |
130 |
|
# real test for it is in test_transient_table_query. However, |
131 |
|
# it's important to check that the column handling works |
132 |
|
# correctly because the AutoTransientTable and it's underlying |
133 |
|
# transient table use different column object types. |
134 |
|
self.assertEquals(table.SimpleQuery(table.Column("AREA"), ">", 10.0), |
135 |
|
[144]) |
136 |
|
|
137 |
def test_transient_joined_table(self): |
def test_transient_joined_table(self): |
138 |
"""Test TransientJoinedTable""" |
"""Test TransientJoinedTable""" |
139 |
simple = SimpleTable([("type", FIELDTYPE_STRING), |
simple = MemoryTable([("type", FIELDTYPE_STRING), |
140 |
("code", FIELDTYPE_INT)], |
("code", FIELDTYPE_INT)], |
141 |
[("OTHER/UNKNOWN", 0), |
[("OTHER/UNKNOWN", 0), |
142 |
("RUINS", 1), |
("RUINS", 1), |
152 |
table = TransientJoinedTable(self.transientdb, landmarks, "CLPTLABEL", |
table = TransientJoinedTable(self.transientdb, landmarks, "CLPTLABEL", |
153 |
auto, "type") |
auto, "type") |
154 |
|
|
155 |
self.assertEquals(table.record_count(), 34) |
self.assertEquals(table.NumRows(), 34) |
156 |
self.assertEquals(table.field_count(), 8) |
self.assertEquals(table.NumColumns(), 8) |
157 |
self.assertEquals(table.field_info(0), ('double', 'AREA', 0, 0)) |
self.assertEquals(table.Column(0).type, FIELDTYPE_DOUBLE) |
158 |
self.assertEquals(table.field_info(7), ('int', 'code', 0, 0)) |
self.assertEquals(table.Column(0).name, 'AREA') |
159 |
self.assertEquals(table.field_info(4), ('string', 'CLPTLABEL', 0, 0)) |
self.assertEquals(table.Column(7).type, FIELDTYPE_INT) |
160 |
|
self.assertEquals(table.Column(7).name, 'code') |
161 |
|
self.assertEquals(table.Column(4).type, FIELDTYPE_STRING) |
162 |
|
self.assertEquals(table.Column(4).name, 'CLPTLABEL') |
163 |
|
# HasColumn |
164 |
|
self.failUnless(table.HasColumn("AREA")) |
165 |
|
self.failUnless(table.HasColumn(1)) |
166 |
|
# HasColumn for non-exisiting columns |
167 |
|
self.failIf(table.HasColumn("non_existing_name")) |
168 |
|
self.failIf(table.HasColumn(100)) |
169 |
|
|
170 |
# Read an `interesting' record |
# Reading rows and values |
171 |
self.assertEquals(table.read_record(22), |
self.assertEquals(table.ReadRowAsDict(22), |
172 |
{'PERIMETER': 0.0, 'CLPOINT_': 23, |
{'PERIMETER': 0.0, 'CLPOINT_': 23, |
173 |
'AREA': 0.0, 'CLPTLABEL': 'RUINS', |
'AREA': 0.0, 'CLPTLABEL': 'RUINS', |
174 |
'CLPOINT_ID': 38, 'CLPTFLAG': 0, |
'CLPOINT_ID': 38, 'CLPTFLAG': 0, |
175 |
'code': 1, 'type': 'RUINS'}) |
'code': 1, 'type': 'RUINS'}) |
176 |
|
self.assertEquals(table.ReadValue(22, "type"), 'RUINS') |
177 |
|
self.assertEquals(table.ReadValue(22, 7), 1) |
178 |
|
|
179 |
# The transient_table method should return the table itself |
# The transient_table method should return the table itself |
180 |
self.assert_(table is table.transient_table()) |
self.assert_(table is table.transient_table()) |
181 |
|
|
182 |
|
|
183 |
|
def test_transient_table_read_twice(self): |
184 |
|
"""Test TransientTable.ReadRowAsDict() reading the same record twice""" |
185 |
|
simple = MemoryTable([("type", FIELDTYPE_STRING), |
186 |
|
("code", FIELDTYPE_INT)], |
187 |
|
[("OTHER/UNKNOWN", 0), |
188 |
|
("RUINS", 1), |
189 |
|
("FARM", 2), |
190 |
|
("BUILDING", 3), |
191 |
|
("HUT", 4), |
192 |
|
("LIGHTHOUSE", 5)]) |
193 |
|
table = TransientTable(self.transientdb, simple) |
194 |
|
|
195 |
|
# There was a bug where reading the same record twice would |
196 |
|
# raise an exception in the second call because of an |
197 |
|
# unitialized local variable, so for passing the test it's |
198 |
|
# enough if reading simply succeeds. OTOH, while we're at it we |
199 |
|
# might as well check whether the results are equal anyway :) |
200 |
|
result1 = table.ReadRowAsDict(3) |
201 |
|
result2 = table.ReadRowAsDict(3) |
202 |
|
self.assertEquals(result1, result2) |
203 |
|
|
204 |
|
|
205 |
|
def test_transient_table_query(self): |
206 |
|
"""Test TransientTable.SimpleQuery()""" |
207 |
|
simple = MemoryTable([("type", FIELDTYPE_STRING), |
208 |
|
("value", FIELDTYPE_DOUBLE), |
209 |
|
("code", FIELDTYPE_INT)], |
210 |
|
[("OTHER/UNKNOWN", -1.5, 11), |
211 |
|
("RUINS", 0.0, 1), |
212 |
|
("FARM", 3.141, 2), |
213 |
|
("BUILDING", 2.5, 3), |
214 |
|
("HUT", 1e6, 4), |
215 |
|
("LIGHTHOUSE", -0.01, 5)]) |
216 |
|
table = TransientTable(self.transientdb, simple) |
217 |
|
|
218 |
|
# A column and a value |
219 |
|
self.assertEquals(table.SimpleQuery(table.Column(0), "==", "RUINS"), |
220 |
|
[1]) |
221 |
|
self.assertEquals(table.SimpleQuery(table.Column(2), "!=", 2), |
222 |
|
[0, 1, 3, 4, 5]) |
223 |
|
self.assertEquals(table.SimpleQuery(table.Column(1), "<", 1.0), |
224 |
|
[0, 1, 5]) |
225 |
|
self.assertEquals(table.SimpleQuery(table.Column(1), "<=", -1.5), |
226 |
|
[0]) |
227 |
|
self.assertEquals(table.SimpleQuery(table.Column(2), ">", 3), |
228 |
|
[0, 4, 5]) |
229 |
|
self.assertEquals(table.SimpleQuery(table.Column(2), ">=", 3), |
230 |
|
[0, 3, 4, 5]) |
231 |
|
|
232 |
|
# Two columns as operands |
233 |
|
self.assertEquals(table.SimpleQuery(table.Column(1), |
234 |
|
"<=", table.Column(2)), |
235 |
|
[0, 1, 3, 5]) |
236 |
|
|
237 |
|
# Test whether invalid operators raise a ValueError |
238 |
|
self.assertRaises(ValueError, |
239 |
|
table.SimpleQuery, |
240 |
|
table.Column(1), "<<", table.Column(2)) |
241 |
|
|
242 |
|
|
243 |
if __name__ == "__main__": |
if __name__ == "__main__": |
244 |
support.run_tests() |
support.run_tests() |