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


Python MagicMock.get方法代码示例

本文整理汇总了Python中unittest.mock.MagicMock.get方法的典型用法代码示例。如果您正苦于以下问题:Python MagicMock.get方法的具体用法?Python MagicMock.get怎么用?Python MagicMock.get使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在unittest.mock.MagicMock的用法示例。


在下文中一共展示了MagicMock.get方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: _get_async

# 需要导入模块: from unittest.mock import MagicMock [as 别名]
# 或者: from unittest.mock.MagicMock import get [as 别名]
    def _get_async(path):
        def _get_iresult(block):
            assert block
            return _get(path)

        mock = MagicMock()
        mock.get = _get_iresult
        return mock
开发者ID:zalando-incubator,项目名称:bubuku,代码行数:10,代码来源:test_zookeeper.py

示例2: test_run

# 需要导入模块: from unittest.mock import MagicMock [as 别名]
# 或者: from unittest.mock.MagicMock import get [as 别名]
    def test_run(self):
        host = MagicMock()
        port = MagicMock()
        shell_host = MagicMock()
        shell_port = MagicMock()
        config_file = MagicMock()
        log = MagicMock()
        loop = MagicMock()
        app = MagicMock()
        app_factory = MagicMock(return_value=app)
        shell = MagicMock()
        push_handler_factory = MagicMock()
        repo_manager_factory = MagicMock()
        cmd_factory = MagicMock(return_value=shell)
        telnet_shell_factory = MagicMock()
        telnet_server_factory = MagicMock()
        config = MagicMock()
        config_factory = MagicMock(return_value=config)
        config_source = MagicMock()
        config_source_factory = MagicMock(return_value=config_source)
        app_handler = MagicMock()
        hook_server = MagicMock()
        telnet_server = MagicMock()
        app.make_handler = MagicMock(return_value=app_handler)
        loop.create_server = MagicMock(side_effect=[hook_server, telnet_server])
        loop.run_forever = MagicMock()

        with patch.object(server, 'init_logging') as init_logging_mock,\
             patch.object(server.asyncio, 'get_event_loop', MagicMock(return_value=loop)) as get_event_loop_mock:
            server.run(host, port, shell_host, shell_port, config_file, log,
                       application_factory=app_factory,
                       config_factory=config_factory,
                       config_source_factory=config_source_factory,
                       push_handler_factory=push_handler_factory,
                       repo_manager_factory=repo_manager_factory,
                       cmd_factory=cmd_factory,
                       telnet_shell_factory=telnet_shell_factory,
                       telnet_server_factory=telnet_server_factory)

        init_logging_mock.assert_called_once_with(log, config.get('name'))
        get_event_loop_mock.assert_called_once_with()
        app_factory.assert_called_once_with()
        config_source_factory.assert_called_once_with(config_file)
        config_factory.assert_called_once_with(config_source)
        app.router.add_route.assert_called_once_with('POST', '/push', ANY) # TODO: mock lambda
        app.make_handler.assert_called_once_with()
        cmd_factory.assert_called_once_with(config=config,
                                            push_handler_factory=push_handler_factory,
                                            repo_manager_factory=repo_manager_factory,
                                            forward=True)
        loop.create_server.assert_has_calls([call(app_handler, host, port),
                                             call(ANY, shell_host, shell_port)], # TODO: ANY - server
                                            any_order=True)
        loop.run_until_complete.assert_has_calls([call(hook_server),
                                                  call(telnet_server)],
                                                 any_order=True)
        loop.run_forever.assert_called_once_with()
开发者ID:insolite,项目名称:mergeit,代码行数:59,代码来源:test_server.py

示例3: add_device

# 需要导入模块: from unittest.mock import MagicMock [as 别名]
# 或者: from unittest.mock.MagicMock import get [as 别名]
 def add_device(self, devname, size):
     device = MagicMock()
     device.name = devname
     device.sys_name = devname
     device.size = size
     device.get = {
         'ID_FS_TYPE': "none",
         'ID_PART_TABLE_TYPE': "none"
     }.get
     self.devices.append(device)
开发者ID:aszlig,项目名称:nixpart,代码行数:12,代码来源:devtree.py

示例4: test_ReturnsResultsInOutputQueue

# 需要导入模块: from unittest.mock import MagicMock [as 别名]
# 或者: from unittest.mock.MagicMock import get [as 别名]
    def test_ReturnsResultsInOutputQueue(self):
        mock_input_queue = MagicMock()
        mock_output_queue = MagicMock()
        mock_output_queue.empty = MagicMock(side_effect=[False, False, True])
        mock_output_queue.get = MagicMock(side_effect=["a", "b"])
        sut = linkRequester.LinkRequester(
            None, None, mock_input_queue, mock_output_queue)
        expected = ["a", "b"]

        actual = sut.get_results()

        self.assertEqual(expected, actual)
开发者ID:markberryman,项目名称:LinkChecker,代码行数:14,代码来源:test_LinkRequester.py

示例5: test_enqueue_dict

# 需要导入模块: from unittest.mock import MagicMock [as 别名]
# 或者: from unittest.mock.MagicMock import get [as 别名]
    def test_enqueue_dict(self, *mocks):
        get_formatter, get_resource_service, _, _ = mocks
        publish_queue = get_resource_service.return_value
        fake_post = publish_queue.post
        service = enqueue_service.EnqueueService()
        fake_formatter = get_formatter.return_value
        doc_dict = {ITEM_TYPE: CONTENT_TYPE.TEXT, PUBLISHED_IN_PACKAGE: False}
        fake_doc = MagicMock()
        fake_doc.__getitem__ = lambda s, k: doc_dict.get(k, MagicMock())
        fake_doc.get = doc_dict.get
        fake_destination = MagicMock()
        fake_subscriber = MagicMock()
        subs_dict = {'destinations': [fake_destination], 'api_enabled': False}
        fake_subscriber.__getitem__ = lambda s, k: subs_dict.get(k, MagicMock())
        fake_subscriber['destinations'] = [fake_destination]
        subscribers = [fake_subscriber]
        fake_formatter.format.return_value = [{'published_seq_num': 42,
                                               'formatted_item': 'test OK'}]
        service.get_destinations = MagicMock(return_value=fake_subscriber['destinations'])
        service.queue_transmission(fake_doc, subscribers)
        self.assertEqual(len(fake_post.call_args_list), 1)
        self.assertEqual(len(fake_post.call_args_list[0][0][0]), 1)
        doc = fake_post.call_args_list[0][0][0][0]
        self.assertEqual(doc['published_seq_num'], 42)
        self.assertEqual(doc['formatted_item'], 'test OK')

        fake_post = publish_queue.post = MagicMock()
        fake_formatter.format.return_value = [{'this_should_not': 'work',
                                               'bad_key': 'value'}]
        service.queue_transmission(fake_doc, subscribers)
        # post should not have been called here,
        # because the dict is lacking the mandatory keys
        self.assertFalse(fake_post.called)

        fake_post = publish_queue.post = MagicMock()
        fake_formatter.format.return_value = [(42, 'test tuple OK')]
        service.queue_transmission(fake_doc, subscribers)
        self.assertEqual(len(fake_post.call_args_list), 1)
        self.assertEqual(len(fake_post.call_args_list[0][0][0]), 1)
        doc = fake_post.call_args_list[0][0][0][0]
        self.assertEqual(doc['published_seq_num'], 42)
        self.assertEqual(doc['formatted_item'], 'test tuple OK')

        fake_post = publish_queue.post = MagicMock()
        fake_formatter.format.return_value = [(1, "2", 3)]
        service.queue_transmission(fake_doc, subscribers)
        # post should not have been called here,
        # because the tuple should be in (published_seq_num, formatted_item) format
        self.assertFalse(fake_post.called)
开发者ID:jerome-poisson,项目名称:superdesk-core,代码行数:51,代码来源:publish_content_tests.py

示例6: test_rebalance_invoked_on_broker_list_change

# 需要导入模块: from unittest.mock import MagicMock [as 别名]
# 或者: from unittest.mock.MagicMock import get [as 别名]
    def test_rebalance_invoked_on_broker_list_change(self):
        zk = MagicMock()

        zk.get = MagicMock(side_effect=NoNodeError)

        check = RebalanceOnBrokerListCheck(zk, MagicMock())
        zk.get_broker_ids.return_value = ['1', '2', '3']

        assert check.check() is not None
        assert check.check() is None
        zk.get_broker_ids.return_value = ['1', '2', '3']
        assert check.check() is None
        zk.get_broker_ids.return_value = ['1', '2', '4']
        assert check.check() is not None
        assert check.check() is None
开发者ID:zalando-incubator,项目名称:bubuku,代码行数:17,代码来源:test_rebalance.py

示例7: test_get

# 需要导入模块: from unittest.mock import MagicMock [as 别名]
# 或者: from unittest.mock.MagicMock import get [as 别名]
def test_get():
    output_device = NetworkedPrinterOutputDevice(device_id="test", address="127.0.0.1", properties={})
    mocked_network_manager = MagicMock()
    output_device._manager = mocked_network_manager

    # Create a fake reply (we cant use a QReply, since those are abstract C++)
    reply = MagicMock()
    reply.operation = MagicMock(return_value=QNetworkAccessManager.PostOperation)
    reply.url = MagicMock(return_value=QUrl("127.0.0.1"))
    mocked_network_manager.get = MagicMock(return_value=reply)

    mocked_callback_handler = MagicMock()
    output_device.get("whatever", on_finished=mocked_callback_handler.onFinished)

    # So we now fake that the request was sucesful.
    output_device._handleOnFinished(reply)

    # We expect to get a callback regarding this.
    mocked_callback_handler.onFinished.assert_called_once_with(reply)
开发者ID:Ultimaker,项目名称:Cura,代码行数:21,代码来源:TestNetworkedPrinterOutputDevice.py

示例8: test_is_broker_registered

# 需要导入模块: from unittest.mock import MagicMock [as 别名]
# 或者: from unittest.mock.MagicMock import get [as 别名]
def test_is_broker_registered():
    def _get(path):
        if path == '/brokers/ids/123':
            return '123', object()
        elif path == '/brokers/ids/321':
            return None, None
        else:
            raise NoNodeError()

    exhibitor_mock = MagicMock()
    exhibitor_mock.get = _get
    buku = BukuExhibitor(exhibitor_mock)

    assert buku.is_broker_registered('123')
    assert buku.is_broker_registered(123)
    assert not buku.is_broker_registered('321')
    assert not buku.is_broker_registered(321)
    assert not buku.is_broker_registered(333)
    assert not buku.is_broker_registered('333')
开发者ID:zalando-incubator,项目名称:bubuku,代码行数:21,代码来源:test_zookeeper.py

示例9: object

# 需要导入模块: from unittest.mock import MagicMock [as 别名]
# 或者: from unittest.mock.MagicMock import get [as 别名]
            return json.dumps({'partitions': {0: [1, 2, 3], 1: [3, 2, 1]}}).encode('utf-8'), object()
        elif path == '/brokers/topics/t02':
            return json.dumps({'partitions': {0: [4, 5, 6], 1: [5, 1, 2]}}).encode('utf-8'), object()
        else:
            raise NotImplementedError()

    def _get_async(path):
        def _get_iresult(block):
            assert block
            return _get(path)

        mock = MagicMock()
        mock.get = _get_iresult
        return mock

    exhibitor_mock.get = _get
    exhibitor_mock.get_async = _get_async
    exhibitor_mock.get_children = _get_children

    buku_ex = BukuExhibitor(exhibitor_mock, async)

    expected_result = [
        ('t01', 0, [1, 2, 3]),
        ('t01', 1, [3, 2, 1]),
        ('t02', 0, [4, 5, 6]),
        ('t02', 1, [5, 1, 2]),
    ]
    result = [r for r in buku_ex.load_partition_assignment()]
    assert len(expected_result) == len(result)
    for e in expected_result:
        assert e in result
开发者ID:zalando-incubator,项目名称:bubuku,代码行数:33,代码来源:test_zookeeper.py


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