当前位置: 首页>>代码示例>>Python>>正文


Python mock.MagicMock方法代码示例

本文整理汇总了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}) 
开发者ID:google,项目名称:macops,代码行数:25,代码来源:airport_test.py

示例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'),
    ] 
开发者ID:frictionlessdata,项目名称:goodtables-py,代码行数:21,代码来源:test_unique_constraint.py

示例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') 
开发者ID:google,项目名称:macops,代码行数:19,代码来源:profiles_test.py

示例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}) 
开发者ID:google,项目名称:macops,代码行数:21,代码来源:profiles_test.py

示例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) 
开发者ID:opentracing,项目名称:opentracing-python,代码行数:25,代码来源:scope_check.py

示例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) 
开发者ID:opentracing,项目名称:opentracing-python,代码行数:19,代码来源:scope_check.py

示例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 
开发者ID:ParadropLabs,项目名称:Paradrop,代码行数:25,代码来源:test_firewall.py

示例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 
开发者ID:ParadropLabs,项目名称:Paradrop,代码行数:25,代码来源:test_firewall.py

示例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 
开发者ID:ParadropLabs,项目名称:Paradrop,代码行数:27,代码来源:test_wireless.py

示例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 
开发者ID:ParadropLabs,项目名称:Paradrop,代码行数:24,代码来源:test_command.py

示例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 
开发者ID:ParadropLabs,项目名称:Paradrop,代码行数:26,代码来源:test_dockerapi.py

示例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 
开发者ID:ParadropLabs,项目名称:Paradrop,代码行数:26,代码来源:test_procmon.py

示例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 
开发者ID:ParadropLabs,项目名称:Paradrop,代码行数:23,代码来源:test_pdinstall.py

示例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" 
开发者ID:ParadropLabs,项目名称:Paradrop,代码行数:19,代码来源:test_audio_api.py

示例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" 
开发者ID:ParadropLabs,项目名称:Paradrop,代码行数:19,代码来源:test_audio_api.py


注:本文中的mock.MagicMock方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。