Code

. lots of date/interval related changes:
[roundup.git] / test / test_db.py
1 #
2 # Copyright (c) 2001 Bizar Software Pty Ltd (http://www.bizarsoftware.com.au/)
3 # This module is free software, and you may redistribute it and/or modify
4 # under the same terms as Python, so long as this copyright message and
5 # disclaimer are retained in their original form.
6 #
7 # IN NO EVENT SHALL BIZAR SOFTWARE PTY LTD BE LIABLE TO ANY PARTY FOR
8 # DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING
9 # OUT OF THE USE OF THIS CODE, EVEN IF THE AUTHOR HAS BEEN ADVISED OF THE
10 # POSSIBILITY OF SUCH DAMAGE.
11 #
12 # BIZAR SOFTWARE PTY LTD SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
13 # BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
14 # FOR A PARTICULAR PURPOSE.  THE CODE PROVIDED HEREUNDER IS ON AN "AS IS"
15 # BASIS, AND THERE IS NO OBLIGATION WHATSOEVER TO PROVIDE MAINTENANCE,
16 # SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
17
18 # $Id: test_db.py,v 1.14 2002-01-16 07:02:57 richard Exp $ 
20 import unittest, os, shutil
22 from roundup.hyperdb import String, Password, Link, Multilink, Date, \
23     Interval, Class, DatabaseError
24 from roundup.roundupdb import FileClass
25 from roundup import date
27 def setupSchema(db, create):
28     status = Class(db, "status", name=String())
29     status.setkey("name")
30     if create:
31         status.create(name="unread")
32         status.create(name="in-progress")
33         status.create(name="testing")
34         status.create(name="resolved")
35     Class(db, "user", username=String(), password=Password())
36     Class(db, "issue", title=String(), status=Link("status"),
37         nosy=Multilink("user"), deadline=Date(), foo=Interval())
38     FileClass(db, "file", name=String(), type=String())
39     db.commit()
41 class MyTestCase(unittest.TestCase):
42     def tearDown(self):
43         if os.path.exists('_test_dir'):
44             shutil.rmtree('_test_dir')
46 class config:
47     DATABASE='_test_dir'
48     MAILHOST = 'localhost'
49     MAIL_DOMAIN = 'fill.me.in.'
50     INSTANCE_NAME = 'Roundup issue tracker'
51     ISSUE_TRACKER_EMAIL = 'issue_tracker@%s'%MAIL_DOMAIN
52     ISSUE_TRACKER_WEB = 'http://some.useful.url/'
53     ADMIN_EMAIL = 'roundup-admin@%s'%MAIL_DOMAIN
54     FILTER_POSITION = 'bottom'      # one of 'top', 'bottom', 'top and bottom'
55     ANONYMOUS_ACCESS = 'deny'       # either 'deny' or 'allow'
56     ANONYMOUS_REGISTER = 'deny'     # either 'deny' or 'allow'
57     MESSAGES_TO_AUTHOR = 'no'       # either 'yes' or 'no'
58     EMAIL_SIGNATURE_POSITION = 'bottom'
60 class anydbmDBTestCase(MyTestCase):
61     def setUp(self):
62         from roundup.backends import anydbm
63         # remove previous test, ignore errors
64         if os.path.exists(config.DATABASE):
65             shutil.rmtree(config.DATABASE)
66         os.makedirs(config.DATABASE + '/files')
67         self.db = anydbm.Database(config, 'test')
68         setupSchema(self.db, 1)
70     def testChanges(self):
71         self.db.issue.create(title="spam", status='1')
72         self.db.issue.create(title="eggs", status='2')
73         self.db.issue.create(title="ham", status='4')
74         self.db.issue.create(title="arguments", status='2')
75         self.db.issue.create(title="abuse", status='1')
76         self.db.issue.addprop(fixer=Link("user"))
77         props = self.db.issue.getprops()
78         keys = props.keys()
79         keys.sort()
80         self.assertEqual(keys, ['deadline', 'fixer', 'foo', 'id', 'nosy',
81             'status', 'title'])
82         self.db.issue.set('5', status='2')
83         self.db.issue.get('5', "status")
85         a = self.db.issue.get('5', "deadline")
86         self.db.issue.set('5', deadline=date.Date())
87         self.assertNotEqual(a, self.db.issue.get('5', "deadline"))
89         a = self.db.issue.get('5', "foo")
90         self.db.issue.set('5', foo=date.Interval('-1d'))
91         self.assertNotEqual(a, self.db.issue.get('5', "foo"))
93         self.db.status.get('2', "name")
94         self.db.issue.get('5', "title")
95         self.db.issue.find(status = self.db.status.lookup("in-progress"))
96         self.db.commit()
97         self.db.issue.history('5')
98         self.db.status.history('1')
99         self.db.status.history('2')
101     def testTransactions(self):
102         num_issues = len(self.db.issue.list())
103         files_dir = os.path.join('_test_dir', 'files')
104         if os.path.exists(files_dir):
105             num_files = len(os.listdir(files_dir))
106         else:
107             num_files = 0
108         self.db.issue.create(title="don't commit me!", status='1')
109         self.assertNotEqual(num_issues, len(self.db.issue.list()))
110         self.db.rollback()
111         self.assertEqual(num_issues, len(self.db.issue.list()))
112         self.db.issue.create(title="please commit me!", status='1')
113         self.assertNotEqual(num_issues, len(self.db.issue.list()))
114         self.db.commit()
115         self.assertNotEqual(num_issues, len(self.db.issue.list()))
116         self.db.rollback()
117         self.assertNotEqual(num_issues, len(self.db.issue.list()))
118         self.db.file.create(name="test", type="text/plain", content="hi")
119         self.db.rollback()
120         self.assertEqual(num_files, len(os.listdir(files_dir)))
121         self.db.file.create(name="test", type="text/plain", content="hi")
122         self.db.commit()
123         self.assertNotEqual(num_files, len(os.listdir(files_dir)))
124         num_files2 = len(os.listdir(files_dir))
125         self.db.file.create(name="test", type="text/plain", content="hi")
126         self.db.rollback()
127         self.assertNotEqual(num_files, len(os.listdir(files_dir)))
128         self.assertEqual(num_files2, len(os.listdir(files_dir)))
131     def testExceptions(self):
132         # this tests the exceptions that should be raised
133         ar = self.assertRaises
135         #
136         # class create
137         #
138         # string property
139         ar(TypeError, self.db.status.create, name=1)
140         # invalid property name
141         ar(KeyError, self.db.status.create, foo='foo')
142         # key name clash
143         ar(ValueError, self.db.status.create, name='unread')
144         # invalid link index
145         ar(IndexError, self.db.issue.create, title='foo', status='bar')
146         # invalid link value
147         ar(ValueError, self.db.issue.create, title='foo', status=1)
148         # invalid multilink type
149         ar(TypeError, self.db.issue.create, title='foo', status='1',
150             nosy='hello')
151         # invalid multilink index type
152         ar(ValueError, self.db.issue.create, title='foo', status='1',
153             nosy=[1])
154         # invalid multilink index
155         ar(IndexError, self.db.issue.create, title='foo', status='1',
156             nosy=['10'])
158         #
159         # class get
160         #
161         # invalid node id
162         ar(IndexError, self.db.status.get, '10', 'name')
163         # invalid property name
164         ar(KeyError, self.db.status.get, '2', 'foo')
166         #
167         # class set
168         #
169         # invalid node id
170         ar(IndexError, self.db.issue.set, '1', name='foo')
171         # invalid property name
172         ar(KeyError, self.db.status.set, '1', foo='foo')
173         # string property
174         ar(TypeError, self.db.status.set, '1', name=1)
175         # key name clash
176         ar(ValueError, self.db.status.set, '2', name='unread')
177         # set up a valid issue for me to work on
178         self.db.issue.create(title="spam", status='1')
179         # invalid link index
180         ar(IndexError, self.db.issue.set, '1', title='foo', status='bar')
181         # invalid link value
182         ar(ValueError, self.db.issue.set, '1', title='foo', status=1)
183         # invalid multilink type
184         ar(TypeError, self.db.issue.set, '1', title='foo', status='1',
185             nosy='hello')
186         # invalid multilink index type
187         ar(ValueError, self.db.issue.set, '1', title='foo', status='1',
188             nosy=[1])
189         # invalid multilink index
190         ar(IndexError, self.db.issue.set, '1', title='foo', status='1',
191             nosy=['10'])
193     def testRetire(self):
194         pass
197 class anydbmReadOnlyDBTestCase(MyTestCase):
198     def setUp(self):
199         from roundup.backends import anydbm
200         # remove previous test, ignore errors
201         if os.path.exists(config.DATABASE):
202             shutil.rmtree(config.DATABASE)
203         os.makedirs(config.DATABASE + '/files')
204         db = anydbm.Database(config, 'test')
205         setupSchema(db, 1)
206         self.db = anydbm.Database(config)
207         setupSchema(self.db, 0)
209     def testExceptions(self):
210         # this tests the exceptions that should be raised
211         ar = self.assertRaises
213         # this tests the exceptions that should be raised
214         ar(DatabaseError, self.db.status.create, name="foo")
215         ar(DatabaseError, self.db.status.set, '1', name="foo")
216         ar(DatabaseError, self.db.status.retire, '1')
219 class bsddbDBTestCase(anydbmDBTestCase):
220     def setUp(self):
221         from roundup.backends import bsddb
222         # remove previous test, ignore errors
223         if os.path.exists(config.DATABASE):
224             shutil.rmtree(config.DATABASE)
225         os.makedirs(config.DATABASE + '/files')
226         self.db = bsddb.Database(config, 'test')
227         setupSchema(self.db, 1)
229 class bsddbReadOnlyDBTestCase(anydbmReadOnlyDBTestCase):
230     def setUp(self):
231         from roundup.backends import bsddb
232         # remove previous test, ignore errors
233         if os.path.exists(config.DATABASE):
234             shutil.rmtree(config.DATABASE)
235         os.makedirs(config.DATABASE + '/files')
236         db = bsddb.Database(config, 'test')
237         setupSchema(db, 1)
238         self.db = bsddb.Database(config)
239         setupSchema(self.db, 0)
242 class bsddb3DBTestCase(anydbmDBTestCase):
243     def setUp(self):
244         from roundup.backends import bsddb3
245         # remove previous test, ignore errors
246         if os.path.exists(config.DATABASE):
247             shutil.rmtree(config.DATABASE)
248         os.makedirs(config.DATABASE + '/files')
249         self.db = bsddb3.Database(config, 'test')
250         setupSchema(self.db, 1)
252 class bsddb3ReadOnlyDBTestCase(anydbmReadOnlyDBTestCase):
253     def setUp(self):
254         from roundup.backends import bsddb3
255         # remove previous test, ignore errors
256         if os.path.exists(config.DATABASE):
257             shutil.rmtree(config.DATABASE)
258         os.makedirs(config.DATABASE + '/files')
259         db = bsddb3.Database(config, 'test')
260         setupSchema(db, 1)
261         self.db = bsddb3.Database(config)
262         setupSchema(self.db, 0)
265 def suite():
266     l = [unittest.makeSuite(anydbmDBTestCase, 'test'),
267          unittest.makeSuite(anydbmReadOnlyDBTestCase, 'test')
268     ]
270     try:
271         import bsddb
272         l.append(unittest.makeSuite(bsddbDBTestCase, 'test'))
273         l.append(unittest.makeSuite(bsddbReadOnlyDBTestCase, 'test'))
274     except:
275         print 'bsddb module not found, skipping bsddb DBTestCase'
277 #    try:
278 #        import bsddb3
279 #        l.append(unittest.makeSuite(bsddb3DBTestCase, 'test'))
280 #        l.append(unittest.makeSuite(bsddb3ReadOnlyDBTestCase, 'test'))
281 #    except:
282 #        print 'bsddb3 module not found, skipping bsddb3 DBTestCase'
284     return unittest.TestSuite(l)
287 # $Log: not supported by cvs2svn $
288 # Revision 1.13  2002/01/14 02:20:15  richard
289 #  . changed all config accesses so they access either the instance or the
290 #    config attriubute on the db. This means that all config is obtained from
291 #    instance_config instead of the mish-mash of classes. This will make
292 #    switching to a ConfigParser setup easier too, I hope.
294 # At a minimum, this makes migration a _little_ easier (a lot easier in the
295 # 0.5.0 switch, I hope!)
297 # Revision 1.12  2001/12/17 03:52:48  richard
298 # Implemented file store rollback. As a bonus, the hyperdb is now capable of
299 # storing more than one file per node - if a property name is supplied,
300 # the file is called designator.property.
301 # I decided not to migrate the existing files stored over to the new naming
302 # scheme - the FileClass just doesn't specify the property name.
304 # Revision 1.11  2001/12/10 23:17:20  richard
305 # Added transaction tests to test_db
307 # Revision 1.10  2001/12/03 21:33:39  richard
308 # Fixes so the tests use commit and not close
310 # Revision 1.9  2001/12/02 05:06:16  richard
311 # . We now use weakrefs in the Classes to keep the database reference, so
312 #   the close() method on the database is no longer needed.
313 #   I bumped the minimum python requirement up to 2.1 accordingly.
314 # . #487480 ] roundup-server
315 # . #487476 ] INSTALL.txt
317 # I also cleaned up the change message / post-edit stuff in the cgi client.
318 # There's now a clearly marked "TODO: append the change note" where I believe
319 # the change note should be added there. The "changes" list will obviously
320 # have to be modified to be a dict of the changes, or somesuch.
322 # More testing needed.
324 # Revision 1.8  2001/10/09 07:25:59  richard
325 # Added the Password property type. See "pydoc roundup.password" for
326 # implementation details. Have updated some of the documentation too.
328 # Revision 1.7  2001/08/29 06:23:59  richard
329 # Disabled the bsddb3 module entirely in the unit testing. See CHANGES for
330 # details.
332 # Revision 1.6  2001/08/07 00:24:43  richard
333 # stupid typo
335 # Revision 1.5  2001/08/07 00:15:51  richard
336 # Added the copyright/license notice to (nearly) all files at request of
337 # Bizar Software.
339 # Revision 1.4  2001/07/30 03:45:56  richard
340 # Added more DB to test_db. Can skip tests where imports fail.
342 # Revision 1.3  2001/07/29 07:01:39  richard
343 # Added vim command to all source so that we don't get no steenkin' tabs :)
345 # Revision 1.2  2001/07/29 04:09:20  richard
346 # Added the fabricated property "id" to all hyperdb classes.
348 # Revision 1.1  2001/07/27 06:55:07  richard
349 # moving tests -> test
351 # Revision 1.7  2001/07/27 06:26:43  richard
352 # oops - wasn't deleting the test dir after the read-only tests
354 # Revision 1.6  2001/07/27 06:23:59  richard
355 # consistency
357 # Revision 1.5  2001/07/27 06:23:09  richard
358 # Added some new hyperdb tests to make sure we raise the right exceptions.
360 # Revision 1.4  2001/07/25 04:34:31  richard
361 # Added id and log to tests files...
364 # vim: set filetype=python ts=4 sw=4 et si