20 |
support.initthuban() |
support.initthuban() |
21 |
|
|
22 |
from Thuban.Model.table import DBFTable, MemoryTable, FIELDTYPE_STRING, \ |
from Thuban.Model.table import DBFTable, MemoryTable, FIELDTYPE_STRING, \ |
23 |
FIELDTYPE_INT |
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 |
|
|
54 |
# decimal precision is always 0. |
# decimal precision is always 0. |
55 |
columns = table.Columns() |
columns = table.Columns() |
56 |
self.assertEquals(columns[0].name, 'AREA') |
self.assertEquals(columns[0].name, 'AREA') |
57 |
self.assertEquals(columns[0].type, 'double') |
self.assertEquals(columns[0].type, FIELDTYPE_DOUBLE) |
58 |
self.assertEquals(columns[3].name, 'PONET_ID') |
self.assertEquals(columns[3].name, 'PONET_ID') |
59 |
self.assertEquals(columns[3].type, 'int') |
self.assertEquals(columns[3].type, FIELDTYPE_INT) |
60 |
self.assertEquals(columns[6].name, 'POPYCOUN') |
self.assertEquals(columns[6].name, 'POPYCOUN') |
61 |
self.assertEquals(columns[6].type, 'string') |
self.assertEquals(columns[6].type, FIELDTYPE_STRING) |
62 |
|
|
63 |
# Read an `interesting' record |
# 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 |
|
# Reading rows and values. |
71 |
self.assertEquals(table.ReadRowAsDict(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 |
101 |
# The transient_table method should return the table itself |
# The transient_table method should return the table itself |
102 |
self.assert_(table is table.transient_table()) |
self.assert_(table is table.transient_table()) |
103 |
|
|
104 |
|
# The TransientTable class itself doesn't implement the |
105 |
|
# Dependencies method, so we don't test it. |
106 |
|
|
107 |
|
|
108 |
def test_auto_transient_table(self): |
def test_auto_transient_table(self): |
109 |
"""Test AutoTransientTable(dbftable) |
"""Test AutoTransientTable(dbftable) |
123 |
self.run_iceland_political_tests(table) |
self.run_iceland_political_tests(table) |
124 |
self.run_iceland_political_tests(table) |
self.run_iceland_political_tests(table) |
125 |
|
|
126 |
|
def test_auto_transient_table_query(self): |
127 |
|
"""Test AutoTransientTable.SimpleQuery()""" |
128 |
|
orig_table = DBFTable(os.path.join("..", "Data", "iceland", |
129 |
|
"political.dbf")) |
130 |
|
table = AutoTransientTable(self.transientdb, orig_table) |
131 |
|
# Only a simple test here. The AutoTransientTable simply |
132 |
|
# delegates to its transient table so it should be OK that the |
133 |
|
# real test for it is in test_transient_table_query. However, |
134 |
|
# it's important to check that the column handling works |
135 |
|
# correctly because the AutoTransientTable and it's underlying |
136 |
|
# transient table use different column object types. |
137 |
|
self.assertEquals(table.SimpleQuery(table.Column("AREA"), ">", 10.0), |
138 |
|
[144]) |
139 |
|
|
140 |
|
# test using a Column object as the right parameter |
141 |
|
self.assertEquals(table.SimpleQuery(table.Column("POPYTYPE"), |
142 |
|
"==", |
143 |
|
table.Column("POPYREG")), |
144 |
|
range(156)) |
145 |
|
|
146 |
|
def test_auto_transient_table_dependencies(self): |
147 |
|
"""Test AutoTransientTable.Dependencies()""" |
148 |
|
orig_table = DBFTable(os.path.join("..", "Data", "iceland", |
149 |
|
"political.dbf")) |
150 |
|
table = AutoTransientTable(self.transientdb, orig_table) |
151 |
|
self.assertEquals(table.Dependencies(), (orig_table,)) |
152 |
|
|
153 |
def test_transient_joined_table(self): |
def test_transient_joined_table(self): |
154 |
"""Test TransientJoinedTable""" |
"""Test TransientJoinedTable""" |
170 |
|
|
171 |
self.assertEquals(table.NumRows(), 34) |
self.assertEquals(table.NumRows(), 34) |
172 |
self.assertEquals(table.NumColumns(), 8) |
self.assertEquals(table.NumColumns(), 8) |
173 |
self.assertEquals(table.field_info(0), ('double', 'AREA', 0, 0)) |
self.assertEquals(table.Column(0).type, FIELDTYPE_DOUBLE) |
174 |
self.assertEquals(table.field_info(7), ('int', 'code', 0, 0)) |
self.assertEquals(table.Column(0).name, 'AREA') |
175 |
self.assertEquals(table.field_info(4), ('string', 'CLPTLABEL', 0, 0)) |
self.assertEquals(table.Column(7).type, FIELDTYPE_INT) |
176 |
|
self.assertEquals(table.Column(7).name, 'code') |
177 |
|
self.assertEquals(table.Column(4).type, FIELDTYPE_STRING) |
178 |
|
self.assertEquals(table.Column(4).name, 'CLPTLABEL') |
179 |
|
# HasColumn |
180 |
|
self.failUnless(table.HasColumn("AREA")) |
181 |
|
self.failUnless(table.HasColumn(1)) |
182 |
|
# HasColumn for non-exisiting columns |
183 |
|
self.failIf(table.HasColumn("non_existing_name")) |
184 |
|
self.failIf(table.HasColumn(100)) |
185 |
|
|
186 |
# Read an `interesting' record |
# Reading rows and values |
187 |
self.assertEquals(table.read_record(22), |
self.assertEquals(table.ReadRowAsDict(22), |
188 |
{'PERIMETER': 0.0, 'CLPOINT_': 23, |
{'PERIMETER': 0.0, 'CLPOINT_': 23, |
189 |
'AREA': 0.0, 'CLPTLABEL': 'RUINS', |
'AREA': 0.0, 'CLPTLABEL': 'RUINS', |
190 |
'CLPOINT_ID': 38, 'CLPTFLAG': 0, |
'CLPOINT_ID': 38, 'CLPTFLAG': 0, |
191 |
'code': 1, 'type': 'RUINS'}) |
'code': 1, 'type': 'RUINS'}) |
192 |
|
self.assertEquals(table.ReadValue(22, "type"), 'RUINS') |
193 |
|
self.assertEquals(table.ReadValue(22, 7), 1) |
194 |
|
|
195 |
# The transient_table method should return the table itself |
# The transient_table method should return the table itself |
196 |
self.assert_(table is table.transient_table()) |
self.assert_(table is table.transient_table()) |
197 |
|
|
198 |
|
# The TransientJoinedTable depends on both input tables |
199 |
|
self.assertEquals(table.Dependencies(), (landmarks, auto)) |
200 |
|
|
201 |
|
|
202 |
def test_transient_table_read_twice(self): |
def test_transient_table_read_twice(self): |
203 |
"""Test TransientTable.read_record() reading the same record twice""" |
"""Test TransientTable.ReadRowAsDict() reading the same record twice""" |
204 |
simple = MemoryTable([("type", FIELDTYPE_STRING), |
simple = MemoryTable([("type", FIELDTYPE_STRING), |
205 |
("code", FIELDTYPE_INT)], |
("code", FIELDTYPE_INT)], |
206 |
[("OTHER/UNKNOWN", 0), |
[("OTHER/UNKNOWN", 0), |
216 |
# unitialized local variable, so for passing the test it's |
# unitialized local variable, so for passing the test it's |
217 |
# enough if reading simply succeeds. OTOH, while we're at it we |
# enough if reading simply succeeds. OTOH, while we're at it we |
218 |
# might as well check whether the results are equal anyway :) |
# might as well check whether the results are equal anyway :) |
219 |
result1 = table.read_record(3) |
result1 = table.ReadRowAsDict(3) |
220 |
result2 = table.read_record(3) |
result2 = table.ReadRowAsDict(3) |
221 |
self.assertEquals(result1, result2) |
self.assertEquals(result1, result2) |
222 |
|
|
223 |
|
|
224 |
|
def test_transient_table_query(self): |
225 |
|
"""Test TransientTable.SimpleQuery()""" |
226 |
|
simple = MemoryTable([("type", FIELDTYPE_STRING), |
227 |
|
("value", FIELDTYPE_DOUBLE), |
228 |
|
("code", FIELDTYPE_INT)], |
229 |
|
[("OTHER/UNKNOWN", -1.5, 11), |
230 |
|
("RUINS", 0.0, 1), |
231 |
|
("FARM", 3.141, 2), |
232 |
|
("BUILDING", 2.5, 3), |
233 |
|
("HUT", 1e6, 4), |
234 |
|
("LIGHTHOUSE", -0.01, 5)]) |
235 |
|
table = TransientTable(self.transientdb, simple) |
236 |
|
|
237 |
|
# A column and a value |
238 |
|
self.assertEquals(table.SimpleQuery(table.Column(0), "==", "RUINS"), |
239 |
|
[1]) |
240 |
|
self.assertEquals(table.SimpleQuery(table.Column(2), "!=", 2), |
241 |
|
[0, 1, 3, 4, 5]) |
242 |
|
self.assertEquals(table.SimpleQuery(table.Column(1), "<", 1.0), |
243 |
|
[0, 1, 5]) |
244 |
|
self.assertEquals(table.SimpleQuery(table.Column(1), "<=", -1.5), |
245 |
|
[0]) |
246 |
|
self.assertEquals(table.SimpleQuery(table.Column(2), ">", 3), |
247 |
|
[0, 4, 5]) |
248 |
|
self.assertEquals(table.SimpleQuery(table.Column(2), ">=", 3), |
249 |
|
[0, 3, 4, 5]) |
250 |
|
|
251 |
|
# Two columns as operands |
252 |
|
self.assertEquals(table.SimpleQuery(table.Column(1), |
253 |
|
"<=", table.Column(2)), |
254 |
|
[0, 1, 3, 5]) |
255 |
|
|
256 |
|
# Test whether invalid operators raise a ValueError |
257 |
|
self.assertRaises(ValueError, |
258 |
|
table.SimpleQuery, |
259 |
|
table.Column(1), "<<", table.Column(2)) |
260 |
|
|
261 |
|
|
262 |
if __name__ == "__main__": |
if __name__ == "__main__": |
263 |
support.run_tests() |
support.run_tests() |