/[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 805 - (hide annotations)
Fri May 2 16:42:35 2003 UTC (21 years, 10 months ago) by jan
Original Path: trunk/thuban/test/test_transientdb.py
File MIME type: text/x-python
File size: 6678 byte(s)
(SimpleTable): Removed.
(TestTransientTable.test_transient_joined_table,
(TestTransientTable.test_transient_table_read_twice): Replaced
SimpleTable by MemoryTable.

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     self.assertEquals(table.record_count(), 156)
51     self.assertEquals(table.field_count(), 8)
52    
53     # Check one each of the possible field types. The width and
54     # decimal precision is always 0.
55     self.assertEquals(table.field_info(0), ('double', 'AREA', 0, 0))
56     self.assertEquals(table.field_info(3), ('int', 'PONET_ID', 0, 0))
57     self.assertEquals(table.field_info(6), ('string', 'POPYCOUN', 0, 0))
58    
59     # Read an `interesting' record
60     self.assertEquals(table.read_record(144),
61     {'POPYCOUN': 'IC', 'POPYADMIN': '', 'PONET_': 146,
62     'AREA': 19.462,
63     'POPYTYPE': 1, 'PERIMETER': 88.518000000000001,
64     'POPYREG': '1',
65     'PONET_ID': 145})
66    
67     # field_range may induce a copy to the transient database.
68     # Therefore we put it last so that we can execute this method
69     # twice to check whether the other methods still work after the
70     # copy
71     self.assertEquals(table.field_range("AREA"),
72     ((0.0, None), (19.462, None)))
73    
74     unique = table.GetUniqueValues("PONET_ID")
75     unique.sort()
76     self.assertEquals(unique, range(1, 157))
77    
78     def test_transient_table(self):
79     """Test TransientTable(dbftable)
80    
81     The TransientTable should copy the data to the
82     TransientDatabase.
83     """
84     orig_table = DBFTable(os.path.join("..", "Data", "iceland",
85     "political.dbf"))
86     table = TransientTable(self.transientdb, orig_table)
87     self.run_iceland_political_tests(table)
88    
89     # The transient_table method should return the table itself
90     self.assert_(table is table.transient_table())
91    
92    
93     def test_auto_transient_table(self):
94     """Test AutoTransientTable(dbftable)
95    
96     The AutoTransientTable should copy the data to the
97     TransientDatabase on demand.
98     """
99     orig_table = DBFTable(os.path.join("..", "Data", "iceland",
100     "political.dbf"))
101     table = AutoTransientTable(self.transientdb, orig_table)
102    
103     # Run the tests twice so that we execute them once when the data
104     # has not been copied to the transient db yet and once when it
105     # has. This assumes that run_iceland_political_tests does at
106     # least one call to a method that copies to the transient db at
107     # its end.
108     self.run_iceland_political_tests(table)
109     self.run_iceland_political_tests(table)
110    
111    
112     def test_transient_joined_table(self):
113     """Test TransientJoinedTable"""
114 jan 805 simple = MemoryTable([("type", FIELDTYPE_STRING),
115 bh 765 ("code", FIELDTYPE_INT)],
116     [("OTHER/UNKNOWN", 0),
117     ("RUINS", 1),
118     ("FARM", 2),
119     ("BUILDING", 3),
120     ("HUT", 4),
121     ("LIGHTHOUSE", 5)])
122     auto = AutoTransientTable(self.transientdb, simple)
123     filename = os.path.join("..", "Data", "iceland",
124     "cultural_landmark-point.dbf")
125     landmarks = AutoTransientTable(self.transientdb, DBFTable(filename))
126    
127     table = TransientJoinedTable(self.transientdb, landmarks, "CLPTLABEL",
128     auto, "type")
129    
130     self.assertEquals(table.record_count(), 34)
131     self.assertEquals(table.field_count(), 8)
132     self.assertEquals(table.field_info(0), ('double', 'AREA', 0, 0))
133     self.assertEquals(table.field_info(7), ('int', 'code', 0, 0))
134     self.assertEquals(table.field_info(4), ('string', 'CLPTLABEL', 0, 0))
135    
136     # Read an `interesting' record
137     self.assertEquals(table.read_record(22),
138     {'PERIMETER': 0.0, 'CLPOINT_': 23,
139     'AREA': 0.0, 'CLPTLABEL': 'RUINS',
140     'CLPOINT_ID': 38, 'CLPTFLAG': 0,
141     'code': 1, 'type': 'RUINS'})
142    
143     # The transient_table method should return the table itself
144     self.assert_(table is table.transient_table())
145    
146    
147 bh 785 def test_transient_table_read_twice(self):
148     """Test TransientTable.read_record() reading the same record twice"""
149 jan 805 simple = MemoryTable([("type", FIELDTYPE_STRING),
150 bh 785 ("code", FIELDTYPE_INT)],
151     [("OTHER/UNKNOWN", 0),
152     ("RUINS", 1),
153     ("FARM", 2),
154     ("BUILDING", 3),
155     ("HUT", 4),
156     ("LIGHTHOUSE", 5)])
157     table = TransientTable(self.transientdb, simple)
158 bh 765
159 bh 785 # There was a bug where reading the same record twice would
160     # raise an exception in the second call because of an
161     # unitialized local variable, so for passing the test it's
162     # enough if reading simply succeeds. OTOH, while we're at it we
163     # might as well check whether the results are equal anyway :)
164     result1 = table.read_record(3)
165     result2 = table.read_record(3)
166     self.assertEquals(result1, result2)
167    
168 bh 765 if __name__ == "__main__":
169     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