1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388
|
# -*- coding: utf8 -*-
"""
Tests for pika.callback
"""
import logging
import mock
try:
import unittest2 as unittest
except ImportError:
import unittest
from pika import amqp_object
from pika import callback
from pika import frame
from pika import spec
class CallbackTests(unittest.TestCase):
KEY = 'Test Key'
ARGUMENTS = callback.CallbackManager.ARGUMENTS
CALLS = callback.CallbackManager.CALLS
CALLBACK = callback.CallbackManager.CALLBACK
ONE_SHOT = callback.CallbackManager.ONE_SHOT
ONLY_CALLER = callback.CallbackManager.ONLY_CALLER
PREFIX_CLASS = spec.Basic.Consume
PREFIX = 'Basic.Consume'
ARGUMENTS_VALUE = {'foo': 'bar'}
@property
def _callback_dict(self):
return {self.CALLBACK: self.callback_mock,
self.ONE_SHOT: True,
self.ONLY_CALLER: self.mock_caller,
self.ARGUMENTS: self.ARGUMENTS_VALUE,
self.CALLS: 1}
def setUp(self):
self.obj = callback.CallbackManager()
self.callback_mock = mock.Mock()
self.mock_caller = mock.Mock()
def tearDown(self):
del self.obj
del self.callback_mock
del self.mock_caller
def test_initialization(self):
obj = callback.CallbackManager()
self.assertDictEqual(obj._stack, {})
def test_name_or_value_method_object(self):
value = spec.Basic.Consume()
self.assertEqual(callback._name_or_value(value), self.PREFIX)
def test_name_or_value_basic_consume_object(self):
self.assertEqual(callback._name_or_value(spec.Basic.Consume()),
self.PREFIX)
def test_name_or_value_amqpobject_class(self):
self.assertEqual(callback._name_or_value(self.PREFIX_CLASS),
self.PREFIX)
def test_name_or_value_protocol_header(self):
self.assertEqual(callback._name_or_value(frame.ProtocolHeader()),
'ProtocolHeader')
def test_name_or_value_method_frame(self):
value = frame.Method(1, self.PREFIX_CLASS())
self.assertEqual(callback._name_or_value(value), self.PREFIX)
def test_name_or_value_str(self):
value = 'Test String Value'
expectation = value
self.assertEqual(callback._name_or_value(value), expectation)
def test_name_or_value_unicode(self):
value = u'Это тест значения'
expectation = ('\xd0\xad\xd1\x82\xd0\xbe \xd1\x82\xd0\xb5\xd1\x81\xd1'
'\x82 \xd0\xb7\xd0\xbd\xd0\xb0\xd1\x87\xd0\xb5\xd0\xbd'
'\xd0\xb8\xd1\x8f')
self.assertEqual(callback._name_or_value(value), expectation)
def test_empty_callbacks_on_init(self):
self.assertFalse(self.obj._stack)
def test_sanitize_decorator_with_args_only(self):
self.obj.add(self.PREFIX_CLASS, self.KEY, None)
self.assertIn(self.PREFIX, self.obj._stack.keys())
def test_sanitize_decorator_with_kwargs(self):
self.obj.add(prefix=self.PREFIX_CLASS, key=self.KEY, callback=None)
self.assertIn(self.PREFIX, self.obj._stack.keys())
def test_sanitize_decorator_with_mixed_args_and_kwargs(self):
self.obj.add(self.PREFIX_CLASS, key=self.KEY, callback=None)
self.assertIn(self.PREFIX, self.obj._stack.keys())
def test_add_first_time_prefix_added(self):
self.obj.add(self.PREFIX, self.KEY, None)
self.assertIn(self.PREFIX, self.obj._stack)
def test_add_first_time_key_added(self):
self.obj.add(self.PREFIX, self.KEY, None)
self.assertIn(self.KEY, self.obj._stack[self.PREFIX])
def test_add_first_time_callback_added(self):
self.obj.add(self.PREFIX, self.KEY, self.callback_mock)
self.assertEqual(self.callback_mock,
self.obj._stack[self.PREFIX][self.KEY][0][self.CALLBACK])
def test_add_oneshot_default_is_true(self):
self.obj.add(self.PREFIX, self.KEY, None)
self.assertTrue(self.obj._stack[self.PREFIX][self.KEY][0][self.ONE_SHOT])
def test_add_oneshot_is_false(self):
self.obj.add(self.PREFIX, self.KEY, None, False)
self.assertFalse(self.obj._stack[self.PREFIX][self.KEY][0][self.ONE_SHOT])
def test_add_only_caller_default_is_false(self):
self.obj.add(self.PREFIX, self.KEY, None)
self.assertFalse(self.obj._stack[self.PREFIX][self.KEY][0][self.ONLY_CALLER])
def test_add_only_caller_true(self):
self.obj.add(self.PREFIX, self.KEY, None, only_caller=True)
self.assertTrue(self.obj._stack[self.PREFIX][self.KEY][0][self.ONLY_CALLER])
def test_add_returns_prefix_value_and_key(self):
self.assertEqual(self.obj.add(self.PREFIX, self.KEY, None),
(self.PREFIX, self.KEY))
def test_add_duplicate_callback(self):
mock_callback = mock.Mock()
def add_callback():
self.obj.add(self.PREFIX, self.KEY, mock_callback, False)
with mock.patch('pika.callback.LOGGER', spec=logging.Logger) as logger:
logger.warning = mock.Mock()
add_callback()
add_callback()
DUPLICATE_WARNING = callback.CallbackManager.DUPLICATE_WARNING
logger.warning.assert_called_once_with(DUPLICATE_WARNING,
self.PREFIX, self.KEY)
def test_add_duplicate_callback_returns_prefix_value_and_key(self):
self.obj.add(self.PREFIX, self.KEY, None)
self.assertEqual(self.obj.add(self.PREFIX, self.KEY, None),
(self.PREFIX, self.KEY))
def test_clear(self):
self.obj.add(self.PREFIX, self.KEY, None)
self.obj.clear()
self.assertDictEqual(self.obj._stack, dict())
def test_cleanup_removes_prefix(self):
OTHER_PREFIX = 'Foo'
self.obj.add(self.PREFIX, self.KEY, None)
self.obj.add(OTHER_PREFIX, 'Bar', None)
self.obj.cleanup(self.PREFIX)
self.assertNotIn(self.PREFIX, self.obj._stack)
def test_cleanup_keeps_other_prefix(self):
OTHER_PREFIX = 'Foo'
self.obj.add(self.PREFIX, self.KEY, None)
self.obj.add(OTHER_PREFIX, 'Bar', None)
self.obj.cleanup(self.PREFIX)
self.assertIn(OTHER_PREFIX, self.obj._stack)
def test_cleanup_returns_true(self):
self.obj.add(self.PREFIX, self.KEY, None)
self.assertTrue(self.obj.cleanup(self.PREFIX))
def test_missing_prefix(self):
self.assertFalse(self.obj.cleanup(self.PREFIX))
def test_pending_none(self):
self.assertIsNone(self.obj.pending(self.PREFIX_CLASS, self.KEY))
def test_pending_one(self):
self.obj.add(self.PREFIX, self.KEY, None)
self.assertEqual(self.obj.pending(self.PREFIX_CLASS, self.KEY), 1)
def test_pending_two(self):
self.obj.add(self.PREFIX, self.KEY, None)
self.obj.add(self.PREFIX, self.KEY, lambda x: True)
self.assertEqual(self.obj.pending(self.PREFIX_CLASS, self.KEY), 2)
def test_process_callback_false(self):
self.obj._stack = dict()
self.assertFalse(self.obj.process('FAIL', 'False', 'Empty',
self.mock_caller, []))
def test_process_false(self):
self.assertFalse(self.obj.process(self.PREFIX_CLASS, self.KEY, self))
def test_process_true(self):
self.obj.add(self.PREFIX, self.KEY, self.callback_mock)
self.assertTrue(self.obj.process(self.PREFIX_CLASS, self.KEY, self))
def test_process_mock_called(self):
args = (1, None, 'Hi')
self.obj.add(self.PREFIX, self.KEY, self.callback_mock)
self.obj.process(self.PREFIX, self.KEY, self, args)
self.callback_mock.assert_called_once_with(args)
def test_process_one_shot_removed(self):
args = (1, None, 'Hi')
self.obj.add(self.PREFIX, self.KEY, self.callback_mock)
self.obj.process(self.PREFIX, self.KEY, self, args)
self.assertNotIn(self.PREFIX, self.obj._stack)
def test_process_non_one_shot_prefix_not_removed(self):
self.obj.add(self.PREFIX, self.KEY, self.callback_mock, one_shot=False)
self.obj.process(self.PREFIX, self.KEY, self)
self.assertIn(self.PREFIX, self.obj._stack)
def test_process_non_one_shot_key_not_removed(self):
self.obj.add(self.PREFIX, self.KEY, self.callback_mock, one_shot=False)
self.obj.process(self.PREFIX, self.KEY, self)
self.assertIn(self.KEY, self.obj._stack[self.PREFIX])
def test_process_non_one_shot_callback_not_removed(self):
self.obj.add(self.PREFIX, self.KEY, self.callback_mock, one_shot=False)
self.obj.process(self.PREFIX, self.KEY, self)
self.assertEqual(self.obj._stack[self.PREFIX][self.KEY][0][self.CALLBACK],
self.callback_mock)
def test_process_only_caller_fails(self):
self.obj.add(self.PREFIX_CLASS, self.KEY, self.callback_mock,
only_caller=self.mock_caller)
self.obj.process(self.PREFIX_CLASS, self.KEY, self)
self.assertFalse(self.callback_mock.called)
def test_process_only_caller_fails_no_removal(self):
self.obj.add(self.PREFIX_CLASS, self.KEY, self.callback_mock,
only_caller=self.mock_caller)
self.obj.process(self.PREFIX_CLASS, self.KEY, self)
self.assertEqual(self.obj._stack[self.PREFIX][self.KEY][0][self.CALLBACK],
self.callback_mock)
def test_remove_with_no_callbacks_pending(self):
self.obj = callback.CallbackManager()
self.assertFalse(self.obj.remove(self.PREFIX, self.KEY,
self.callback_mock))
def test_remove_with_callback_true(self):
self.obj.add(self.PREFIX_CLASS, self.KEY, self.callback_mock)
self.assertTrue(self.obj.remove(self.PREFIX, self.KEY,
self.callback_mock))
def test_remove_with_callback_false(self):
self.obj.add(self.PREFIX_CLASS, self.KEY, None)
self.assertTrue(self.obj.remove(self.PREFIX, self.KEY,
self.callback_mock))
def test_remove_with_callback_true_empty_stack(self):
self.obj.add(self.PREFIX_CLASS, self.KEY, self.callback_mock)
self.obj.remove(prefix=self.PREFIX, key=self.KEY,
callback_value=self.callback_mock)
self.assertDictEqual(self.obj._stack, dict())
def test_remove_with_callback_true_non_empty_stack(self):
self.obj.add(self.PREFIX_CLASS, self.KEY, self.callback_mock)
self.obj.add(self.PREFIX_CLASS, self.KEY, self.mock_caller)
self.obj.remove(self.PREFIX, self.KEY, self.callback_mock)
self.assertEqual(self.mock_caller,
self.obj._stack[self.PREFIX][self.KEY][0][self.CALLBACK])
def test_remove_prefix_key_with_other_key_prefix_remains(self):
OTHER_KEY = 'Other Key'
self.obj.add(self.PREFIX_CLASS, self.KEY, self.callback_mock)
self.obj.add(self.PREFIX_CLASS, OTHER_KEY, self.mock_caller)
self.obj.remove(self.PREFIX, self.KEY, self.callback_mock)
self.assertIn(self.PREFIX, self.obj._stack)
def test_remove_prefix_key_with_other_key_remains(self):
OTHER_KEY = 'Other Key'
self.obj.add(self.PREFIX_CLASS, self.KEY, self.callback_mock)
self.obj.add(prefix=self.PREFIX_CLASS, key=OTHER_KEY,
callback=self.mock_caller)
self.obj.remove(self.PREFIX, self.KEY)
self.assertIn(OTHER_KEY, self.obj._stack[self.PREFIX])
def test_remove_prefix_key_with_other_key_callback_remains(self):
OTHER_KEY = 'Other Key'
self.obj.add(self.PREFIX_CLASS, self.KEY, self.callback_mock)
self.obj.add(self.PREFIX_CLASS, OTHER_KEY, self.mock_caller)
self.obj.remove(self.PREFIX, self.KEY)
self.assertEqual(self.mock_caller,
self.obj._stack[self.PREFIX][OTHER_KEY][0][self.CALLBACK])
def test_remove_all(self):
self.obj.add(self.PREFIX_CLASS, self.KEY, self.callback_mock)
self.obj.remove_all(self.PREFIX, self.KEY)
self.assertNotIn(self.PREFIX, self.obj._stack)
def test_should_process_callback_true(self):
self.obj.add(self.PREFIX_CLASS, self.KEY, self.callback_mock)
value = self.obj._callback_dict(self.callback_mock, False, None, None)
self.assertTrue(self.obj._should_process_callback(value,
self.mock_caller, []))
def test_should_process_callback_false_argument_fail(self):
self.obj.clear()
self.obj.add(self.PREFIX_CLASS, self.KEY, self.callback_mock,
arguments={'foo': 'baz'})
self.assertFalse(self.obj._should_process_callback(self._callback_dict,
self.mock_caller,
[{'foo': 'baz'}]))
def test_should_process_callback_false_only_caller_failure(self):
self.obj.add(self.PREFIX_CLASS, self.KEY, self.callback_mock)
value = self.obj._callback_dict(self.callback_mock, False, self, None)
self.assertTrue(self.obj._should_process_callback(value,
self.mock_caller, []))
def test_should_process_callback_false_only_caller_failure(self):
self.obj.add(self.PREFIX_CLASS, self.KEY, self.callback_mock)
value = self.obj._callback_dict(self.callback_mock, False,
self.mock_caller, None)
self.assertTrue(self.obj._should_process_callback(value,
self.mock_caller, []))
def test_dict(self):
self.assertDictEqual(self.obj._callback_dict(self.callback_mock,
True, self.mock_caller,
self.ARGUMENTS_VALUE),
self._callback_dict)
def test_arguments_match_no_arguments(self):
self.assertFalse(self.obj._arguments_match(self._callback_dict, []))
def test_arguments_match_dict_argument(self):
self.assertTrue(self.obj._arguments_match(self._callback_dict,
[self.ARGUMENTS_VALUE]))
def test_arguments_match_dict_argument_no_attribute(self):
self.assertFalse(self.obj._arguments_match(self._callback_dict,
[{}]))
def test_arguments_match_dict_argument_no_match(self):
self.assertFalse(self.obj._arguments_match(self._callback_dict,
[{'foo': 'baz'}]))
def test_arguments_match_obj_argument(self):
class TestObj(object):
foo = 'bar'
test_instance = TestObj()
self.assertTrue(self.obj._arguments_match(self._callback_dict,
[test_instance]))
def test_arguments_match_obj_no_attribute(self):
class TestObj(object):
qux = 'bar'
test_instance = TestObj()
self.assertFalse(self.obj._arguments_match(self._callback_dict,
[test_instance]))
def test_arguments_match_obj_argument_no_match(self):
class TestObj(object):
foo = 'baz'
test_instance = TestObj()
self.assertFalse(self.obj._arguments_match(self._callback_dict,
[test_instance]))
def test_arguments_match_obj_argument_with_method(self):
class TestFrame(object):
method = None
class MethodObj(object):
foo = 'bar'
test_instance = TestFrame()
test_instance.method = MethodObj()
self.assertTrue(self.obj._arguments_match(self._callback_dict,
[test_instance]))
def test_arguments_match_obj_argument_with_method_no_match(self):
class TestFrame(object):
method = None
class MethodObj(object):
foo = 'baz'
test_instance = TestFrame()
test_instance.method = MethodObj()
self.assertFalse(self.obj._arguments_match(self._callback_dict,
[test_instance]))
|