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.

Attachment: signature.asc
Description: Digital signature

_______________________________________________
For more information about ZODB, see the ZODB Wiki:
http://www.zope.org/Wikis/ZODB/

ZODB-Dev mailing list  -  ZODB-Dev@zope.org
https://mail.zope.org/mailman/listinfo/zodb-dev

Reply via email to