1 |
# Copyright (c) 2002, 2003 by Intevation GmbH |
2 |
# Authors: |
3 |
# Bernhard Herzog <[email protected]> |
4 |
# |
5 |
# This program is free software under the GPL (>=v2) |
6 |
# Read the file COPYING coming with Thuban for details. |
7 |
|
8 |
""" |
9 |
Test the Transient DB classes |
10 |
""" |
11 |
|
12 |
__version__ = "$Revision$" |
13 |
# $Source$ |
14 |
# $Id$ |
15 |
|
16 |
import os |
17 |
import unittest |
18 |
|
19 |
import support |
20 |
support.initthuban() |
21 |
|
22 |
from Thuban.Model.table import DBFTable, MemoryTable, FIELDTYPE_STRING, \ |
23 |
FIELDTYPE_INT |
24 |
from Thuban.Model.transientdb import TransientDatabase, TransientTable, \ |
25 |
TransientJoinedTable, AutoTransientTable |
26 |
|
27 |
|
28 |
class TestTransientTable(unittest.TestCase, support.FileTestMixin): |
29 |
|
30 |
def setUp(self): |
31 |
"""Create a transient database as self.transientdb""" |
32 |
filename = self.temp_file_name("transient_table.sqlite") |
33 |
if os.path.exists(filename): |
34 |
os.remove(filename) |
35 |
journal = filename + "-journal" |
36 |
if os.path.exists(journal): |
37 |
print "removing journal", journal |
38 |
os.remove(journal) |
39 |
self.transientdb = TransientDatabase(filename) |
40 |
|
41 |
def tearDown(self): |
42 |
self.transientdb.close() |
43 |
|
44 |
def run_iceland_political_tests(self, table): |
45 |
"""Run some tests on tablte |
46 |
|
47 |
Assume that table holds the data of the file |
48 |
../Data/iceland/political.dbf sample file. |
49 |
""" |
50 |
self.assertEquals(table.NumRows(), 156) |
51 |
self.assertEquals(table.NumColumns(), 8) |
52 |
|
53 |
# Check one each of the possible field types. The width and |
54 |
# decimal precision is always 0. |
55 |
columns = table.Columns() |
56 |
self.assertEquals(columns[0].name, 'AREA') |
57 |
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 |
64 |
self.assertEquals(table.ReadRowAsDict(144), |
65 |
{'POPYCOUN': 'IC', 'POPYADMIN': '', 'PONET_': 146, |
66 |
'AREA': 19.462, |
67 |
'POPYTYPE': 1, 'PERIMETER': 88.518000000000001, |
68 |
'POPYREG': '1', |
69 |
'PONET_ID': 145}) |
70 |
|
71 |
# field_range may induce a copy to the transient database. |
72 |
# Therefore we put it last so that we can execute this method |
73 |
# twice to check whether the other methods still work after the |
74 |
# copy |
75 |
self.assertEquals(table.ValueRange("AREA"), (0.0, 19.462)) |
76 |
|
77 |
unique = table.UniqueValues("PONET_ID") |
78 |
unique.sort() |
79 |
self.assertEquals(unique, range(1, 157)) |
80 |
|
81 |
def test_transient_table(self): |
82 |
"""Test TransientTable(dbftable) |
83 |
|
84 |
The TransientTable should copy the data to the |
85 |
TransientDatabase. |
86 |
""" |
87 |
orig_table = DBFTable(os.path.join("..", "Data", "iceland", |
88 |
"political.dbf")) |
89 |
table = TransientTable(self.transientdb, orig_table) |
90 |
self.run_iceland_political_tests(table) |
91 |
|
92 |
# The transient_table method should return the table itself |
93 |
self.assert_(table is table.transient_table()) |
94 |
|
95 |
|
96 |
def test_auto_transient_table(self): |
97 |
"""Test AutoTransientTable(dbftable) |
98 |
|
99 |
The AutoTransientTable should copy the data to the |
100 |
TransientDatabase on demand. |
101 |
""" |
102 |
orig_table = DBFTable(os.path.join("..", "Data", "iceland", |
103 |
"political.dbf")) |
104 |
table = AutoTransientTable(self.transientdb, orig_table) |
105 |
|
106 |
# Run the tests twice so that we execute them once when the data |
107 |
# has not been copied to the transient db yet and once when it |
108 |
# has. This assumes that run_iceland_political_tests does at |
109 |
# least one call to a method that copies to the transient db at |
110 |
# its end. |
111 |
self.run_iceland_political_tests(table) |
112 |
self.run_iceland_political_tests(table) |
113 |
|
114 |
|
115 |
def test_transient_joined_table(self): |
116 |
"""Test TransientJoinedTable""" |
117 |
simple = MemoryTable([("type", FIELDTYPE_STRING), |
118 |
("code", FIELDTYPE_INT)], |
119 |
[("OTHER/UNKNOWN", 0), |
120 |
("RUINS", 1), |
121 |
("FARM", 2), |
122 |
("BUILDING", 3), |
123 |
("HUT", 4), |
124 |
("LIGHTHOUSE", 5)]) |
125 |
auto = AutoTransientTable(self.transientdb, simple) |
126 |
filename = os.path.join("..", "Data", "iceland", |
127 |
"cultural_landmark-point.dbf") |
128 |
landmarks = AutoTransientTable(self.transientdb, DBFTable(filename)) |
129 |
|
130 |
table = TransientJoinedTable(self.transientdb, landmarks, "CLPTLABEL", |
131 |
auto, "type") |
132 |
|
133 |
self.assertEquals(table.NumRows(), 34) |
134 |
self.assertEquals(table.NumColumns(), 8) |
135 |
self.assertEquals(table.field_info(0), ('double', 'AREA', 0, 0)) |
136 |
self.assertEquals(table.field_info(7), ('int', 'code', 0, 0)) |
137 |
self.assertEquals(table.field_info(4), ('string', 'CLPTLABEL', 0, 0)) |
138 |
|
139 |
# Read an `interesting' record |
140 |
self.assertEquals(table.read_record(22), |
141 |
{'PERIMETER': 0.0, 'CLPOINT_': 23, |
142 |
'AREA': 0.0, 'CLPTLABEL': 'RUINS', |
143 |
'CLPOINT_ID': 38, 'CLPTFLAG': 0, |
144 |
'code': 1, 'type': 'RUINS'}) |
145 |
|
146 |
# The transient_table method should return the table itself |
147 |
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__": |
173 |
support.run_tests() |