[Zope3-dev] Re: SVN: Zope3/trunk/src/zope/app/file/browser/ fixed the way absolute_url is retrieved in ImageData.tag()

Philipp von Weitershausen philipp at weitershausen.de
Mon Apr 10 01:19:43 EDT 2006


Tarek Ziadé wrote:
> Modified: Zope3/trunk/src/zope/app/file/browser/tests/test_imagedata.py
> ===================================================================
> --- Zope3/trunk/src/zope/app/file/browser/tests/test_imagedata.py	2006-04-09 21:31:11 UTC (rev 66754)
> +++ Zope3/trunk/src/zope/app/file/browser/tests/test_imagedata.py	2006-04-10 01:09:35 UTC (rev 66755)
> @@ -20,6 +20,9 @@
>  from zope.app.file.image import Image
>  from zope.app.file.browser.image import ImageData
>  
> +class FakeRequest(object):
> +    pass
> +
...
> +        # faking absolute_url getter .
> +        def absolute_url(context, request):
>              return '/img'
...
> +        from zope.app import zapi
> +        old_absoluteURL = zapi.absoluteURL
> +        try:
> +            zapi.absoluteURL = absolute_url
> +            self.assertEqual(fe.tag(),
> +                '<img src="/img" alt="" height="-1" width="-1" border="0" />')
> +            self.assertEqual(fe.tag(alt="Test Image"),
> +                '<img src="/img" alt="Test Image" '
> +                'height="-1" width="-1" border="0" />')
> +            self.assertEqual(fe.tag(height=100, width=100),
> +                ('<img src="/img" alt="" height="100" '
> +                 'width="100" border="0" />'))
> +            self.assertEqual(fe.tag(border=1),
> +                '<img src="/img" alt="" height="-1" width="-1" border="1" />')
> +            self.assertEqual(fe.tag(css_class="Image"),
> +                '<img src="/img" alt="" '
> +                'height="-1" width="-1" border="0" class="Image" />')
> +            self.assertEqual(fe.tag(height=100, width="100",
> +                            border=1, css_class="Image"),
> +                '<img src="/img" alt="" '
> +                'height="100" width="100" class="Image" border="1" />')
> +        finally:
> +            zapi.absoluteURL = old_absoluteURL

Perhaps I may suggest an alternative solution for the test that won't
require a) a monkey patch to zapi.absoluteURL nor b) a try/finally
clause (which is pointless in a test since you really wouldn't want
errors to be eaten away).

zapi.absoluteURL does nothing else than return a multiadapter from
(object, request) to IAbsoluteURL. I would therefore recommend
registering a stub IAbsoluteURL adapter instead of monkey patching a
stub zapi.absoluteURL. This adapter would be very simple:

class StubAbsoluteURL(object):
    adapts(ImageData, FakeRequest)
    implements(IAbsoluteURL)

    def __str__(self):
        return '/img'

    __call__ = __str__

Then you only need to register it in the test:
zope.component.provideAdapter(StubAbsoluteURL). Clean up will be handled
if you call zope.component.testing.cleanUp() or make the test inherit
from zope.component.testing.PlacelessSetup (zope.app.testing's
PlacelessSetup would be overkill).

Philipp



More information about the Zope3-dev mailing list