/[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 984 - (hide annotations)
Thu May 22 16:37:48 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: 6297 byte(s)
Implement a way to discover dependencies between tables and
shapestores.

* Thuban/Model/transientdb.py (TransientTableBase.Dependencies)
(TransientJoinedTable.Dependencies)
(AutoTransientTable.SimpleQuery): New. Implement the dependencies
interface
(TransientJoinedTable.__init__): Keep tack of the original table
objects in addition to the corresponding transient tables.

* Thuban/Model/table.py (DBFTable.Dependencies)
(MemoryTable.Dependencies): New. Implement the dependencies
interface

* Thuban/Model/data.py (ShapeTable): New. Helper class for
ShapefileStore
(ShapefileStore.__init__): Use ShapeTable instead of
AutoTransientTable
(ShapefileStore.Table, ShapefileStore.Shapefile): Add doc-strings
(ShapefileStore.FileName, ShapefileStore.FileType): New. Accessor
methods for filename and type
(ShapefileStore.Dependencies): New. Implement the dependencies
interface
(DerivedShapeStore): New class to replace SimpleStore. The main
difference to SimpleStore is that it depends not on a shapefile
but another shapestore which expresses the dependencies a bit
better
(SimpleStore.__init__): Add deprecation warning.

* test/test_dbf_table.py (TestDBFTable.test_dependencies): New.
Test for the Dependencies method.

* test/test_memory_table.py (TestMemoryTable.test_dependencies):
New. Test for the Dependencies method.

* test/test_transientdb.py
(TestTransientTable.test_auto_transient_table_dependencies): New.
Test for the Dependencies method.
(TestTransientTable.test_transient_joined_table): Add test for the
Dependencies method.

* test/test_session.py (TestSessionSimple.setUp)
(TestSessionSimple.tearDown): New. Implement a better way to
destroy the sessions.
(TestSessionSimple.test_initial_state)
(TestSessionSimple.test_add_table): Bind session to self.session
so that it's destroyed by tearDown
(TestSessionSimple.test_open_shapefile): New. Test for
OpenShapefile and the object it returns

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