[Zope3-dev] testing and savepoints

Stephan Richter srichter at cosmos.phy.tufts.edu
Tue Oct 17 15:37:32 EDT 2006


On Tuesday 17 October 2006 14:47, Jim Fulton wrote:
> You do realize that you can stack demo storages.
> Create a demo storage for the layer and test and
> toss the database when you're done.  Look at the way
> zope.app.testing.functional works for an example.

Yep, we utilize that functional setup too, but provide our own base_storage. 
This way we can have a generated testing database that can be deleted and is 
automatically generated when missing. Each new functional test using this 
layer gets a fresh database of this kind. Here is our layer setup:

class RecruiterSiteLayer(object):
    """A special layer that sets up a well-populated recruiter site."""

    __name__ = "RecruiterSiteLayer"
    __bases__ = (functional.Functional,)

    seed = 'Recruiter'

    def setUp(self):
        fsetup = functional.FunctionalTestSetup()
        self.original = fsetup.base_storage
        filename = os.path.join(os.path.dirname(__file__), 'testing.fs')

        if not os.path.exists(filename):
            oldPolicy = management.setSecurityPolicy(PermissiveSecurityPolicy)
            management.newInteraction(SetupConfigurationParticipation())

            # Generate a new database from scratch and the fill it with the
            # sample data
            db = database(filename)
            connection = db.open()
            root = connection.root()
            app = root[ZopePublication.root_name]
            generator.generate(app, self.seed)
            transaction.commit()
            connection.close()
            db.close()

            management.endInteraction()
            management.setSecurityPolicy(oldPolicy)

        fsetup.base_storage = FileStorage(filename)
        fsetup.setUp()

    def tearDown(self):
        fsetup = functional.FunctionalTestSetup()
        fsetup.base_storage = self.original
        fsetup.tearDown()

RecruiterSiteLayer = RecruiterSiteLayer()

The generator is a z3c.sampledata generator.

Regards,
Stephan
-- 
Stephan Richter
CBU Physics & Chemistry (B.S.) / Tufts Physics (Ph.D. student)
Web2k - Web Software Design, Development and Training


More information about the Zope3-dev mailing list