本文整理汇总了Python中unittest.mock.Mock.assert_has_calls方法的典型用法代码示例。如果您正苦于以下问题:Python Mock.assert_has_calls方法的具体用法?Python Mock.assert_has_calls怎么用?Python Mock.assert_has_calls使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类unittest.mock.Mock
的用法示例。
在下文中一共展示了Mock.assert_has_calls方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_normal
# 需要导入模块: from unittest.mock import Mock [as 别名]
# 或者: from unittest.mock.Mock import assert_has_calls [as 别名]
def test_normal(self):
decode = Mock(return_value="test123")
part = self.create_part("text", "utf-8", decode)
self.parts.append(part)
result = pad.message.Message._iter_parts(self.msg)
self.assertEqual(list(result), [(u"test123", part)])
decode.assert_has_calls([call("utf-8", "ignore")])
示例2: test_strict_charset
# 需要导入模块: from unittest.mock import Mock [as 别名]
# 或者: from unittest.mock.Mock import assert_has_calls [as 别名]
def test_strict_charset(self):
decode = Mock(return_value="test123")
part = self.create_part("text", "quopri", decode)
self.parts.append(part)
result = pad.message.Message._iter_parts(self.msg)
self.assertEqual(list(result), [(u"test123", part)])
decode.assert_has_calls([call("quopri", "strict")])
示例3: test_event_handle_registation_with_list_of_strings
# 需要导入模块: from unittest.mock import Mock [as 别名]
# 或者: from unittest.mock.Mock import assert_has_calls [as 别名]
def test_event_handle_registation_with_list_of_strings(self):
func_mock = Mock()
handler("foo", "bar")(func_mock)
event1 = self._call_handlers("foo.bar", {"data": "foo"}) # handled
event2 = self._call_handlers("bar.foo", {"data": "bar"}) # handled
self.assertEqual(2, func_mock.call_count)
func_mock.assert_has_calls([call(event=event1), call(event=event2)])
示例4: test_form_valid
# 需要导入模块: from unittest.mock import Mock [as 别名]
# 或者: from unittest.mock.Mock import assert_has_calls [as 别名]
def test_form_valid(self, existing):
instance = sqlalchemy.ModelFormMixin()
if existing:
instance.object = obj = Mock()
instance.model = Mock()
form = Mock()
mocks = Mock()
with patch.object(sqlalchemy, 'session') as m1:
with patch.object(sqlalchemy.FormMixin, 'form_valid') as m2:
with patch.object(sqlalchemy, '_touch') as m3:
mocks.attach_mock(m1, 'session')
mocks.attach_mock(form, 'form')
mocks.attach_mock(m3, 'touch')
assert instance.form_valid(form) == m2.return_value
m2.assert_called_once_with(form)
calls = [call.form.populate_obj(instance.object),
call.session.commit(),
call.touch(instance.object)]
if not existing:
assert instance.object == instance.model.return_value
calls.insert(0, call.session.add(instance.object))
else:
assert instance.object == obj
mocks.assert_has_calls(calls)
示例5: test_normal_send
# 需要导入模块: from unittest.mock import Mock [as 别名]
# 或者: from unittest.mock.Mock import assert_has_calls [as 别名]
def test_normal_send(self):
""" Test normal messages sending """
socket_mock = Mock()
logger_mock = Mock()
output_wrapper = Output(socket_mock, logger_mock)
output_wrapper.info('info(%s)', 'arg1')
output_wrapper.debug('debug(%s)', 'arg2')
output_wrapper.error('error(%s)', 'arg3')
output_wrapper.error('no_args_error(%s)')
output_wrapper.log('log(%s)', 'arg4')
output_wrapper.say('say(%s)', 'arg5')
output_wrapper.say('no_args_say(%s)')
socket_mock.assert_has_calls([
call.send(b'{"text": "info(arg1)"}\x00'),
call.send(b'{"error": "error(arg3)"}\x00'),
call.send(b'{"error": "no_args_error(%s)"}\x00'),
call.send(b'{"text": "log(arg4)"}\x00'),
call.send(b'{"text": "say(arg5)"}\x00'),
call.send(b'{"text": "no_args_say(%s)"}\x00'),
])
logger_mock.assert_has_calls([
call.info('info(%s)', 'arg1'),
call.debug('debug(%s)', 'arg2'),
call.error('error(%s)', 'arg3'),
call.error('no_args_error(%s)'),
call.info('log(%s)', 'arg4'),
call.info('say(%s)', 'arg5'),
call.info('no_args_say(%s)'),
])
示例6: test_scan_videos
# 需要导入模块: from unittest.mock import Mock [as 别名]
# 或者: from unittest.mock.Mock import assert_has_calls [as 别名]
def test_scan_videos(movies, tmpdir, monkeypatch):
man_of_steel = tmpdir.ensure('movies', movies['man_of_steel'].name)
tmpdir.ensure('movies', '.private', 'sextape.mkv')
tmpdir.ensure('movies', '.hidden_video.mkv')
tmpdir.ensure('movies', movies['enders_game'].name)
tmpdir.ensure('movies', movies['interstellar'].name)
tmpdir.ensure('movies', os.path.splitext(movies['enders_game'].name)[0] + '.nfo')
tmpdir.ensure('movies', 'watched', dir=True)
tmpdir.join('movies', 'watched', os.path.split(movies['man_of_steel'].name)[1]).mksymlinkto(man_of_steel)
# mock scan_video and scan_archive with the correct types
mock_video = Mock(subtitle_languages=set())
mock_scan_video = Mock(return_value=mock_video)
monkeypatch.setattr('subliminal.core.scan_video', mock_scan_video)
mock_scan_archive = Mock(return_value=mock_video)
monkeypatch.setattr('subliminal.core.scan_archive', mock_scan_archive)
monkeypatch.chdir(str(tmpdir))
videos = scan_videos('movies')
# general asserts
assert len(videos) == 3
assert mock_scan_video.call_count == 2
assert mock_scan_archive.call_count == 1
# scan_video calls
kwargs = dict()
scan_video_calls = [((os.path.join('movies', movies['man_of_steel'].name),), kwargs),
((os.path.join('movies', movies['enders_game'].name),), kwargs)]
mock_scan_video.assert_has_calls(scan_video_calls, any_order=True)
# scan_archive calls
kwargs = dict()
scan_archive_calls = [((os.path.join('movies', movies['interstellar'].name),), kwargs)]
mock_scan_archive.assert_has_calls(scan_archive_calls, any_order=True)
示例7: test_the_pump_should_dispatch_a_command_processor
# 需要导入模块: from unittest.mock import Mock [as 别名]
# 或者: from unittest.mock.Mock import assert_has_calls [as 别名]
def test_the_pump_should_dispatch_a_command_processor(self):
"""
Given that I have a message pump for a channel
When I read a message from that channel
Then the message should be dispatched to a handler
"""
handler = MyCommandHandler()
request = MyCommand()
channel = Mock(spec=Channel)
command_processor = Mock(spec=CommandProcessor)
message_pump = MessagePump(command_processor, channel, map_to_message)
header = BrightsideMessageHeader(uuid4(), request.__class__.__name__, BrightsideMessageType.command)
body = BrightsideMessageBody(JsonRequestSerializer(request=request).serialize_to_json(),
BrightsideMessageBodyType.application_json)
message = BrightsideMessage(header, body)
quit_message = BrightsideMessageFactory.create_quit_message()
# add messages to that when channel is called it returns first message then qui tmessage
response_queue = [message, quit_message]
channel_spec = {"receive.side_effect" : response_queue}
channel.configure_mock(**channel_spec)
message_pump.run()
channel_calls = [call.receive(), call.receive]
channel.assert_has_calls(channel_calls)
cp_calls = [call.send(request)]
command_processor.assert_has_calls(cp_calls)
示例8: test_error_all
# 需要导入模块: from unittest.mock import Mock [as 别名]
# 或者: from unittest.mock.Mock import assert_has_calls [as 别名]
def test_error_all(self):
decode = Mock(side_effect=UnicodeError)
part = self.create_part("text", "invalid", decode)
self.parts.append(part)
result = pad.message.Message._iter_parts(self.msg)
self.assertEqual(list(result), [])
decode.assert_has_calls([call("invalid", "ignore"),
call("ascii", "ignore")])
示例9: setup
# 需要导入模块: from unittest.mock import Mock [as 别名]
# 或者: from unittest.mock.Mock import assert_has_calls [as 别名]
class TestXZeroRelatedMethods:
def setup(self):
self.nemf = 3
self.nobs = 100
self.nfac = 4
self.order_X_zeros = 2
self.factors = ['f1', 'f2', 'f3', 'f4']
def test_initial_X_zero(self):
res1, res2 = smo._initial_X_zero(self)
aae(res1, np.zeros((100, 3, 4)))
aae(res2, np.zeros((300, 4)))
def test_that_initial_X_zeros_are_views_on_same_memory(self):
res1, res2 = smo._initial_X_zero(self)
res1[:] = 1
aae(res2, np.ones((300, 4)))
def test_X_zero_filler(self):
aae(smo._X_zero_filler(self), np.zeros((3, 4)))
def test_params_slice_for_X_zero(self):
self._general_params_slice = Mock()
smo._params_slice_for_X_zero(self, params_type='short')
self._general_params_slice.assert_has_calls([call(12)])
def test_x_zero_replacements(self):
expected = [[(1, 2), (0, 2)], [(2, 2), (1, 2)]]
assert_equal(smo._X_zero_replacements(self), expected)
def test_set_bounds_for_X_zero(self):
self.lower_bound = np.empty(100, dtype=object)
self.lower_bound[:] = None
params_slice = slice(10, 22)
expected = self.lower_bound.copy()
expected[[16, 20]] = 0
smo._set_bounds_for_X_zero(self, params_slice=params_slice)
aae(self.lower_bound, expected)
def test_x_zero_names_short_params(self):
expected = [
'X_zero__0__f1', 'X_zero__0__f2', 'X_zero__0__f3', 'X_zero__0__f4',
'X_zero__1__f1', 'X_zero__1__f2', 'diff_X_zero__1__f3',
'X_zero__1__f4',
'X_zero__2__f1', 'X_zero__2__f2', 'diff_X_zero__2__f3',
'X_zero__2__f4']
assert_equal(smo._X_zero_names(self, params_type='short'), expected)
def test_x_zero_names_long_params(self):
expected = [
'X_zero__0__f1', 'X_zero__0__f2', 'X_zero__0__f3', 'X_zero__0__f4',
'X_zero__1__f1', 'X_zero__1__f2', 'X_zero__1__f3', 'X_zero__1__f4',
'X_zero__2__f1', 'X_zero__2__f2', 'X_zero__2__f3', 'X_zero__2__f4']
assert_equal(smo._X_zero_names(self, params_type='long'), expected)
示例10: test_call_should_retry_on_token_error
# 需要导入模块: from unittest.mock import Mock [as 别名]
# 或者: from unittest.mock.Mock import assert_has_calls [as 别名]
def test_call_should_retry_on_token_error(self):
transport = Mock(side_effect=[errors.InvalidAuthToken(), None])
client = BaseAPIClient(transport, None, None, None)
client._authenticate = Mock()
client.login("foo", "bar")
client("method")
client._authenticate.assert_called_with()
transport.assert_has_calls([call("method"), call("method")])
示例11: test_event_subtype_handler_registration
# 需要导入模块: from unittest.mock import Mock [as 别名]
# 或者: from unittest.mock.Mock import assert_has_calls [as 别名]
def test_event_subtype_handler_registration(self):
global_func_mock = Mock()
handler_all()(global_func_mock)
func_mock = Mock()
handler("foo.bar")(func_mock)
event1 = self._call_handlers("foo.bar", {"data": "foo"}) # handled
event2 = self._call_handlers("foo.bar.wib", {"data": "foo"}) # handled
self._call_handlers("foo.baz", {"data": "foo"}) # not handled
self.assertEqual(3, global_func_mock.call_count) # called each time
self.assertEqual(2, func_mock.call_count)
func_mock.assert_has_calls([call(event=event1), call(event=event2)])
示例12: test_error
# 需要导入模块: from unittest.mock import Mock [as 别名]
# 或者: from unittest.mock.Mock import assert_has_calls [as 别名]
def test_error(self):
def _decode(c, e):
if c == "invalid":
raise LookupError()
return "test123"
decode = Mock(side_effect=_decode)
part = self.create_part("text", "invalid", decode)
self.parts.append(part)
result = pad.message.Message._iter_parts(self.msg)
self.assertEqual(list(result), [(u"test123", part)])
decode.assert_has_calls([call("invalid", "ignore"),
call("ascii", "ignore")])
示例13: test_formatter_format_exception
# 需要导入模块: from unittest.mock import Mock [as 别名]
# 或者: from unittest.mock.Mock import assert_has_calls [as 别名]
def test_formatter_format_exception(default_settings, php_view):
from codeformatter import formatter
fill_module_mocks(formatter, default_settings)
mf_format = Mock(side_effect=Exception('fake_exception'))
mf_clean = Mock()
mf_php.return_value.format = mf_format
f_instance = formatter.Formatter(php_view)
f_instance.clean = mf_clean
f_instance.format('')
mf_clean.assert_has_calls([call(''), call('fake_exception')])
示例14: test_with_cursor
# 需要导入模块: from unittest.mock import Mock [as 别名]
# 或者: from unittest.mock.Mock import assert_has_calls [as 别名]
def test_with_cursor(self, seek_cursor, get_next):
''' test when cursor exists '''
parent = Mock()
parent.attach_mock(seek_cursor, 'seek_cursor')
parent.attach_mock(get_next, 'get_next')
self.client(cursor=sentinel.cursor)
# seeks to the cursor and skips first
parent.assert_has_calls([
call.seek_cursor(sentinel.cursor),
call.get_next(),
])
示例15: test_disconnected_client
# 需要导入模块: from unittest.mock import Mock [as 别名]
# 或者: from unittest.mock.Mock import assert_has_calls [as 别名]
def test_disconnected_client(self):
""" Test BrokenPipeError handling on unexpected client disconnect """
socket_mock = Mock()
socket_mock.send.side_effect = [None, BrokenPipeError]
logger_mock = Mock()
output_wrapper = Output(socket_mock, logger_mock)
output_wrapper.info('info(%d)', 1)
output_wrapper.info('info(%d)', 2)
output_wrapper.info('info(%d)', 3)
socket_mock.assert_has_calls([call.send(b'{"text": "info(1)"}\x00'), call.send(b'{"text": "info(2)"}\x00')])
logger_mock.warning.assert_has_calls([call('Control client disconnected before operation has completed')])
logger_mock.info.assert_has_calls([call('info(%d)', 1), call('info(%d)', 2), call('info(%d)', 3)])