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

Annotation of /branches/WIP-pyshapelib-bramz/test/test_dbf_table.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_dbf_table.py
File MIME type: text/x-python
File size: 5705 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 818 # 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 DBFTable class
10     """
11    
12     __version__ = "$Revision$"
13     # $Source$
14     # $Id$
15    
16    
17     import os
18     import unittest
19    
20     import support
21     support.initthuban()
22    
23     from Thuban.Model.table import DBFTable, MemoryTable, \
24     FIELDTYPE_DOUBLE, FIELDTYPE_INT, FIELDTYPE_STRING
25     import dbflib
26    
27    
28    
29     def table_to_dbf(table, filename):
30     """Create the dbf file filename from the table"""
31     dbf = dbflib.create(filename)
32    
33     dbflib_fieldtypes = {FIELDTYPE_STRING: dbflib.FTString,
34     FIELDTYPE_INT: dbflib.FTInteger,
35     FIELDTYPE_DOUBLE: dbflib.FTDouble}
36    
37     for col in table.Columns():
38     if col.type == FIELDTYPE_DOUBLE:
39     prec = 10
40     else:
41     prec = 0
42     dbf.add_field(col.name, dbflib_fieldtypes[col.type], 20, prec)
43    
44     for i in range(table.NumRows()):
45     record = table.ReadRowAsDict(i)
46     dbf.write_record(i, record)
47     dbf.close()
48    
49    
50    
51     class TestDBFTable(unittest.TestCase, support.FileTestMixin):
52    
53     def setUp(self):
54     """Create a new dbf file. The name is in self.filename"""
55     simple = MemoryTable([("type", FIELDTYPE_STRING),
56     ("value", FIELDTYPE_DOUBLE),
57     ("code", FIELDTYPE_INT)],
58     [("UNKNOWN", 0.0, 0),
59     ("Foo", 0.5, -1),
60     ("Foo", 0.25, 100),
61     ("bar", 1e10, 17)])
62    
63     self.filename = self.temp_file_name("test_dbf_read.dbf")
64     table_to_dbf(simple, self.filename)
65    
66     def test_num_rows(self):
67     """Test DBFTable.NumRows()"""
68     table = DBFTable(self.filename)
69     self.assertEquals(table.NumRows(), 4)
70    
71     def test_num_columns(self):
72     """Test DBFTable.NumColumns()"""
73     table = DBFTable(self.filename)
74     self.assertEquals(table.NumColumns(), 3)
75    
76     def test_columns(self):
77     """Test DBFTable.Columns()"""
78     table = DBFTable(self.filename)
79     columns = table.Columns()
80     self.assertEquals(columns[0].name, "type")
81     self.assertEquals(columns[0].type, FIELDTYPE_STRING)
82     self.assertEquals(columns[1].name, "value")
83     self.assertEquals(columns[1].type, FIELDTYPE_DOUBLE)
84     self.assertEquals(columns[2].name, "code")
85     self.assertEquals(columns[2].type, FIELDTYPE_INT)
86    
87     def test_column(self):
88     """Test DBFTable.Column()"""
89     table = DBFTable(self.filename)
90     # The Column method can be called with either an index or a name
91     col = table.Column(2)
92     self.assertEquals(col.name, "code")
93     self.assertEquals(col.type, FIELDTYPE_INT)
94     col = table.Column("value")
95     self.assertEquals(col.name, "value")
96     self.assertEquals(col.type, FIELDTYPE_DOUBLE)
97    
98     def test_read_row_as_dict(self):
99     """Test DBFTable.ReadRowAsDict()"""
100     table = DBFTable(self.filename)
101     self.assertEquals(table.ReadRowAsDict(1),
102     {"type": "Foo", "value": 0.5, "code": -1})
103    
104     def test_read_value(self):
105     """Test DBFTable.ReadValue()"""
106     table = DBFTable(self.filename)
107     # The column in ReadValue may be given as either name or index
108     self.assertEquals(table.ReadValue(2, 0), "Foo")
109     self.assertEquals(table.ReadValue(3, "code"), 17)
110    
111     def test_value_range(self):
112     """Test DBFTable.ValueRange()"""
113     table = DBFTable(self.filename)
114     self.assertEquals(table.ValueRange("code"), (-1, 100))
115     self.assertEquals(table.ValueRange(1), (0, 1e10))
116    
117     def test_unique_values(self):
118     """Test DBFTable.UniqueValues()"""
119     table = DBFTable(self.filename)
120    
121     # The column can be specified by name or index
122     self.assertEquals(table.UniqueValues("type"),
123     ["Foo", "UNKNOWN", "bar"])
124     self.assertEquals(table.UniqueValues(2), [-1, 0, 17, 100])
125    
126     class TestDBFTableWriting(unittest.TestCase, support.FileTestMixin):
127    
128     def test_write(self):
129     """Test DBFTable.write_record()"""
130     eq = self.assertEquals
131    
132     # First create a DBF file
133     dbffilename = self.temp_file_name("dbfttable_write.dbf")
134     dbf = dbflib.create(dbffilename)
135     dbf.add_field("NAME", dbflib.FTString, 20, 0)
136     dbf.add_field("INT", dbflib.FTInteger, 10, 0)
137     dbf.add_field("FLOAT", dbflib.FTDouble, 10, 4)
138     dbf.write_record(0, {'NAME': "Weatherwax", "INT":1,
139     "FLOAT":3.1415926535})
140     dbf.close()
141    
142     # Create the table
143     table = DBFTable(dbffilename)
144     record = table.ReadRowAsDict(0)
145     # The FLOAT value is different from above because of rounding
146     eq(record, {'NAME': "Weatherwax", "INT":1, "FLOAT":3.1415999999999999})
147    
148     # change only one field
149     table.write_record(0, {"NAME": "Ogg"})
150     # check whether it has been written immediately
151     dbf = dbflib.DBFFile(dbffilename)
152     control = dbf.read_record(0)
153     eq(control, {'NAME': "Ogg", "INT":1, "FLOAT":3.1415999999999999})
154     dbf.close()
155    
156     # check whether the table itself returns the new value
157     eq(table.ReadRowAsDict(0),
158     {'NAME': "Ogg", "INT":1, "FLOAT":3.1415999999999999})
159    
160     # Check whether we can specify the record as a tuple
161     table.write_record(0, ("Garlick", 2, 1.5))
162     eq(table.ReadRowAsDict(0), {"NAME": "Garlick", "INT": 2, "FLOAT": 1.5})
163    
164     table.Destroy()
165    
166    
167     if __name__ == "__main__":
168     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