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 |
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, 'double') |
58 |
|
self.assertEquals(columns[3].name, 'PONET_ID') |
59 |
|
self.assertEquals(columns[3].type, 'int') |
60 |
|
self.assertEquals(columns[6].name, 'POPYCOUN') |
61 |
|
self.assertEquals(columns[6].type, 'string') |
62 |
|
|
63 |
# Read an `interesting' record |
# Read an `interesting' record |
64 |
self.assertEquals(table.read_record(144), |
self.assertEquals(table.ReadRowAsDict(144), |
65 |
{'POPYCOUN': 'IC', 'POPYADMIN': '', 'PONET_': 146, |
{'POPYCOUN': 'IC', 'POPYADMIN': '', 'PONET_': 146, |
66 |
'AREA': 19.462, |
'AREA': 19.462, |
67 |
'POPYTYPE': 1, 'PERIMETER': 88.518000000000001, |
'POPYTYPE': 1, 'PERIMETER': 88.518000000000001, |
72 |
# Therefore we put it last so that we can execute this method |
# Therefore we put it last so that we can execute this method |
73 |
# twice to check whether the other methods still work after the |
# twice to check whether the other methods still work after the |
74 |
# copy |
# copy |
75 |
self.assertEquals(table.field_range("AREA"), |
self.assertEquals(table.ValueRange("AREA"), (0.0, 19.462)) |
|
((0.0, None), (19.462, None))) |
|
76 |
|
|
77 |
unique = table.GetUniqueValues("PONET_ID") |
unique = table.UniqueValues("PONET_ID") |
78 |
unique.sort() |
unique.sort() |
79 |
self.assertEquals(unique, range(1, 157)) |
self.assertEquals(unique, range(1, 157)) |
80 |
|
|
114 |
|
|
115 |
def test_transient_joined_table(self): |
def test_transient_joined_table(self): |
116 |
"""Test TransientJoinedTable""" |
"""Test TransientJoinedTable""" |
117 |
simple = SimpleTable([("type", FIELDTYPE_STRING), |
simple = MemoryTable([("type", FIELDTYPE_STRING), |
118 |
("code", FIELDTYPE_INT)], |
("code", FIELDTYPE_INT)], |
119 |
[("OTHER/UNKNOWN", 0), |
[("OTHER/UNKNOWN", 0), |
120 |
("RUINS", 1), |
("RUINS", 1), |
130 |
table = TransientJoinedTable(self.transientdb, landmarks, "CLPTLABEL", |
table = TransientJoinedTable(self.transientdb, landmarks, "CLPTLABEL", |
131 |
auto, "type") |
auto, "type") |
132 |
|
|
133 |
self.assertEquals(table.record_count(), 34) |
self.assertEquals(table.NumRows(), 34) |
134 |
self.assertEquals(table.field_count(), 8) |
self.assertEquals(table.NumColumns(), 8) |
135 |
self.assertEquals(table.field_info(0), ('double', 'AREA', 0, 0)) |
self.assertEquals(table.field_info(0), ('double', 'AREA', 0, 0)) |
136 |
self.assertEquals(table.field_info(7), ('int', 'code', 0, 0)) |
self.assertEquals(table.field_info(7), ('int', 'code', 0, 0)) |
137 |
self.assertEquals(table.field_info(4), ('string', 'CLPTLABEL', 0, 0)) |
self.assertEquals(table.field_info(4), ('string', 'CLPTLABEL', 0, 0)) |
147 |
self.assert_(table is table.transient_table()) |
self.assert_(table is table.transient_table()) |
148 |
|
|
149 |
|
|
150 |
|
def test_transient_table_read_twice(self): |
151 |
|
"""Test TransientTable.read_record() reading the same record twice""" |
152 |
|
simple = MemoryTable([("type", FIELDTYPE_STRING), |
153 |
|
("code", FIELDTYPE_INT)], |
154 |
|
[("OTHER/UNKNOWN", 0), |
155 |
|
("RUINS", 1), |
156 |
|
("FARM", 2), |
157 |
|
("BUILDING", 3), |
158 |
|
("HUT", 4), |
159 |
|
("LIGHTHOUSE", 5)]) |
160 |
|
table = TransientTable(self.transientdb, simple) |
161 |
|
|
162 |
|
# There was a bug where reading the same record twice would |
163 |
|
# raise an exception in the second call because of an |
164 |
|
# unitialized local variable, so for passing the test it's |
165 |
|
# enough if reading simply succeeds. OTOH, while we're at it we |
166 |
|
# might as well check whether the results are equal anyway :) |
167 |
|
result1 = table.read_record(3) |
168 |
|
result2 = table.read_record(3) |
169 |
|
self.assertEquals(result1, result2) |
170 |
|
|
171 |
|
|
172 |
if __name__ == "__main__": |
if __name__ == "__main__": |
173 |
support.run_tests() |
support.run_tests() |