本文整理汇总了Python中unittest.mock.MagicMock.json方法的典型用法代码示例。如果您正苦于以下问题:Python MagicMock.json方法的具体用法?Python MagicMock.json怎么用?Python MagicMock.json使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类unittest.mock.MagicMock
的用法示例。
在下文中一共展示了MagicMock.json方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_duplicate_node
# 需要导入模块: from unittest.mock import MagicMock [as 别名]
# 或者: from unittest.mock.MagicMock import json [as 别名]
def test_duplicate_node(http_controller, tmpdir, project, compute, node):
response = MagicMock()
response.json({"console": 2035})
compute.post = AsyncioMagicMock(return_value=response)
response = http_controller.post("/projects/{}/nodes/{}/duplicate".format(
project.id, node.id),
{"x": 10, "y": 5, "z": 0},
example=True)
assert response.status == 201, response.body.decode()
示例2: test_get_bad_status
# 需要导入模块: from unittest.mock import MagicMock [as 别名]
# 或者: from unittest.mock.MagicMock import json [as 别名]
def test_get_bad_status(self, mock_get):
url = "http://httpbin.org/get"
resp = MagicMock()
resp.status_code = 400
resp.json = MagicMock(return_value={})
mock_get.return_value = resp
block = HTTPRequestsPostSignal()
self.configure_block(block, {
"http_method": "GET",
"url": url
})
block.logger.warning = MagicMock()
signals = [Signal()]
block.start()
block.process_signals(signals)
block.stop()
block.logger.warning.assert_called_with(
'HTTPMethod.GET request to http://httpbin.org/get '
'returned with response code: 400'
)
self.assertTrue(mock_get.called)
self.assertEqual(self.last_notified[DEFAULT_TERMINAL][0].to_dict(),
signals[0].to_dict())
示例3: test_add_node_non_local
# 需要导入模块: from unittest.mock import MagicMock [as 别名]
# 或者: from unittest.mock.MagicMock import json [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__())
示例4: test_list_link
# 需要导入模块: from unittest.mock import MagicMock [as 别名]
# 或者: from unittest.mock.MagicMock import json [as 别名]
def test_list_link(http_controller, tmpdir, project, compute, async_run):
response = MagicMock()
response.json = {"console": 2048}
compute.post = AsyncioMagicMock(return_value=response)
node1 = async_run(project.add_node(compute, "node1", None, node_type="qemu"))
node1._ports = [EthernetPort("E0", 0, 0, 3)]
node2 = async_run(project.add_node(compute, "node2", None, node_type="qemu"))
node2._ports = [EthernetPort("E0", 0, 2, 4)]
with asyncio_patch("gns3server.controller.udp_link.UDPLink.create") as mock:
response = http_controller.post("/projects/{}/links".format(project.id), {
"nodes": [
{
"node_id": node1.id,
"adapter_number": 0,
"port_number": 3
},
{
"node_id": node2.id,
"adapter_number": 2,
"port_number": 4
}
]
})
response = http_controller.get("/projects/{}/links".format(project.id), example=True)
assert response.status == 200
assert len(response.json) == 1
示例5: test_update_link
# 需要导入模块: from unittest.mock import MagicMock [as 别名]
# 或者: from unittest.mock.MagicMock import json [as 别名]
def test_update_link(http_controller, tmpdir, project, compute, async_run):
response = MagicMock()
response.json = {"console": 2048}
compute.post = AsyncioMagicMock(return_value=response)
node1 = async_run(project.add_node(compute, "node1", None, node_type="qemu"))
node1._ports = [EthernetPort("E0", 0, 0, 3)]
node2 = async_run(project.add_node(compute, "node2", None, node_type="qemu"))
node2._ports = [EthernetPort("E0", 0, 2, 4)]
filters = {
"latency": [10],
"frequency_drop": [50]
}
with asyncio_patch("gns3server.controller.udp_link.UDPLink.create"):
response = http_controller.post("/projects/{}/links".format(project.id), {
"nodes": [
{
"node_id": node1.id,
"adapter_number": 0,
"port_number": 3,
"label": {
"text": "Text",
"x": 42,
"y": 0
}
},
{
"node_id": node2.id,
"adapter_number": 2,
"port_number": 4
}
]
})
link_id = response.json["link_id"]
assert response.json["nodes"][0]["label"]["x"] == 42
response = http_controller.put("/projects/{}/links/{}".format(project.id, link_id), {
"nodes": [
{
"node_id": node1.id,
"adapter_number": 0,
"port_number": 3,
"label": {
"text": "Hello",
"x": 64,
"y": 0
}
},
{
"node_id": node2.id,
"adapter_number": 2,
"port_number": 4
}
],
"filters": filters
}, example=True)
assert response.status == 201
assert response.json["nodes"][0]["label"]["x"] == 64
assert list(project.links.values())[0].filters == filters
示例6: test_duplicate
# 需要导入模块: from unittest.mock import MagicMock [as 别名]
# 或者: from unittest.mock.MagicMock import json [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"
示例7: test_update_properties
# 需要导入模块: from unittest.mock import MagicMock [as 别名]
# 或者: from unittest.mock.MagicMock import json [as 别名]
def test_update_properties(node, compute, project, async_run, controller):
"""
properties will be updated by the answer from compute
"""
response = MagicMock()
response.json = {"console": 2048}
compute.put = AsyncioMagicMock(return_value=response)
controller._notification = AsyncioMagicMock()
async_run(node.update(x=42, console=2048, console_type="vnc", properties={"startup_script": "hello world"}, name="demo"))
data = {
"console": 2048,
"console_type": "vnc",
"startup_script": "hello world",
"name": "demo"
}
compute.put.assert_called_with("/projects/{}/vpcs/nodes/{}".format(node.project.id, node.id), data=data)
assert node._console == 2048
assert node.x == 42
assert node._properties == {"startup_script": "echo test"}
# The notif should contain the old properties because it's the compute that will emit
# the correct info
node_notif = copy.deepcopy(node.__json__())
node_notif["properties"]["startup_script"] = "echo test"
controller._notification.emit.assert_called_with("node.updated", node_notif)
示例8: test_dynamips_idlepc_proposals
# 需要导入模块: from unittest.mock import MagicMock [as 别名]
# 或者: from unittest.mock.MagicMock import json [as 别名]
def test_dynamips_idlepc_proposals(http_controller, tmpdir, project, compute, node):
response = MagicMock()
response.json = ["0x60606f54", "0x33805a22"]
compute.get = AsyncioMagicMock(return_value=response)
response = http_controller.get("/projects/{}/nodes/{}/dynamips/idlepc_proposals".format(project.id, node.id), example=True)
assert response.status == 200
assert response.json == ["0x60606f54", "0x33805a22"]
示例9: test_dynamips_idle_pc
# 需要导入模块: from unittest.mock import MagicMock [as 别名]
# 或者: from unittest.mock.MagicMock import json [as 别名]
def test_dynamips_idle_pc(node, async_run, compute):
node._node_type = "dynamips"
response = MagicMock()
response.json = {"idlepc": "0x60606f54"}
compute.get = AsyncioMagicMock(return_value=response)
async_run(node.dynamips_auto_idlepc())
compute.get.assert_called_with("/projects/{}/dynamips/nodes/{}/auto_idlepc".format(node.project.id, node.id), timeout=240)
示例10: test_dynamips_idlepc_proposals
# 需要导入模块: from unittest.mock import MagicMock [as 别名]
# 或者: from unittest.mock.MagicMock import json [as 别名]
def test_dynamips_idlepc_proposals(node, async_run, compute):
node._node_type = "dynamips"
response = MagicMock()
response.json = ["0x60606f54", "0x30ff6f37"]
compute.get = AsyncioMagicMock(return_value=response)
async_run(node.dynamips_idlepc_proposals())
compute.get.assert_called_with("/projects/{}/dynamips/nodes/{}/idlepc_proposals".format(node.project.id, node.id), timeout=240)
示例11: compute2_callback
# 需要导入模块: from unittest.mock import MagicMock [as 别名]
# 或者: from unittest.mock.MagicMock import json [as 别名]
async def compute2_callback(path, data={}, **kwargs):
"""
Fake server
"""
if "/ports/udp" in path:
response = MagicMock()
response.json = {"udp_port": 2048}
return response
示例12: test_dynamips_idle_pc
# 需要导入模块: from unittest.mock import MagicMock [as 别名]
# 或者: from unittest.mock.MagicMock import json [as 别名]
def test_dynamips_idle_pc(http_controller, tmpdir, project, compute, node):
response = MagicMock()
response.json = {"idlepc": "0x60606f54"}
compute.get = AsyncioMagicMock(return_value=response)
response = http_controller.get("/projects/{}/nodes/{}/dynamips/auto_idlepc".format(project.id, node.id), example=True)
assert response.status == 200
assert response.json["idlepc"] == "0x60606f54"
示例13: test_get_user_info
# 需要导入模块: from unittest.mock import MagicMock [as 别名]
# 或者: from unittest.mock.MagicMock import json [as 别名]
def test_get_user_info(self, mock_request_get):
api_request = MagicMock()
api_request.status_code = 200
api_request.ok = PropertyMock(return_value=True)
api_request.json = lambda: dict(success=True)
mock_request_get.return_value = api_request
self.assertIsInstance(
self.remote_user_client.get_user_information('123'), dict
)
示例14: resp
# 需要导入模块: from unittest.mock import MagicMock [as 别名]
# 或者: from unittest.mock.MagicMock import json [as 别名]
async def resp(*args, **kwargs):
node.__calls += 1
response = MagicMock()
if node.__calls == 1:
response.status = 409
response.json = {"image": "linux.img", "exception": "ImageMissingError"}
else:
response.status = 200
return response
示例15: test_search_users
# 需要导入模块: from unittest.mock import MagicMock [as 别名]
# 或者: from unittest.mock.MagicMock import json [as 别名]
def test_search_users(self, mock_get, mock_request_post):
request_post = MagicMock()
request_post.status_code = 200
request_post.ok = PropertyMock(return_value=True)
request_post.json = lambda: dict(success=True)
mock_request_post.return_value = request_post
self.assertIsInstance(
self.remote_user_client.search_users('foo'), dict
)