[ZODB-Dev] DemoStorage stacking in zope.testing layers

Marius Gedminas marius at gedmin.as
Mon Apr 5 12:54:09 EDT 2010


On Mon, Apr 05, 2010 at 10:36:49PM +0800, Martin Aspeli wrote:
> I'm trying to do something like the following:
> 
>   - A test layer "EmptyZODB" sets up an empty ZODB using DemoStorage and 
> exposes the DB object to other layers and tests.
> 
>   - A test layer "Foo" has "EmptyZODB" as a base. It opens a connection 
> during layer setup, makes some changes, commits those, and closes the 
> connection.
> 
>   - A test layer "Bar" has "Foo" as a base. It pushes a new DemoStorage 
> on top of the one that was modified by "Foo", opens a new connection, 
> makes changes, commits those, and closes the connection.
> 
>   - On layer tear-down, "Bar" pops the demo storage, leaving the 
> pristine fixture from "Foo".

I did this for SchoolTool once (poorly, because there were no layers
yet).  Note that when you stack multiple DemoStorages, ZODB gets
very noticeably slow.

As Hanno mentioned, zope.app.testing.functional also does this (with
just one level of DemoStorage, substituting a new one for every test).

You may want to looking up older versions of functional.py, since those
were much simpler (because they didn't have support for multiple
simultaneous databases).

> The problem with this scenario is that the database that was created by 
> "EmptyZODB" and then modified by "Foo" is by necessity already "active" 
> (even if it has no open connections). From my reading of the 
> docs/interfaces, it's not possible to "swap out" a storage of a DB 
> object, i.e. the storage is only settable in the constructor.
> 
> What is the correct way to use DemoStorage stacking in test layers? 

Close and discard the old database object, create a new demo storage
wrapping the old storage, create a new DB for it.

> Should "Bar" create a new DB object of its own and expose that to the 
> tests that need it? Or is there some way to keep the same "DB" object 
> (which would make the tests easier to write in this case) and simply 
> tell it to use the top of the stack at all times?

IMHO it's easier to find a way to pass the current database to the tests
than to make the database switch storages on the fly.

Marius Gedminas
-- 
Never trust a smiling Gates.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 197 bytes
Desc: Digital signature
Url : http://mail.zope.org/pipermail/zodb-dev/attachments/20100405/bb7e34a6/attachment.bin 


More information about the ZODB-Dev mailing list