/[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 839 - (hide annotations)
Tue May 6 15:54:18 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: 6077 byte(s)
Convert all table users to use the new table interface. This only
covers Thuban itself, not GREAT-ER or other applications built on
Thuban yet, so the compatibility interface stays in place for the
time being but it now issues DeprecationWarnings.

Finally, the new Table interface has a new method, HasColumn.

* Thuban/Model/table.py (OldTableInterfaceMixin): All methods
issue deprecation warnings when they're. The warnings refer to the
caller of the method.
(OldTableInterfaceMixin.__deprecation_warning): New. Helper method
for the deprecation warnings

* test/test_table.py: Ignore the deprecation warnings for the old
table in the tests in this module. The purpose of the tests is to
test the old interface, after all.

* test/test_transientdb.py
(TestTransientTable.run_iceland_political_tests): Use the
constants for the types. Add a test for HasColumn
(TestTransientTable.test_transient_joined_table): Adapt to new
table interface. Add a test for HasColumn
(TestTransientTable.test_transient_table_read_twice): Adapt to new
table interface

* Thuban/Model/transientdb.py (TransientTableBase.HasColumn)
(AutoTransientTable.HasColumn): Implement the new table interface
method
(AutoTransientTable.ReadRowAsDict, AutoTransientTable.ValueRange)
(AutoTransientTable.UniqueValues): Adapt to new table interface

* Thuban/Model/layer.py (Layer.SetShapeStore, Layer.GetFieldType):
Adapt to new table interface

* test/test_layer.py (TestLayer.open_shapefile): Helper method to
simplify opening shapefiles a bit easier.
(TestLayer.test_arc_layer, TestLayer.test_polygon_layer)
(TestLayer.test_point_layer): Use the new helper method
(TestLayer.test_get_field_type): New. Test for the GetFieldType
method

* test/test_dbf_table.py (TestDBFTable.test_has_column): Test for
the new table method

* test/test_memory_table.py (TestMemoryTable.test_has_column):
Test for the new table method HasColumn

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     class TestDBFTableWriting(unittest.TestCase, support.FileTestMixin):
137    
138     def test_write(self):
139     """Test DBFTable.write_record()"""
140     eq = self.assertEquals
141    
142     # First create a DBF file
143     dbffilename = self.temp_file_name("dbfttable_write.dbf")
144     dbf = dbflib.create(dbffilename)
145     dbf.add_field("NAME", dbflib.FTString, 20, 0)
146     dbf.add_field("INT", dbflib.FTInteger, 10, 0)
147     dbf.add_field("FLOAT", dbflib.FTDouble, 10, 4)
148     dbf.write_record(0, {'NAME': "Weatherwax", "INT":1,
149     "FLOAT":3.1415926535})
150     dbf.close()
151    
152     # Create the table
153     table = DBFTable(dbffilename)
154     record = table.ReadRowAsDict(0)
155     # The FLOAT value is different from above because of rounding
156     eq(record, {'NAME': "Weatherwax", "INT":1, "FLOAT":3.1415999999999999})
157    
158     # change only one field
159     table.write_record(0, {"NAME": "Ogg"})
160     # check whether it has been written immediately
161     dbf = dbflib.DBFFile(dbffilename)
162     control = dbf.read_record(0)
163     eq(control, {'NAME': "Ogg", "INT":1, "FLOAT":3.1415999999999999})
164     dbf.close()
165    
166     # check whether the table itself returns the new value
167     eq(table.ReadRowAsDict(0),
168     {'NAME': "Ogg", "INT":1, "FLOAT":3.1415999999999999})
169    
170     # Check whether we can specify the record as a tuple
171     table.write_record(0, ("Garlick", 2, 1.5))
172     eq(table.ReadRowAsDict(0), {"NAME": "Garlick", "INT": 2, "FLOAT": 1.5})
173    
174     table.Destroy()
175    
176    
177     if __name__ == "__main__":
178     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