本文整理匯總了Python中mock.MagicMock方法的典型用法代碼示例。如果您正苦於以下問題:Python mock.MagicMock方法的具體用法?Python mock.MagicMock怎麽用?Python mock.MagicMock使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類mock
的用法示例。
在下文中一共展示了mock.MagicMock方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: testScanForNetworksNoSSID
# 需要導入模塊: import mock [as 別名]
# 或者: from mock import MagicMock [as 別名]
def testScanForNetworksNoSSID(self):
mock_network = mock.MagicMock()
mock_network.ssid.return_value = 'GuestSSID'
mock_network.rssiValue.return_value = -78
mock_network2 = mock.MagicMock()
mock_network2.ssid.return_value = 'GuestSSID'
mock_network2.rssiValue.return_value = -62
mock_network3 = mock.MagicMock()
mock_network3.ssid.return_value = 'SSID'
mock_network3.rssiValue.return_value = -25
networks = [mock_network, mock_network2, mock_network3]
self.mock_intf.scanForNetworksWithName_error_.return_value = [
networks, None]
retval = airport.ScanForNetworks(None)
self.mock_intf.scanForNetworksWithName_error_.assert_called_once_with(None,
None)
self.assertEqual(retval, {'GuestSSID': mock_network2,
'SSID': mock_network3})
示例2: test_check_unique_constraint
# 需要導入模塊: import mock [as 別名]
# 或者: from mock import MagicMock [as 別名]
def test_check_unique_constraint(log):
mock_field = mock.MagicMock()
mock_field.descriptor = {'primaryKey': True}
mock_field.constraints['unique'] = True
cells1 = [
goodtables.cells.create_cell('name', 'value', mock_field, row_number=1, column_number=1),
goodtables.cells.create_cell('value', '50', mock_field, row_number=1, column_number=2),
]
cells2 = [
goodtables.cells.create_cell('name', 'value', mock_field, row_number=2, column_number=1),
goodtables.cells.create_cell('value', '100', mock_field, row_number=2, column_number=2),
]
duplicate_row = UniqueConstraint()
errors = duplicate_row.check_row(cells1)
errors += duplicate_row.check_row(cells2)
assert log(errors) == [
(2, 1, 'unique-constraint'),
]
示例3: testAddMachineCertificateInvalidKey
# 需要導入模塊: import mock [as 別名]
# 或者: from mock import MagicMock [as 別名]
def testAddMachineCertificateInvalidKey(self, mock_certificate, mock_pkcs12,
mock_loadcert, mock_loadkey):
mock_certobj = mock.MagicMock()
mock_certobj.subject_cn = 'My Cert Subject'
mock_certobj.osx_fingerprint = '0011223344556677889900'
mock_certificate.return_value = mock_certobj
mock_pkcs12obj = mock.MagicMock()
mock_pkcs12obj.export.side_effect = profiles.crypto.Error
mock_pkcs12.return_value = mock_pkcs12obj
mock_loadcert.return_value = 'certobj'
mock_loadkey.return_value = 'keyobj_from_different_cert'
profile = profiles.NetworkProfile('testuser')
with self.assertRaises(profiles.CertificateError):
profile.AddMachineCertificate('fakecert', 'otherfakekey')
示例4: testAddAnchorCertificateSuccess
# 需要導入模塊: import mock [as 別名]
# 或者: from mock import MagicMock [as 別名]
def testAddAnchorCertificateSuccess(self, mock_certificate, mock_addpayload):
mock_certobj = mock.MagicMock()
mock_certobj.subject_cn = 'My Cert Subject'
mock_certobj.osx_fingerprint = '0011223344556677889900'
mock_certificate.return_value = mock_certobj
profile = profiles.NetworkProfile('testuser')
profile.AddAnchorCertificate('my_cert')
mock_certificate.assert_called_once_with('my_cert')
mock_addpayload.assert_called_once_with(
{profiles.PAYLOADKEYS_IDENTIFIER:
'com.megacorp.networkprofile.0011223344556677889900',
profiles.PAYLOADKEYS_TYPE: 'com.apple.security.pkcs1',
profiles.PAYLOADKEYS_DISPLAYNAME: 'My Cert Subject',
profiles.PAYLOADKEYS_CONTENT: profiles.plistlib.Data('my_cert'),
profiles.PAYLOADKEYS_ENABLED: True,
profiles.PAYLOADKEYS_VERSION: 1,
profiles.PAYLOADKEYS_UUID: mock.ANY})
示例5: test_activate_nested
# 需要導入模塊: import mock [as 別名]
# 或者: from mock import MagicMock [as 別名]
def test_activate_nested(self):
def fn():
# when a Scope is closed, the previous one must be re-activated.
scope_manager = self.scope_manager()
parent_span = mock.MagicMock(spec=Span)
child_span = mock.MagicMock(spec=Span)
with scope_manager.activate(parent_span, True) as parent:
assert parent is not None
assert scope_manager.active is parent
with scope_manager.activate(child_span, True) as child:
assert child is not None
assert scope_manager.active is child
assert scope_manager.active is parent
assert parent_span.finish.call_count == 1
assert child_span.finish.call_count == 1
assert scope_manager.active is None
self.run_test(fn)
示例6: test_activate_finish_on_close_nested
# 需要導入模塊: import mock [as 別名]
# 或者: from mock import MagicMock [as 別名]
def test_activate_finish_on_close_nested(self):
def fn():
# finish_on_close must be correctly handled
scope_manager = self.scope_manager()
parent_span = mock.MagicMock(spec=Span)
child_span = mock.MagicMock(spec=Span)
parent = scope_manager.activate(parent_span, False)
with scope_manager.activate(child_span, True):
pass
parent.close()
assert parent_span.finish.call_count == 0
assert child_span.finish.call_count == 1
assert scope_manager.active is None
self.run_test(fn)
示例7: test_ConfigZone
# 需要導入模塊: import mock [as 別名]
# 或者: from mock import MagicMock [as 別名]
def test_ConfigZone():
wanInterface = MagicMock()
wanInterface.config_ifname = "eth0"
allConfigs = {
("network", "interface", "wan"): wanInterface
}
config = firewall.ConfigZone()
config.name = "wan"
config.network = ["wan"]
config.conntrack = True
config.masq = True
config.manager = MagicMock()
config.manager.forwardingCount = 0
config.manager.conntrackLoaded = False
commands = config.apply(allConfigs)
assert len(commands) >= 17
commands = config.revert(allConfigs)
assert len(commands) >= 16
示例8: test_ConfigForwarding
# 需要導入模塊: import mock [as 別名]
# 或者: from mock import MagicMock [as 別名]
def test_ConfigForwarding():
wanInterface = MagicMock()
lanZone = MagicMock()
wanZone = MagicMock()
wanZone.network = ["wan"]
allConfigs = {
("firewall", "zone", "wan"): wanZone,
("firewall", "zone", "lan"): lanZone,
("network", "interface", "wan"): wanInterface
}
config = firewall.ConfigForwarding()
config.src = "lan"
config.dest = "wan"
commands = config.apply(allConfigs)
assert len(commands) == 1
commands = config.revert(allConfigs)
assert len(commands) == 1
示例9: test_HostapdConfGenerator_get11rOptions
# 需要導入模塊: import mock [as 別名]
# 或者: from mock import MagicMock [as 別名]
def test_HostapdConfGenerator_get11rOptions():
wifiIface = ConfigWifiIface()
wifiIface.r0kh = [
"02:01:02:03:04:05 r0kh-1.example.com 000102030405060708090a0b0c0d0e0f000102030405060708090a0b0c0d0e0f",
"02:01:02:03:04:06 r0kh-2.example.com 00112233445566778899aabbccddeeff00112233445566778899aabbccddeeff"
]
wifiIface.r1kh = [
"00:00:00:00:00:00 00:00:00:00:00:00 00112233445566778899aabbccddeeff"
]
wifiDevice = MagicMock()
interface = MagicMock()
generator = HostapdConfGenerator(wifiIface, wifiDevice, interface)
# Should raise an exception because nasid is not set.
assert_raises(Exception, generator.get11rOptions)
wifiIface.nasid = "ap.example.com"
options = generator.get11rOptions()
# There should be two r0kh entries and one r1kh entry.
assert sum(k[0] == 'r0kh' for k in options) == 2
assert sum(k[0] == 'r1kh' for k in options) == 1
示例10: test_Command_execute
# 需要導入模塊: import mock [as 別名]
# 或者: from mock import MagicMock [as 別名]
def test_Command_execute(Popen, out):
"""
Test the Command.execute method
"""
from paradrop.confd.command import Command
proc = MagicMock()
proc.stdout = ["output"]
proc.stderr = ["error"]
Popen.return_value = proc
command = Command(["callme"])
command.parent = MagicMock()
command.execute()
assert out.verbose.called_once_with("callme: output")
assert out.verbose.called_once_with("callme: error")
assert command.parent.executed.append.called
Popen.side_effect = Exception("Boom!")
command.execute()
assert out.info.called
示例11: test_build_host_config
# 需要導入模塊: import mock [as 別名]
# 或者: from mock import MagicMock [as 別名]
def test_build_host_config(getBridgeGateway, ChuteContainer):
"""
Test that the build_host_config function does it's job.
"""
# We don't want to open an actual Docker client connection to do this unit
# test, so mock out the create_host_config to return whatever is passed to
# it.
client = MagicMock()
client.create_host_config = fake_create_host_config
update = MagicMock()
service = MagicMock()
service.requests = {}
#Check that an empty host_config gives us certain default settings
res = dockerapi.build_host_config(update, service)
assert res['network_mode'] == 'bridge'
#Check that passing things through host_config works
service.requests['port-bindings'] = {80: 9000}
service.requests['privileged'] = True
res = dockerapi.build_host_config(update, service)
assert res['privileged'] is True
示例12: test_ProcessMonitor
# 需要導入模塊: import mock [as 別名]
# 或者: from mock import MagicMock [as 別名]
def test_ProcessMonitor(psutil, glob, os, mock_open):
mon = ProcessMonitor("foo")
# No process or pid file.
psutil.process_iter.return_value = []
glob.iglob.return_value = []
assert mon.check() is False
proc = MagicMock()
proc.pid = 1
proc.name.return_value = "foo"
# Found process but no pid file.
psutil.process_iter.return_value = [proc]
assert mon.check() is False
pidfile = MagicMock(spec=file)
pidfile.read.return_value = "2"
mock_open.return_value = pidfile
glob.iglob.return_value = ["ignored"]
# Found process and stale pid file. It should try to remove the pid file.
assert mon.check() is False
assert os.remove.called
示例13: test_sendCommand
# 需要導入模塊: import mock [as 別名]
# 或者: from mock import MagicMock [as 別名]
def test_sendCommand(socket):
from paradrop.lib.misc.pdinstall import sendCommand
command = "install"
data = {
'sources': ["paradrop_0.1.0_all.snap"]
}
sock = MagicMock()
socket.return_value = sock
assert sendCommand(command, data)
assert sock.connect.called
assert sock.send.called
assert sock.close.called
sock.reset_mock
sock.connect.side_effect = Exception("Boom!")
assert sendCommand(command, data) is False
assert sock.close.called
示例14: test_get_sinks
# 需要導入模塊: import mock [as 別名]
# 或者: from mock import MagicMock [as 別名]
def test_get_sinks(self):
request = MagicMock()
sink = MagicMock()
sink.channel_count = 2
sink.channel_list = ["front-left", "front-right"]
sink.description = "test"
sink.index = 0
sink.name = "test"
sink.volume = MagicMock()
sink.volume.values = [1.0, 1.0]
self.pulse.sink_list.return_value = [sink]
response = self.api.get_sinks(request)
data = json.loads(response)
assert len(data) == 1
assert data[0]['name'] == "test"
示例15: test_get_sources
# 需要導入模塊: import mock [as 別名]
# 或者: from mock import MagicMock [as 別名]
def test_get_sources(self):
request = MagicMock()
source = MagicMock()
source.channel_count = 2
source.channel_list = ["front-left", "front-right"]
source.description = "test"
source.index = 0
source.name = "test"
source.volume = MagicMock()
source.volume.values = [1.0, 1.0]
self.pulse.source_list.return_value = [source]
response = self.api.get_sources(request)
data = json.loads(response)
assert len(data) == 1
assert data[0]['name'] == "test"