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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 948 - (hide annotations)
Tue May 20 15:27:31 2003 UTC (21 years, 9 months ago) by jonathan
Original Path: trunk/thuban/test/test_transientdb.py
File MIME type: text/x-python
File size: 10526 byte(s)
(TestTransientTable.test_auto_transient_table_query): Added a test
        for using a Column object as the "right" parameter to a query.

1 bh 765 # 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 jan 805 from Thuban.Model.table import DBFTable, MemoryTable, FIELDTYPE_STRING, \
23 bh 839 FIELDTYPE_INT, FIELDTYPE_DOUBLE
24 bh 765 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 bh 818 self.assertEquals(table.NumRows(), 156)
51     self.assertEquals(table.NumColumns(), 8)
52 bh 765
53     # Check one each of the possible field types. The width and
54     # decimal precision is always 0.
55 bh 818 columns = table.Columns()
56     self.assertEquals(columns[0].name, 'AREA')
57 bh 839 self.assertEquals(columns[0].type, FIELDTYPE_DOUBLE)
58 bh 818 self.assertEquals(columns[3].name, 'PONET_ID')
59 bh 839 self.assertEquals(columns[3].type, FIELDTYPE_INT)
60 bh 818 self.assertEquals(columns[6].name, 'POPYCOUN')
61 bh 839 self.assertEquals(columns[6].type, FIELDTYPE_STRING)
62 bh 765
63 bh 839 # 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 bh 849 # Reading rows and values.
71 bh 818 self.assertEquals(table.ReadRowAsDict(144),
72 bh 765 {'POPYCOUN': 'IC', 'POPYADMIN': '', 'PONET_': 146,
73     'AREA': 19.462,
74     'POPYTYPE': 1, 'PERIMETER': 88.518000000000001,
75     'POPYREG': '1',
76     'PONET_ID': 145})
77 bh 849 self.assertEquals(table.ReadValue(144, "AREA"), 19.462)
78     self.assertEquals(table.ReadValue(144, 3), 145)
79 bh 765
80 bh 839 # ValueRange may induce a copy to the transient database.
81 bh 765 # 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 bh 818 self.assertEquals(table.ValueRange("AREA"), (0.0, 19.462))
85 bh 765
86 bh 818 unique = table.UniqueValues("PONET_ID")
87 bh 765 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    
105     def test_auto_transient_table(self):
106     """Test AutoTransientTable(dbftable)
107    
108     The AutoTransientTable should copy the data to the
109     TransientDatabase on demand.
110     """
111     orig_table = DBFTable(os.path.join("..", "Data", "iceland",
112     "political.dbf"))
113     table = AutoTransientTable(self.transientdb, orig_table)
114    
115     # Run the tests twice so that we execute them once when the data
116     # has not been copied to the transient db yet and once when it
117     # has. This assumes that run_iceland_political_tests does at
118     # least one call to a method that copies to the transient db at
119     # its end.
120     self.run_iceland_political_tests(table)
121     self.run_iceland_political_tests(table)
122    
123 bh 845 def test_auto_transient_table_query(self):
124     """Test AutoTransientTable.SimpleQuery()"""
125     orig_table = DBFTable(os.path.join("..", "Data", "iceland",
126     "political.dbf"))
127     table = AutoTransientTable(self.transientdb, orig_table)
128 jonathan 948 # Only a simple test here. The AutoTransientTable simply
129 bh 845 # delegates to its transient table so it should be OK that the
130     # real test for it is in test_transient_table_query. However,
131     # it's important to check that the column handling works
132     # correctly because the AutoTransientTable and it's underlying
133     # transient table use different column object types.
134     self.assertEquals(table.SimpleQuery(table.Column("AREA"), ">", 10.0),
135     [144])
136 bh 765
137 jonathan 948 # test using a Column object as the right parameter
138     self.assertEquals(table.SimpleQuery(table.Column("POPYTYPE"),
139     "==",
140     table.Column("POPYREG")),
141     range(156))
142    
143 bh 765 def test_transient_joined_table(self):
144     """Test TransientJoinedTable"""
145 jan 805 simple = MemoryTable([("type", FIELDTYPE_STRING),
146 bh 765 ("code", FIELDTYPE_INT)],
147     [("OTHER/UNKNOWN", 0),
148     ("RUINS", 1),
149     ("FARM", 2),
150     ("BUILDING", 3),
151     ("HUT", 4),
152     ("LIGHTHOUSE", 5)])
153     auto = AutoTransientTable(self.transientdb, simple)
154     filename = os.path.join("..", "Data", "iceland",
155     "cultural_landmark-point.dbf")
156     landmarks = AutoTransientTable(self.transientdb, DBFTable(filename))
157    
158     table = TransientJoinedTable(self.transientdb, landmarks, "CLPTLABEL",
159     auto, "type")
160    
161 bh 818 self.assertEquals(table.NumRows(), 34)
162     self.assertEquals(table.NumColumns(), 8)
163 bh 839 self.assertEquals(table.Column(0).type, FIELDTYPE_DOUBLE)
164     self.assertEquals(table.Column(0).name, 'AREA')
165     self.assertEquals(table.Column(7).type, FIELDTYPE_INT)
166     self.assertEquals(table.Column(7).name, 'code')
167     self.assertEquals(table.Column(4).type, FIELDTYPE_STRING)
168     self.assertEquals(table.Column(4).name, 'CLPTLABEL')
169     # HasColumn
170     self.failUnless(table.HasColumn("AREA"))
171     self.failUnless(table.HasColumn(1))
172     # HasColumn for non-exisiting columns
173     self.failIf(table.HasColumn("non_existing_name"))
174     self.failIf(table.HasColumn(100))
175 bh 765
176 bh 849 # Reading rows and values
177 bh 839 self.assertEquals(table.ReadRowAsDict(22),
178 bh 765 {'PERIMETER': 0.0, 'CLPOINT_': 23,
179     'AREA': 0.0, 'CLPTLABEL': 'RUINS',
180     'CLPOINT_ID': 38, 'CLPTFLAG': 0,
181     'code': 1, 'type': 'RUINS'})
182 bh 849 self.assertEquals(table.ReadValue(22, "type"), 'RUINS')
183     self.assertEquals(table.ReadValue(22, 7), 1)
184 bh 765
185     # The transient_table method should return the table itself
186     self.assert_(table is table.transient_table())
187    
188    
189 bh 785 def test_transient_table_read_twice(self):
190 bh 842 """Test TransientTable.ReadRowAsDict() reading the same record twice"""
191 jan 805 simple = MemoryTable([("type", FIELDTYPE_STRING),
192 bh 785 ("code", FIELDTYPE_INT)],
193     [("OTHER/UNKNOWN", 0),
194     ("RUINS", 1),
195     ("FARM", 2),
196     ("BUILDING", 3),
197     ("HUT", 4),
198     ("LIGHTHOUSE", 5)])
199     table = TransientTable(self.transientdb, simple)
200 bh 765
201 bh 785 # There was a bug where reading the same record twice would
202     # raise an exception in the second call because of an
203     # unitialized local variable, so for passing the test it's
204     # enough if reading simply succeeds. OTOH, while we're at it we
205     # might as well check whether the results are equal anyway :)
206 bh 839 result1 = table.ReadRowAsDict(3)
207     result2 = table.ReadRowAsDict(3)
208 bh 785 self.assertEquals(result1, result2)
209    
210 bh 818
211 bh 842 def test_transient_table_query(self):
212     """Test TransientTable.SimpleQuery()"""
213     simple = MemoryTable([("type", FIELDTYPE_STRING),
214     ("value", FIELDTYPE_DOUBLE),
215     ("code", FIELDTYPE_INT)],
216     [("OTHER/UNKNOWN", -1.5, 11),
217     ("RUINS", 0.0, 1),
218     ("FARM", 3.141, 2),
219     ("BUILDING", 2.5, 3),
220     ("HUT", 1e6, 4),
221     ("LIGHTHOUSE", -0.01, 5)])
222     table = TransientTable(self.transientdb, simple)
223    
224     # A column and a value
225     self.assertEquals(table.SimpleQuery(table.Column(0), "==", "RUINS"),
226     [1])
227     self.assertEquals(table.SimpleQuery(table.Column(2), "!=", 2),
228     [0, 1, 3, 4, 5])
229     self.assertEquals(table.SimpleQuery(table.Column(1), "<", 1.0),
230     [0, 1, 5])
231     self.assertEquals(table.SimpleQuery(table.Column(1), "<=", -1.5),
232     [0])
233     self.assertEquals(table.SimpleQuery(table.Column(2), ">", 3),
234     [0, 4, 5])
235     self.assertEquals(table.SimpleQuery(table.Column(2), ">=", 3),
236     [0, 3, 4, 5])
237    
238     # Two columns as operands
239     self.assertEquals(table.SimpleQuery(table.Column(1),
240     "<=", table.Column(2)),
241     [0, 1, 3, 5])
242    
243     # Test whether invalid operators raise a ValueError
244     self.assertRaises(ValueError,
245     table.SimpleQuery,
246     table.Column(1), "<<", table.Column(2))
247    
248    
249 bh 765 if __name__ == "__main__":
250     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