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

Tarek Ziadé tziade at nuxeo.com
Mon Apr 10 05:15:49 EDT 2006


Philipp von Weitershausen wrote:

>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).
>  
>
b) errors are not eaten away with a finally clause, and still pops,
it is just here to make sure zapi.absoluteURL leaves the test in its
formal shape
no matters what happens.

>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).
>
>  
>
Great, way better, i'll change to use your solution,
thanks

Regards,

Tarek

>Philipp
>
>  
>



More information about the Zope3-dev mailing list