Testing templates with Zope 2.10

Projects

This article shows how to avoid this error: TraversalError: (‘No traversable adapter found’,

The bug

When I was trying to make ZPTDebugger works with Zope 2.10 I got this error:

After quick googling it became clear that the problem is caused by new Zope 2.10 architecture. This is because it becomes more similar to Zope 3 in this version. I’ve found usefull clue: . you just need to do zope.component.provideAdapter(zope.traversing.adapters.DefaultTraversable). The Zope 3 page template engine will use ITraversable adapters to traverse just about anything. So, basically, wherever you’re employing a page template in CMF tests, you’ll want this adapter. (by Philipp von Weitershausen).

Solution

What I had to do is just provide this adapter. I’ve played with Zope 3 almost year ago so it took me some time to solve this problem. But finally I’ve managed to modify my code to work with Zope 2.10, and hopefuly wilth later versions. Neverthless it is still working with all previosus versions 2.7, 2.8 and 2.9.

Simple unit test

from ZPublisher.BaseRequest import BaseRequest as Request

def test_myTemplate (self):
obj = createInstance()
obj = wrapObjectForPageTemplates (object)
result = obj.myTemplate(REQUEST=Request())
#result is a string containing rendered template
self.assertEqual(result. find ( ‘error message’ )>= 0 , False )

Provide the adapter

def createInstance ():
“””
To make tests work under Zope 2.10 class must be
implements interface and provide adapter.
“””
if ZOPE_MINOR_VERSION > 9 :
from zope.traversing.adapters import DefaultTraversable
from zope.traversing.interfaces import ITraversable
from zope.component import provideAdapter
from zope import interface
from zope.interface import implements

class MyClass2(MyClass):
implements (ITraversable)
provideAdapter(DefaultTraversable,
(interface.Interface,),ITraversable)
object = MyClass2 ()
else :
object = MyClass ()
return object

How to obtain Zope version

Add below method to your utilities and create variable that stores Zope minor version. Minor version is the number after first dot: 2.7.5, 2.8.6 etc. Knowing that we can create conditions that will execute blocks of code appropriate to the version. They can be completely different and may not work in other verions but product won’t break. That is how ZPTDebugger supports both 2.7-2.9 family and now 2.10.

from App.version_txt import getZopeVersion
def getVersion():
“””
Gets Zope version
“””
version = getZopeVersion ()
result = < 'major' :version[ 0 ],
‘minor’ :version[ 1 ],
‘micro’ :version[ 2 ],
‘status’ :version[ 3 ],
‘release’ :version[ 4 ]>
return result

ZOPE_MINOR_VERSION = getVersion ()[ ‘minor’ ]

Testing templates with Zope 2.10

This article shows how to avoid this error: TraversalError: (‘No traversable adapter found’,

The bug

When I was trying to make ZPTDebugger works with Zope 2.10 I got this error:

After quick googling it became clear that the problem is caused by new Zope 2.10 architecture. This is because it becomes more similar to Zope 3 in this version. I’ve found usefull clue: . you just need to do zope.component.provideAdapter(zope.traversing.adapters.DefaultTraversable). The Zope 3 page template engine will use ITraversable adapters to traverse just about anything. So, basically, wherever you’re employing a page template in CMF tests, you’ll want this adapter. (by Philipp von Weitershausen).

Solution

What I had to do is just provide this adapter. I’ve played with Zope 3 almost year ago so it took me some time to solve this problem. But finally I’ve managed to modify my code to work with Zope 2.10, and hopefuly wilth later versions. Neverthless it is still working with all previosus versions 2.7, 2.8 and 2.9.

Simple unit test

from ZPublisher.BaseRequest import BaseRequest as Request

def test_myTemplate (self):
obj = createInstance()
obj = wrapObjectForPageTemplates (object)
result = obj.myTemplate(REQUEST=Request())
#result is a string containing rendered template
self.assertEqual(result. find ( ‘error message’ )>= 0 , False )

Provide the adapter

def createInstance ():
“””
To make tests work under Zope 2.10 class must be
implements interface and provide adapter.
“””
if ZOPE_MINOR_VERSION > 9 :
from zope.traversing.adapters import DefaultTraversable
from zope.traversing.interfaces import ITraversable
from zope.component import provideAdapter
from zope import interface
from zope.interface import implements

class MyClass2(MyClass):
implements (ITraversable)
provideAdapter(DefaultTraversable,
(interface.Interface,),ITraversable)
object = MyClass2 ()
else :
object = MyClass ()
return object

How to obtain Zope version

Add below method to your utilities and create variable that stores Zope minor version. Minor version is the number after first dot: 2.7.5, 2.8.6 etc. Knowing that we can create conditions that will execute blocks of code appropriate to the version. They can be completely different and may not work in other verions but product won’t break. That is how ZPTDebugger supports both 2.7-2.9 family and now 2.10.

from App.version_txt import getZopeVersion
def getVersion():
“””
Gets Zope version
“””
version = getZopeVersion ()
result = < 'major' :version[ 0 ],
‘minor’ :version[ 1 ],
‘micro’ :version[ 2 ],
‘status’ :version[ 3 ],
‘release’ :version[ 4 ]>
return result

ZOPE_MINOR_VERSION = getVersion ()[ ‘minor’ ]

Rate article
llakomy.com
Add a comment