From 4589c41fc516fc8ce7cc2ee7e4ee10516a0ddccb Mon Sep 17 00:00:00 2001 From: Ask Solem Date: Thu, 22 Nov 2012 14:27:00 +0000 Subject: [PATCH] More tests --- kombu/tests/test_utils.py | 121 +++++++++++++++++++-- kombu/tests/transport/virtual/test_base.py | 27 +++++ kombu/tests/utilities/test_amq_manager.py | 36 ++++++ kombu/tests/utilities/test_debug.py | 58 ++++++++++ kombu/utils/amq_manager.py | 6 +- setup.cfg | 1 + 6 files changed, 239 insertions(+), 10 deletions(-) create mode 100644 kombu/tests/utilities/test_amq_manager.py create mode 100644 kombu/tests/utilities/test_debug.py diff --git a/kombu/tests/test_utils.py b/kombu/tests/test_utils.py index 457658d8..305e7feb 100644 --- a/kombu/tests/test_utils.py +++ b/kombu/tests/test_utils.py @@ -1,9 +1,10 @@ -from __future__ import absolute_import +from __future__ import absolute_import, with_statement import pickle import sys from functools import wraps +from mock import Mock, patch if sys.version_info >= (3, 0): from io import StringIO, BytesIO @@ -13,8 +14,10 @@ else: from kombu import utils from kombu.utils.compat import next -from .utils import redirect_stdouts, mask_modules, skip_if_module -from .utils import TestCase +from .utils import ( + TestCase, + redirect_stdouts, mask_modules, module_exists, skip_if_module, +) class OldString(object): @@ -177,10 +180,21 @@ class test_retry_over_time(TestCase): @insomnia def test_simple(self): - x = utils.retry_over_time(self.myfun, self.Predicate, - errback=self.errback, interval_max=14) - self.assertEqual(x, 42) - self.assertEqual(self.index, 9) + prev_count, utils.count = utils.count, Mock() + try: + utils.count.return_value = range(1) + x = utils.retry_over_time(self.myfun, self.Predicate, + errback=None, interval_max=14) + self.assertIsNone(x) + utils.count.return_value = range(10) + cb = Mock() + x = utils.retry_over_time(self.myfun, self.Predicate, + errback=self.errback, callback=cb, interval_max=14) + self.assertEqual(x, 42) + self.assertEqual(self.index, 9) + cb.assert_called_with() + finally: + utils.count = prev_count @insomnia def test_retry_once(self): @@ -203,6 +217,26 @@ class test_retry_over_time(TestCase): class test_cached_property(TestCase): + def test_deleting(self): + + class X(object): + xx = False + + @utils.cached_property + def foo(self): + return 42 + + @foo.deleter + def foo(self, value): + self.xx = value + + x = X() + del(x.foo) + self.assertFalse(x.xx) + x.__dict__['foo'] = 'here' + del(x.foo) + self.assertEqual(x.xx, 'here') + def test_when_access_from_class(self): class X(object): @@ -229,3 +263,76 @@ class test_cached_property(TestCase): self.assertEqual(x.xx, 10) del(x.foo) + + +class test_symbol_by_name(TestCase): + + def test_instance_returns_instance(self): + instance = object() + self.assertIs(utils.symbol_by_name(instance), instance) + + def test_returns_default(self): + default = object() + self.assertIs(utils.symbol_by_name('xyz.ryx.qedoa.weq:foz', + default=default), default) + + def test_no_default(self): + with self.assertRaises(ImportError): + utils.symbol_by_name('xyz.ryx.qedoa.weq:foz') + + def test_imp_reraises_ValueError(self): + imp = Mock() + imp.side_effect = ValueError() + with self.assertRaises(ValueError): + utils.symbol_by_name('kombu.Connection', imp=imp) + + + def test_package(self): + from kombu.entity import Exchange + self.assertIs(utils.symbol_by_name('.entity:Exchange', + package='kombu'), Exchange) + self.assertTrue(utils.symbol_by_name(':Consumer', package='kombu')) + + +class test_ChannelPromise(TestCase): + + def test_repr(self): + self.assertEqual(repr(utils.ChannelPromise(lambda: 'foo')), + "") + + +class test_entrypoints(TestCase): + + @mask_modules('pkg_resources') + def test_without_pkg_resources(self): + self.assertListEqual(list(utils.entrypoints('kombu.test')), []) + + @module_exists('pkg_resources') + def test_with_pkg_resources(self): + with patch('pkg_resources.iter_entry_points', create=True) as iterep: + eps = iterep.return_value = [Mock(), Mock()] + + self.assertTrue(list(utils.entrypoints('kombu.test'))) + iterep.assert_called_with('kombu.test') + eps[0].load.assert_called_with() + eps[1].load.assert_called_with() + + +class test_shufflecycle(TestCase): + + def test_shuffles(self): + prev_repeat, utils.repeat = utils.repeat, Mock() + try: + utils.repeat.return_value = range(10) + values = set(['A', 'B', 'C']) + cycle = utils.shufflecycle(values) + seen = set() + for i in xrange(10): + cycle.next() + utils.repeat.assert_called_with(None) + self.assertTrue(seen.issubset(values)) + with self.assertRaises(StopIteration): + cycle.next() + cycle.next() + finally: + utils.repeat = prev_repeat diff --git a/kombu/tests/transport/virtual/test_base.py b/kombu/tests/transport/virtual/test_base.py index af716ede..4adfa9e6 100644 --- a/kombu/tests/transport/virtual/test_base.py +++ b/kombu/tests/transport/virtual/test_base.py @@ -93,6 +93,9 @@ class test_QoS(TestCase): self.assertTrue(stderr.getvalue()) self.assertFalse(stdout.getvalue()) + self.q.restore_at_shutdown = False + self.q.restore_unacked_once() + def test_get(self): self.q._delivered['foo'] = 1 self.assertEqual(self.q.get('foo'), 1) @@ -178,10 +181,34 @@ class test_Channel(TestCase): if self.channel._qos is not None: self.channel._qos._on_collect.cancel() + def test_exchange_bind_interface(self): + with self.assertRaises(NotImplementedError): + self.channel.exchange_bind('dest', 'src', 'key') + + def test_exchange_unbind_interface(self): + with self.assertRaises(NotImplementedError): + self.channel.exchange_unbind('dest', 'src', 'key') + + def test_queue_unbind_interface(self): + with self.assertRaises(NotImplementedError): + self.channel.queue_unbind('dest', 'ex', 'key') + + def test_management(self): + m = self.channel.connection.client.get_manager() + self.assertTrue(m) + m.get_bindings() + m.close() + def test_exchange_declare(self): c = self.channel + + with self.assertRaises(StdChannelError): + c.exchange_declare('test_exchange_declare', 'direct', + durable=True, auto_delete=True, passive=True) c.exchange_declare('test_exchange_declare', 'direct', durable=True, auto_delete=True) + c.exchange_declare('test_exchange_declare', 'direct', + durable=True, auto_delete=True, passive=True) self.assertIn('test_exchange_declare', c.state.exchanges) # can declare again with same values c.exchange_declare('test_exchange_declare', 'direct', diff --git a/kombu/tests/utilities/test_amq_manager.py b/kombu/tests/utilities/test_amq_manager.py new file mode 100644 index 00000000..cf86bfa9 --- /dev/null +++ b/kombu/tests/utilities/test_amq_manager.py @@ -0,0 +1,36 @@ +from __future__ import absolute_import, with_statement + +from mock import Mock, patch + +from kombu import Connection +from kombu.utils.amq_manager import get_manager +from kombu.tests.utils import TestCase, mask_modules, module_exists + + +class test_get_manager(TestCase): + + @mask_modules('pyrabbit') + def test_without_pyrabbit(self): + with self.assertRaises(ImportError): + Connection('amqp://').get_manager() + + @module_exists('pyrabbit') + def test_with_pyrabbit(self): + with patch('pyrabbit.Client', create=True) as Client: + manager = Connection('amqp://').get_manager() + Client.assert_called_with('localhost:55672', + 'guest', 'guest') + + @module_exists('pyrabbit') + def test_transport_options(self): + with patch('pyrabbit.Client', create=True) as Client: + manager = Connection('amqp://', transport_options={ + 'manager_hostname': 'admin.mq.vandelay.com', + 'manager_port': 808, + 'manager_userid': 'george', + 'manager_password': 'bosco', + }).get_manager() + Client.assert_called_with('admin.mq.vandelay.com:808', + 'george', 'bosco') + + diff --git a/kombu/tests/utilities/test_debug.py b/kombu/tests/utilities/test_debug.py new file mode 100644 index 00000000..d9f83ae1 --- /dev/null +++ b/kombu/tests/utilities/test_debug.py @@ -0,0 +1,58 @@ +from __future__ import absolute_import, with_statement + +import logging + +from mock import Mock, patch + +from kombu.utils.debug import ( + setup_logging, + Logwrapped, +) +from kombu.tests.utils import TestCase + + +class test_setup_logging(TestCase): + + def test_adds_handlers_sets_level(self): + with patch('kombu.utils.debug.get_logger') as get_logger: + logger = get_logger.return_value = Mock() + setup_logging(loggers=['kombu.test']) + + get_logger.assert_called_with('kombu.test') + + self.assertTrue(logger.addHandler.called) + logger.setLevel.assert_called_with(logging.DEBUG) + + +class test_Logwrapped(TestCase): + + def test_wraps(self): + with patch('kombu.utils.debug.get_logger') as get_logger: + logger = get_logger.return_value = Mock() + + W = Logwrapped(Mock(), 'kombu.test') + get_logger.assert_called_with('kombu.test') + self.assertIsNotNone(W.instance) + self.assertIs(W.logger, logger) + + W.instance.__repr__ = lambda s: 'foo' + self.assertEqual(repr(W), 'foo') + self.assertListEqual(dir(W), dir(W.instance)) + + W.instance.some_attr = 303 + self.assertEqual(W.some_attr, 303) + + W.instance.some_method.__name__ = 'some_method' + W.some_method(1, 2, kw=1) + W.instance.some_method.assert_called_with(1, 2, kw=1) + + W.some_method() + W.instance.some_method.assert_called_with() + + W.some_method(kw=1) + W.instance.some_method.assert_called_with(kw=1) + + W.ident = 'ident' + W.some_method(kw=1) + self.assertTrue(logger.debug.called) + self.assertIn('ident', logger.debug.call_args[0][0]) diff --git a/kombu/utils/amq_manager.py b/kombu/utils/amq_manager.py index 0bb9ce4c..62a7a95a 100644 --- a/kombu/utils/amq_manager.py +++ b/kombu/utils/amq_manager.py @@ -6,10 +6,10 @@ def get_manager(client, hostname=None, port=None, userid=None, import pyrabbit opt = client.transport_options.get host = (hostname if hostname is not None - else opt('manager_hostname', client.hostname)) + else opt('manager_hostname', client.hostname or 'localhost')) port = port if port is not None else opt('manager_port', 55672) return pyrabbit.Client('%s:%s' % (host, port), userid if userid is not None - else opt('manager_userid', client.userid), + else opt('manager_userid', client.userid or 'guest'), password if password is not None - else opt('manager_password', client.password)) + else opt('manager_password', client.password or 'guest')) diff --git a/setup.cfg b/setup.cfg index 88ad9aa1..019ee20d 100644 --- a/setup.cfg +++ b/setup.cfg @@ -14,6 +14,7 @@ cover3-exclude = kombu kombu.transport.beanstalk kombu.transport.SQS kombu.transport.zookeeper + kombu.transport.zmq [build_sphinx] source-dir = docs/