Author: bct Date: 2008-06-12 05:56:47 +0200 (Thu, 12 Jun 2008) New Revision: 9790
Added: trunk/test/test_caps.py Modified: trunk/src/common/caps.py Log: CapsCache: made docstring match the API, fixed minor bugs, added tests
Modified: trunk/src/common/caps.py =================================================================== --- trunk/src/common/caps.py 2008-06-12 03:56:41 UTC (rev 9789) +++ trunk/src/common/caps.py 2008-06-12 03:56:47 UTC (rev 9790) @@ -24,7 +24,7 @@
class CapsCache(object): ''' This object keeps the mapping between caps data and real disco - features they represent, and provides simple way to query that info. + features they represent, and provides simple way to query that info. It is application-wide, that is there's one object for all connections. Goals: @@ -44,46 +44,39 @@ # object creation >>> cc=CapsCache(logger_object)
- >>> caps=('http://exodus.jabberstudio.org/caps', '0.9', None) # node, ver, ext - >>> muc='http://jabber.org/protocol/muc' - >>> chatstates='http://jabber.org/protocol/chatstates' + >>> caps = ('sha-1', '66/0NaeaBKkwk85efJTGmU47vXI=') + >>> muc = 'http://jabber.org/protocol/muc' + >>> chatstates = 'http://jabber.org/protocol/chatstates'
+ # setting data + >>> cc[caps].identities = [{'category':'client', 'type':'pc'}] + >>> cc[caps].features = [muc] + # retrieving data >>> muc in cc[caps].features True - >>> muc in cc[caps] - True - >>> chatstates in cc[caps] + >>> chatstates in cc[caps].features False >>> cc[caps].identities - set({'category':'client', 'type':'pc'}) - >>> x=cc[caps] # more efficient if making several queries for one set of caps + [{'category': 'client', 'type': 'pc'}] + >>> x = cc[caps] # more efficient if making several queries for one set of caps ATypicalBlackBoxObject - >>> muc in x + >>> muc in x.features True - >>> x.node - 'http://exodus.jabberstudio.org/caps'
# retrieving data (multiple exts case) >>> caps=('http://gajim.org/caps', '0.9', ('csn', 'ft')) - >>> muc in cc[caps] + >>> muc in cc[caps].features True
- # setting data - >>> newcaps=('http://exodus.jabberstudio.org/caps', '0.9a', None) - >>> cc[newcaps].identities.add({'category':'client', 'type':'pc', 'name':'Gajim'}) - >>> cc[newcaps].features+=muc # same as: - >>> cc[newcaps]+=muc - >>> cc[newcaps]['csn']+=chatstates # adding data as if ext was 'csn' - # warning: no feature removal! ''' def __init__(self, logger=None): ''' Create a cache for entity capabilities. ''' # our containers: - # __cache is a dictionary mapping: pair of node and version maps + # __cache is a dictionary mapping: pair of hash method and hash maps # to CapsCacheItem object # __CacheItem is a class that stores data about particular - # client (node/version pair) + # client (hash method/hash pair) self.__cache = {}
class CacheItem(object): @@ -129,7 +122,7 @@ d['xml:lang'] = i[2] if i[3]: d['name'] = i[3] - list.append(d) + list_.append(d) return list_ def _set_identities(ciself, value): ciself._identities = [] @@ -172,7 +165,9 @@ def __getitem__(self, caps): if caps in self.__cache: return self.__cache[caps] - hash_method, hash = caps[0], caps[1] + + hash_method, hash = caps + x = self.__CacheItem(hash_method, hash) self.__cache[(hash_method, hash)] = x return x
Added: trunk/test/test_caps.py =================================================================== --- trunk/test/test_caps.py (rev 0) +++ trunk/test/test_caps.py 2008-06-12 03:56:47 UTC (rev 9790) @@ -0,0 +1,47 @@ +# tests for capabilities and the capabilities cache +import unittest + +import testlib +testlib.setup_env() + +from common import gajim +from common import xmpp + +from common.caps import CapsCache + +from mock import Mock + +class MockLogger(Mock): + def __init__(self, *args): + Mock.__init__(self, *args) + +class TestCapsCache(unittest.TestCase): + def setUp(self): + self.logger = MockLogger() + self.cc = CapsCache(self.logger) + + def test_examples(self): + '''tests the examples given in common/caps.py''' + + caps = ('sha-1', '66/0NaeaBKkwk85efJTGmU47vXI=') + identity = {'category': 'client', 'type': 'pc'} + + muc = 'http://jabber.org/protocol/muc' + chatstates = 'http://jabber.org/protocol/chatstates' + + self.cc[caps].identities = [identity] + self.cc[caps].features = [muc] + + self.assert_(muc in self.cc[caps].features) + self.assert_(chatstates not in self.cc[caps].features) + + id = self.cc[caps].identities + + self.assertEqual(1, len(id)) + + id = id[0] + self.assertEqual('client', id['category']) + self.assertEqual('pc', id['type']) + +if __name__ == '__main__': + unittest.main()