本文整理汇总了Python中unittest.mock.MagicMock.assert_has_calls方法的典型用法代码示例。如果您正苦于以下问题:Python MagicMock.assert_has_calls方法的具体用法?Python MagicMock.assert_has_calls怎么用?Python MagicMock.assert_has_calls使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类unittest.mock.MagicMock
的用法示例。
在下文中一共展示了MagicMock.assert_has_calls方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: CallableRecursionTest
# 需要导入模块: from unittest.mock import MagicMock [as 别名]
# 或者: from unittest.mock.MagicMock import assert_has_calls [as 别名]
class CallableRecursionTest(DBConBase):
"""Callable recursion test."""
def setUp(self):
"""Setup."""
super(CallableRecursionTest, self).setUp()
self.check_depth = MagicMock(
side_effect=lambda doc, cur_depth: doc.is_last
)
class TestDoc(gj.Document):
"""Test document."""
name = db.StringField()
is_last = db.BooleanField()
ref = gj.FollowReferenceField(
"self", max_depth=self.check_depth
)
def __str__(self):
"""Return stringified summary."""
return ("TestDoc<ID: {}, is_last: {}, ref: {}>").format(
self.name, self.is_last, getattr(self.ref, "id", None)
)
def to_dict(self):
"""Return the dict."""
return str(self.id) if self.is_last else {
"id": str(self.id),
"name": self.name,
"is_last": self.is_last,
"ref": self.ref.to_dict()
}
self.docs = [
TestDoc.objects.create(
name=("Test {}").format(count),
is_last=(count == 4)
) for count in range(6)
]
for (index, doc) in enumerate(self.docs):
doc.ref = self.docs[(index + 1) % len(self.docs)]
doc.save()
# Needs to reset mock because doc.save calls to_mongo
self.check_depth.reset_mock()
def test_from_first(self):
"""Should encode the element from first to the element is_last=True."""
max_depth_level = 4
correct_data = self.docs[0].to_dict()
actual_data = json.loads(self.docs[0].to_json())
self.maxDiff = None
self.assertEqual(correct_data, actual_data)
self.assertEqual(self.check_depth.call_count, max_depth_level)
self.check_depth.assert_has_calls([
call(self.docs[count], count - 1)
for count in range(1, max_depth_level + 1)
])
示例2: test_startFeedProgramAndScheduledOffProgramOnToggleTimer
# 需要导入模块: from unittest.mock import MagicMock [as 别名]
# 或者: from unittest.mock.MagicMock import assert_has_calls [as 别名]
def test_startFeedProgramAndScheduledOffProgramOnToggleTimer(self):
startProgramMock = MagicMock()
self.eventHelper.startProgram = startProgramMock
self.eventHelper._programIndex = 0
with patch('flicintegration.eventhelper.startProgram', new=startProgramMock):
self.eventHelper.handleEvent(EventHelper.eventTypes["toggleTimer"])
startProgramMock.assert_has_calls([call("feed"), call("scheduledOff", {"duration": 600})], False)
示例3: test_command_removed1
# 需要导入模块: from unittest.mock import MagicMock [as 别名]
# 或者: from unittest.mock.MagicMock import assert_has_calls [as 别名]
def test_command_removed1(command):
mock = MagicMock()
command.end = lambda: mock("end")
command.interrupted = lambda: mock("interrupted")
command.removed()
mock.assert_has_calls([])
示例4: test_scheduler_run1
# 需要导入模块: from unittest.mock import MagicMock [as 别名]
# 或者: from unittest.mock.MagicMock import assert_has_calls [as 别名]
def test_scheduler_run1(scheduler):
# calls buttons, last in called first
mock = MagicMock()
scheduler.addButton(lambda: mock(1))
scheduler.addButton(lambda: mock(2))
scheduler.run()
mock.assert_has_calls([call(2), call(1)], any_order=False)
示例5: test_command_run1
# 需要导入模块: from unittest.mock import MagicMock [as 别名]
# 或者: from unittest.mock.MagicMock import assert_has_calls [as 别名]
def test_command_run1(command, enable_robot):
mock = MagicMock()
command.initialize = lambda: mock("initialize")
command.execute = lambda: mock("execute")
command.startRunning()
command.run()
mock.assert_has_calls([call("initialize"), call("execute")])
示例6: test_lock_with_lock_poll_interval
# 需要导入模块: from unittest.mock import MagicMock [as 别名]
# 或者: from unittest.mock.MagicMock import assert_has_calls [as 别名]
def test_lock_with_lock_poll_interval(self):
lock_poll_interval_generator = MagicMock(return_value=DEFAULT_LOCK_POLL_INTERVAL_GENERATOR(1) / 4)
lock_result = action_when_locked(
TestConsulLockManager._build_executor(Action.LOCK, action_kwargs=dict(
lock_poll_interval_generator=lock_poll_interval_generator)),
timeout=DEFAULT_LOCK_POLL_INTERVAL_GENERATOR(1) / 1.5)
assert isinstance(lock_result.exception, TestActionTimeoutError)
lock_poll_interval_generator.assert_has_calls([call(1), call(2)])
示例7: test_command_run3
# 需要导入模块: from unittest.mock import MagicMock [as 别名]
# 或者: from unittest.mock.MagicMock import assert_has_calls [as 别名]
def test_command_run3(command):
mock = MagicMock()
command.initialize = lambda: mock("initialize")
command.execute = lambda: mock("execute")
command.startRunning()
command.cancel()
command.run()
mock.assert_has_calls([])
示例8: test_command_removed2
# 需要导入模块: from unittest.mock import MagicMock [as 别名]
# 或者: from unittest.mock.MagicMock import assert_has_calls [as 别名]
def test_command_removed2(command, enable_robot):
mock = MagicMock()
command.end = lambda: mock("end")
command.interrupted = lambda: mock("interrupted")
command.startRunning()
command.run()
command.removed()
mock.assert_has_calls([call("end")])
示例9: test_run
# 需要导入模块: from unittest.mock import MagicMock [as 别名]
# 或者: from unittest.mock.MagicMock import assert_has_calls [as 别名]
def test_run(self):
cycles = 10
listener = MagicMock()
self.logger.add = MagicMock()
self.retrieval_manager.add_listener(listener)
self._setup_to_do_n_cycles(cycles, self.updates)
self.assertEqual(self.logger.record.call_count, cycles)
listener.assert_has_calls([call(self.updates) for _ in range(cycles)])
示例10: TestEventTarget
# 需要导入模块: from unittest.mock import MagicMock [as 别名]
# 或者: from unittest.mock.MagicMock import assert_has_calls [as 别名]
class TestEventTarget(TestCase):
def setUp(self):
self.target = EventTarget()
self.mock = MagicMock(_is_coroutine=False)
self.e = Event("click")
def test_event_dispatch(self):
self.target.addEventListener("click", self.mock)
self.assertEqual(len(self.target._listeners), 1)
self.target.dispatchEvent(self.e)
self.mock.assert_called_once_with(self.e)
def test_event_dispatch_empty(self):
self.target.dispatchEvent(self.e)
self.mock.assert_not_called()
def test_event_dispatch_multi(self):
e1 = Event("click")
e2 = Event("click")
self.target.addEventListener("click", self.mock)
self.target.dispatchEvent(e1)
self.target.dispatchEvent(e2)
self.assertEqual(self.mock.call_count, 2)
self.mock.assert_has_calls([call(e1), call(e2)])
def test_defferent_event_dispatch(self):
mock1 = MagicMock(_is_coroutine=False)
mock2 = MagicMock(_is_coroutine=False)
e1 = Event("click")
e2 = Event("event")
self.target.addEventListener("click", mock1)
self.target.addEventListener("event", mock2)
self.assertEqual(len(self.target._listeners), 2)
self.target.dispatchEvent(e1)
mock1.assert_called_once_with(e1)
mock2.assert_not_called()
self.target.dispatchEvent(e2)
mock1.assert_called_once_with(e1)
mock2.assert_called_once_with(e2)
def test_remove_event(self):
self.target.addEventListener("click", self.mock)
self.target.removeEventListener("click", self.mock)
self.target.dispatchEvent(self.e)
self.mock.assert_not_called()
def test_remove_event_multi(self):
self.target.addEventListener("click", self.mock)
self.assertEqual(len(self.target._listeners), 1)
self.target.removeEventListener("click", self.mock)
self.assertEqual(len(self.target._listeners), 0)
self.target.dispatchEvent(self.e)
self.mock.assert_not_called()
示例11: test_sqs_message_processor
# 需要导入模块: from unittest.mock import MagicMock [as 别名]
# 或者: from unittest.mock.MagicMock import assert_has_calls [as 别名]
def test_sqs_message_processor(self):
mock = MagicMock()
mock.body = '{"Records":[{"s3":{"bucket":{"name":"name"},"object":{"key":"key"}}}]}'
processor = SQSMessageProcessor()
with patch.object(SQSMessageProcessor, 'processor_class', create=True) as mock_processor_class:
processor.process(mock)
mock_processor_class.assert_has_calls([
call(),
call().process({'Records': [{'s3': {'bucket': {'name': 'name'}, 'object': {'key': 'key'}}}]})])
mock.assert_has_calls([call.delete()])
示例12: test_should_convert_url_set_to_sources
# 需要导入模块: from unittest.mock import MagicMock [as 别名]
# 或者: from unittest.mock.MagicMock import assert_has_calls [as 别名]
def test_should_convert_url_set_to_sources(self):
number_of_urls = 4
urls_in_set = [self.url + '-{0}'.format(i) for i in range(number_of_urls)]
urlset_func = MagicMock(return_value=urls_in_set)
urlsrcs_for_set = [MagicMock() for url in urls_in_set]
urlsrc_func = MagicMock(side_effect=urlsrcs_for_set)
converter = source.SourceConverter(urlsrc_func, urlset_func)
srcs = converter.to_sources(self.url)
urlset_func.assert_called_with(self.url)
urlsrc_func.assert_has_calls([call(url) for url in urls_in_set])
self.assertEqual(srcs, urlsrcs_for_set)
示例13: test_scheduler_run2
# 需要导入模块: from unittest.mock import MagicMock [as 别名]
# 或者: from unittest.mock.MagicMock import assert_has_calls [as 别名]
def test_scheduler_run2(scheduler, wpilib):
# calls subsystems, no order
mock = MagicMock()
subsystem1 = wpilib.command.Subsystem()
subsystem1.periodic = lambda: mock(1)
subsystem2 = wpilib.command.Subsystem()
subsystem2.periodic = lambda: mock(2)
scheduler.registerSubsystem(subsystem1)
scheduler.registerSubsystem(subsystem2)
scheduler.run()
mock.assert_has_calls([call(1), call(2)], any_order=True)
示例14: ReduceWithoutInitTest
# 需要导入模块: from unittest.mock import MagicMock [as 别名]
# 或者: from unittest.mock.MagicMock import assert_has_calls [as 别名]
class ReduceWithoutInitTest(ut.TestCase):
"""Test for reduce(fn, it) test."""
def setUp(self):
"""Setup."""
self.func = MagicMock(
side_effect=lambda x, y, index: y
)
self.data = [1, 2, 3, 4]
def test_call_reduce(self):
"""The function should be called properly."""
omm.helper.reduce_with_index(self.func, self.data)
self.assertEqual(self.func.call_count, 3)
self.func.assert_has_calls([
call(1, 2, 0), call(2, 3, 1), call(3, 4, 2)
])
示例15: test_that_log_it_logs_input_and_output_correctly
# 需要导入模块: from unittest.mock import MagicMock [as 别名]
# 或者: from unittest.mock.MagicMock import assert_has_calls [as 别名]
def test_that_log_it_logs_input_and_output_correctly():
mock_logger = MagicMock()
result = chain(
5,
add_two,
multiply_by_two,
wrap_with=log_it(mock_logger)
)
mock_logger.assert_has_calls(
[
call('add_two called with args: (5,) and kwargs {}'),
call('add_two returned result 7'),
call('unknown function name; probably a lambda or partially applied function... '
'called with args: (7,) and kwargs {}'),
call('unknown function name; probably a lambda or partially applied function... returned result 14')
])