本文整理汇总了Python中unittest.mock.MagicMock.id方法的典型用法代码示例。如果您正苦于以下问题:Python MagicMock.id方法的具体用法?Python MagicMock.id怎么用?Python MagicMock.id使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类unittest.mock.MagicMock
的用法示例。
在下文中一共展示了MagicMock.id方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_images
# 需要导入模块: from unittest.mock import MagicMock [as 别名]
# 或者: from unittest.mock.MagicMock import id [as 别名]
def test_images(monkeypatch):
image = MagicMock()
image.id = "ami-123"
image.name = "BrandNewImage"
image.creationDate = datetime.datetime.utcnow().isoformat("T") + "Z"
old_image_still_used = MagicMock()
old_image_still_used.id = "ami-456"
old_image_still_used.name = "OldImage"
old_image_still_used.creationDate = (datetime.datetime.utcnow() - datetime.timedelta(days=30)).isoformat("T") + "Z"
instance = MagicMock()
instance.id = "i-777"
instance.image_id = "ami-456"
instance.tags = {"aws:cloudformation:stack-name": "mystack"}
ec2 = MagicMock()
ec2.get_all_images.return_value = [image, old_image_still_used]
ec2.get_only_instances.return_value = [instance]
monkeypatch.setattr("boto.cloudformation.connect_to_region", lambda x: MagicMock())
monkeypatch.setattr("boto.ec2.connect_to_region", lambda x: ec2)
monkeypatch.setattr("boto.iam.connect_to_region", lambda x: MagicMock())
runner = CliRunner()
with runner.isolated_filesystem():
result = runner.invoke(cli, ["images", "--region=myregion"], catch_exceptions=False)
assert "ami-123" in result.output
assert "ami-456" in result.output
assert "mystack" in result.output
示例2: test_component_stups_auto_configuration
# 需要导入模块: from unittest.mock import MagicMock [as 别名]
# 或者: from unittest.mock.MagicMock import id [as 别名]
def test_component_stups_auto_configuration(monkeypatch):
args = MagicMock()
args.region = 'myregion'
configuration = {
'Name': 'Config',
'AvailabilityZones': ['az-1']
}
sn1 = MagicMock()
sn1.id = 'sn-1'
sn1.tags = [{'Key': 'Name', 'Value': 'dmz-1'}]
sn1.availability_zone = 'az-1'
sn2 = MagicMock()
sn2.id = 'sn-2'
sn2.tags = [{'Key': 'Name', 'Value': 'dmz-2'}]
sn2.availability_zone = 'az-2'
sn3 = MagicMock()
sn3.id = 'sn-3'
sn3.tags = [{'Key': 'Name', 'Value': 'internal-3'}]
sn3.availability_zone = 'az-1'
ec2 = MagicMock()
ec2.subnets.filter.return_value = [sn1, sn2, sn3]
image = MagicMock()
ec2.images.filter.return_value = [image]
monkeypatch.setattr('boto3.resource', lambda x, y: ec2)
result = component_stups_auto_configuration({}, configuration, args, MagicMock(), False, MagicMock())
assert {'myregion': {'Subnets': ['sn-1']}} == result['Mappings']['LoadBalancerSubnets']
assert {'myregion': {'Subnets': ['sn-3']}} == result['Mappings']['ServerSubnets']
示例3: test_component_stups_auto_configuration
# 需要导入模块: from unittest.mock import MagicMock [as 别名]
# 或者: from unittest.mock.MagicMock import id [as 别名]
def test_component_stups_auto_configuration(monkeypatch):
args = MagicMock()
args.region = "myregion"
configuration = {"Name": "Config", "AvailabilityZones": ["az-1"]}
sn1 = MagicMock()
sn1.id = "sn-1"
sn1.tags.get.return_value = "dmz-1"
sn1.availability_zone = "az-1"
sn2 = MagicMock()
sn2.id = "sn-2"
sn2.tags.get.return_value = "dmz-2"
sn2.availability_zone = "az-2"
sn3 = MagicMock()
sn3.id = "sn-3"
sn3.tags.get.return_value = "internal-3"
sn3.availability_zone = "az-1"
vpc = MagicMock()
vpc.get_all_subnets.return_value = [sn1, sn2, sn3]
image = MagicMock()
ec2 = MagicMock()
ec2.get_all_images.return_value = [image]
monkeypatch.setattr("boto.vpc.connect_to_region", lambda x: vpc)
monkeypatch.setattr("boto.ec2.connect_to_region", lambda x: ec2)
result = component_stups_auto_configuration({}, configuration, args, MagicMock(), False)
assert {"myregion": {"Subnets": ["sn-1"]}} == result["Mappings"]["LoadBalancerSubnets"]
assert {"myregion": {"Subnets": ["sn-3"]}} == result["Mappings"]["ServerSubnets"]
示例4: test_component_stups_auto_configuration
# 需要导入模块: from unittest.mock import MagicMock [as 别名]
# 或者: from unittest.mock.MagicMock import id [as 别名]
def test_component_stups_auto_configuration(monkeypatch):
args = MagicMock()
args.region = 'myregion'
configuration = {
'Name': 'Config',
'AvailabilityZones': ['az-1']
}
sn1 = MagicMock()
sn1.id = 'sn-1'
sn1.tags.get.return_value = 'dmz-1'
sn1.availability_zone = 'az-1'
sn2 = MagicMock()
sn2.id = 'sn-2'
sn2.tags.get.return_value = 'dmz-2'
sn2.availability_zone = 'az-2'
sn3 = MagicMock()
sn3.id = 'sn-3'
sn3.tags.get.return_value = 'internal-3'
sn3.availability_zone = 'az-1'
vpc = MagicMock()
vpc.get_all_subnets.return_value = [sn1, sn2, sn3]
image = MagicMock()
ec2 = MagicMock()
ec2.get_all_images.return_value = [image]
monkeypatch.setattr('boto.vpc.connect_to_region', lambda x: vpc)
monkeypatch.setattr('boto.ec2.connect_to_region', lambda x: ec2)
result = component_stups_auto_configuration({}, configuration, args, MagicMock(), False)
assert {'myregion': {'Subnets': ['sn-1']}} == result['Mappings']['LoadBalancerSubnets']
assert {'myregion': {'Subnets': ['sn-3']}} == result['Mappings']['ServerSubnets']
示例5: test_user_replace_avatar
# 需要导入模块: from unittest.mock import MagicMock [as 别名]
# 或者: from unittest.mock.MagicMock import id [as 别名]
def test_user_replace_avatar(self):
user_id = 1
old_avatar_file_id = 1
new_avatar_file_id = 2
user = MagicMock(spec=dir(User))
old_avatar = MagicMock(spec=dir(File))
expected_avatar = MagicMock(spec=dir(File))
user.id = user_id
user.avatar_file_id = old_avatar_file_id
old_avatar.id = old_avatar_file_id
expected_avatar.id = new_avatar_file_id
user_repository_mock.find_by_id.return_value = user
file_service_mock.add_file.return_value = expected_avatar
file_service_mock.get_file_by_id.return_value = old_avatar
user_service.set_avatar(user_id, avatar_file_data)
self.assertEqual(user.avatar_file_id, new_avatar_file_id)
file_service_mock.add_file.assert_called_once_with(
FileCategory.USER_AVATAR,
avatar_file_data, avatar_file_data.filename)
file_service_mock.get_file_by_id.assert_called_once_with(
old_avatar_file_id)
file_service_mock.delete_file.assert_called_once_with(old_avatar)
user_repository_mock.save.assert_called_once_with(user)
示例6: test_component_subnet_auto_configuration
# 需要导入模块: from unittest.mock import MagicMock [as 别名]
# 或者: from unittest.mock.MagicMock import id [as 别名]
def test_component_subnet_auto_configuration(monkeypatch):
configuration = {
'PublicOnly': True,
'VpcId': 'vpc-123'
}
info = {'StackName': 'foobar', 'StackVersion': '0.1'}
definition = {"Resources": {}}
args = MagicMock()
args.region = "foo"
subnet1 = MagicMock()
subnet1.id = 'subnet-1'
subnet2 = MagicMock()
subnet2.id = 'subnet-2'
ec2 = MagicMock()
ec2.subnets.filter.return_value = [subnet1, subnet2]
monkeypatch.setattr('boto3.resource', lambda *args: ec2)
result = component_subnet_auto_configuration(definition, configuration, args, info, False, MagicMock())
assert ['subnet-1', 'subnet-2'] == result['Mappings']['ServerSubnets']['foo']['Subnets']
configuration = {
'PublicOnly': False,
'VpcId': 'vpc-123'
}
result = component_subnet_auto_configuration(definition, configuration, args, info, False, MagicMock())
assert ['subnet-1', 'subnet-2'] == result['Mappings']['ServerSubnets']['foo']['Subnets']
示例7: test_filter_images
# 需要导入模块: from unittest.mock import MagicMock [as 别名]
# 或者: from unittest.mock.MagicMock import id [as 别名]
def test_filter_images(self):
up = MagicMock()
up.id = 'up'
down = MagicMock()
down.id = 'down'
self.ir.image_retriever.get_images.return_value = [up, down]
self.ir.upvote_image('up', 10)
self.ir.downvote_image('down', -20)
self.assertEqual(self.ir.image_score('up'), 10)
self.assertEqual(self.ir.image_score('down'), -20)
self.assertEqual(self.ir.get_image_sample(), [up])
示例8: test_print_replace_mustache
# 需要导入模块: from unittest.mock import MagicMock [as 别名]
# 或者: from unittest.mock.MagicMock import id [as 别名]
def test_print_replace_mustache(monkeypatch):
sg = MagicMock()
sg.name = 'app-master-mind'
sg.id = 'sg-007'
monkeypatch.setattr('boto.cloudformation.connect_to_region', lambda x: MagicMock())
monkeypatch.setattr('boto.ec2.connect_to_region', lambda x: MagicMock(get_all_security_groups=lambda: [sg]))
monkeypatch.setattr('boto.iam.connect_to_region', lambda x: MagicMock())
data = {'SenzaInfo': {'StackName': 'test',
'Parameters': [{'ApplicationId': {'Description': 'Application ID from kio'}}]},
'SenzaComponents': [{'Configuration': {'ServerSubnets': {'eu-west-1': ['subnet-123']},
'Type': 'Senza::Configuration'}},
{'AppServer': {'Image': 'AppImage',
'InstanceType': 't2.micro',
'SecurityGroups': ['app-{{Arguments.ApplicationId}}'],
'IamRoles': ['app-{{Arguments.ApplicationId}}'],
'TaupageConfig': {'runtime': 'Docker',
'source': 'foo/bar'},
'Type': 'Senza::TaupageAutoScalingGroup'}}]
}
runner = CliRunner()
with runner.isolated_filesystem():
with open('myapp.yaml', 'w') as fd:
yaml.dump(data, fd)
result = runner.invoke(cli, ['print', 'myapp.yaml', '--region=myregion', '123', 'master-mind'],
catch_exceptions=False)
assert 'AWSTemplateFormatVersion' in result.output
assert 'subnet-123' in result.output
assert 'app-master-mind' in result.output
assert 'sg-007' in result.output
示例9: test_add_node_non_local
# 需要导入模块: from unittest.mock import MagicMock [as 别名]
# 或者: from unittest.mock.MagicMock import id [as 别名]
def test_add_node_non_local(async_run, controller):
"""
For a non local server we do not send the project path
"""
compute = MagicMock()
compute.id = "remote"
project = Project(controller=controller, name="Test")
controller._notification = MagicMock()
response = MagicMock()
response.json = {"console": 2048}
compute.post = AsyncioMagicMock(return_value=response)
node = async_run(project.add_node(compute, "test", None, node_type="vpcs", properties={"startup_script": "test.cfg"}))
compute.post.assert_any_call('/projects', data={
"name": project._name,
"project_id": project._id
})
compute.post.assert_any_call('/projects/{}/vpcs/nodes'.format(project.id),
data={'node_id': node.id,
'startup_script': 'test.cfg',
'name': 'test'},
timeout=1200)
assert compute in project._project_created_on_compute
controller.notification.emit.assert_any_call("node.created", node.__json__())
示例10: test_get_file_content_headers_no_display_name
# 需要导入模块: from unittest.mock import MagicMock [as 别名]
# 或者: from unittest.mock.MagicMock import id [as 别名]
def test_get_file_content_headers_no_display_name(self):
# === Initialization ===
display_name = None
extension = 'png'
category = FileCategory.ACTIVITY_PICTURE
_hash = '123456789abcdefghiklmnopqrstuvwxyz'
full_display_name = None
expected_content_type = "image/png"
_file = MagicMock(spec=dir(File))
_file.id = 1
_file.hash = _hash
_file.category = category
_file.display_name = display_name
_file.extension = extension
_file.full_display_name = full_display_name
# === Service function call ===
with patch.object(file_service, 'get_file_mimetype',
return_value=expected_content_type):
headers = file_service.get_file_content_headers(_file)
# === Assertions ===
assert "Content-Type" in headers
assert "Content-Disposition" not in headers
self.assertEqual(headers["Content-Type"], expected_content_type)
示例11: test_get_file_mimetype_txt
# 需要导入模块: from unittest.mock import MagicMock [as 别名]
# 或者: from unittest.mock.MagicMock import id [as 别名]
def test_get_file_mimetype_txt(self):
# === Initialization ===
display_name = 'test123'
extension = 'txt'
category = FileCategory.UPLOADS
_hash = '123456789abcdefghiklmnopqrstuvwxyz'
expected_mimetype = 'text/plain'
expected_mimetype_charset = 'text/plain; charset=utf-8'
_file = MagicMock(spec=dir(File))
_file.id = 1
_file.hash = _hash
_file.category = category
_file.display_name = display_name
_file.extension = extension
# === Service function call ===
mimetype = file_service.get_file_mimetype(
_file, add_http_text_charset=None)
mimetype_charset = file_service.get_file_mimetype(_file)
# === Assertions ===
self.assertEqual(mimetype, expected_mimetype)
self.assertEqual(mimetype_charset, expected_mimetype_charset)
示例12: test_get_file_content
# 需要导入模块: from unittest.mock import MagicMock [as 别名]
# 或者: from unittest.mock.MagicMock import id [as 别名]
def test_get_file_content(self):
# === Initialization ===
display_name = 'test123'
extension = 'png'
category = FileCategory.UPLOADS
_hash = '123456789abcdefghiklmnopqrstuvwxyz'
data = '1eb55d09d99d4d0686581a7fdb8a3346'
data_reader = StringIO(data)
_file = MagicMock(spec=dir(File))
_file.id = 1
_file.hash = _hash
_file.category = category
_file.display_name = display_name
_file.extension = extension
# === Set return values ===
hashfs_mock.open.return_value = data_reader
# === Service function call ===
content = file_service.get_file_content(_file)
# === Assertions ===
self.assertEqual(data, content)
示例13: test_recursive_removal
# 需要导入模块: from unittest.mock import MagicMock [as 别名]
# 或者: from unittest.mock.MagicMock import id [as 别名]
def test_recursive_removal(self):
item1 = MagicMock()
item1.id = "item1"
item1._deps = ["item2"]
item2 = MagicMock()
item2.id = "item2"
item2._deps = ["item3"]
item3 = MagicMock()
item3.id = "item3"
item3._deps = []
items = [item1, item2, item3]
self.assertEqual(
deps.remove_item_dependents(items, item3),
([item3], [item2, item1]),
)
示例14: test_export_vm
# 需要导入模块: from unittest.mock import MagicMock [as 别名]
# 或者: from unittest.mock.MagicMock import id [as 别名]
def test_export_vm(tmpdir, project, async_run):
"""
If data is on a remote server export it locally before
sending it in the archive.
"""
compute = MagicMock()
compute.id = "vm"
compute.list_files = AsyncioMagicMock(return_value=[{"path": "vm-1/dynamips/test"}])
# Fake file that will be download from the vm
mock_response = AsyncioMagicMock()
mock_response.content = AsyncioBytesIO()
async_run(mock_response.content.write(b"HELLO"))
mock_response.content.seek(0)
compute.download_file = AsyncioMagicMock(return_value=mock_response)
project._project_created_on_compute.add(compute)
path = project.path
os.makedirs(os.path.join(path, "vm-1", "dynamips"))
# The .gns3 should be renamed project.gns3 in order to simplify import
with open(os.path.join(path, "test.gns3"), 'w+') as f:
f.write("{}")
with aiozipstream.ZipFile() as z:
async_run(export_project(z, project, str(tmpdir)))
assert compute.list_files.called
async_run(write_file(str(tmpdir / 'zipfile.zip'), z))
with zipfile.ZipFile(str(tmpdir / 'zipfile.zip')) as myzip:
with myzip.open("vm-1/dynamips/test") as myfile:
content = myfile.read()
assert content == b"HELLO"
示例15: test_duplicate
# 需要导入模块: from unittest.mock import MagicMock [as 别名]
# 或者: from unittest.mock.MagicMock import id [as 别名]
def test_duplicate(project, async_run, controller):
"""
Duplicate a project, the node should remain on the remote server
if they were on remote server
"""
compute = MagicMock()
compute.id = "remote"
compute.list_files = AsyncioMagicMock(return_value=[])
controller._computes["remote"] = compute
response = MagicMock()
response.json = {"console": 2048}
compute.post = AsyncioMagicMock(return_value=response)
remote_vpcs = async_run(project.add_node(compute, "test", None, node_type="vpcs", properties={"startup_config": "test.cfg"}))
# We allow node not allowed for standard import / export
remote_virtualbox = async_run(project.add_node(compute, "test", None, node_type="vmware", properties={"startup_config": "test.cfg"}))
new_project = async_run(project.duplicate(name="Hello"))
assert new_project.id != project.id
assert new_project.name == "Hello"
async_run(new_project.open())
assert list(new_project.nodes.values())[0].compute.id == "remote"
assert list(new_project.nodes.values())[1].compute.id == "remote"