~menesis/ubuntu/natty/zope.app.appsetup/natty

« back to all changes in this revision

Viewing changes to src/zope/app/appsetup/testlayer.py

  • Committer: Gediminas Paulauskas
  • Date: 2010-08-31 17:05:30 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: menesis@pov.lt-20100831170530-1kqlch6txq2pru8l
Tags: 3.14.0-0ubuntu1
* New upstream release.
* LICENSE.txt: add the license.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
##############################################################################
 
2
#
 
3
# Copyright (c) 2010 Zope Corporation and Contributors.
 
4
# All Rights Reserved.
 
5
#
 
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.
 
12
#
 
13
##############################################################################
 
14
 
 
15
from ZODB.DB import DB
 
16
from ZODB.DemoStorage import DemoStorage
 
17
import ZODB.ActivityMonitor
 
18
import ZODB.interfaces
 
19
import transaction
 
20
 
 
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
 
26
 
 
27
 
 
28
def createTestDB(name='main'):
 
29
    """This create a test storage and register it.
 
30
    """
 
31
    storage = DemoStorage(name)
 
32
    db = DB(storage, database_name=name)
 
33
    db.setActivityMonitor(ZODB.ActivityMonitor.ActivityMonitor())
 
34
 
 
35
    # DB are registered as utilities
 
36
    component.provideUtility(db, ZODB.interfaces.IDatabase, name)
 
37
 
 
38
    # And we send a event that our DB is available
 
39
    notify(zope.processlifetime.DatabaseOpened(db))
 
40
    return db
 
41
 
 
42
 
 
43
class ZODBLayer(ZCMLFileLayer):
 
44
    """This layer load a ZCML configuration and create a test database.
 
45
 
 
46
    You can access the test database with layer.getRootFolder().
 
47
    """
 
48
 
 
49
    db = None
 
50
    db_name = 'main'
 
51
    connection = None
 
52
 
 
53
    def getRootFolder(self):
 
54
        """This return the root object of the database or assert if
 
55
        the database have not been created yet.
 
56
        """
 
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]
 
61
 
 
62
    def testSetUp(self):
 
63
        super(ZODBLayer, self).testSetUp()
 
64
        self.db = createTestDB(self.db_name)
 
65
 
 
66
    def testTearDown(self):
 
67
        # Close any opened connections
 
68
        if self.connection is not None:
 
69
            transaction.abort()
 
70
            self.connection.close()
 
71
            self.connection = None
 
72
 
 
73
        # Close the Database
 
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)
 
79
            self.db.close()
 
80
            self.db = None
 
81
 
 
82
        super(ZODBLayer, self).testTearDown()