50 |
self.assertEquals(table.NumRows(), 156) |
self.assertEquals(table.NumRows(), 156) |
51 |
self.assertEquals(table.NumColumns(), 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. |
|
# decimal precision is always 0. |
|
54 |
columns = table.Columns() |
columns = table.Columns() |
55 |
self.assertEquals(columns[0].name, 'AREA') |
self.assertEquals(columns[0].name, 'AREA') |
56 |
self.assertEquals(columns[0].type, FIELDTYPE_DOUBLE) |
self.assertEquals(columns[0].type, FIELDTYPE_DOUBLE) |
100 |
# The transient_table method should return the table itself |
# The transient_table method should return the table itself |
101 |
self.assert_(table is table.transient_table()) |
self.assert_(table is table.transient_table()) |
102 |
|
|
103 |
|
# The title is simply copied over from the original table |
104 |
|
self.assertEquals(table.Title(), orig_table.Title()) |
105 |
|
|
106 |
# The TransientTable class itself doesn't implement the |
# The TransientTable class itself doesn't implement the |
107 |
# Dependencies method, so we don't test it. |
# Dependencies method, so we don't test it. |
108 |
|
|
152 |
table = AutoTransientTable(self.transientdb, orig_table) |
table = AutoTransientTable(self.transientdb, orig_table) |
153 |
self.assertEquals(table.Dependencies(), (orig_table,)) |
self.assertEquals(table.Dependencies(), (orig_table,)) |
154 |
|
|
155 |
|
def test_auto_transient_table_title(self): |
156 |
|
"""Test AutoTransientTable.Title()""" |
157 |
|
orig_table = DBFTable(os.path.join("..", "Data", "iceland", |
158 |
|
"political.dbf")) |
159 |
|
table = AutoTransientTable(self.transientdb, orig_table) |
160 |
|
# The title is of course the same as that of the original table |
161 |
|
self.assertEquals(table.Title(), orig_table.Title()) |
162 |
|
|
163 |
def test_transient_joined_table(self): |
def test_transient_joined_table(self): |
164 |
"""Test TransientJoinedTable""" |
"""Test TransientJoinedTable""" |
165 |
simple = MemoryTable([("type", FIELDTYPE_STRING), |
simple = MemoryTable([("type", FIELDTYPE_STRING), |
208 |
# The TransientJoinedTable depends on both input tables |
# The TransientJoinedTable depends on both input tables |
209 |
self.assertEquals(table.Dependencies(), (landmarks, auto)) |
self.assertEquals(table.Dependencies(), (landmarks, auto)) |
210 |
|
|
211 |
|
# The title is constructed from the titles of the input tables. |
212 |
|
self.assertEquals(table.Title(), |
213 |
|
"Join of %s and %s" % (landmarks.Title(), |
214 |
|
auto.Title())) |
215 |
|
|
216 |
|
|
217 |
|
def test_transient_joined_table_same_column_name(self): |
218 |
|
"""Test TransientJoinedTable join on columns with same name |
219 |
|
|
220 |
|
The transient DB maps the column names used by the tables to |
221 |
|
another set of names used only inside the SQLite database. There |
222 |
|
was a bug in the way this mapping was used when joining on |
223 |
|
fields with the same names in both tables so that the joined |
224 |
|
table ended up joining on the same column in the same table. |
225 |
|
""" |
226 |
|
mem_stretches = MemoryTable([("stretch_id", FIELDTYPE_INT)], |
227 |
|
[(i,) for i in range(4)]) |
228 |
|
stretches = AutoTransientTable(self.transientdb, mem_stretches) |
229 |
|
|
230 |
|
mem_discharges = MemoryTable([("disch_id", FIELDTYPE_INT), |
231 |
|
("stretch_id", FIELDTYPE_INT)], |
232 |
|
[(1, 0), (2, 3)]) |
233 |
|
discharges = AutoTransientTable(self.transientdb, mem_discharges) |
234 |
|
|
235 |
|
table = TransientJoinedTable(self.transientdb, stretches, "stretch_id", |
236 |
|
discharges, "stretch_id", |
237 |
|
outer_join = True) |
238 |
|
|
239 |
|
self.assertEquals(table.NumRows(), 4) |
240 |
|
self.assertEquals(table.NumColumns(), 3) |
241 |
|
|
242 |
|
# HasColumn |
243 |
|
self.failUnless(table.HasColumn("stretch_id")) |
244 |
|
self.failUnless(table.HasColumn("disch_id")) |
245 |
|
|
246 |
|
|
247 |
|
def test_transient_joined_table_with_equal_column_names(self): |
248 |
|
"""Test TransientJoinedTable join on tables with equal column names |
249 |
|
|
250 |
|
If a name collision occurs for the field names, underscores are |
251 |
|
appended as long as any collision is resolved. |
252 |
|
""" |
253 |
|
mem_stretches = MemoryTable([("stretch_id", FIELDTYPE_INT), |
254 |
|
("name", FIELDTYPE_INT)], |
255 |
|
[(0, 10), (1, 11), (2, 12), (3, 13) ]) |
256 |
|
stretches = AutoTransientTable(self.transientdb, mem_stretches) |
257 |
|
|
258 |
|
mem_discharges = MemoryTable([("disch_id", FIELDTYPE_INT), |
259 |
|
("stretch_id", FIELDTYPE_INT), |
260 |
|
("name", FIELDTYPE_INT)], |
261 |
|
[(1, 0, 1), (2, 3, 2)]) |
262 |
|
discharges = AutoTransientTable(self.transientdb, mem_discharges) |
263 |
|
|
264 |
|
table = TransientJoinedTable(self.transientdb, stretches, "stretch_id", |
265 |
|
discharges, "stretch_id", |
266 |
|
outer_join = True) |
267 |
|
|
268 |
|
self.assertEquals(table.NumRows(), 4) |
269 |
|
self.assertEquals(table.NumColumns(), 5) |
270 |
|
|
271 |
|
# HasColumn |
272 |
|
self.assertEquals([c.name for c in table.Columns()], |
273 |
|
["stretch_id", "name", "disch_id", "stretch_id_", |
274 |
|
"name_"]) |
275 |
|
|
276 |
|
def test_transient_joined_table_name_collisions_dont_modify_in_place(self): |
277 |
|
"""Test TransientJoinedTable name-collisions do not modifying in place |
278 |
|
|
279 |
|
The name collision work-around by appending underscores |
280 |
|
accidentally modified the column objects in place. We do two |
281 |
|
joins therefore in reverse order to detect this: The first join |
282 |
|
will lead to a modified name in the column object of the right |
283 |
|
table which is then used as the left table in the second join so |
284 |
|
the underscored name will appear before the non-underscored one |
285 |
|
in the list of column names after the second join. |
286 |
|
""" |
287 |
|
mem1 = MemoryTable([("stretch_id", FIELDTYPE_INT), |
288 |
|
("name", FIELDTYPE_INT)], |
289 |
|
[(0, 10), (1, 11), (2, 12), (3, 13) ]) |
290 |
|
table1 = AutoTransientTable(self.transientdb, mem1) |
291 |
|
|
292 |
|
mem2 = MemoryTable([("stretch_id", FIELDTYPE_INT), |
293 |
|
("name", FIELDTYPE_INT)], |
294 |
|
[(0, 10), (1, 11), (2, 12), (3, 13) ]) |
295 |
|
table2 = AutoTransientTable(self.transientdb, mem2) |
296 |
|
|
297 |
|
table = TransientJoinedTable(self.transientdb, table1, "stretch_id", |
298 |
|
table2, "stretch_id", outer_join = True) |
299 |
|
table = TransientJoinedTable(self.transientdb, table2, "stretch_id", |
300 |
|
table1, "stretch_id", outer_join = True) |
301 |
|
|
302 |
|
self.assertEquals([c.name for c in table.Columns()], |
303 |
|
["stretch_id", "name", "stretch_id_", "name_"]) |
304 |
|
|
305 |
def test_transient_table_read_twice(self): |
def test_transient_table_read_twice(self): |
306 |
"""Test TransientTable.ReadRowAsDict() reading the same record twice""" |
"""Test TransientTable.ReadRowAsDict() reading the same record twice""" |