Mercurial > p > roundup > code
comparison test/db_test_base.py @ 2089:93f03c6714d8
A few big changes in this commit:
1. The current indexer has been moved to backends/indexer_dbm in
anticipation of my writing an indexer_rdbms,
2. Changed indexer invocation during create / set to follow the pattern
set by the metakit backend, which was much cleaner, and
3. The "content" property of FileClass is now mutable in all but the
metakit backend.
Metakit needs to be changed to support the editing of "content". Hey, and
I learnt today that the metakit backend implements its own indexer. How
about that... :)
| author | Richard Jones <richard@users.sourceforge.net> |
|---|---|
| date | Fri, 19 Mar 2004 04:47:59 +0000 |
| parents | c091cacdc505 |
| children | 18addf2a8596 |
comparison
equal
deleted
inserted
replaced
| 2088:90769be53b4b | 2089:93f03c6714d8 |
|---|---|
| 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: db_test_base.py,v 1.17 2004-03-18 01:58:46 richard Exp $ | 18 # $Id: db_test_base.py,v 1.18 2004-03-19 04:47:59 richard Exp $ |
| 19 | 19 |
| 20 import unittest, os, shutil, errno, imp, sys, time, pprint | 20 import unittest, os, shutil, errno, imp, sys, time, pprint |
| 21 | 21 |
| 22 from roundup.hyperdb import String, Password, Link, Multilink, Date, \ | 22 from roundup.hyperdb import String, Password, Link, Multilink, Date, \ |
| 23 Interval, DatabaseError, Boolean, Number, Node | 23 Interval, DatabaseError, Boolean, Number, Node |
| 24 from roundup import date, password | 24 from roundup import date, password |
| 25 from roundup import init | 25 from roundup import init |
| 26 from roundup.indexer import Indexer | |
| 27 | 26 |
| 28 def setupSchema(db, create, module): | 27 def setupSchema(db, create, module): |
| 29 status = module.Class(db, "status", name=String()) | 28 status = module.Class(db, "status", name=String()) |
| 30 status.setkey("name") | 29 status.setkey("name") |
| 31 user = module.Class(db, "user", username=String(), password=Password(), | 30 user = module.Class(db, "user", username=String(), password=Password(), |
| 87 | 86 |
| 88 # | 87 # |
| 89 # automatic properties (well, the two easy ones anyway) | 88 # automatic properties (well, the two easy ones anyway) |
| 90 # | 89 # |
| 91 def testCreatorProperty(self): | 90 def testCreatorProperty(self): |
| 92 id1 = self.db.issue.create() | 91 i = self.db.issue |
| 92 id1 = i.create(title='spam') | |
| 93 self.db.commit() | 93 self.db.commit() |
| 94 self.db.close() | 94 self.db.close() |
| 95 self.db = self.module.Database(config, 'fred') | 95 self.db = self.module.Database(config, 'fred') |
| 96 setupSchema(self.db, 0, self.module) | 96 setupSchema(self.db, 0, self.module) |
| 97 i = self.db.issue | 97 i = self.db.issue |
| 98 id2 = i.create() | 98 id2 = i.create(title='spam') |
| 99 self.assertNotEqual(id1, id2) | 99 self.assertNotEqual(id1, id2) |
| 100 self.assertNotEqual(i.get(id1, 'creator'), i.get(id2, 'creator')) | 100 self.assertNotEqual(i.get(id1, 'creator'), i.get(id2, 'creator')) |
| 101 | 101 |
| 102 def testActorProperty(self): | 102 def testActorProperty(self): |
| 103 id1 = self.db.issue.create() | 103 i = self.db.issue |
| 104 id1 = i.create(title='spam') | |
| 104 self.db.commit() | 105 self.db.commit() |
| 105 self.db.close() | 106 self.db.close() |
| 106 self.db = self.module.Database(config, 'fred') | 107 self.db = self.module.Database(config, 'fred') |
| 107 setupSchema(self.db, 0, self.module) | 108 setupSchema(self.db, 0, self.module) |
| 108 i = self.db.issue | 109 i = self.db.issue |
| 119 | 120 |
| 120 def testEmptySet(self): | 121 def testEmptySet(self): |
| 121 id1 = self.db.issue.create(title="spam", status='1') | 122 id1 = self.db.issue.create(title="spam", status='1') |
| 122 self.db.issue.set(id1) | 123 self.db.issue.set(id1) |
| 123 | 124 |
| 125 # String | |
| 124 def testStringChange(self): | 126 def testStringChange(self): |
| 125 for commit in (0,1): | 127 for commit in (0,1): |
| 126 # test set & retrieve | 128 # test set & retrieve |
| 127 nid = self.db.issue.create(title="spam", status='1') | 129 nid = self.db.issue.create(title="spam", status='1') |
| 128 self.assertEqual(self.db.issue.get(nid, 'title'), 'spam') | 130 self.assertEqual(self.db.issue.get(nid, 'title'), 'spam') |
| 140 # make sure we can unset | 142 # make sure we can unset |
| 141 self.db.issue.set(nid, title=None) | 143 self.db.issue.set(nid, title=None) |
| 142 if commit: self.db.commit() | 144 if commit: self.db.commit() |
| 143 self.assertEqual(self.db.issue.get(nid, "title"), None) | 145 self.assertEqual(self.db.issue.get(nid, "title"), None) |
| 144 | 146 |
| 147 # FileClass "content" property (no unset test) | |
| 148 def testFileClassContentChange(self): | |
| 149 for commit in (0,1): | |
| 150 # test set & retrieve | |
| 151 nid = self.db.file.create(content="spam") | |
| 152 self.assertEqual(self.db.file.get(nid, 'content'), 'spam') | |
| 153 | |
| 154 # change and make sure we retrieve the correct value | |
| 155 self.db.file.set(nid, content='eggs') | |
| 156 if commit: self.db.commit() | |
| 157 self.assertEqual(self.db.file.get(nid, 'content'), 'eggs') | |
| 158 | |
| 159 # Link | |
| 145 def testLinkChange(self): | 160 def testLinkChange(self): |
| 146 self.assertRaises(IndexError, self.db.issue.create, title="spam", | 161 self.assertRaises(IndexError, self.db.issue.create, title="spam", |
| 147 status='100') | 162 status='100') |
| 148 for commit in (0,1): | 163 for commit in (0,1): |
| 149 nid = self.db.issue.create(title="spam", status='1') | 164 nid = self.db.issue.create(title="spam", status='1') |
| 159 if commit: self.db.commit() | 174 if commit: self.db.commit() |
| 160 self.db.issue.set(nid, status=None) | 175 self.db.issue.set(nid, status=None) |
| 161 if commit: self.db.commit() | 176 if commit: self.db.commit() |
| 162 self.assertEqual(self.db.issue.get(nid, "status"), None) | 177 self.assertEqual(self.db.issue.get(nid, "status"), None) |
| 163 | 178 |
| 179 # Multilink | |
| 164 def testMultilinkChange(self): | 180 def testMultilinkChange(self): |
| 165 for commit in (0,1): | 181 for commit in (0,1): |
| 166 self.assertRaises(IndexError, self.db.issue.create, title="spam", | 182 self.assertRaises(IndexError, self.db.issue.create, title="spam", |
| 167 nosy=['foo%s'%commit]) | 183 nosy=['foo%s'%commit]) |
| 168 u1 = self.db.user.create(username='foo%s'%commit) | 184 u1 = self.db.user.create(username='foo%s'%commit) |
| 173 self.db.issue.set(nid, nosy=[]) | 189 self.db.issue.set(nid, nosy=[]) |
| 174 if commit: self.db.commit() | 190 if commit: self.db.commit() |
| 175 self.assertEqual(self.db.issue.get(nid, "nosy"), []) | 191 self.assertEqual(self.db.issue.get(nid, "nosy"), []) |
| 176 self.db.issue.set(nid, nosy=[u1,u2]) | 192 self.db.issue.set(nid, nosy=[u1,u2]) |
| 177 if commit: self.db.commit() | 193 if commit: self.db.commit() |
| 178 self.assertEqual(self.db.issue.get(nid, "nosy"), [u1,u2]) | 194 l = [u1,u2]; l.sort() |
| 179 | 195 m = self.db.issue.get(nid, "nosy"); m.sort() |
| 196 self.assertEqual(l, m) | |
| 197 | |
| 198 # Date | |
| 180 def testDateChange(self): | 199 def testDateChange(self): |
| 181 self.assertRaises(TypeError, self.db.issue.create, | 200 self.assertRaises(TypeError, self.db.issue.create, |
| 182 title='spam', deadline=1) | 201 title='spam', deadline=1) |
| 183 for commit in (0,1): | 202 for commit in (0,1): |
| 184 nid = self.db.issue.create(title="spam", status='1') | 203 nid = self.db.issue.create(title="spam", status='1') |
| 199 self.assertNotEqual(self.db.issue.get(nid, "deadline"), None) | 218 self.assertNotEqual(self.db.issue.get(nid, "deadline"), None) |
| 200 self.db.issue.set(nid, deadline=None) | 219 self.db.issue.set(nid, deadline=None) |
| 201 if commit: self.db.commit() | 220 if commit: self.db.commit() |
| 202 self.assertEqual(self.db.issue.get(nid, "deadline"), None) | 221 self.assertEqual(self.db.issue.get(nid, "deadline"), None) |
| 203 | 222 |
| 223 # Interval | |
| 204 def testIntervalChange(self): | 224 def testIntervalChange(self): |
| 205 self.assertRaises(TypeError, self.db.issue.create, | 225 self.assertRaises(TypeError, self.db.issue.create, |
| 206 title='spam', foo=1) | 226 title='spam', foo=1) |
| 207 for commit in (0,1): | 227 for commit in (0,1): |
| 208 nid = self.db.issue.create(title="spam", status='1') | 228 nid = self.db.issue.create(title="spam", status='1') |
| 228 self.assertNotEqual(self.db.issue.get(nid, "foo"), None) | 248 self.assertNotEqual(self.db.issue.get(nid, "foo"), None) |
| 229 self.db.issue.set(nid, foo=None) | 249 self.db.issue.set(nid, foo=None) |
| 230 if commit: self.db.commit() | 250 if commit: self.db.commit() |
| 231 self.assertEqual(self.db.issue.get(nid, "foo"), None) | 251 self.assertEqual(self.db.issue.get(nid, "foo"), None) |
| 232 | 252 |
| 253 # Boolean | |
| 233 def testBooleanChange(self): | 254 def testBooleanChange(self): |
| 234 userid = self.db.user.create(username='foo', assignable=1) | 255 userid = self.db.user.create(username='foo', assignable=1) |
| 235 self.assertEqual(1, self.db.user.get(userid, 'assignable')) | 256 self.assertEqual(1, self.db.user.get(userid, 'assignable')) |
| 236 self.db.user.set(userid, assignable=0) | 257 self.db.user.set(userid, assignable=0) |
| 237 self.assertEqual(self.db.user.get(userid, 'assignable'), 0) | 258 self.assertEqual(self.db.user.get(userid, 'assignable'), 0) |
| 239 def testBooleanUnset(self): | 260 def testBooleanUnset(self): |
| 240 nid = self.db.user.create(username='foo', assignable=1) | 261 nid = self.db.user.create(username='foo', assignable=1) |
| 241 self.db.user.set(nid, assignable=None) | 262 self.db.user.set(nid, assignable=None) |
| 242 self.assertEqual(self.db.user.get(nid, "assignable"), None) | 263 self.assertEqual(self.db.user.get(nid, "assignable"), None) |
| 243 | 264 |
| 265 # Number | |
| 244 def testNumberChange(self): | 266 def testNumberChange(self): |
| 245 nid = self.db.user.create(username='foo', age=1) | 267 nid = self.db.user.create(username='foo', age=1) |
| 246 self.assertEqual(1, self.db.user.get(nid, 'age')) | 268 self.assertEqual(1, self.db.user.get(nid, 'age')) |
| 247 self.db.user.set(nid, age=3) | 269 self.db.user.set(nid, age=3) |
| 248 self.assertNotEqual(self.db.user.get(nid, 'age'), 1) | 270 self.assertNotEqual(self.db.user.get(nid, 'age'), 1) |
| 257 def testNumberUnset(self): | 279 def testNumberUnset(self): |
| 258 nid = self.db.user.create(username='foo', age=1) | 280 nid = self.db.user.create(username='foo', age=1) |
| 259 self.db.user.set(nid, age=None) | 281 self.db.user.set(nid, age=None) |
| 260 self.assertEqual(self.db.user.get(nid, "age"), None) | 282 self.assertEqual(self.db.user.get(nid, "age"), None) |
| 261 | 283 |
| 284 # Password | |
| 262 def testPasswordChange(self): | 285 def testPasswordChange(self): |
| 263 x = password.Password('x') | 286 x = password.Password('x') |
| 264 userid = self.db.user.create(username='foo', password=x) | 287 userid = self.db.user.create(username='foo', password=x) |
| 265 self.assertEqual(x, self.db.user.get(userid, 'password')) | 288 self.assertEqual(x, self.db.user.get(userid, 'password')) |
| 266 self.assertEqual(self.db.user.get(userid, 'password'), 'x') | 289 self.assertEqual(self.db.user.get(userid, 'password'), 'x') |
| 275 x = password.Password('x') | 298 x = password.Password('x') |
| 276 nid = self.db.user.create(username='foo', password=x) | 299 nid = self.db.user.create(username='foo', password=x) |
| 277 self.db.user.set(nid, assignable=None) | 300 self.db.user.set(nid, assignable=None) |
| 278 self.assertEqual(self.db.user.get(nid, "assignable"), None) | 301 self.assertEqual(self.db.user.get(nid, "assignable"), None) |
| 279 | 302 |
| 303 # key value | |
| 280 def testKeyValue(self): | 304 def testKeyValue(self): |
| 281 self.assertRaises(ValueError, self.db.user.create) | 305 self.assertRaises(ValueError, self.db.user.create) |
| 282 | 306 |
| 283 newid = self.db.user.create(username="spam") | 307 newid = self.db.user.create(username="spam") |
| 284 self.assertEqual(self.db.user.lookup('spam'), newid) | 308 self.assertEqual(self.db.user.lookup('spam'), newid) |
| 293 # try to restore old node. this shouldn't succeed! | 317 # try to restore old node. this shouldn't succeed! |
| 294 self.assertRaises(KeyError, self.db.user.restore, newid) | 318 self.assertRaises(KeyError, self.db.user.restore, newid) |
| 295 | 319 |
| 296 self.assertRaises(TypeError, self.db.issue.lookup, 'fubar') | 320 self.assertRaises(TypeError, self.db.issue.lookup, 'fubar') |
| 297 | 321 |
| 322 # label property | |
| 298 def testLabelProp(self): | 323 def testLabelProp(self): |
| 299 # key prop | 324 # key prop |
| 300 self.assertEqual(self.db.status.labelprop(), 'name') | 325 self.assertEqual(self.db.status.labelprop(), 'name') |
| 301 self.assertEqual(self.db.user.labelprop(), 'username') | 326 self.assertEqual(self.db.user.labelprop(), 'username') |
| 302 # title | 327 # title |
| 304 # name | 329 # name |
| 305 self.assertEqual(self.db.file.labelprop(), 'name') | 330 self.assertEqual(self.db.file.labelprop(), 'name') |
| 306 # id | 331 # id |
| 307 self.assertEqual(self.db.stuff.labelprop(default_to_id=1), 'id') | 332 self.assertEqual(self.db.stuff.labelprop(default_to_id=1), 'id') |
| 308 | 333 |
| 334 # retirement | |
| 309 def testRetire(self): | 335 def testRetire(self): |
| 310 self.db.issue.create(title="spam", status='1') | 336 self.db.issue.create(title="spam", status='1') |
| 311 b = self.db.status.get('1', 'name') | 337 b = self.db.status.get('1', 'name') |
| 312 a = self.db.status.list() | 338 a = self.db.status.list() |
| 313 self.db.status.retire('1') | 339 self.db.status.retire('1') |
| 607 # we should have the create and last set entries now | 633 # we should have the create and last set entries now |
| 608 self.assertEqual(jlen-1, len(self.db.getjournal('issue', id))) | 634 self.assertEqual(jlen-1, len(self.db.getjournal('issue', id))) |
| 609 | 635 |
| 610 def testIndexerSearching(self): | 636 def testIndexerSearching(self): |
| 611 f1 = self.db.file.create(content='hello', type="text/plain") | 637 f1 = self.db.file.create(content='hello', type="text/plain") |
| 638 # content='world' has the wrong content-type and won't be indexed | |
| 612 f2 = self.db.file.create(content='world', type="text/frozz", | 639 f2 = self.db.file.create(content='world', type="text/frozz", |
| 613 comment='blah blah') | 640 comment='blah blah') |
| 614 i1 = self.db.issue.create(files=[f1, f2], title="flebble plop") | 641 i1 = self.db.issue.create(files=[f1, f2], title="flebble plop") |
| 615 i2 = self.db.issue.create(title="flebble frooz") | 642 i2 = self.db.issue.create(title="flebble frooz") |
| 616 self.db.commit() | 643 self.db.commit() |
| 621 {i2: {}}) | 648 {i2: {}}) |
| 622 self.assertEquals(self.db.indexer.search(['flebble'], self.db.issue), | 649 self.assertEquals(self.db.indexer.search(['flebble'], self.db.issue), |
| 623 {i1: {}, i2: {}}) | 650 {i1: {}, i2: {}}) |
| 624 | 651 |
| 625 def testReindexing(self): | 652 def testReindexing(self): |
| 626 self.db.issue.create(title="frooz") | 653 search = self.db.indexer.search |
| 627 self.db.commit() | 654 issue = self.db.issue |
| 628 self.assertEquals(self.db.indexer.search(['frooz'], self.db.issue), | 655 i1 = issue.create(title="flebble plop") |
| 629 {'1': {}}) | 656 i2 = issue.create(title="flebble frooz") |
| 630 self.db.issue.set('1', title="dooble") | 657 self.db.commit() |
| 631 self.db.commit() | 658 self.assertEquals(search(['plop'], issue), {i1: {}}) |
| 632 self.assertEquals(self.db.indexer.search(['dooble'], self.db.issue), | 659 self.assertEquals(search(['flebble'], issue), {i1: {}, i2: {}}) |
| 633 {'1': {}}) | 660 |
| 634 self.assertEquals(self.db.indexer.search(['frooz'], self.db.issue), {}) | 661 # change i1's title |
| 662 issue.set(i1, title="plop") | |
| 663 self.db.commit() | |
| 664 self.assertEquals(search(['plop'], issue), {i1: {}}) | |
| 665 self.assertEquals(search(['flebble'], issue), {i2: {}}) | |
| 666 | |
| 667 # unset i1's title | |
| 668 issue.set(i1, title="") | |
| 669 self.db.commit() | |
| 670 self.assertEquals(search(['plop'], issue), {}) | |
| 671 self.assertEquals(search(['flebble'], issue), {i2: {}}) | |
| 672 | |
| 673 def testFileClassReindexing(self): | |
| 674 f1 = self.db.file.create(content='hello') | |
| 675 f2 = self.db.file.create(content='hello, world') | |
| 676 i1 = self.db.issue.create(files=[f1, f2]) | |
| 677 self.db.commit() | |
| 678 d = self.db.indexer.search(['hello'], self.db.issue) | |
| 679 d[i1]['files'].sort() | |
| 680 self.assertEquals(d, {i1: {'files': [f1, f2]}}) | |
| 681 self.assertEquals(self.db.indexer.search(['world'], self.db.issue), | |
| 682 {i1: {'files': [f2]}}) | |
| 683 self.db.file.set(f1, content="world") | |
| 684 self.db.commit() | |
| 685 d = self.db.indexer.search(['world'], self.db.issue) | |
| 686 d[i1]['files'].sort() | |
| 687 self.assertEquals(d, {i1: {'files': [f1, f2]}}) | |
| 688 self.assertEquals(self.db.indexer.search(['hello'], self.db.issue), | |
| 689 {i1: {'files': [f2]}}) | |
| 690 | |
| 635 | 691 |
| 636 def testForcedReindexing(self): | 692 def testForcedReindexing(self): |
| 637 self.db.issue.create(title="flebble frooz") | 693 self.db.issue.create(title="flebble frooz") |
| 638 self.db.commit() | 694 self.db.commit() |
| 639 self.assertEquals(self.db.indexer.search(['flebble'], self.db.issue), | 695 self.assertEquals(self.db.indexer.search(['flebble'], self.db.issue), |
| 887 l = items.keys(); l.sort() | 943 l = items.keys(); l.sort() |
| 888 m = klass.list(); m.sort() | 944 m = klass.list(); m.sort() |
| 889 ae(l, m) | 945 ae(l, m) |
| 890 for id, props in items.items(): | 946 for id, props in items.items(): |
| 891 for name, value in props.items(): | 947 for name, value in props.items(): |
| 892 ae(klass.get(id, name), value) | 948 l = klass.get(id, name) |
| 949 if isinstance(value, type([])): | |
| 950 value.sort() | |
| 951 l.sort() | |
| 952 ae(l, value) | |
| 893 | 953 |
| 894 # make sure the retired items are actually imported | 954 # make sure the retired items are actually imported |
| 895 ae(self.db.user.get('3', 'username'), 'blop') | 955 ae(self.db.user.get('4', 'username'), 'blop') |
| 896 ae(self.db.issue.get('2', 'title'), 'issue two') | 956 ae(self.db.issue.get('2', 'title'), 'issue two') |
| 897 | 957 |
| 898 # make sure id counters are set correctly | 958 # make sure id counters are set correctly |
| 899 maxid = max([int(id) for id in self.db.user.list()]) | 959 maxid = max([int(id) for id in self.db.user.list()]) |
| 900 newid = self.db.user.create(username='testing') | 960 newid = self.db.user.create(username='testing') |
