Package ldaptor :: Package test :: Module test_autofill
[hide private]
[frames] | no frames]

Source Code for Module ldaptor.test.test_autofill

 1  """ 
 2  Test cases for ldaptor.protocols.ldap.autofill module. 
 3  """ 
 4   
 5  from twisted.trial import unittest 
 6  from ldaptor.protocols.ldap import ldapsyntax 
 7  from ldaptor.testutil import LDAPClientTestDriver 
 8   
9 -class Autofill_sum: #TODO baseclass
10 - def __init__(self, resultAttr, sumAttrs):
11 self.resultAttr = resultAttr 12 self.sumAttrs = sumAttrs
13
14 - def start(self, ldapObject):
15 pass
16
17 - def notify(self, ldapObject, attributeType):
18 if attributeType not in self.sumAttrs: 19 return 20 21 sum = 0 22 for sumAttr in self.sumAttrs: 23 if sumAttr not in ldapObject: 24 continue 25 for val in ldapObject[sumAttr]: 26 val = int(val) 27 sum += val 28 sum = str(sum) 29 ldapObject[self.resultAttr] = [sum]
30
31 -class LDAPAutoFill_Simple(unittest.TestCase):
32 - def testSimpleSum(self):
33 """A simple autofiller that calculates sums of attributes should work..""" 34 client = LDAPClientTestDriver() 35 o=ldapsyntax.LDAPEntryWithAutoFill(client=client, 36 dn='cn=foo,dc=example,dc=com', 37 attributes={ 38 'objectClass': ['some', 'other'], 39 }) 40 d = o.addAutofiller(Autofill_sum(resultAttr='sum', 41 sumAttrs=['a', 'b'])) 42 def cb(dummy): 43 client.assertNothingSent() 44 45 o['a'] = ['1'] 46 o['b'] = ['2', '3'] 47 48 self.failUnless('sum' in o) 49 self.failUnlessEqual(o['sum'], ['6'])
50 d.addCallback(cb) 51 return d
52