Mercurial > p > roundup > code
comparison test/test_db.py @ 704:54333751e98d search_indexing-0-4-2-branch
Brought search_indexing-branch up to date with latest changes in HEAD.
| author | Roche Compaan <rochecompaan@users.sourceforge.net> |
|---|---|
| date | Thu, 02 May 2002 13:09:11 +0000 |
| parents | 07abfe8f0c01 |
| children |
comparison
equal
deleted
inserted
replaced
| 703:8d2cb0d09da4 | 704:54333751e98d |
|---|---|
| 13 # BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS | 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" | 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, | 15 # BASIS, AND THERE IS NO OBLIGATION WHATSOEVER TO PROVIDE MAINTENANCE, |
| 16 # SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. | 16 # SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. |
| 17 # | 17 # |
| 18 # $Id: test_db.py,v 1.19 2002-02-25 14:34:31 grubert Exp $ | 18 # $Id: test_db.py,v 1.19.2.1 2002-05-02 13:09:10 rochecompaan Exp $ |
| 19 | 19 |
| 20 import unittest, os, shutil | 20 import unittest, os, shutil |
| 21 | 21 |
| 22 from roundup.hyperdb import String, Password, Link, Multilink, Date, \ | 22 from roundup.hyperdb import String, Password, Link, Multilink, Date, \ |
| 23 Interval, Class, DatabaseError | 23 Interval, Class, DatabaseError |
| 24 from roundup.roundupdb import FileClass | 24 from roundup.roundupdb import FileClass |
| 25 from roundup import date | 25 from roundup import date, password |
| 26 | 26 |
| 27 def setupSchema(db, create): | 27 def setupSchema(db, create): |
| 28 status = Class(db, "status", name=String()) | 28 status = Class(db, "status", name=String()) |
| 29 status.setkey("name") | 29 status.setkey("name") |
| 30 if create: | 30 if create: |
| 64 if os.path.exists(config.DATABASE): | 64 if os.path.exists(config.DATABASE): |
| 65 shutil.rmtree(config.DATABASE) | 65 shutil.rmtree(config.DATABASE) |
| 66 os.makedirs(config.DATABASE + '/files') | 66 os.makedirs(config.DATABASE + '/files') |
| 67 self.db = anydbm.Database(config, 'test') | 67 self.db = anydbm.Database(config, 'test') |
| 68 setupSchema(self.db, 1) | 68 setupSchema(self.db, 1) |
| 69 self.db2 = anydbm.Database(config, 'test') | |
| 70 setupSchema(self.db2, 0) | |
| 69 | 71 |
| 70 def testChanges(self): | 72 def testChanges(self): |
| 71 self.db.issue.create(title="spam", status='1') | 73 self.db.issue.create(title="spam", status='1') |
| 72 self.db.issue.create(title="eggs", status='2') | 74 self.db.issue.create(title="eggs", status='2') |
| 73 self.db.issue.create(title="ham", status='4') | 75 self.db.issue.create(title="ham", status='4') |
| 82 self.db.issue.set('5', status='2') | 84 self.db.issue.set('5', status='2') |
| 83 self.db.issue.get('5', "status") | 85 self.db.issue.get('5', "status") |
| 84 | 86 |
| 85 a = self.db.issue.get('5', "deadline") | 87 a = self.db.issue.get('5', "deadline") |
| 86 self.db.issue.set('5', deadline=date.Date()) | 88 self.db.issue.set('5', deadline=date.Date()) |
| 87 self.assertNotEqual(a, self.db.issue.get('5', "deadline")) | 89 b = self.db.issue.get('5', "deadline") |
| 90 self.db.commit() | |
| 91 self.assertNotEqual(a, b) | |
| 92 self.assertNotEqual(b, date.Date('1970-1-1 00:00:00')) | |
| 93 self.db.issue.set('5', deadline=date.Date()) | |
| 88 | 94 |
| 89 a = self.db.issue.get('5', "foo") | 95 a = self.db.issue.get('5', "foo") |
| 90 self.db.issue.set('5', foo=date.Interval('-1d')) | 96 self.db.issue.set('5', foo=date.Interval('-1d')) |
| 91 self.assertNotEqual(a, self.db.issue.get('5', "foo")) | 97 self.assertNotEqual(a, self.db.issue.get('5', "foo")) |
| 92 | 98 |
| 95 self.db.issue.find(status = self.db.status.lookup("in-progress")) | 101 self.db.issue.find(status = self.db.status.lookup("in-progress")) |
| 96 self.db.commit() | 102 self.db.commit() |
| 97 self.db.issue.history('5') | 103 self.db.issue.history('5') |
| 98 self.db.status.history('1') | 104 self.db.status.history('1') |
| 99 self.db.status.history('2') | 105 self.db.status.history('2') |
| 106 | |
| 107 def testSerialisation(self): | |
| 108 self.db.issue.create(title="spam", status='1', | |
| 109 deadline=date.Date(), foo=date.Interval('-1d')) | |
| 110 self.db.commit() | |
| 111 assert isinstance(self.db.issue.get('1', 'deadline'), date.Date) | |
| 112 assert isinstance(self.db.issue.get('1', 'foo'), date.Interval) | |
| 113 self.db.user.create(username="fozzy", | |
| 114 password=password.Password('t. bear')) | |
| 115 self.db.commit() | |
| 116 assert isinstance(self.db.user.get('1', 'password'), password.Password) | |
| 100 | 117 |
| 101 def testTransactions(self): | 118 def testTransactions(self): |
| 102 # remember the number of items we started | 119 # remember the number of items we started |
| 103 num_issues = len(self.db.issue.list()) | 120 num_issues = len(self.db.issue.list()) |
| 104 num_files = self.db.numfiles() | 121 num_files = self.db.numfiles() |
| 123 self.assertNotEqual(num_files, num_files2) | 140 self.assertNotEqual(num_files, num_files2) |
| 124 self.db.file.create(name="test", type="text/plain", content="hi") | 141 self.db.file.create(name="test", type="text/plain", content="hi") |
| 125 self.db.rollback() | 142 self.db.rollback() |
| 126 self.assertNotEqual(num_files, self.db.numfiles()) | 143 self.assertNotEqual(num_files, self.db.numfiles()) |
| 127 self.assertEqual(num_files2, self.db.numfiles()) | 144 self.assertEqual(num_files2, self.db.numfiles()) |
| 128 | |
| 129 | |
| 130 | 145 |
| 131 def testExceptions(self): | 146 def testExceptions(self): |
| 132 # this tests the exceptions that should be raised | 147 # this tests the exceptions that should be raised |
| 133 ar = self.assertRaises | 148 ar = self.assertRaises |
| 134 | 149 |
| 175 # key name clash | 190 # key name clash |
| 176 ar(ValueError, self.db.status.set, '2', name='unread') | 191 ar(ValueError, self.db.status.set, '2', name='unread') |
| 177 # set up a valid issue for me to work on | 192 # set up a valid issue for me to work on |
| 178 self.db.issue.create(title="spam", status='1') | 193 self.db.issue.create(title="spam", status='1') |
| 179 # invalid link index | 194 # invalid link index |
| 180 ar(IndexError, self.db.issue.set, '1', title='foo', status='bar') | 195 ar(IndexError, self.db.issue.set, '6', title='foo', status='bar') |
| 181 # invalid link value | 196 # invalid link value |
| 182 ar(ValueError, self.db.issue.set, '1', title='foo', status=1) | 197 ar(ValueError, self.db.issue.set, '6', title='foo', status=1) |
| 183 # invalid multilink type | 198 # invalid multilink type |
| 184 ar(TypeError, self.db.issue.set, '1', title='foo', status='1', | 199 ar(TypeError, self.db.issue.set, '6', title='foo', status='1', |
| 185 nosy='hello') | 200 nosy='hello') |
| 186 # invalid multilink index type | 201 # invalid multilink index type |
| 187 ar(ValueError, self.db.issue.set, '1', title='foo', status='1', | 202 ar(ValueError, self.db.issue.set, '6', title='foo', status='1', |
| 188 nosy=[1]) | 203 nosy=[1]) |
| 189 # invalid multilink index | 204 # invalid multilink index |
| 190 ar(IndexError, self.db.issue.set, '1', title='foo', status='1', | 205 ar(IndexError, self.db.issue.set, '6', title='foo', status='1', |
| 191 nosy=['10']) | 206 nosy=['10']) |
| 192 | 207 |
| 193 def testJournals(self): | 208 def testJournals(self): |
| 194 self.db.issue.addprop(fixer=Link("user", do_journal='yes')) | 209 self.db.issue.addprop(fixer=Link("user", do_journal='yes')) |
| 195 self.db.user.create(username="mary") | 210 self.db.user.create(username="mary") |
| 252 self.assertEqual(2, len(journal)) | 267 self.assertEqual(2, len(journal)) |
| 253 | 268 |
| 254 def testRetire(self): | 269 def testRetire(self): |
| 255 pass | 270 pass |
| 256 | 271 |
| 272 def testIDGeneration(self): | |
| 273 id1 = self.db.issue.create(title="spam", status='1') | |
| 274 id2 = self.db2.issue.create(title="eggs", status='2') | |
| 275 self.assertNotEqual(id1, id2) | |
| 276 | |
| 257 | 277 |
| 258 class anydbmReadOnlyDBTestCase(MyTestCase): | 278 class anydbmReadOnlyDBTestCase(MyTestCase): |
| 259 def setUp(self): | 279 def setUp(self): |
| 260 from roundup.backends import anydbm | 280 from roundup.backends import anydbm |
| 261 # remove previous test, ignore errors | 281 # remove previous test, ignore errors |
| 264 os.makedirs(config.DATABASE + '/files') | 284 os.makedirs(config.DATABASE + '/files') |
| 265 db = anydbm.Database(config, 'test') | 285 db = anydbm.Database(config, 'test') |
| 266 setupSchema(db, 1) | 286 setupSchema(db, 1) |
| 267 self.db = anydbm.Database(config) | 287 self.db = anydbm.Database(config) |
| 268 setupSchema(self.db, 0) | 288 setupSchema(self.db, 0) |
| 289 self.db2 = anydbm.Database(config, 'test') | |
| 290 setupSchema(self.db2, 0) | |
| 269 | 291 |
| 270 def testExceptions(self): | 292 def testExceptions(self): |
| 271 # this tests the exceptions that should be raised | 293 # this tests the exceptions that should be raised |
| 272 ar = self.assertRaises | 294 ar = self.assertRaises |
| 273 | 295 |
| 284 if os.path.exists(config.DATABASE): | 306 if os.path.exists(config.DATABASE): |
| 285 shutil.rmtree(config.DATABASE) | 307 shutil.rmtree(config.DATABASE) |
| 286 os.makedirs(config.DATABASE + '/files') | 308 os.makedirs(config.DATABASE + '/files') |
| 287 self.db = bsddb.Database(config, 'test') | 309 self.db = bsddb.Database(config, 'test') |
| 288 setupSchema(self.db, 1) | 310 setupSchema(self.db, 1) |
| 311 self.db2 = bsddb.Database(config, 'test') | |
| 312 setupSchema(self.db2, 0) | |
| 289 | 313 |
| 290 class bsddbReadOnlyDBTestCase(anydbmReadOnlyDBTestCase): | 314 class bsddbReadOnlyDBTestCase(anydbmReadOnlyDBTestCase): |
| 291 def setUp(self): | 315 def setUp(self): |
| 292 from roundup.backends import bsddb | 316 from roundup.backends import bsddb |
| 293 # remove previous test, ignore errors | 317 # remove previous test, ignore errors |
| 296 os.makedirs(config.DATABASE + '/files') | 320 os.makedirs(config.DATABASE + '/files') |
| 297 db = bsddb.Database(config, 'test') | 321 db = bsddb.Database(config, 'test') |
| 298 setupSchema(db, 1) | 322 setupSchema(db, 1) |
| 299 self.db = bsddb.Database(config) | 323 self.db = bsddb.Database(config) |
| 300 setupSchema(self.db, 0) | 324 setupSchema(self.db, 0) |
| 325 self.db2 = bsddb.Database(config, 'test') | |
| 326 setupSchema(self.db2, 0) | |
| 301 | 327 |
| 302 | 328 |
| 303 class bsddb3DBTestCase(anydbmDBTestCase): | 329 class bsddb3DBTestCase(anydbmDBTestCase): |
| 304 def setUp(self): | 330 def setUp(self): |
| 305 from roundup.backends import bsddb3 | 331 from roundup.backends import bsddb3 |
| 307 if os.path.exists(config.DATABASE): | 333 if os.path.exists(config.DATABASE): |
| 308 shutil.rmtree(config.DATABASE) | 334 shutil.rmtree(config.DATABASE) |
| 309 os.makedirs(config.DATABASE + '/files') | 335 os.makedirs(config.DATABASE + '/files') |
| 310 self.db = bsddb3.Database(config, 'test') | 336 self.db = bsddb3.Database(config, 'test') |
| 311 setupSchema(self.db, 1) | 337 setupSchema(self.db, 1) |
| 338 self.db2 = bsddb3.Database(config, 'test') | |
| 339 setupSchema(self.db2, 0) | |
| 312 | 340 |
| 313 class bsddb3ReadOnlyDBTestCase(anydbmReadOnlyDBTestCase): | 341 class bsddb3ReadOnlyDBTestCase(anydbmReadOnlyDBTestCase): |
| 314 def setUp(self): | 342 def setUp(self): |
| 315 from roundup.backends import bsddb3 | 343 from roundup.backends import bsddb3 |
| 316 # remove previous test, ignore errors | 344 # remove previous test, ignore errors |
| 319 os.makedirs(config.DATABASE + '/files') | 347 os.makedirs(config.DATABASE + '/files') |
| 320 db = bsddb3.Database(config, 'test') | 348 db = bsddb3.Database(config, 'test') |
| 321 setupSchema(db, 1) | 349 setupSchema(db, 1) |
| 322 self.db = bsddb3.Database(config) | 350 self.db = bsddb3.Database(config) |
| 323 setupSchema(self.db, 0) | 351 setupSchema(self.db, 0) |
| 352 self.db2 = bsddb3.Database(config, 'test') | |
| 353 setupSchema(self.db2, 0) | |
| 324 | 354 |
| 325 | 355 |
| 326 def suite(): | 356 def suite(): |
| 327 l = [unittest.makeSuite(anydbmDBTestCase, 'test'), | 357 l = [ |
| 358 unittest.makeSuite(anydbmDBTestCase, 'test'), | |
| 328 unittest.makeSuite(anydbmReadOnlyDBTestCase, 'test') | 359 unittest.makeSuite(anydbmReadOnlyDBTestCase, 'test') |
| 329 ] | 360 ] |
| 330 | 361 |
| 331 try: | 362 try: |
| 332 import bsddb | 363 import bsddb |
| 344 | 375 |
| 345 return unittest.TestSuite(l) | 376 return unittest.TestSuite(l) |
| 346 | 377 |
| 347 # | 378 # |
| 348 # $Log: not supported by cvs2svn $ | 379 # $Log: not supported by cvs2svn $ |
| 380 # Revision 1.21 2002/04/15 23:25:15 richard | |
| 381 # . node ids are now generated from a lockable store - no more race conditions | |
| 382 # | |
| 383 # We're using the portalocker code by Jonathan Feinberg that was contributed | |
| 384 # to the ASPN Python cookbook. This gives us locking across Unix and Windows. | |
| 385 # | |
| 386 # Revision 1.20 2002/04/03 05:54:31 richard | |
| 387 # Fixed serialisation problem by moving the serialisation step out of the | |
| 388 # hyperdb.Class (get, set) into the hyperdb.Database. | |
| 389 # | |
| 390 # Also fixed htmltemplate after the showid changes I made yesterday. | |
| 391 # | |
| 392 # Unit tests for all of the above written. | |
| 393 # | |
| 394 # Revision 1.19 2002/02/25 14:34:31 grubert | |
| 395 # . use blobfiles in back_anydbm which is used in back_bsddb. | |
| 396 # change test_db as dirlist does not work for subdirectories. | |
| 397 # ATTENTION: blobfiles now creates subdirectories for files. | |
| 398 # | |
| 349 # Revision 1.18 2002/01/22 07:21:13 richard | 399 # Revision 1.18 2002/01/22 07:21:13 richard |
| 350 # . fixed back_bsddb so it passed the journal tests | 400 # . fixed back_bsddb so it passed the journal tests |
| 351 # | 401 # |
| 352 # ... it didn't seem happy using the back_anydbm _open method, which is odd. | 402 # ... it didn't seem happy using the back_anydbm _open method, which is odd. |
| 353 # Yet another occurrance of whichdb not being able to recognise older bsddb | 403 # Yet another occurrance of whichdb not being able to recognise older bsddb |
