[Zope3-Users] Access to request in content object and object path in doctests

Marius Gedminas mgedmin at b4net.lt
Wed Jan 24 18:06:01 EST 2007


On Wed, Jan 24, 2007 at 11:50:21PM +0100, Maciej Wisniowski wrote:
> Is this possible to get
> request object in content class. In Zope2 this
> was possible with simple self.REQUEST. In Zope3
> I tried self.request but I only get errors.
> Maybe this is a feature, and I'm not supposed
> to access request object from content class?

Yes.  You're not supposed to do that.  Views work with requests, not
content objects.

If you told us what you want to achieve, we could help you find a way to
do it that works with Zope 3 rather than against it.

> Another question. I'm trying to write tests
> for my content object.
> Because I want some caching facilities
> I'm using getLocationForCache from
> zope.app.cache.caching.
> and because of this: zapi.getPath(obj)
> 
> In short it is like:
> 
> class MyClass(Persistent):
>     (...)
>     def myfunction(self):
>         location = zapi.getPath(self)
>         return location
> 
> In the tests I always get location == None.
> I'm using doctests like:
> 
>    >>> test_content = MyClass()
>    >>> test_content.myfunction()
> 
> placefulsetup adds some objects like root etc.
> but I'm not sure how I should add my object to this.
> Any clues?

Lately I've been using this pattern in my tests:

    #!/usr/bin/env python
    import unittest

    from zope.testing import doctest
    from zope.app.testing import setup
    from zope.app.folder import rootFolder
    from zope.traversing.api import getPath

    from mypackage import MyObject


    def doctest_something():
        """Bla bla bla

            >>> root = rootFolder()
            >>> root['my_object'] = my_object = MyObject()
            >>> getPath(my_object)
            '/my_object'

        """


    def setUp(test):
        setup.placelessSetUp()
        setup.setUpTraversal()


    def tearDown(test):
        setup.placelessTearDown()


    def test_suite():
        return doctest.DocTestSuite(setUp=setUp, tearDown=tearDown)


    if __name__ == '__main__':
        unittest.main(defaultTest='test_suite'))


I avoid setup.placefulSetUp, and I avoid zapi/ztapi.

Cheers,
Marius Gedminas
-- 
System going down at 5 this afternoon to install scheduler bug.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: Digital signature
Url : http://mail.zope.org/pipermail/zope3-users/attachments/20070125/b64a8400/attachment.bin


More information about the Zope3-users mailing list