Code

e28825c70c3f6a24de0c5b86cb4f748d67d3dcb6
[roundup.git] / test / test_db.py
1 # $Id: test_db.py,v 1.4 2001-07-30 03:45:56 richard Exp $ 
3 import unittest, os, shutil
5 from roundup.hyperdb import String, Link, Multilink, Date, Interval, Class, \
6     DatabaseError
8 def setupSchema(db, create):
9     status = Class(db, "status", name=String())
10     status.setkey("name")
11     if create:
12         status.create(name="unread")
13         status.create(name="in-progress")
14         status.create(name="testing")
15         status.create(name="resolved")
16     Class(db, "user", username=String(), password=String())
17     Class(db, "issue", title=String(), status=Link("status"),
18         nosy=Multilink("user"))
20 #class MyTestResult(unittest._TestResult):
21 #    def addError(self, test, err):
22 #        print `err`
23 #        TestResult.addError(self, test, err)
24 #        if self.showAll:
25 #            self.stream.writeln("ERROR")
26 #        elif self.dots:
27 #            self.stream.write('E')
28 #        if err[0] is KeyboardInterrupt:
29 #            self.shouldStop = 1
31 class MyTestCase(unittest.TestCase):
32 #    def defaultTestResult(self):
33 #        return MyTestResult()
34     def tearDown(self):
35         if self.db is not None:
36             self.db.close()
37             shutil.rmtree('_test_dir')
38     
39 class DBTestCase(MyTestCase):
40     def setUp(self):
41         from roundup.backends import anydbm
42         # remove previous test, ignore errors
43         if os.path.exists('_test_dir'):
44             shutil.rmtree('_test_dir')
45         os.mkdir('_test_dir')
46         self.db = anydbm.Database('_test_dir', 'test')
47         setupSchema(self.db, 1)
49     def testChanges(self):
50         self.db.issue.create(title="spam", status='1')
51         self.db.issue.create(title="eggs", status='2')
52         self.db.issue.create(title="ham", status='4')
53         self.db.issue.create(title="arguments", status='2')
54         self.db.issue.create(title="abuse", status='1')
55         self.db.issue.addprop(fixer=Link("user"))
56         props = self.db.issue.getprops()
57         keys = props.keys()
58         keys.sort()
59         self.assertEqual(keys, ['fixer', 'id', 'nosy', 'status', 'title'])
60         self.db.issue.set('5', status='2')
61         self.db.issue.get('5', "status")
62         self.db.status.get('2', "name")
63         self.db.issue.get('5', "title")
64         self.db.issue.find(status = self.db.status.lookup("in-progress"))
65         self.db.issue.history('5')
66         self.db.status.history('1')
67         self.db.status.history('2')
69     def testExceptions(self):
70         # this tests the exceptions that should be raised
71         ar = self.assertRaises
73         #
74         # class create
75         #
76         # string property
77         ar(TypeError, self.db.status.create, name=1)
78         # invalid property name
79         ar(KeyError, self.db.status.create, foo='foo')
80         # key name clash
81         ar(ValueError, self.db.status.create, name='unread')
82         # invalid link index
83         ar(IndexError, self.db.issue.create, title='foo', status='bar')
84         # invalid link value
85         ar(ValueError, self.db.issue.create, title='foo', status=1)
86         # invalid multilink type
87         ar(TypeError, self.db.issue.create, title='foo', status='1',
88             nosy='hello')
89         # invalid multilink index type
90         ar(ValueError, self.db.issue.create, title='foo', status='1',
91             nosy=[1])
92         # invalid multilink index
93         ar(IndexError, self.db.issue.create, title='foo', status='1',
94             nosy=['10'])
96         #
97         # class get
98         #
99         # invalid node id
100         ar(IndexError, self.db.status.get, '10', 'name')
101         # invalid property name
102         ar(KeyError, self.db.status.get, '2', 'foo')
104         #
105         # class set
106         #
107         # invalid node id
108         ar(IndexError, self.db.issue.set, '1', name='foo')
109         # invalid property name
110         ar(KeyError, self.db.status.set, '1', foo='foo')
111         # string property
112         ar(TypeError, self.db.status.set, '1', name=1)
113         # key name clash
114         ar(ValueError, self.db.status.set, '2', name='unread')
115         # set up a valid issue for me to work on
116         self.db.issue.create(title="spam", status='1')
117         # invalid link index
118         ar(IndexError, self.db.issue.set, '1', title='foo', status='bar')
119         # invalid link value
120         ar(ValueError, self.db.issue.set, '1', title='foo', status=1)
121         # invalid multilink type
122         ar(TypeError, self.db.issue.set, '1', title='foo', status='1',
123             nosy='hello')
124         # invalid multilink index type
125         ar(ValueError, self.db.issue.set, '1', title='foo', status='1',
126             nosy=[1])
127         # invalid multilink index
128         ar(IndexError, self.db.issue.set, '1', title='foo', status='1',
129             nosy=['10'])
131     def testRetire(self):
132         pass
135 class ReadOnlyDBTestCase(MyTestCase):
136     def setUp(self):
137         from roundup.backends import anydbm
138         # remove previous test, ignore errors
139         if os.path.exists('_test_dir'):
140             shutil.rmtree('_test_dir')
141         os.mkdir('_test_dir')
142         db = anydbm.Database('_test_dir', 'test')
143         setupSchema(db, 1)
144         db.close()
145         self.db = anydbm.Database('_test_dir')
146         setupSchema(self.db, 0)
148     def testExceptions(self):
149         # this tests the exceptions that should be raised
150         ar = self.assertRaises
152         # this tests the exceptions that should be raised
153         ar(DatabaseError, self.db.status.create, name="foo")
154         ar(DatabaseError, self.db.status.set, '1', name="foo")
155         ar(DatabaseError, self.db.status.retire, '1')
158 class bsddbDBTestCase(DBTestCase):
159     def setUp(self):
160         from roundup.backends import bsddb
161         # remove previous test, ignore errors
162         if os.path.exists('_test_dir'):
163             shutil.rmtree('_test_dir')
164         os.mkdir('_test_dir')
165         self.db = bsddb.Database('_test_dir', 'test')
166         setupSchema(self.db, 1)
168 class bsddbReadOnlyDBTestCase(ReadOnlyDBTestCase):
169     def setUp(self):
170         from roundup.backends import bsddb
171         # remove previous test, ignore errors
172         if os.path.exists('_test_dir'):
173             shutil.rmtree('_test_dir')
174         os.mkdir('_test_dir')
175         db = bsddb.Database('_test_dir', 'test')
176         setupSchema(db, 1)
177         db.close()
178         self.db = bsddb.Database('_test_dir')
179         setupSchema(self.db, 0)
182 class bsddb3DBTestCase(DBTestCase):
183     def setUp(self):
184         from roundup.backends import bsddb3
185         # remove previous test, ignore errors
186         if os.path.exists('_test_dir'):
187             shutil.rmtree('_test_dir')
188         os.mkdir('_test_dir')
189         self.db = bsddb3.Database('_test_dir', 'test')
190         setupSchema(self.db, 1)
192 class bsddb3ReadOnlyDBTestCase(ReadOnlyDBTestCase):
193     def setUp(self):
194         from roundup.backends import bsddb3
195         # remove previous test, ignore errors
196         if os.path.exists('_test_dir'):
197             shutil.rmtree('_test_dir')
198         os.mkdir('_test_dir')
199         db = bsddb3.Database('_test_dir', 'test')
200         setupSchema(db, 1)
201         db.close()
202         self.db = bsddb3.Database('_test_dir')
203         setupSchema(self.db, 0)
206 def suite():
207     l = [unittest.makeSuite(DBTestCase, 'test'),
208          unittest.makeSuite(ReadOnlyDBTestCase, 'test')]
210     try:
211         import bsddb
212         l.append(unittest.makeSuite(bsddbDBTestCase, 'test'))
213         l.append(unittest.makeSuite(bsddbReadOnlyDBTestCase, 'test'))
214     except:
215         print 'bsddb module not found, skipping bsddb DBTestCase'
217     try:
218         import bsddb3
219         l.append(unittest.makeSuite(bsddb3DBTestCase, 'test'))
220         l.append(unittest.makeSuite(bsddb3ReadOnlyDBTestCase, 'test'))
221     except:
222         print 'bsddb3 module not found, skipping bsddb3 DBTestCase'
224     return unittest.TestSuite(l)
227 # $Log: not supported by cvs2svn $
228 # Revision 1.3  2001/07/29 07:01:39  richard
229 # Added vim command to all source so that we don't get no steenkin' tabs :)
231 # Revision 1.2  2001/07/29 04:09:20  richard
232 # Added the fabricated property "id" to all hyperdb classes.
234 # Revision 1.1  2001/07/27 06:55:07  richard
235 # moving tests -> test
237 # Revision 1.7  2001/07/27 06:26:43  richard
238 # oops - wasn't deleting the test dir after the read-only tests
240 # Revision 1.6  2001/07/27 06:23:59  richard
241 # consistency
243 # Revision 1.5  2001/07/27 06:23:09  richard
244 # Added some new hyperdb tests to make sure we raise the right exceptions.
246 # Revision 1.4  2001/07/25 04:34:31  richard
247 # Added id and log to tests files...
250 # vim: set filetype=python ts=4 sw=4 et si