/[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 818 - (hide annotations)
Mon May 5 17:18:31 2003 UTC (21 years, 10 months ago) by bh
Original Path: trunk/thuban/test/test_transientdb.py
File MIME type: text/x-python
File size: 6750 byte(s)
Convert the table implementations to a new table interface. All
tables use a common mixin class to provide backwards compatibility
until all table users have been updated.

* Thuban/Model/table.py (OldTableInterfaceMixin): Mixin class to
provide backwards compatibility for table classes implementing the
new interface
(DBFTable, MemoryTable): Implement the new table interface. Use
OldTableInterfaceMixin as base for compatibility
(DBFColumn, MemoryColumn): New. Column description for DBFTable
and MemoryTable resp.

* test/test_dbf_table.py: New. Test cases for the DBFTable with
the new table interface.

* test/test_memory_table.py: New. Test cases for the MemoryTable
with the new table interface.

* test/test_table.py: Document the all tests in this file as only
for backwards compatibility. The equivalent tests for the new
interface are in test_memory_table.py and test_dbf_table.py
(MemoryTableTest.test_read): field_info should be returning tuples
with four items
(MemoryTableTest.test_write): Make doc-string a more precise.

* Thuban/Model/transientdb.py (TransientTableBase): Convert to new
table interface. Derive from from OldTableInterfaceMixin for
compatibility.
(TransientTableBase.create): New intance variable column_map to
map from names and indices to column objects
(TransientTable.create): Use the new table interface of the input
table
(AutoTransientTable): Convert to new table interface. Derive from
from OldTableInterfaceMixin for compatibility.
(AutoTransientTable.write_record): Removed. It's not implemented
yet and we still have to decide how to handle writing with the new
table and data framework.

* test/test_transientdb.py
(TestTransientTable.run_iceland_political_tests)
(TestTransientTable.test_transient_joined_table): Use the new
table interface

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     FIELDTYPE_INT
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     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 bh 765
63     # Read an `interesting' record
64 bh 818 self.assertEquals(table.ReadRowAsDict(144),
65 bh 765 {'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 bh 818 self.assertEquals(table.ValueRange("AREA"), (0.0, 19.462))
76 bh 765
77 bh 818 unique = table.UniqueValues("PONET_ID")
78 bh 765 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 jan 805 simple = MemoryTable([("type", FIELDTYPE_STRING),
118 bh 765 ("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 bh 818 self.assertEquals(table.NumRows(), 34)
134     self.assertEquals(table.NumColumns(), 8)
135 bh 765 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 bh 785 def test_transient_table_read_twice(self):
151     """Test TransientTable.read_record() reading the same record twice"""
152 jan 805 simple = MemoryTable([("type", FIELDTYPE_STRING),
153 bh 785 ("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 bh 765
162 bh 785 # 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 bh 818
172 bh 765 if __name__ == "__main__":
173     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