Layers
======

zope.app.appsetup.testlayer define a test layer which creates a test
database.

ZODBLayer
---------

We can instantiate a ZODBLayer::

  >>> from zope.app.appsetup.testlayer import ZODBLayer
  >>> from zope.app.appsetup import testpackage

  >>> layer = ZODBLayer(testpackage)
  >>> layer
  <zope.app.appsetup.testlayer.ZODBLayer object at ...>

Now we run some tests with this layer that checks that we have a
working database::

  >>> import unittest
  >>> import transaction
  >>> from zope import component
  >>> from zope.app.appsetup.testpackage import testobject
  >>> from ZODB.interfaces import IDatabase

  >>> class TestCase(unittest.TestCase):
  ...    layer = layer
  ...
  ...    def testAddObjectInDB(self):
  ...        root = self.layer.getRootFolder()
  ...        root['object'] = testobject.TestObject()
  ...        transaction.commit()
  ...        self.failUnless('object' in root)
  ...    def testNoMoreObjectInDB(self):
  ...        root = self.layer.getRootFolder()
  ...        self.failIf('object' in root)
  ...    def testApplicationInDB(self):
  ...        root = self.layer.getRootFolder()
  ...        self.assertEquals(
  ...            repr(root.__class__), "<class 'zope.site.folder.Folder'>")
  ...    def testDBRegistered(self):
  ...        root = self.layer.getRootFolder()
  ...        db = component.getUtility(IDatabase, name='main')
  ...        self.assertEquals(db, root._p_jar.db())

We define a suite with our test:

  >>> suite = unittest.TestSuite()
  >>> suite.addTest(unittest.makeSuite(TestCase))

And run that suite:

  >>> from zope.testrunner.runner import Runner
  >>> runner = Runner(args=[], found_suites=[suite])
  >>> succeeded = runner.run()
  Running zope.app.appsetup.testpackage.ZODBLayer tests:
    Set up zope.app.appsetup.testpackage.ZODBLayer in ... seconds.
    Ran 4 tests with 0 failures and 0 errors in ... seconds.
  Tearing down left over layers:
    Tear down zope.app.appsetup.testpackage.ZODBLayer in ... seconds.
