Hi !
I work at the present time to an project with a lot data provided by an ldap server. The schema of this ldap is really elaborate . To reproduce it in a real ldap server is very difficult. How can I test my terrific code witch consist on an specific vocabularies and a set of PAS plugins ?
The response : a mock ldap object that give to my eggs a real API for ldap.
You can found it here : http://products.ingeniweb.com/catalog/iw.mock.ldap
The goal of this egg : testing component witch use ldap server without ldap. But also provide some good data in order to have some good test. So iw.mock.ldap read an real ldif file and you can search via ldap api (search_s).
It’s not perfect but for me I found very easy to test ldap component.
So how use it ?
In your buildout
[buildout]
eggs =
…
iw.mock.ldap
[zinstance]
eggs =
…
iw.mock.ldap
zcml =
…
iw.mock.ldap
After in you’re project where you want have some test you create a structure like that::
path-to-your-egg/mock
path-to-your-egg/mock/__init__.py
path-to-your-egg/mock/data.ldif
In __init__.py you put this boiler code::
import sys
from zope.interface import implements
from zope.component import adapts
from zope.component import provideAdapter
from iw.mock.ldap.interfaces import ILdifFile
from iw.mock.ldap.interfaces import ILdifReader
import iw.mock
class LdifFile(object):
implements(ILdifFile)
adapts(ILdifReader)
def __init__(self, context):
self.context = context
def open(self):
return file(os.path.join(os.path.dirname(__file__),’data.ldif’))
provideAdapter(LdifFile)
sys.path.insert(0, os.path.dirname(iw.mock.__file__))
from iw.mock import ldap
reload(ldap)
You can also register your adapter in an zcml (example test.zcml)
<configure xmlns=”http://namespaces.zope.org/zope”>
<adapter factory=”.mock.LdifFile”/>
</configure>
And now in you’re test you import path-to-your-egg/mock and all connections and request ldap pass
by iw.mock.ldap and use your data in data.ldif
