Skip to content

Commit f2204af

Browse files
committed
merge heads
2 parents 7f3552a + 35d2f0b commit f2204af

File tree

9 files changed

+52
-20
lines changed

9 files changed

+52
-20
lines changed

roundup/backends/__init__.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@ def have_backend(name):
5454
else:
5555
modname = e.args[0][16:] if e.args[0].startswith('No module named ') else None
5656

57+
# It's always ok if memorydb is not found
58+
if modname.endswith('back_memorydb'):
59+
return 0
5760
if modname and (modname in _modules.get(name, (name,))):
5861
return 0
5962
raise
@@ -65,9 +68,15 @@ def list_backends():
6568
This function has side-effect of registering backward-compatible
6669
globals for all available backends.
6770
71+
Note: Since memorydb does not live in the backends directory, it will
72+
never be found in the default setup. It *can* be enabled by preloading
73+
test/memorydb and injecting into roundup.backends. So the normal user
74+
can never configure memorydb but it makes using the tests easier
75+
because we do not need to monkey-patch list_backends.
76+
6877
'''
6978
l = []
70-
for name in 'anydbm', 'mysql', 'sqlite', 'postgresql':
79+
for name in 'anydbm', 'mysql', 'sqlite', 'postgresql', 'memorydb':
7180
if have_backend(name):
7281
l.append(name)
7382
return l

roundup/test/memorydb.py

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ def new_config(debug=False, prefix=default_prefix):
3838
return config
3939

4040
def create(journaltag, create=True, debug=False, prefix=default_prefix):
41+
# "Nuke" in-memory db
42+
db_nuke('')
43+
4144
db = Database(new_config(debug), journaltag)
4245

4346
# load standard schema
@@ -214,12 +217,12 @@ class Database(back_anydbm.Database):
214217

215218
dbtype = "memorydb"
216219

220+
# Make it a little more persistent across re-open
221+
memdb = {}
222+
217223
def __init__(self, config, journaltag=None):
218224
self.config, self.journaltag = config, journaltag
219225
self.classes = {}
220-
self.items = {}
221-
self.ids = {}
222-
self.journals = {}
223226
self.files = {}
224227
self.tx_files = {}
225228
self.security = security.Security(self)
@@ -236,6 +239,10 @@ def __init__(self, config, journaltag=None):
236239
self.destroyednodes = {}# keep track of the destroyed nodes by class
237240
self.transactions = []
238241
self.tx_Source = None
242+
# persistence across re-open
243+
self.items = self.__class__.memdb.get('items', {})
244+
self.ids = self.__class__.memdb.get('ids', {})
245+
self.journals = self.__class__.memdb.get('journals', {})
239246

240247
def filename(self, classname, nodeid, property=None, create=0):
241248
shutil.copyfile(__file__, __file__+'.dummy')
@@ -290,6 +297,10 @@ def close(self):
290297
# kill the schema too
291298
self.classes = {}
292299
# just keep the .items
300+
# persistence across re-open
301+
self.__class__.memdb['items'] = self.items
302+
self.__class__.memdb['ids'] = self.ids
303+
self.__class__.memdb['journals'] = self.journals
293304

294305
#
295306
# Classes
@@ -472,4 +483,13 @@ def __init__(self, db, classname, **properties):
472483
properties['superseder'] = hyperdb.Multilink(classname)
473484
Class.__init__(self, db, classname, **properties)
474485

486+
# Methods to check for existence and nuke the db
487+
# We don't support multiple named databases
488+
489+
def db_exists(name):
490+
return bool(Database.memdb)
491+
492+
def db_nuke(name):
493+
Database.memdb = {}
494+
475495
# vim: set et sts=4 sw=4 :

test/db_test_base.py

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
from roundup.anypy.cmp_ import NoneAndDictComparable
4646
from roundup.anypy.email_ import message_from_bytes
4747

48-
from .mocknull import MockNull
48+
from roundup.test.mocknull import MockNull
4949

5050
config = configuration.CoreConfig()
5151
config.DATABASE = "db"
@@ -1379,15 +1379,13 @@ def testJournalNonexistingProperty(self):
13791379
self.assertEqual(result [4][4], jp3)
13801380
self.db.close()
13811381
# Verify that normal user doesn't see obsolete props/classes
1382-
# Backend memorydb cannot re-open db for different user
1383-
if self.db.dbtype != 'memorydb':
1384-
self.open_database('mary')
1385-
setupSchema(self.db, 0, self.module)
1386-
# allow mary to see issue fields like title
1387-
self.db.security.addPermissionToRole('User', 'View', 'issue')
1388-
result=self.db.issue.history(id)
1389-
self.assertEqual(len(result), 2)
1390-
self.assertEqual(result [1][4], jp0)
1382+
self.open_database('mary')
1383+
setupSchema(self.db, 0, self.module)
1384+
# allow mary to see issue fields like title
1385+
self.db.security.addPermissionToRole('User', 'View', 'issue')
1386+
result=self.db.issue.history(id)
1387+
self.assertEqual(len(result), 2)
1388+
self.assertEqual(result [1][4], jp0)
13911389

13921390
def testJournalPreCommit(self):
13931391
id = self.db.user.create(username="mary")

test/rest_common.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ def dst(self, dt):
4444

4545
from .db_test_base import setupTracker
4646

47-
from .mocknull import MockNull
47+
from roundup.test.mocknull import MockNull
4848

4949
from io import BytesIO
5050
import json

test/test_actions.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
from time import sleep
1414
from datetime import datetime
1515

16-
from .mocknull import MockNull
16+
from roundup.test.mocknull import MockNull
1717

1818
def true(*args, **kwargs):
1919
return 1

test/test_cgi.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
# For testing very simple rendering
2929
from roundup.cgi.engine_zopetal import RoundupPageTemplate
3030

31-
from .mocknull import MockNull
31+
from roundup.test.mocknull import MockNull
3232

3333
from . import db_test_base
3434
from .db_test_base import FormTestParent, setupTracker, FileUpload

test/test_mailgw.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,7 @@ def tearDown(self):
223223
if os.path.exists(SENDMAILDEBUG):
224224
os.remove(SENDMAILDEBUG)
225225
self.db.close()
226+
memorydb.db_nuke('')
226227

227228
def _allowAnonymousSubmit(self):
228229
p = [

test/test_memorydb.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,14 @@ class memorydbOpener:
1010

1111
def nuke_database(self):
1212
# really kill it
13+
memorydb.db_nuke('')
1314
self.db = None
1415

1516
db = None
16-
def open_database(self):
17-
if self.db is None:
18-
self.db = self.module.Database(config, 'admin')
17+
def open_database(self, user='admin'):
18+
if self.db:
19+
self.db.close()
20+
self.db = self.module.Database(config, user)
1921
return self.db
2022

2123
def setUp(self):
@@ -25,6 +27,8 @@ def setUp(self):
2527
def tearDown(self):
2628
if self.db is not None:
2729
self.db.close()
30+
self.db = None
31+
self.nuke_database()
2832

2933
# nuke and re-create db for restore
3034
def nukeAndCreate(self):

0 commit comments

Comments
 (0)