本文整理汇总了Python中mock.Mock.__getitem__方法的典型用法代码示例。如果您正苦于以下问题:Python Mock.__getitem__方法的具体用法?Python Mock.__getitem__怎么用?Python Mock.__getitem__使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类mock.Mock
的用法示例。
在下文中一共展示了Mock.__getitem__方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_magic_method_wrapping
# 需要导入模块: from mock import Mock [as 别名]
# 或者: from mock.Mock import __getitem__ [as 别名]
def test_magic_method_wrapping(self):
mock = Mock()
def f(self, name):
return self, 'fish'
mock.__getitem__ = f
self.assertIsNot(mock.__getitem__, f)
self.assertEqual(mock['foo'], (mock, 'fish'))
self.assertEqual(mock.__getitem__('foo'), (mock, 'fish'))
mock.__getitem__ = mock
self.assertIs(mock.__getitem__, mock)
示例2: case_no_endpoint_match
# 需要导入模块: from mock import Mock [as 别名]
# 或者: from mock.Mock import __getitem__ [as 别名]
def case_no_endpoint_match(self, scObj):
# empty endpoint list
scObj.catalog = dict()
scObj.catalog['endpoints'] = list()
self.assertRaises(exceptions.EndpointNotFound, scObj._url_for)
def side_effect_func_ep(attr):
return "test_attr_value"
# simulating dict
endpoint = Mock()
mock = Mock()
mock.side_effect = side_effect_func_ep
endpoint.__getitem__ = mock
scObj.catalog['endpoints'].append(endpoint)
# not-empty list but not matching endpoint
filter_value = "not_matching_value"
self.assertRaises(exceptions.EndpointNotFound, scObj._url_for,
attr="test_attr", filter_value=filter_value)
filter_value = "test_attr_value" # so that we have an endpoint match
scObj.root_key = "access"
scObj.catalog[scObj.root_key] = dict()
self.assertRaises(exceptions.EndpointNotFound, scObj._url_for,
attr="test_attr", filter_value=filter_value)
示例3: data
# 需要导入模块: from mock import Mock [as 别名]
# 或者: from mock.Mock import __getitem__ [as 别名]
def data(self, addr_type, ver=False, connect=False, rsv=False):
addr_types = {
'ipv4': '\x01',
'domain name': '\x03',
'ipv6': '\x04',
'unsupported': 'unsupported'
}
end_data = {
'ipv4': ['a'] * 4 + ['b'] * 2,
'domain name': ['c'] + ['d'] * 5 + ['e'] * 2,
'ipv6': ['f'] * 16 + ['g'] * 2,
'unsupported': []
}
data_arr = [
'\x05' if ver else 'ver',
'\x01' if connect else 'connect',
'\x00' if rsv else 'rsv',
addr_types[addr_type]] + end_data[addr_type]
# creating a custom getitem operator is easier than creating a new
# bytestring from scratch (overloading array[slice])
data = Mock()
data.__getitem__ = lambda s, val: (
''.join(data_arr[val.start:val.stop]) if isinstance(val, slice)
else data_arr[val])
return data
示例4: prepare_orm
# 需要导入模块: from mock import Mock [as 别名]
# 或者: from mock.Mock import __getitem__ [as 别名]
def prepare_orm(self, spec):
orm = Mock(autospec=spec)
items = {
"social_auth.UserSocialAuth": Mock()
}
orm.__getitem__ = lambda self, name: items[name]
return orm
示例5: testDictMethods
# 需要导入模块: from mock import Mock [as 别名]
# 或者: from mock.Mock import __getitem__ [as 别名]
def testDictMethods(self):
mock = Mock()
self.assertRaises(TypeError, lambda: mock['foo'])
def _del():
del mock['foo']
def _set():
mock['foo'] = 3
self.assertRaises(TypeError, _del)
self.assertRaises(TypeError, _set)
_dict = {}
def getitem(s, name):
return _dict[name]
def setitem(s, name, value):
_dict[name] = value
def delitem(s, name):
del _dict[name]
mock.__setitem__ = setitem
mock.__getitem__ = getitem
mock.__delitem__ = delitem
self.assertRaises(KeyError, lambda: mock['foo'])
mock['foo'] = 'bar'
self.assertEqual(_dict, {'foo': 'bar'})
self.assertEqual(mock['foo'], 'bar')
del mock['foo']
self.assertEqual(_dict, {})
示例6: qs
# 需要导入模块: from mock import Mock [as 别名]
# 或者: from mock.Mock import __getitem__ [as 别名]
def qs(self, count):
"""Returns mock queryset with given count."""
qs = Mock()
qs.count.return_value = count
qs.empty.return_value = []
qs.__getitem__ = Mock()
return qs
示例7: test_ServerDetails
# 需要导入模块: from mock import Mock [as 别名]
# 或者: from mock.Mock import __getitem__ [as 别名]
def test_ServerDetails(self):
hw_id = 1234
client = Mock()
client.__getitem__ = Mock()
service = client['Hardware_Server']
service.getObject = hardware_mock.getObject_Mock(1000)
dns_mock = hardware_mock.getReverseDomainRecords_Mock(1000)
service.getReverseDomainRecords = dns_mock
args = {'<identifier>': hw_id, '--passwords': True, '--price': True}
output = server.ServerDetails.execute(client, args)
expected = {
'status': 'ACTIVE',
'datacenter': 'TEST00',
'created': '2013-08-01 15:23:45',
'notes': 'These are test notes.',
'hostname': 'hardware-test1.test.sftlyr.ws',
'public_ip': '172.16.1.100',
'private_ip': '10.1.0.2',
'price rate': 1.54,
'memory': 2048,
'cores': 2,
'ptr': '2.0.1.10.in-addr.arpa',
'os': 'Ubuntu',
'id': 1000,
'tags': ['test_tag'],
'users': ['root abc123'],
'vlans': [{'id': 9653, 'number': 1800, 'type': 'PRIVATE'},
{'id': 19082, 'number': 3672, 'type': 'PUBLIC'}]
}
self.assertEqual(expected, format_output(output, 'python'))
示例8: setUp
# 需要导入模块: from mock import Mock [as 别名]
# 或者: from mock.Mock import __getitem__ [as 别名]
def setUp(self):
# NOTE this context will not push and pop so we can inspect it
self.context = {}
mock_cxt = Mock()
mock_cxt.__getitem__ = lambda _, x: self.context[x]
mock_cxt.__setitem__ = lambda _, x, y: self.context.__setitem__(x, y)
self.mock_cxt = mock_cxt
示例9: _dict_mock
# 需要导入模块: from mock import Mock [as 别名]
# 或者: from mock.Mock import __getitem__ [as 别名]
def _dict_mock(self, **kwargs):
"""
Get a mock that allows __getitem__
"""
item = Mock()
item.data = kwargs
item.__getitem__ = lambda s, k: s.data[k]
return item
示例10: testMagicMethodWrapping
# 需要导入模块: from mock import Mock [as 别名]
# 或者: from mock.Mock import __getitem__ [as 别名]
def testMagicMethodWrapping(self):
mock = Mock()
def f(self, name):
return self, 'fish'
mock.__getitem__ = f
self.assertFalse(mock.__getitem__ is f)
self.assertEqual(mock['foo'], (mock, 'fish'))
# When you pull the function back of the *instance*
# the first argument (self) is removed
def instance_f(name):
pass
self.assertEqual(inspect.getargspec(mock.__getitem__), inspect.getargspec(instance_f))
mock.__getitem__ = mock
self.assertTrue(mock.__getitem__ is mock)
示例11: test_input_sample_init_getitem_noexpand_param
# 需要导入模块: from mock import Mock [as 别名]
# 或者: from mock.Mock import __getitem__ [as 别名]
def test_input_sample_init_getitem_noexpand_param():
mock_obj = Mock()
mock_obj.__getitem__ = Mock(return_value=[0, 1, 2])
mock_obj.__setitem__ = Mock()
s = InputSample(4, 3, mock_obj, expand=False)
assert hasattr(s, "__getitem__")
assert s[1] == [0, 1, 2]
mock_obj.__getitem__.assert_called_once_with(1)
示例12: testDeletingMagicMethods
# 需要导入模块: from mock import Mock [as 别名]
# 或者: from mock.Mock import __getitem__ [as 别名]
def testDeletingMagicMethods(self):
mock = Mock()
self.assertFalse(hasattr(mock, '__getitem__'))
mock.__getitem__ = Mock()
self.assertTrue(hasattr(mock, '__getitem__'))
del mock.__getitem__
self.assertFalse(hasattr(mock, '__getitem__'))
示例13: test_del_variabledefattr_backwards
# 需要导入模块: from mock import Mock [as 别名]
# 或者: from mock.Mock import __getitem__ [as 别名]
def test_del_variabledefattr_backwards(self):
migration = self._pick_migration("0013_del_variabledefattr")
orm = Mock(autospec=migration.prev_orm())
available_models = {"platform.variabledef": Mock()}
orm.__getitem__ = lambda self, model: available_models[model.lower()]
with patch("wirecloud.platform.south_migrations.0013_del_variabledefattr.db", autospec=True):
migration_instance = migration.migration_instance()
with patch.object(migration_instance, "gf", autospec=True):
migration_instance.backwards(orm)
示例14: test_global_read
# 需要导入模块: from mock import Mock [as 别名]
# 或者: from mock.Mock import __getitem__ [as 别名]
def test_global_read(self):
o = Mock()
o.__getitem__ = Mock(return_value=3)
o.__setitem__ = Mock()
self.e.register_global('test', o)
self.assertEqual(self.e['test'][1], 3)
o.__getitem__.assert_called_once()
self.e['test'][1] = 3
o.__setitem__.assert_called_with(1, 3)
self.assertEqual(self.e, Environment())
示例15: test_input_sample_init_getitem_expand_param
# 需要导入模块: from mock import Mock [as 别名]
# 或者: from mock.Mock import __getitem__ [as 别名]
def test_input_sample_init_getitem_expand_param():
mock_obj = Mock()
mock_obj.__getitem__ = Mock(return_value=[0, 1, 2])
mock_obj.__setitem__ = Mock()
s = InputSample(4, 3, mock_obj, expand=True)
assert s._array == [[0, 1, 2]] * 4
expected = [((0,), {})] * 3 + [((1,), {})] * 3 + [((2,), {})] * 3 + [((3,), {})] * 3
assert mock_obj.__getitem__.call_args_list == expected
s[3] = ["s"]
assert s._setitem == s._assign_to_array
assert not mock_obj.__setitem__.called # expand=T copied the contents