/[thuban]/branches/WIP-pyshapelib-bramz/test/test_transientdb.py
ViewVC logotype

Contents of /branches/WIP-pyshapelib-bramz/test/test_transientdb.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 984 - (show annotations)
Thu May 22 16:37:48 2003 UTC (21 years, 9 months ago) by bh
Original Path: trunk/thuban/test/test_transientdb.py
File MIME type: text/x-python
File size: 11139 byte(s)
Implement a way to discover dependencies between tables and
shapestores.

* Thuban/Model/transientdb.py (TransientTableBase.Dependencies)
(TransientJoinedTable.Dependencies)
(AutoTransientTable.SimpleQuery): New. Implement the dependencies
interface
(TransientJoinedTable.__init__): Keep tack of the original table
objects in addition to the corresponding transient tables.

* Thuban/Model/table.py (DBFTable.Dependencies)
(MemoryTable.Dependencies): New. Implement the dependencies
interface

* Thuban/Model/data.py (ShapeTable): New. Helper class for
ShapefileStore
(ShapefileStore.__init__): Use ShapeTable instead of
AutoTransientTable
(ShapefileStore.Table, ShapefileStore.Shapefile): Add doc-strings
(ShapefileStore.FileName, ShapefileStore.FileType): New. Accessor
methods for filename and type
(ShapefileStore.Dependencies): New. Implement the dependencies
interface
(DerivedShapeStore): New class to replace SimpleStore. The main
difference to SimpleStore is that it depends not on a shapefile
but another shapestore which expresses the dependencies a bit
better
(SimpleStore.__init__): Add deprecation warning.

* test/test_dbf_table.py (TestDBFTable.test_dependencies): New.
Test for the Dependencies method.

* test/test_memory_table.py (TestMemoryTable.test_dependencies):
New. Test for the Dependencies method.

* test/test_transientdb.py
(TestTransientTable.test_auto_transient_table_dependencies): New.
Test for the Dependencies method.
(TestTransientTable.test_transient_joined_table): Add test for the
Dependencies method.

* test/test_session.py (TestSessionSimple.setUp)
(TestSessionSimple.tearDown): New. Implement a better way to
destroy the sessions.
(TestSessionSimple.test_initial_state)
(TestSessionSimple.test_add_table): Bind session to self.session
so that it's destroyed by tearDown
(TestSessionSimple.test_open_shapefile): New. Test for
OpenShapefile and the object it returns

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, FIELDTYPE_DOUBLE
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, FIELDTYPE_DOUBLE)
58 self.assertEquals(columns[3].name, 'PONET_ID')
59 self.assertEquals(columns[3].type, FIELDTYPE_INT)
60 self.assertEquals(columns[6].name, 'POPYCOUN')
61 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 # Reading rows and values.
71 self.assertEquals(table.ReadRowAsDict(144),
72 {'POPYCOUN': 'IC', 'POPYADMIN': '', 'PONET_': 146,
73 'AREA': 19.462,
74 'POPYTYPE': 1, 'PERIMETER': 88.518000000000001,
75 'POPYREG': '1',
76 'PONET_ID': 145})
77 self.assertEquals(table.ReadValue(144, "AREA"), 19.462)
78 self.assertEquals(table.ReadValue(144, 3), 145)
79
80 # ValueRange may induce a copy to the transient database.
81 # Therefore we put it last so that we can execute this method
82 # twice to check whether the other methods still work after the
83 # copy
84 self.assertEquals(table.ValueRange("AREA"), (0.0, 19.462))
85
86 unique = table.UniqueValues("PONET_ID")
87 unique.sort()
88 self.assertEquals(unique, range(1, 157))
89
90 def test_transient_table(self):
91 """Test TransientTable(dbftable)
92
93 The TransientTable should copy the data to the
94 TransientDatabase.
95 """
96 orig_table = DBFTable(os.path.join("..", "Data", "iceland",
97 "political.dbf"))
98 table = TransientTable(self.transientdb, orig_table)
99 self.run_iceland_political_tests(table)
100
101 # The transient_table method should return the table itself
102 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):
109 """Test AutoTransientTable(dbftable)
110
111 The AutoTransientTable should copy the data to the
112 TransientDatabase on demand.
113 """
114 orig_table = DBFTable(os.path.join("..", "Data", "iceland",
115 "political.dbf"))
116 table = AutoTransientTable(self.transientdb, orig_table)
117
118 # Run the tests twice so that we execute them once when the data
119 # has not been copied to the transient db yet and once when it
120 # has. This assumes that run_iceland_political_tests does at
121 # least one call to a method that copies to the transient db at
122 # its end.
123 self.run_iceland_political_tests(table)
124 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):
154 """Test TransientJoinedTable"""
155 simple = MemoryTable([("type", FIELDTYPE_STRING),
156 ("code", FIELDTYPE_INT)],
157 [("OTHER/UNKNOWN", 0),
158 ("RUINS", 1),
159 ("FARM", 2),
160 ("BUILDING", 3),
161 ("HUT", 4),
162 ("LIGHTHOUSE", 5)])
163 auto = AutoTransientTable(self.transientdb, simple)
164 filename = os.path.join("..", "Data", "iceland",
165 "cultural_landmark-point.dbf")
166 landmarks = AutoTransientTable(self.transientdb, DBFTable(filename))
167
168 table = TransientJoinedTable(self.transientdb, landmarks, "CLPTLABEL",
169 auto, "type")
170
171 self.assertEquals(table.NumRows(), 34)
172 self.assertEquals(table.NumColumns(), 8)
173 self.assertEquals(table.Column(0).type, FIELDTYPE_DOUBLE)
174 self.assertEquals(table.Column(0).name, 'AREA')
175 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 # Reading rows and values
187 self.assertEquals(table.ReadRowAsDict(22),
188 {'PERIMETER': 0.0, 'CLPOINT_': 23,
189 'AREA': 0.0, 'CLPTLABEL': 'RUINS',
190 'CLPOINT_ID': 38, 'CLPTFLAG': 0,
191 '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
196 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):
203 """Test TransientTable.ReadRowAsDict() reading the same record twice"""
204 simple = MemoryTable([("type", FIELDTYPE_STRING),
205 ("code", FIELDTYPE_INT)],
206 [("OTHER/UNKNOWN", 0),
207 ("RUINS", 1),
208 ("FARM", 2),
209 ("BUILDING", 3),
210 ("HUT", 4),
211 ("LIGHTHOUSE", 5)])
212 table = TransientTable(self.transientdb, simple)
213
214 # There was a bug where reading the same record twice would
215 # raise an exception in the second call because of an
216 # unitialized local variable, so for passing the test it's
217 # enough if reading simply succeeds. OTOH, while we're at it we
218 # might as well check whether the results are equal anyway :)
219 result1 = table.ReadRowAsDict(3)
220 result2 = table.ReadRowAsDict(3)
221 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__":
263 support.run_tests()

Properties

Name Value
svn:eol-style native
svn:keywords Author Date Id Revision

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26