本文整理汇总了Python中unittest.mock.called方法的典型用法代码示例。如果您正苦于以下问题:Python mock.called方法的具体用法?Python mock.called怎么用?Python mock.called使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类unittest.mock
的用法示例。
在下文中一共展示了mock.called方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_140_pool_listdrivers
# 需要导入模块: from unittest import mock [as 别名]
# 或者: from unittest.mock import called [as 别名]
def test_140_pool_listdrivers(self, mock_parameters, mock_drivers):
self.app.pools = ['file', 'lvm']
mock_drivers.return_value = ['driver1', 'driver2']
mock_parameters.side_effect = \
lambda driver: {
'driver1': ['param1', 'param2'],
'driver2': ['param3', 'param4']
}[driver]
value = self.call_mgmt_func(b'admin.pool.ListDrivers', b'dom0')
self.assertEqual(value,
'driver1 param1 param2\ndriver2 param3 param4\n')
self.assertEqual(mock_drivers.mock_calls, [unittest.mock.call()])
self.assertEqual(mock_parameters.mock_calls,
[unittest.mock.call('driver1'), unittest.mock.call('driver2')])
self.assertFalse(self.app.save.called)
示例2: test_152_pool_info_included_in
# 需要导入模块: from unittest import mock [as 别名]
# 或者: from unittest.mock import called [as 别名]
def test_152_pool_info_included_in(self):
self.app.pools = {
'pool1': unittest.mock.MagicMock(config={
'param1': 'value1',
'param2': 'value2'},
usage=102400,
size=204800)
}
self.app.pools['pool1'].included_in.return_value = \
self.app.pools['pool1']
self.app.pools['pool1'].__str__.return_value = 'pool1'
value = self.call_mgmt_func(b'admin.pool.Info', b'dom0', b'pool1')
self.assertEqual(value,
'param1=value1\nparam2=value2\nsize=204800\nusage=102400'
'\nincluded_in=pool1\n')
self.assertFalse(self.app.save.called)
示例3: test_153_pool_usage
# 需要导入模块: from unittest import mock [as 别名]
# 或者: from unittest.mock import called [as 别名]
def test_153_pool_usage(self):
self.app.pools = {
'pool1': unittest.mock.Mock(config={
'param1': 'value1', 'param2': 'value2'},
usage_details={
'data_usage': 102400,
'data_size': 204800,
'metadata_size': 1024,
'metadata_usage': 50})
}
self.app.pools['pool1'].included_in.return_value = None
value = self.call_mgmt_func(b'admin.pool.UsageDetails', b'dom0', b'pool1')
self.assertEqual(value,
'data_size=204800\ndata_usage=102400\nmetadata_size=1024\nmetadata_usage=50\n')
self.assertFalse(self.app.save.called)
示例4: test_160_pool_add
# 需要导入模块: from unittest import mock [as 别名]
# 或者: from unittest.mock import called [as 别名]
def test_160_pool_add(self, mock_parameters, mock_drivers):
self.app.pools = {
'file': unittest.mock.Mock(),
'lvm': unittest.mock.Mock()
}
mock_drivers.return_value = ['driver1', 'driver2']
mock_parameters.side_effect = \
lambda driver: {
'driver1': ['param1', 'param2'],
'driver2': ['param3', 'param4']
}[driver]
add_pool_mock, self.app.add_pool = self.coroutine_mock()
value = self.call_mgmt_func(b'admin.pool.Add', b'dom0', b'driver1',
b'name=test-pool\nparam1=some-value\n')
self.assertIsNone(value)
self.assertEqual(mock_drivers.mock_calls, [unittest.mock.call()])
self.assertEqual(mock_parameters.mock_calls,
[unittest.mock.call('driver1')])
self.assertEqual(add_pool_mock.mock_calls,
[unittest.mock.call(name='test-pool', driver='driver1',
param1='some-value')])
self.assertTrue(self.app.save.called)
示例5: test_160_pool_add_invalid_driver
# 需要导入模块: from unittest import mock [as 别名]
# 或者: from unittest.mock import called [as 别名]
def test_160_pool_add_invalid_driver(self, mock_parameters, mock_drivers):
self.app.pools = {
'file': unittest.mock.Mock(),
'lvm': unittest.mock.Mock()
}
mock_drivers.return_value = ['driver1', 'driver2']
mock_parameters.side_effect = \
lambda driver: {
'driver1': ['param1', 'param2'],
'driver2': ['param3', 'param4']
}[driver]
add_pool_mock, self.app.add_pool = self.coroutine_mock()
with self.assertRaises(qubes.api.PermissionDenied):
self.call_mgmt_func(b'admin.pool.Add', b'dom0',
b'no-such-driver', b'name=test-pool\nparam1=some-value\n')
self.assertEqual(mock_drivers.mock_calls, [unittest.mock.call()])
self.assertEqual(mock_parameters.mock_calls, [])
self.assertEqual(add_pool_mock.mock_calls, [])
self.assertFalse(self.app.save.called)
示例6: test_160_pool_add_invalid_param
# 需要导入模块: from unittest import mock [as 别名]
# 或者: from unittest.mock import called [as 别名]
def test_160_pool_add_invalid_param(self, mock_parameters, mock_drivers):
self.app.pools = {
'file': unittest.mock.Mock(),
'lvm': unittest.mock.Mock()
}
mock_drivers.return_value = ['driver1', 'driver2']
mock_parameters.side_effect = \
lambda driver: {
'driver1': ['param1', 'param2'],
'driver2': ['param3', 'param4']
}[driver]
add_pool_mock, self.app.add_pool = self.coroutine_mock()
with self.assertRaises(qubes.exc.QubesException):
self.call_mgmt_func(b'admin.pool.Add', b'dom0',
b'driver1', b'name=test-pool\nparam3=some-value\n')
self.assertEqual(mock_drivers.mock_calls, [unittest.mock.call()])
self.assertEqual(mock_parameters.mock_calls,
[unittest.mock.call('driver1')])
self.assertEqual(add_pool_mock.mock_calls, [])
self.assertFalse(self.app.save.called)
示例7: test_160_pool_add_existing_pool
# 需要导入模块: from unittest import mock [as 别名]
# 或者: from unittest.mock import called [as 别名]
def test_160_pool_add_existing_pool(self, mock_parameters, mock_drivers):
self.app.pools = {
'file': unittest.mock.Mock(),
'lvm': unittest.mock.Mock()
}
mock_drivers.return_value = ['driver1', 'driver2']
mock_parameters.side_effect = \
lambda driver: {
'driver1': ['param1', 'param2'],
'driver2': ['param3', 'param4']
}[driver]
add_pool_mock, self.app.add_pool = self.coroutine_mock()
with self.assertRaises(qubes.api.PermissionDenied):
self.call_mgmt_func(b'admin.pool.Add', b'dom0',
b'driver1', b'name=file\nparam1=value\nparam2=some-value\n')
self.assertEqual(mock_drivers.mock_calls, [unittest.mock.call()])
self.assertEqual(mock_parameters.mock_calls, [])
self.assertEqual(add_pool_mock.mock_calls, [])
self.assertFalse(self.app.save.called)
示例8: test_160_pool_add_invalid_config_format
# 需要导入模块: from unittest import mock [as 别名]
# 或者: from unittest.mock import called [as 别名]
def test_160_pool_add_invalid_config_format(self, mock_parameters,
mock_drivers):
self.app.pools = {
'file': unittest.mock.Mock(),
'lvm': unittest.mock.Mock()
}
mock_drivers.return_value = ['driver1', 'driver2']
mock_parameters.side_effect = \
lambda driver: {
'driver1': ['param1', 'param2'],
'driver2': ['param3', 'param4']
}[driver]
add_pool_mock, self.app.add_pool = self.coroutine_mock()
with self.assertRaises(qubes.api.PermissionDenied):
self.call_mgmt_func(b'admin.pool.Add', b'dom0',
b'driver1', b'name=test-pool\nparam 1=value\n_param2\n')
self.assertEqual(mock_drivers.mock_calls, [unittest.mock.call()])
self.assertEqual(mock_parameters.mock_calls, [])
self.assertEqual(add_pool_mock.mock_calls, [])
self.assertFalse(self.app.save.called)
示例9: test_200_label_create
# 需要导入模块: from unittest import mock [as 别名]
# 或者: from unittest.mock import called [as 别名]
def test_200_label_create(self):
self.app.get_label = unittest.mock.Mock()
self.app.get_label.side_effect=KeyError
self.app.labels = unittest.mock.MagicMock()
labels_config = {
'keys.return_value': range(1, 9),
}
self.app.labels.configure_mock(**labels_config)
value = self.call_mgmt_func(b'admin.label.Create', b'dom0', b'cyan',
b'0x00ffff')
self.assertIsNone(value)
self.assertEqual(self.app.get_label.mock_calls,
[unittest.mock.call('cyan')])
self.assertEqual(self.app.labels.mock_calls,
[unittest.mock.call.keys(),
unittest.mock.call.__getattr__('__setitem__')(9,
qubes.Label(9, '0x00ffff', 'cyan'))])
self.assertTrue(self.app.save.called)
示例10: test_200_label_create_invalid_name
# 需要导入模块: from unittest import mock [as 别名]
# 或者: from unittest.mock import called [as 别名]
def test_200_label_create_invalid_name(self):
self.app.get_label = unittest.mock.Mock()
self.app.get_label.side_effect=KeyError
self.app.labels = unittest.mock.MagicMock()
labels_config = {
'keys.return_value': range(1, 9),
}
self.app.labels.configure_mock(**labels_config)
with self.assertRaises(qubes.api.PermissionDenied):
self.call_mgmt_func(b'admin.label.Create', b'dom0', b'01',
b'0xff0000')
with self.assertRaises(qubes.api.PermissionDenied):
self.call_mgmt_func(b'admin.label.Create', b'dom0', b'../xxx',
b'0xff0000')
with self.assertRaises(qubes.api.PermissionDenied):
self.call_mgmt_func(b'admin.label.Create', b'dom0',
b'strange-name!@#$',
b'0xff0000')
self.assertEqual(self.app.get_label.mock_calls, [])
self.assertEqual(self.app.labels.mock_calls, [])
self.assertFalse(self.app.save.called)
示例11: test_340_vm_create_in_pool_app
# 需要导入模块: from unittest import mock [as 别名]
# 或者: from unittest.mock import called [as 别名]
def test_340_vm_create_in_pool_app(self, storage_mock):
storage_mock.side_effect = self.dummy_coro
self.call_mgmt_func(b'admin.vm.CreateInPool.AppVM',
b'dom0', b'test-template', b'name=test-vm2 label=red '
b'pool=test')
self.assertIn('test-vm2', self.app.domains)
vm = self.app.domains['test-vm2']
self.assertEqual(vm.label, self.app.get_label('red'))
self.assertEqual(vm.template, self.app.domains['test-template'])
# setting pool= affect only volumes actually created for this VM,
# not used from a template or so
self.assertEqual(vm.volume_config['root']['pool'],
self.template.volumes['root'].pool)
self.assertEqual(vm.volume_config['private']['pool'], 'test')
self.assertEqual(vm.volume_config['volatile']['pool'], 'test')
self.assertEqual(vm.volume_config['kernel']['pool'], 'linux-kernel')
self.assertEqual(storage_mock.mock_calls,
[unittest.mock.call(self.app.domains['test-vm2']).create()])
self.assertTrue(os.path.exists(os.path.join(
self.test_base_dir, 'appvms', 'test-vm2')))
self.assertTrue(self.app.save.called)
示例12: test_520_vm_volume_clone
# 需要导入模块: from unittest import mock [as 别名]
# 或者: from unittest.mock import called [as 别名]
def test_520_vm_volume_clone(self):
self.setup_for_clone()
token = self.call_mgmt_func(b'admin.vm.volume.CloneFrom',
b'test-vm1', b'private', b'')
# token
self.assertEqual(len(token), 32)
self.assertFalse(self.app.save.called)
value = self.call_mgmt_func(b'admin.vm.volume.CloneTo',
b'test-vm2', b'private', token.encode())
self.assertIsNone(value)
self.vm2.volumes['private'].import_volume.assert_called_once_with(
self.vm.volumes['private']
)
self.vm2.volumes['private'].import_volume.assert_called_once_with(
self.vm2.volumes['private']
)
self.app.save.assert_called_once_with()
示例13: test_constructor
# 需要导入模块: from unittest import mock [as 别名]
# 或者: from unittest.mock import called [as 别名]
def test_constructor(self):
mock = Mock()
self.assertFalse(mock.called, "called not initialised correctly")
self.assertEqual(mock.call_count, 0,
"call_count not initialised correctly")
self.assertTrue(is_instance(mock.return_value, Mock),
"return_value not initialised correctly")
self.assertEqual(mock.call_args, None,
"call_args not initialised correctly")
self.assertEqual(mock.call_args_list, [],
"call_args_list not initialised correctly")
self.assertEqual(mock.method_calls, [],
"method_calls not initialised correctly")
# Can't use hasattr for this test as it always returns True on a mock
self.assertFalse('_items' in mock.__dict__,
"default mock should not have '_items' attribute")
self.assertIsNone(mock._mock_parent,
"parent not initialised correctly")
self.assertIsNone(mock._mock_methods,
"methods not initialised correctly")
self.assertEqual(mock._mock_children, {},
"children not initialised incorrectly")
示例14: test_reset_mock
# 需要导入模块: from unittest import mock [as 别名]
# 或者: from unittest.mock import called [as 别名]
def test_reset_mock(self):
parent = Mock()
spec = ["something"]
mock = Mock(name="child", parent=parent, spec=spec)
mock(sentinel.Something, something=sentinel.SomethingElse)
something = mock.something
mock.something()
mock.side_effect = sentinel.SideEffect
return_value = mock.return_value
return_value()
mock.reset_mock()
self.assertEqual(mock._mock_name, "child",
"name incorrectly reset")
self.assertEqual(mock._mock_parent, parent,
"parent incorrectly reset")
self.assertEqual(mock._mock_methods, spec,
"methods incorrectly reset")
self.assertFalse(mock.called, "called not reset")
self.assertEqual(mock.call_count, 0, "call_count not reset")
self.assertEqual(mock.call_args, None, "call_args not reset")
self.assertEqual(mock.call_args_list, [], "call_args_list not reset")
self.assertEqual(mock.method_calls, [],
"method_calls not initialised correctly: %r != %r" %
(mock.method_calls, []))
self.assertEqual(mock.mock_calls, [])
self.assertEqual(mock.side_effect, sentinel.SideEffect,
"side_effect incorrectly reset")
self.assertEqual(mock.return_value, return_value,
"return_value incorrectly reset")
self.assertFalse(return_value.called, "return value mock not reset")
self.assertEqual(mock._mock_children, {'something': something},
"children reset incorrectly")
self.assertEqual(mock.something, something,
"children incorrectly cleared")
self.assertFalse(mock.something.called, "child not reset")
示例15: test_call
# 需要导入模块: from unittest import mock [as 别名]
# 或者: from unittest.mock import called [as 别名]
def test_call(self):
mock = Mock()
self.assertTrue(is_instance(mock.return_value, Mock),
"Default return_value should be a Mock")
result = mock()
self.assertEqual(mock(), result,
"different result from consecutive calls")
mock.reset_mock()
ret_val = mock(sentinel.Arg)
self.assertTrue(mock.called, "called not set")
self.assertEqual(mock.call_count, 1, "call_count incoreect")
self.assertEqual(mock.call_args, ((sentinel.Arg,), {}),
"call_args not set")
self.assertEqual(mock.call_args_list, [((sentinel.Arg,), {})],
"call_args_list not initialised correctly")
mock.return_value = sentinel.ReturnValue
ret_val = mock(sentinel.Arg, key=sentinel.KeyArg)
self.assertEqual(ret_val, sentinel.ReturnValue,
"incorrect return value")
self.assertEqual(mock.call_count, 2, "call_count incorrect")
self.assertEqual(mock.call_args,
((sentinel.Arg,), {'key': sentinel.KeyArg}),
"call_args not set")
self.assertEqual(mock.call_args_list, [
((sentinel.Arg,), {}),
((sentinel.Arg,), {'key': sentinel.KeyArg})
],
"call_args_list not set")