1
##############################################################################
3
# Copyright (c) 2010 Zope Corporation and Contributors.
6
# This software is subject to the provisions of the Zope Public License,
7
# Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution.
8
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
9
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
10
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
11
# FOR A PARTICULAR PURPOSE.
13
##############################################################################
15
from ZODB.DB import DB
16
from ZODB.DemoStorage import DemoStorage
17
import ZODB.ActivityMonitor
18
import ZODB.interfaces
21
from zope import component
22
from zope.app.publication.zopepublication import ZopePublication
23
from zope.component.testlayer import ZCMLFileLayer
24
from zope.event import notify
25
import zope.processlifetime
28
def createTestDB(name='main'):
29
"""This create a test storage and register it.
31
storage = DemoStorage(name)
32
db = DB(storage, database_name=name)
33
db.setActivityMonitor(ZODB.ActivityMonitor.ActivityMonitor())
35
# DB are registered as utilities
36
component.provideUtility(db, ZODB.interfaces.IDatabase, name)
38
# And we send a event that our DB is available
39
notify(zope.processlifetime.DatabaseOpened(db))
43
class ZODBLayer(ZCMLFileLayer):
44
"""This layer load a ZCML configuration and create a test database.
46
You can access the test database with layer.getRootFolder().
53
def getRootFolder(self):
54
"""This return the root object of the database or assert if
55
the database have not been created yet.
57
if self.connection is None:
58
assert self.db is not None
59
self.connection = self.db.open()
60
return self.connection.root()[ZopePublication.root_name]
63
super(ZODBLayer, self).testSetUp()
64
self.db = createTestDB(self.db_name)
66
def testTearDown(self):
67
# Close any opened connections
68
if self.connection is not None:
70
self.connection.close()
71
self.connection = None
74
if self.db is not None:
75
# Need to unregister DB
76
base = component.getGlobalSiteManager()
77
base.unregisterUtility(
78
self.db, ZODB.interfaces.IDatabase, self.db_name)
82
super(ZODBLayer, self).testTearDown()