/[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 998 - (hide annotations)
Thu May 22 19:29:39 2003 UTC (21 years, 9 months ago) by bh
Original Path: trunk/thuban/test/test_dbf_table.py
File MIME type: text/x-python
File size: 6724 byte(s)
Give the tables titles so that the GUI can display more meaningful
names. For now the titles are fixed but depend on e.g. filenames
or the titles of the joined tables.

* Thuban/Model/transientdb.py (TransientTable.Title)
(TransientJoinedTable.Title, AutoTransientTable.Title): New.

* Thuban/Model/table.py (DBFTable.Title, MemoryTable.Title): New.

* test/test_transientdb.py
(TestTransientTable.test_auto_transient_table_title): New. Test
for the Title method
(TestTransientTable.test_transient_joined_table)
(TestTransientTable.test_transient_table): Add test for the Title
methods

* test/test_memory_table.py (TestMemoryTable.test_title): New.
Test for the Title method

* test/test_dbf_table.py (TestDBFTable.test_title): New. Test for
the Title method

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 bh 839 def test_has_column(self):
99     """Test DBFTable.HasColumn()"""
100     table = DBFTable(self.filename)
101     # HasColumn
102     self.failUnless(table.HasColumn("value"))
103     self.failUnless(table.HasColumn(2))
104     # HasColumn for non-exisiting columns
105     self.failIf(table.HasColumn("non_existing_name"))
106     self.failIf(table.HasColumn(100))
107    
108 bh 818 def test_read_row_as_dict(self):
109     """Test DBFTable.ReadRowAsDict()"""
110     table = DBFTable(self.filename)
111     self.assertEquals(table.ReadRowAsDict(1),
112     {"type": "Foo", "value": 0.5, "code": -1})
113    
114     def test_read_value(self):
115     """Test DBFTable.ReadValue()"""
116     table = DBFTable(self.filename)
117     # The column in ReadValue may be given as either name or index
118     self.assertEquals(table.ReadValue(2, 0), "Foo")
119     self.assertEquals(table.ReadValue(3, "code"), 17)
120    
121     def test_value_range(self):
122     """Test DBFTable.ValueRange()"""
123     table = DBFTable(self.filename)
124     self.assertEquals(table.ValueRange("code"), (-1, 100))
125     self.assertEquals(table.ValueRange(1), (0, 1e10))
126    
127     def test_unique_values(self):
128     """Test DBFTable.UniqueValues()"""
129     table = DBFTable(self.filename)
130    
131     # The column can be specified by name or index
132     self.assertEquals(table.UniqueValues("type"),
133     ["Foo", "UNKNOWN", "bar"])
134     self.assertEquals(table.UniqueValues(2), [-1, 0, 17, 100])
135    
136 bh 984 def test_dependencies(self):
137     """Test DBFTable.Dependencies()"""
138     # A DBFTable doesn't have dependencies
139     table = DBFTable(self.filename)
140     self.assertEquals(len(table.Dependencies()), 0)
141    
142 bh 994 def test_filename(self):
143     """Test DBFTable.FileName()"""
144     # A DBFTable doesn't have dependencies
145     table = DBFTable(self.filename)
146     self.assertEquals(table.FileName(), self.filename)
147    
148 bh 998 def test_title(self):
149     """Test DBFTable.Title()"""
150     # A DBFTable doesn't have dependencies
151     table = DBFTable(self.filename)
152     self.assertEquals(table.Title(), "test_dbf_read.dbf")
153 bh 994
154 bh 998
155 bh 818 class TestDBFTableWriting(unittest.TestCase, support.FileTestMixin):
156    
157     def test_write(self):
158     """Test DBFTable.write_record()"""
159     eq = self.assertEquals
160    
161     # First create a DBF file
162 bh 994 dbffilename = self.temp_file_name("dbftable_write.dbf")
163 bh 818 dbf = dbflib.create(dbffilename)
164     dbf.add_field("NAME", dbflib.FTString, 20, 0)
165     dbf.add_field("INT", dbflib.FTInteger, 10, 0)
166     dbf.add_field("FLOAT", dbflib.FTDouble, 10, 4)
167     dbf.write_record(0, {'NAME': "Weatherwax", "INT":1,
168     "FLOAT":3.1415926535})
169     dbf.close()
170    
171     # Create the table
172     table = DBFTable(dbffilename)
173     record = table.ReadRowAsDict(0)
174     # The FLOAT value is different from above because of rounding
175     eq(record, {'NAME': "Weatherwax", "INT":1, "FLOAT":3.1415999999999999})
176    
177     # change only one field
178     table.write_record(0, {"NAME": "Ogg"})
179     # check whether it has been written immediately
180     dbf = dbflib.DBFFile(dbffilename)
181     control = dbf.read_record(0)
182     eq(control, {'NAME': "Ogg", "INT":1, "FLOAT":3.1415999999999999})
183     dbf.close()
184    
185     # check whether the table itself returns the new value
186     eq(table.ReadRowAsDict(0),
187     {'NAME': "Ogg", "INT":1, "FLOAT":3.1415999999999999})
188    
189     # Check whether we can specify the record as a tuple
190     table.write_record(0, ("Garlick", 2, 1.5))
191     eq(table.ReadRowAsDict(0), {"NAME": "Garlick", "INT": 2, "FLOAT": 1.5})
192    
193     table.Destroy()
194    
195    
196     if __name__ == "__main__":
197     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