本文整理汇总了Python中mock.MagicMock.name方法的典型用法代码示例。如果您正苦于以下问题:Python MagicMock.name方法的具体用法?Python MagicMock.name怎么用?Python MagicMock.name使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类mock.MagicMock
的用法示例。
在下文中一共展示了MagicMock.name方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: create_mock_list_with_name_attribute
# 需要导入模块: from mock import MagicMock [as 别名]
# 或者: from mock.MagicMock import name [as 别名]
def create_mock_list_with_name_attribute():
member1 = MagicMock(id=1)
member2 = MagicMock(id=2)
member1.name = 'testerino'
member2.name = 'anotherone'
content = MagicMock(content=[member1, member2])
return content
示例2: test_user_edit_permissions
# 需要导入模块: from mock import MagicMock [as 别名]
# 或者: from mock.MagicMock import name [as 别名]
def test_user_edit_permissions(self):
mocked_repo = MagicMock()
mocked_repository = MagicMock()
mocked_user = MagicMock()
mock_single_user = MagicMock()
mock_single_user.name = 'another_user'
mock_single_user.__str__ = lambda x: 'another_user'
mocked_repository.name = 'test_repo'
mocked_repository.path = 'path'
mocked_user.get.return_value = mock_single_user
mocked_repo.users = ['user']
with patch.multiple('models.lists.users',
Repo=MagicMock(return_value=mocked_repo),
User=mocked_user):
repo_users = ListUsers(mocked_repository)
repo_users.edit('test', 'R')
pattern = r'(\s*)([RW+DC]*)(\s*)=(\s*)%s' % 'another_user'
string = r"\n %s = %s" % ('R', 'another_user')
mocked_repo.replace.assert_called_once_with(pattern, string)
message = "User another_user has R permission for repository test_repo"
mocked_repository.git.commit.has_calls([call(['conf'], message)])
示例3: test_multiple_notifies_single_change
# 需要导入模块: from mock import MagicMock [as 别名]
# 或者: from mock.MagicMock import name [as 别名]
def test_multiple_notifies_single_change(self):
block_1 = MagicMock(
to_dict=MagicMock(return_value={"attr": "initial_value"}))
block_1.name = "block_1"
block_2 = MagicMock(
to_dict=MagicMock(return_value={"attr2": "initial_value"}))
block_2.name = "block_2"
sub_1 = MagicMock()
sub_1.endpoint = ["block_1"]
sub_1.delta = False
sub_2 = MagicMock()
sub_2.endpoint = ["block_1"]
sub_2.delta = True
sub_3 = MagicMock()
sub_3.endpoint = ["block_2"]
sub_3.delta = False
sub_4 = MagicMock()
sub_4.endpoint = ["block_2"]
sub_4.delta = True
change_1 = [["block_1", "attr"], "final_value"]
change_2 = [["block_2", "attr2"], "final_value"]
request_1 = BlockNotify("block_1")
request_2 = BlockChanged(change_1)
request_3 = BlockChanged(change_2)
request_4 = BlockNotify("block_1")
request_5 = BlockNotify("block_1")
request_6 = BlockNotify("block_2")
p = Process("proc", MagicMock())
p.q.get = MagicMock(side_effect=[request_1, request_2, request_3,
request_4, request_5, request_6,
PROCESS_STOP])
p.q.put = MagicMock(side_effect=p.q.put)
p._subscriptions["block_1"] = [sub_1, sub_2]
p._subscriptions["block_2"] = [sub_3, sub_4]
p._handle_block_add(BlockAdd(block_1))
p._handle_block_add(BlockAdd(block_2))
p.recv_loop()
call_list = sub_1.response_queue.put.call_args_list
self.assertEquals(1, len(call_list))
self.assertIsInstance(call_list[0][0][0], Update)
self.assertEquals({"attr": "final_value"}, call_list[0][0][0].value)
call_list = sub_2.response_queue.put.call_args_list
self.assertEquals(1, len(call_list))
self.assertIsInstance(call_list[0][0][0], Delta)
self.assertEquals([[["attr"], "final_value"]],
call_list[0][0][0].changes)
call_list = sub_3.response_queue.put.call_args_list
self.assertEquals(1, len(call_list))
self.assertIsInstance(call_list[0][0][0], Update)
self.assertEquals({"attr2": "final_value"}, call_list[0][0][0].value)
call_list = sub_4.response_queue.put.call_args_list
self.assertEquals(1, len(call_list))
self.assertIsInstance(call_list[0][0][0], Delta)
self.assertEquals([[["attr2"], "final_value"]],
call_list[0][0][0].changes)
示例4: test___eq__
# 需要导入模块: from mock import MagicMock [as 别名]
# 或者: from mock.MagicMock import name [as 别名]
def test___eq__(self, mock_name, mock_callables, mock_priority):
"""Test the equality operator."""
callables = [MagicMock(), MagicMock(), MagicMock()]
mock_callables.return_value = callables
mock_name.return_value = "name1"
mock_priority.return_value = 3
item1 = ht.events.item.HoudiniEventItem(None, None, None, None)
item2 = MagicMock(spec=ht.events.item.HoudiniEventItem)
item2.callables = []
item2.name = "name2"
item2.priority = 4
# Names differ
self.assertFalse(item1.__eq__(item2))
mock_name.return_value = "name"
item2.name = "name"
# Names the same, callables differ
self.assertFalse(item1.__eq__(item2))
item2.callables = callables
# Names and callables equal, priority differs
self.assertFalse(item1.__eq__(item2))
item2.priority = 3
# Should be equal now
self.assertEqual(item1, item2)
示例5: test_Downloader_extract
# 需要导入模块: from mock import MagicMock [as 别名]
# 或者: from mock.MagicMock import name [as 别名]
def test_Downloader_extract(tarfile):
downloader = Downloader("http://example.com")
downloader.tarFile = "/tmp/test.tar"
downloader.workDir = "/tmp"
tar = MagicMock()
tarfile.open.return_value = tar
badfile = MagicMock()
badfile.name = ".."
tar.__iter__.return_value = [badfile]
# Exception: Archive contains a forbidden path: ..
assert_raises(Exception, downloader.extract)
badfile.name = "/bin/bash"
tar.__iter__.return_value = [badfile]
# Exception: Archive contains an absolute path: /bin/bash
assert_raises(Exception, downloader.extract)
srcdir = MagicMock()
srcdir.name = "project-0123456789abcdef"
tar.__iter__.return_value = [srcdir]
# Exception: Repository does not contain a Dockerfile
assert_raises(Exception, downloader.extract)
dockerfile = MagicMock()
dockerfile.name = "project-0123456789abcdef/Dockerfile"
tar.__iter__.return_value = [srcdir, dockerfile]
rundir = downloader.extract()
assert rundir == "/tmp/project-0123456789abcdef"
示例6: test_users_set
# 需要导入模块: from mock import MagicMock [as 别名]
# 或者: from mock.MagicMock import name [as 别名]
def test_users_set(self):
mocked_repo = MagicMock()
mocked_repository = MagicMock(config="")
mocked_repository.get_config.return_value = ""
mocked_user = MagicMock()
mock_single_user = MagicMock()
mock_single_user.name = 'user'
mocked_repository.name = 'test_repo'
mocked_repository.path = 'path'
mocked_user.get.return_value = mock_single_user
with patch.multiple('pyolite.models.lists.users',
Repo=MagicMock(return_value=mocked_repo),
User=mocked_user):
repo_users = ListUsers(mocked_repository)
repo_users.set((('mocked', 'R'), ('mocked', 'RW+')))
serialized_users = "repo test_repo\n R = user\n" \
" RW+ = user\n"
mocked_repo.overwrite.assert_called_once_with(serialized_users)
message = "Initialized repository test_repo with users: test, user"
mocked_repository.git.commit.has_calls([call(['conf'], message)])
示例7: test_modified_3_cycle_throws_circ_dependencies_error
# 需要导入模块: from mock import MagicMock [as 别名]
# 或者: from mock.MagicMock import name [as 别名]
def test_modified_3_cycle_throws_circ_dependencies_error(self):
"""
Ensures
o o
\ / \
o - o
(right triangle is a 3 cycle)
throws circular dependency error
"""
stack1 = MagicMock(Spec=Stack)
stack2 = MagicMock(Spec=Stack)
stack3 = MagicMock(Spec=Stack)
stack4 = MagicMock(Spec=Stack)
stack1.dependencies = ["stack2"]
stack1.name = "stack1"
stack2.dependencies = ["stack3"]
stack2.name = "stack2"
stack3.dependencies = ["stack4"]
stack3.name = "stack3"
stack4.dependencies = ["stack2"]
stack4.name = "stack4"
stacks = {
"stack1": stack1,
"stack2": stack2,
"stack3": stack3,
"stack4": stack4
}
self.environment.stacks = stacks
with pytest.raises(CircularDependenciesError) as ex:
self.environment._check_for_circular_dependencies()
assert (all(x in str(ex) for x in ['stack4', 'stack3', 'stack2']) and
'stack1' not in str(ex))
示例8: test_4_cycle_throws_circ_dependencies_error
# 需要导入模块: from mock import MagicMock [as 别名]
# 或者: from mock.MagicMock import name [as 别名]
def test_4_cycle_throws_circ_dependencies_error(self):
"""
Ensures
o - o
| |
o - o
throws circular dependency error
"""
stack1 = MagicMock(Spec=Stack)
stack2 = MagicMock(Spec=Stack)
stack3 = MagicMock(Spec=Stack)
stack4 = MagicMock(Spec=Stack)
stack1.dependencies = ["stack4"]
stack1.name = "stack1"
stack2.dependencies = ["stack1"]
stack2.name = "stack2"
stack3.dependencies = ["stack2"]
stack3.name = "stack3"
stack4.dependencies = ["stack3"]
stack4.name = "stack4"
stacks = {
"stack1": stack1,
"stack2": stack2,
"stack3": stack3,
"stack4": stack4
}
self.environment.stacks = stacks
with pytest.raises(CircularDependenciesError) as ex:
self.environment._check_for_circular_dependencies()
assert all(x in str(ex) for x in ['stack4', 'stack3', 'stack2',
'stack1'])
示例9: test_it_should_add_a_new_user_to_repo_if_is_valid
# 需要导入模块: from mock import MagicMock [as 别名]
# 或者: from mock.MagicMock import name [as 别名]
def test_it_should_add_a_new_user_to_repo_if_is_valid(self):
mocked_repo = MagicMock()
mocked_repository = MagicMock()
mocked_user = MagicMock()
mock_single_user = MagicMock()
mock_single_user.name = 'another_user'
mock_single_user.__str__ = lambda x: 'another_user'
mocked_repository.name = 'test_repo'
mocked_repository.path = 'path'
mocked_user.get.return_value = mock_single_user
mocked_repo.users = ['user']
with patch.multiple('models.lists.users',
Repo=MagicMock(return_value=mocked_repo),
User=mocked_user):
repo_users = ListUsers(mocked_repository)
repo_users.add('test', 'RW+')
content = ' RW+ = another_user\n'
mocked_repo.write.assert_called_once_with(content)
message = 'User another_user added to repo test_repo ' \
'with permissions: RW+'
mocked_repository.git.commit.has_calls([call(['conf'], message)])
示例10: test_user_removing
# 需要导入模块: from mock import MagicMock [as 别名]
# 或者: from mock.MagicMock import name [as 别名]
def test_user_removing(self):
mocked_repo = MagicMock()
mocked_repository = MagicMock()
mocked_user = MagicMock()
mock_single_user = MagicMock()
mock_single_user.name = 'another_user'
mock_single_user.__str__ = lambda x: 'another_user'
mocked_repository.name = 'test_repo'
mocked_repository.path = 'path'
mocked_user.get.return_value = mock_single_user
mocked_repo.users = ['user']
with patch.multiple('models.lists.users',
Repo=MagicMock(return_value=mocked_repo),
User=mocked_user):
repo_users = ListUsers(mocked_repository)
repo_users.remove('test')
pattern = r'(\s*)([RW+DC]*)(\s*)=(\s*)%s' % 'another_user'
mocked_repo.replace.assert_called_once_with(pattern, "")
message = "Deleted user another_user from repository test_repo"
mocked_repository.git.commit.has_calls([call(['conf'], message)])
示例11: test_DAG_diamond_with_triangle_throws_no_circ_dependencies_error
# 需要导入模块: from mock import MagicMock [as 别名]
# 或者: from mock.MagicMock import name [as 别名]
def test_DAG_diamond_with_triangle_throws_no_circ_dependencies_error(self):
"""
Ensures
o
/ \
o o
\ / \
o ->o
throws no circular dependency error
"""
stack1 = MagicMock(Spec=Stack)
stack2 = MagicMock(Spec=Stack)
stack3 = MagicMock(Spec=Stack)
stack4 = MagicMock(Spec=Stack)
stack5 = MagicMock(Spec=Stack)
stack1.dependencies = ["stack2", "stack3"]
stack1.name = "stack1"
stack2.dependencies = ["stack4"]
stack2.name = "stack2"
stack3.dependencies = ["stack4", "stack5"]
stack3.name = "stack3"
stack4.dependencies = ["stack5"]
stack4.name = "stack4"
stack5.dependencies = []
stack5.name = "stack5"
stacks = {
"stack1": stack1,
"stack2": stack2,
"stack3": stack3,
"stack4": stack4,
"stack5": stack5
}
self.environment.stacks = stacks
self.environment._check_for_circular_dependencies()
示例12: test_add_tasks_po_from_filextension
# 需要导入模块: from mock import MagicMock [as 别名]
# 或者: from mock.MagicMock import name [as 别名]
def test_add_tasks_po_from_filextension(self, find_mock, po_mock):
"""Test add_tasks po without specifying file extension works."""
project = MagicMock()
project.name = 'name'
project.short_name = 'short_name'
project.description = 'description'
project.info = dict()
find_mock.return_value = project
entry = MagicMock()
entry.msgid = 'English'
entry.msgtr = ''
po = MagicMock()
po.untranslated_entries.return_value = [entry]
po_mock.return_value = po
tasks = MagicMock()
tasks.name = 'tasks.po'
tasks.read.return_value = json.dumps([{'info': {'msgid': 'English',
'msgtr':''}}])
pbclient = MagicMock()
pbclient.create_task.return_value = {'id': 1, 'info': {'msgid': 'English',
'msgtr': ''}}
self.config.pbclient = pbclient
res = _add_tasks(self.config, tasks, None, 0, 30)
assert res == '1 tasks added to project: short_name', res
示例13: test_returns_dict
# 需要导入模块: from mock import MagicMock [as 别名]
# 或者: from mock.MagicMock import name [as 别名]
def test_returns_dict(self, _):
e1 = MagicMock()
e1.name = "one"
a1 = OrderedDict()
e1.to_dict.return_value = a1
e2 = MagicMock()
e2.name = "two"
a2 = OrderedDict()
e2.to_dict.return_value = a2
self.meta_map = MapMeta("Test")
self.meta_map.add_element(e1, required=True)
self.meta_map.add_element(e2, required=False)
expected_elements_dict = OrderedDict()
expected_elements_dict['one'] = a1
expected_elements_dict['two'] = a2
expected_dict = OrderedDict()
expected_dict['elements'] = expected_elements_dict
expected_dict['required'] = ["one"]
response = self.meta_map.to_dict()
self.assertEqual(expected_dict, response)
示例14: test_create_user_case_sensitivity
# 需要导入模块: from mock import MagicMock [as 别名]
# 或者: from mock.MagicMock import name [as 别名]
def test_create_user_case_sensitivity(self, KeystoneManager):
""" Test case sensitivity of check for existence in
the user creation process """
mock_keystone = MagicMock()
KeystoneManager.return_value = mock_keystone
mock_user = MagicMock()
mock_keystone.resolve_user_id.return_value = mock_user
mock_keystone.api.users.list.return_value = [mock_user]
# User found is the same i.e. userA == userA
mock_user.name = 'userA'
utils.create_user('userA', 'passA')
mock_keystone.resolve_user_id.assert_called_with('userA',
user_domain=None)
mock_keystone.create_user.assert_not_called()
# User found has different case but is the same
# i.e. Usera != userA
mock_user.name = 'Usera'
utils.create_user('userA', 'passA')
mock_keystone.resolve_user_id.assert_called_with('userA',
user_domain=None)
mock_keystone.create_user.assert_not_called()
# User is different i.e. UserB != userA
mock_user.name = 'UserB'
utils.create_user('userA', 'passA')
mock_keystone.resolve_user_id.assert_called_with('userA',
user_domain=None)
mock_keystone.create_user.assert_called_with(name='userA',
password='passA',
tenant_id=None,
domain_id=None,
email='[email protected]')
示例15: test_validate_commit_path_with_more_than_one_entry
# 需要导入模块: from mock import MagicMock [as 别名]
# 或者: from mock.MagicMock import name [as 别名]
def test_validate_commit_path_with_more_than_one_entry(self):
mocked_repo = MagicMock()
mocked_commit = MagicMock()
mocked_entry = MagicMock()
mocked_second_entry = MagicMock()
mocked_commit.tree = "tree"
mocked_repo.revparse_single.return_value = mocked_commit
mocked_second_entry.id = 1
mocked_second_entry.name = "complex_entry"
mocked_second_entry.filemode = GIT_FILEMODE_TREE
mocked_entry.name = "simple_entry"
mocked_entry.filemode = GIT_FILEMODE_TREE
mocked_repo.__getitem__.return_value = [mocked_entry]
view = CommitView(repo=mocked_repo, commit_sha1="sha1",
mount_time="now", uid=1, gid=1)
result = view._validate_commit_path([mocked_second_entry,
mocked_entry],
["complex_entry",
"simple_entry"])
assert result is True
mocked_repo.__getitem__.assert_called_once_with(1)