diff options
-rw-r--r-- | src/common/caps.py | 11 | ||||
-rw-r--r-- | test/test_caps.py | 62 |
2 files changed, 58 insertions, 15 deletions
diff --git a/src/common/caps.py b/src/common/caps.py index e41ae52c6..b4211ccb4 100644 --- a/src/common/caps.py +++ b/src/common/caps.py @@ -58,9 +58,10 @@ class AbstractEntityCapabilities(object): Query will only be sent if the data is not already cached. ''' q = self._lookup_in_cache() - if q and q.query_status == q.NOT_QUERIED: - q.query_status = q.QUERIED - q._discover(connection, jid) + if q and q.queried == 0: + self._discover(connection, jid) + q.queried = 1 + def _discover(self, connection, jid): ''' To be implemented by subclassess ''' @@ -92,8 +93,8 @@ class OldEntityCapabilities(AbstractEntityCapabilities): def __init__(self, caps_cache, caps_hash, node): AbstractEntityCapabilities.__init__(self, caps_cache, caps_hash, node) - def _lookup_in_cache(self, caps_cache): - return caps_cache[('old', self._node + '#' + self._hash)] + def _lookup_in_cache(self): + return self._caps_cache[('old', self._node + '#' + self._hash)] def _discover(self, connection, jid): connection.discoverInfo(jid) diff --git a/test/test_caps.py b/test/test_caps.py index 03fa9d9ca..ecf430314 100644 --- a/test/test_caps.py +++ b/test/test_caps.py @@ -8,7 +8,7 @@ lib.setup_env() from common import helpers from common.contacts import Contact -from common.caps import CapsCache +from common.caps import CapsCache, EntityCapabilities, OldEntityCapabilities from mock import Mock @@ -28,21 +28,18 @@ class CommonCapsTest(unittest.TestCase): self.identities = [self.identity] self.features = [self.muc] - - -class TestCapsCache(CommonCapsTest): - - def setUp(self): - CommonCapsTest.setUp(self) - + # Simulate a filled db db_caps_cache = [ (self.caps_method, self.caps_hash, self.identities, self.features), - (self.caps_method, self.caps_hash, self.identities, self.features)] + ('old', self.node + '#' + self.caps_hash, self.identities, self.features)] self.logger = Mock(returnValues={"iter_caps_data":db_caps_cache}) self.cc = CapsCache(self.logger) - + + +class TestCapsCache(CommonCapsTest): + def test_set_retrieve(self): ''' Test basic set / retrieve cycle ''' @@ -117,6 +114,51 @@ class TestCapsCache(CommonCapsTest): self.assertEqual(self.caps_hash, computed_hash) +class TestEntityCapabilities(CommonCapsTest): + + def setUp(self): + CommonCapsTest.setUp(self) + self.entity = EntityCapabilities(self.cc, self.caps_hash, self.node, + self.caps_method) + + def test_no_query_client_of_jid(self): + ''' Client must not be queried if the data is already cached ''' + connection = Mock() + self.cc.initialize_from_db() + self.entity.query_client_of_jid_if_unknown(connection, "test@gajim.org") + + self.assertEqual(0, len(connection.mockGetAllCalls())) + + def test_query_client_of_jid_if_unknown(self): + ''' Client must be queried if the data is unkown ''' + connection = Mock() + self.entity.query_client_of_jid_if_unknown(connection, "test@gajim.org") + + connection.mockCheckCall(0, "discoverInfo", 'test@gajim.org', + 'http://gajim.org#RNzJvJnTWqczirzu+YF4V8am9ro=') + + +class TestOldEntityCapabilities(TestEntityCapabilities): + + def setUp(self): + TestEntityCapabilities.setUp(self) + self.entity = OldEntityCapabilities(self.cc, self.caps_hash, self.node) + + def test_no_query_client_of_jid(self): + ''' Client must not be queried if the data is already cached ''' + connection = Mock() + self.cc.initialize_from_db() + self.entity.query_client_of_jid_if_unknown(connection, "test@gajim.org") + + self.assertEqual(0, len(connection.mockGetAllCalls())) + + def test_query_client_of_jid_if_unknown(self): + ''' Client must be queried if the data is unkown ''' + connection = Mock() + self.entity.query_client_of_jid_if_unknown(connection, "test@gajim.org") + + connection.mockCheckCall(0, "discoverInfo", "test@gajim.org") + if __name__ == '__main__': |