def test_annotate_decorator(self): from celery.app.task import Task class adX(Task): def run(self, y, z, x): return y, z, x check = Mock() def deco(fun): def _inner(*args, **kwargs): check(*args, **kwargs) return fun(*args, **kwargs) return _inner self.app.conf.task_annotations = { adX.name: {'@__call__': deco} } adX.bind(self.app) assert adX.app is self.app i = adX() i(2, 4, x=3) check.assert_called_with(i, 2, 4, x=3) i.annotate() i.annotate()
def test_on_basic_return__handled(self, error_for_code): callback = Mock(name='callback') self.c.events['basic_return'].add(callback) self.c._on_basic_return(404, 'text', 'ex', 'rkey', 'msg') callback.assert_called_with( error_for_code(), 'ex', 'rkey', 'msg', )
def test_revive_connection(self): on_revive = Mock() channel = Mock() common.revive_connection(Mock(), channel, on_revive) on_revive.assert_called_with(channel) common.revive_connection(Mock(), channel, None)
def test_A_simplejson(): with mock.reset_modules('thorn.utils.json'): from thorn.utils import json obj = Mock(name='obj') encode = Mock(name='encode') assert json.dumps(obj, encode=encode) is encode.return_value encode.assert_called_with(obj, cls=json.JsonEncoder)
def test_reverse(self): callback = Mock() x = barrier(self.ps, callback=promise(callback)) for p in self.ps: p() assert x.ready callback.assert_called_with()
def test_apply_timeout(self): self.patching.modules(*gevent_modules) class Timeout(Exception): value = None def __init__(self, value): self.__class__.value = value def __enter__(self): return self def __exit__(self, *exc_info): pass timeout_callback = Mock(name='timeout_callback') apply_target = Mock(name='apply_target') apply_timeout( Mock(), timeout=10, callback=Mock(name='callback'), timeout_callback=timeout_callback, apply_target=apply_target, Timeout=Timeout, ) assert Timeout.value == 10 apply_target.assert_called() apply_target.side_effect = Timeout(10) apply_timeout( Mock(), timeout=10, callback=Mock(), timeout_callback=timeout_callback, apply_target=apply_target, Timeout=Timeout, ) timeout_callback.assert_called_with(False, 10)
def test_enter__exit(self): P = self.hub.poller = Mock() on_close = Mock() self.hub.on_close.add(on_close) try: read_A = Mock() read_B = Mock() self.hub.add_reader(10, read_A) self.hub.add_reader(File(11), read_B) write_A = Mock() write_B = Mock() self.hub.add_writer(20, write_A) self.hub.add_writer(File(21), write_B) assert self.hub.readers assert self.hub.writers finally: assert self.hub.poller self.hub.close() assert not self.hub.readers assert not self.hub.writers P.unregister.assert_has_calls([ call(10), call(11), call(20), call(21), ], any_order=True) on_close.assert_called_with(self.hub)
def test_load(self): with patch('pkg_resources.iter_entry_points') as iterep: with patch('celery.utils.imports.symbol_by_name') as symbyname: ep = Mock() ep.name = 'ep' ep.module_name = 'foo' ep.attrs = ['bar', 'baz'] iterep.return_value = [ep] cls = symbyname.return_value = Mock() register = Mock() e = Extensions('unit', register) e.load() symbyname.assert_called_with('foo:bar') register.assert_called_with(cls, name='ep') with patch('celery.utils.imports.symbol_by_name') as symbyname: symbyname.side_effect = SyntaxError() with patch('warnings.warn') as warn: e.load() warn.assert_called() with patch('celery.utils.imports.symbol_by_name') as symbyname: symbyname.side_effect = KeyError('foo') with pytest.raises(KeyError): e.load()
def test_on_message_ready(self): channel = Mock(name='channel') msg = Mock(name='msg') callback = Mock(name='callback') self.transport._callbacks = {'q1': callback} self.transport.on_message_ready(channel, msg, queue='q1') callback.assert_called_with(msg)
def test_ppartial(): m = Mock() p = ppartial(m, 1) p() m.assert_called_with(1) p = ppartial(m, z=2) p() m.assert_called_with(z=2)
def test_apply_chord(self): header = Mock(name='header') header.results = [Mock(name='t1'), Mock(name='t2')] self.b.apply_chord( header, (1, 2), 'gid', None, options={'max_retries': 10}, ) header.assert_called_with(1, 2, max_retries=10, task_id='gid')
def test_unknown_type_filter(self): unknown_type_filter = Mock() obj = object() assert jsonify(obj, unknown_type_filter=unknown_type_filter) is unknown_type_filter.return_value unknown_type_filter.assert_called_with(obj) with pytest.raises(ValueError): jsonify(obj)
def test_enter_after(self): t = Timer() t._enter = Mock() fun = Mock(name='fun') time = Mock(name='time') time.return_value = 10 t.enter_after(10, fun, time=time) time.assert_called_with() t._enter.assert_called_with(20, 0, fun)
def test_execute_using_pool__defaults_of_hybrid_to_proto2(self): weakref_ref = Mock(name='weakref.ref') headers = strategy.hybrid_to_proto2('', {'id': uuid(), 'task': self.mytask.name})[1] job = self.zRequest(revoked_tasks=set(), ref=weakref_ref, **headers) job.execute_using_pool(self.pool) assert job._apply_result weakref_ref.assert_called_with(self.pool.apply_async()) assert job._apply_result is weakref_ref()
def assert_signal_called(signal, **expected): handler = Mock() call_handler = partial(handler) signal.connect(call_handler) try: yield handler finally: signal.disconnect(call_handler) handler.assert_called_with(signal=signal, **expected)
def test_apply_entry_error_not_handled(self, stdouts): t = Timer() t.schedule.on_error = Mock() fun = Mock() fun.side_effect = ValueError() t.schedule.apply_entry(fun) fun.assert_called_with() assert not stdouts.stderr.getvalue()
def test_wrap(): cb1 = Mock() cb2 = Mock() x = wrap(promise(cb1)) x(1, y=2) cb1.assert_called_with(1, y=2) p2 = promise(cb2) x(p2) p2() cb1.assert_called_with(cb2())
def test_qos_exceeds_16bit(self): with patch('kombu.common.logger') as logger: callback = Mock() qos = QoS(callback, 10) qos.prev = 100 # cannot use 2 ** 32 because of a bug on macOS Py2.5: # https://jira.mongodb.org/browse/PYTHON-389 qos.set(4294967296) logger.warning.assert_called() callback.assert_called_with(prefetch_count=0)
def test_run_raises_UsageError(self): cb = Mock() c = Command(on_usage_error=cb) c.verify_args = Mock() c.run = Mock() exc = c.run.side_effect = c.UsageError('foo', status=3) assert c() == exc.status cb.assert_called_with(exc) c.verify_args.assert_called_with(())
def assert_signal_called(signal, **expected): """Context that verifes signal is called before exiting.""" handler = Mock() call_handler = partial(handler) signal.connect(call_handler) try: yield handler finally: signal.disconnect(call_handler) handler.assert_called_with(signal=signal, **expected)
def test_enable_disable_callbacks(self): on_enable = Mock() on_disable = Mock() with self.app.connection_for_write() as conn: with self.app.events.Dispatcher(conn, enabled=False) as d: d.on_enabled.add(on_enable) d.on_disabled.add(on_disable) d.enable() on_enable.assert_called_with() d.disable() on_disable.assert_called_with()
def test_encode_payload(self): data = Mock(name='data') codec = Mock(name='codec') self._app.settings.THORN_CODECS = { 'application/foo': codec, } assert self.dispatcher.encode_payload( data, 'application/x-mooOOO') is data assert self.dispatcher.encode_payload( data, 'application/foo') is codec.return_value codec.assert_called_with(data)
def test_filter(self): callback = Mock() filt = filter_callback(callback, ["add", "mul"]) t1 = {"task": "add"} t2 = {"task": "div"} message = Mock() filt(t2, message) callback.assert_not_called() filt(t1, message) callback.assert_called_with(t1, message)
def test_move_transform(self): trans = Mock(name="transform") trans.return_value = Queue("bar") with self.move_context(transform=trans) as (callback, pred, republish): pred.return_value = "foo" body, message = self.msgpair() with patch("celery.contrib.migrate.maybe_declare") as maybed: callback(body, message) trans.assert_called_with("foo") maybed.assert_called() republish.assert_called()
def test_poll_writable(self): x = X(self.app) writer = Mock(name='writer') x.hub.add_writer(6, writer, 6) x.hub.on_tick.add(x.close_then_error(Mock(name='tick'), 2)) poller = x.hub.poller poller.poll.return_value = [(6, WRITE)] with pytest.raises(socket.error): asynloop(*x.args) writer.assert_called_with(6) poller.poll.assert_called()
def test_poll_err_readable(self): x = X(self.app) reader = Mock(name='reader') x.hub.add_reader(6, reader, 6, 24) x.hub.on_tick.add(x.close_then_error(Mock(), 2)) poller = x.hub.poller poller.poll.return_value = [(6, ERR)] with pytest.raises(socket.error): asynloop(*x.args) reader.assert_called_with(6, 24) poller.poll.assert_called()
def test_filter(self): callback = Mock() filt = filter_callback(callback, ['add', 'mul']) t1 = {'task': 'add'} t2 = {'task': 'div'} message = Mock() filt(t2, message) callback.assert_not_called() filt(t1, message) callback.assert_called_with(t1, message)
def test_transform(): callback = Mock() def filter_key_value(key, filter_, mapping): return filter_(mapping[key]) x = transform(filter_key_value, promise(callback), 'Value', int) x({'Value': 303}) callback.assert_called_with(303) with pytest.raises(KeyError): x({})
def test_wait_for__on_interval(self): self.patching('time.sleep') b = BaseBackend(app=self.app) b._get_task_meta_for = Mock() b._get_task_meta_for.return_value = {'status': states.PENDING} callback = Mock(name='callback') with pytest.raises(TimeoutError): b.wait_for(task_id='1', on_interval=callback, timeout=1) callback.assert_called_with() b._get_task_meta_for.return_value = {'status': states.SUCCESS} b.wait_for(task_id='1', timeout=None)
def test_receive_callback_accept(self): message = Mock(name='Message') message.errors = [] callback = Mock(name='on_message') c = Consumer(self.connection, accept=['json'], on_message=callback) c.on_decode_error = None c.channel = Mock(name='channel') c.channel.message_to_python = None c._receive_callback(message) callback.assert_called_with(message) assert message.accept == c.accept