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 |
|
# 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 |
# Read an `interesting' record |
71 |
self.assertEquals(table.ReadRowAsDict(144), |
self.assertEquals(table.ReadRowAsDict(144), |
75 |
'POPYREG': '1', |
'POPYREG': '1', |
76 |
'PONET_ID': 145}) |
'PONET_ID': 145}) |
77 |
|
|
78 |
# field_range may induce a copy to the transient database. |
# ValueRange may induce a copy to the transient database. |
79 |
# Therefore we put it last so that we can execute this method |
# Therefore we put it last so that we can execute this method |
80 |
# twice to check whether the other methods still work after the |
# twice to check whether the other methods still work after the |
81 |
# copy |
# copy |
139 |
|
|
140 |
self.assertEquals(table.NumRows(), 34) |
self.assertEquals(table.NumRows(), 34) |
141 |
self.assertEquals(table.NumColumns(), 8) |
self.assertEquals(table.NumColumns(), 8) |
142 |
self.assertEquals(table.field_info(0), ('double', 'AREA', 0, 0)) |
self.assertEquals(table.Column(0).type, FIELDTYPE_DOUBLE) |
143 |
self.assertEquals(table.field_info(7), ('int', 'code', 0, 0)) |
self.assertEquals(table.Column(0).name, 'AREA') |
144 |
self.assertEquals(table.field_info(4), ('string', 'CLPTLABEL', 0, 0)) |
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 |
# Read an `interesting' record |
156 |
self.assertEquals(table.read_record(22), |
self.assertEquals(table.ReadRowAsDict(22), |
157 |
{'PERIMETER': 0.0, 'CLPOINT_': 23, |
{'PERIMETER': 0.0, 'CLPOINT_': 23, |
158 |
'AREA': 0.0, 'CLPTLABEL': 'RUINS', |
'AREA': 0.0, 'CLPTLABEL': 'RUINS', |
159 |
'CLPOINT_ID': 38, 'CLPTFLAG': 0, |
'CLPOINT_ID': 38, 'CLPTFLAG': 0, |
164 |
|
|
165 |
|
|
166 |
def test_transient_table_read_twice(self): |
def test_transient_table_read_twice(self): |
167 |
"""Test TransientTable.read_record() reading the same record twice""" |
"""Test TransientTable.ReadRowAsDict() reading the same record twice""" |
168 |
simple = MemoryTable([("type", FIELDTYPE_STRING), |
simple = MemoryTable([("type", FIELDTYPE_STRING), |
169 |
("code", FIELDTYPE_INT)], |
("code", FIELDTYPE_INT)], |
170 |
[("OTHER/UNKNOWN", 0), |
[("OTHER/UNKNOWN", 0), |
180 |
# unitialized local variable, so for passing the test it's |
# unitialized local variable, so for passing the test it's |
181 |
# enough if reading simply succeeds. OTOH, while we're at it we |
# enough if reading simply succeeds. OTOH, while we're at it we |
182 |
# might as well check whether the results are equal anyway :) |
# might as well check whether the results are equal anyway :) |
183 |
result1 = table.read_record(3) |
result1 = table.ReadRowAsDict(3) |
184 |
result2 = table.read_record(3) |
result2 = table.ReadRowAsDict(3) |
185 |
self.assertEquals(result1, result2) |
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__": |
if __name__ == "__main__": |
227 |
support.run_tests() |
support.run_tests() |