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


Python MagicMock.loop方法代码示例

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


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

示例1: test_callback_view_no_jwt

# 需要导入模块: from unittest.mock import MagicMock [as 别名]
# 或者: from unittest.mock.MagicMock import loop [as 别名]
    def test_callback_view_no_jwt(self, loop, test_client):
        """Test that the notification callback view works without JWT."""
        hass = MagicMock()

        m = mock_open()
        with patch(
            'homeassistant.util.json.open',
            m, create=True
        ):
            hass.config.path.return_value = 'file.conf'
            service = html5.get_service(hass, {})

            assert service is not None

            # assert hass.called
            assert len(hass.mock_calls) == 3

            view = hass.mock_calls[2][1][0]

            hass.loop = loop
            app = mock_http_component_app(hass)
            view.register(app.router)
            client = yield from test_client(app)
            hass.http.is_banned_ip.return_value = False

            resp = yield from client.post(PUBLISH_URL, data=json.dumps({
                'type': 'push',
                'tag': '3bc28d69-0921-41f1-ac6a-7a627ba0aa72'
            }))

            assert resp.status == 401, resp.response
开发者ID:JiShangShiDai,项目名称:home-assistant,代码行数:33,代码来源:test_html5.py

示例2: test_registering_existing_device_fails_view

# 需要导入模块: from unittest.mock import MagicMock [as 别名]
# 或者: from unittest.mock.MagicMock import loop [as 别名]
    def test_registering_existing_device_fails_view(self, loop, test_client):
        """Test sub. is not updated when registering existing device fails."""
        hass = MagicMock()
        expected = {
            'unnamed device': SUBSCRIPTION_1,
        }

        hass.config.path.return_value = CONFIG_FILE
        html5.get_service(hass, {})
        view = hass.mock_calls[1][1][0]

        hass.loop = loop
        app = mock_http_component_app(hass)
        view.register(app.router)
        client = yield from test_client(app)
        hass.http.is_banned_ip.return_value = False

        yield from client.post(REGISTER_URL,
                               data=json.dumps(SUBSCRIPTION_1))

        hass.async_add_job.side_effect = HomeAssistantError()
        resp = yield from client.post(REGISTER_URL,
                                      data=json.dumps(SUBSCRIPTION_4))

        content = yield from resp.text()
        assert resp.status == 500, content
        assert view.registrations == expected
开发者ID:devanl,项目名称:home-assistant,代码行数:29,代码来源:test_html5.py

示例3: test_registering_new_device_expiration_view

# 需要导入模块: from unittest.mock import MagicMock [as 别名]
# 或者: from unittest.mock.MagicMock import loop [as 别名]
    def test_registering_new_device_expiration_view(self, loop, test_client):
        """Test that the HTML view works."""
        hass = MagicMock()
        expected = {
            'unnamed device': SUBSCRIPTION_4,
        }

        hass.config.path.return_value = CONFIG_FILE
        service = html5.get_service(hass, {})

        assert service is not None

        # assert hass.called
        assert len(hass.mock_calls) == 3

        view = hass.mock_calls[1][1][0]
        assert view.json_path == hass.config.path.return_value
        assert view.registrations == {}

        hass.loop = loop
        app = mock_http_component_app(hass)
        view.register(app.router)
        client = yield from test_client(app)
        hass.http.is_banned_ip.return_value = False
        resp = yield from client.post(REGISTER_URL,
                                      data=json.dumps(SUBSCRIPTION_4))

        content = yield from resp.text()
        assert resp.status == 200, content
        assert view.registrations == expected

        hass.async_add_job.assert_called_with(save_json, CONFIG_FILE, expected)
开发者ID:devanl,项目名称:home-assistant,代码行数:34,代码来源:test_html5.py

示例4: test_callback_view_with_jwt

# 需要导入模块: from unittest.mock import MagicMock [as 别名]
# 或者: from unittest.mock.MagicMock import loop [as 别名]
    def test_callback_view_with_jwt(self, loop, test_client):
        """Test that the notification callback view works with JWT."""
        hass = MagicMock()

        data = {
            'device': SUBSCRIPTION_1,
        }

        m = mock_open(read_data=json.dumps(data))
        with patch(
                'homeassistant.components.notify.html5.open', m, create=True
        ):
            hass.config.path.return_value = 'file.conf'
            with patch('homeassistant.components.notify.html5.os.path.isfile',
                       return_value=True):
                service = html5.get_service(hass, {'gcm_sender_id': '100'})

            assert service is not None

            # assert hass.called
            assert len(hass.mock_calls) == 3

            with patch('pywebpush.WebPusher') as mock_wp:
                service.send_message('Hello', target=['device'],
                                     data={'icon': 'beer.png'})

            assert len(mock_wp.mock_calls) == 3

            # WebPusher constructor
            assert mock_wp.mock_calls[0][1][0] == \
                SUBSCRIPTION_1['subscription']
            # Third mock_call checks the status_code of the response.
            assert mock_wp.mock_calls[2][0] == '().send().status_code.__eq__'

            # Call to send
            push_payload = json.loads(mock_wp.mock_calls[1][1][0])

            assert push_payload['body'] == 'Hello'
            assert push_payload['icon'] == 'beer.png'

            view = hass.mock_calls[2][1][0]
            view.registrations = data

            bearer_token = "Bearer {}".format(push_payload['data']['jwt'])

            hass.loop = loop
            app = mock_http_component_app(hass)
            view.register(app.router)
            client = yield from test_client(app)
            hass.http.is_banned_ip.return_value = False

            resp = yield from client.post(PUBLISH_URL, data=json.dumps({
                'type': 'push',
            }), headers={'Authorization': bearer_token})

            assert resp.status == 200
            body = yield from resp.json()
            assert body == {"event": "push", "status": "ok"}
开发者ID:Khabi,项目名称:home-assistant,代码行数:60,代码来源:test_html5.py

示例5: test_start

# 需要导入模块: from unittest.mock import MagicMock [as 别名]
# 或者: from unittest.mock.MagicMock import loop [as 别名]
    def test_start(self, pygame, command_wait):
        """
        Test processing of the commands
        """
        loop = EventLoop.current()

        manager = MagicMock()
        manager.loop = loop
        commands = PlaybackCommands(manager, self.stage)
        commands.testing = True
        commands.test_wait_durations = []

        # we've mocked out the command_wait method of PlaybackCommands
        # use a side_effect to make it work again, but ignore durations
        def test_command_wait(duration):
            commands.test_wait_durations.append(int(duration))
            commands.next_command()
        command_wait.side_effect = test_command_wait

        # start command processing, this is a coroutine and requires that the loop run
        commands.start()

        # run the loop, stopping it in 0.25 seconds
        loop.add_callback(loop.stop, {'seconds': 0.25})
        loop.start()

        # make sure things worked as expected
        pygame.mixer.Sound.assert_has_calls([
            call(commands.sound_file),
            call().play(-1, fade_ms=5000),
            call().fadeout(5000),
            call().set_volume(0.9),

            call(commands.swap_sound_file),
            call().play(-1, fade_ms=5000),
            call().fadeout(5000)
        ])

        # command_wait was mocked out and we logged the durations
        self.assertEqual(commands.test_wait_durations, [5, 5, 5, 0, 5])

        # finish the swap
        original_sound_file = copy.copy(commands.sound_file)
        self.assertTrue(original_sound_file in commands.sounds)
        self.assertNotEqual(commands.swap_sound_file, None)
        self.assertTrue(commands.swapping)
        self.assertFalse(commands.swapped)

        commands._complete_swap()

        self.assertFalse(original_sound_file in commands.sounds)
        self.assertEqual(commands.swap_sound_file, None)
        self.assertFalse(commands.swapping)
        self.assertTrue(commands.swapped)
开发者ID:borgstrom,项目名称:slumber,代码行数:56,代码来源:test_playback.py

示例6: test_registering_new_device_validation

# 需要导入模块: from unittest.mock import MagicMock [as 别名]
# 或者: from unittest.mock.MagicMock import loop [as 别名]
    def test_registering_new_device_validation(self, loop, test_client):
        """Test various errors when registering a new device."""
        hass = MagicMock()

        m = mock_open()
        with patch(
            'homeassistant.util.json.open',
            m, create=True
        ):
            hass.config.path.return_value = 'file.conf'
            service = html5.get_service(hass, {})

            assert service is not None

            # assert hass.called
            assert len(hass.mock_calls) == 3

            view = hass.mock_calls[1][1][0]

            hass.loop = loop
            app = mock_http_component_app(hass)
            view.register(app.router)
            client = yield from test_client(app)
            hass.http.is_banned_ip.return_value = False

            resp = yield from client.post(REGISTER_URL, data=json.dumps({
                'browser': 'invalid browser',
                'subscription': 'sub info',
            }))
            assert resp.status == 400

            resp = yield from client.post(REGISTER_URL, data=json.dumps({
                'browser': 'chrome',
            }))
            assert resp.status == 400

            with patch('homeassistant.components.notify.html5.save_json',
                       return_value=False):
                # resp = view.post(Request(builder.get_environ()))
                resp = yield from client.post(REGISTER_URL, data=json.dumps({
                    'browser': 'chrome',
                    'subscription': 'sub info',
                }))

            assert resp.status == 400
开发者ID:JiShangShiDai,项目名称:home-assistant,代码行数:47,代码来源:test_html5.py

示例7: test_api_base_url

# 需要导入模块: from unittest.mock import MagicMock [as 别名]
# 或者: from unittest.mock.MagicMock import loop [as 别名]
def test_api_base_url(loop):
    """Test setting api url."""

    hass = MagicMock()
    hass.loop = loop

    assert loop.run_until_complete(
        bootstrap.async_setup_component(hass, 'http', {
            'http': {
                'base_url': 'example.com'
            }
        })
    )

    assert hass.config.api.base_url == 'http://example.com'

    assert loop.run_until_complete(
        bootstrap.async_setup_component(hass, 'http', {
            'http': {
                'server_host': '1.1.1.1'
            }
        })
    )

    assert hass.config.api.base_url == 'http://1.1.1.1:8123'

    assert loop.run_until_complete(
        bootstrap.async_setup_component(hass, 'http', {
            'http': {
                'server_host': '1.1.1.1'
            }
        })
    )

    assert hass.config.api.base_url == 'http://1.1.1.1:8123'

    assert loop.run_until_complete(
        bootstrap.async_setup_component(hass, 'http', {
            'http': {
            }
        })
    )

    assert hass.config.api.base_url == 'http://127.0.0.1:8123'
开发者ID:drmcinnes,项目名称:home-assistant,代码行数:46,代码来源:test_init.py

示例8: test_unregistering_device_view_handles_json_safe_error

# 需要导入模块: from unittest.mock import MagicMock [as 别名]
# 或者: from unittest.mock.MagicMock import loop [as 别名]
    def test_unregistering_device_view_handles_json_safe_error(self, loop,
                                                               test_client):
        """Test that the HTML unregister view handles JSON write errors."""
        hass = MagicMock()

        config = {
            'some device': SUBSCRIPTION_1,
            'other device': SUBSCRIPTION_2,
        }

        m = mock_open(read_data=json.dumps(config))
        with patch(
                'homeassistant.components.notify.html5.open', m, create=True
        ):
            hass.config.path.return_value = 'file.conf'
            with patch('homeassistant.components.notify.html5.os.path.isfile',
                       return_value=True):
                service = html5.get_service(hass, {})

            assert service is not None

            # assert hass.called
            assert len(hass.mock_calls) == 3

            view = hass.mock_calls[1][1][0]
            assert view.json_path == hass.config.path.return_value
            assert view.registrations == config

            hass.loop = loop
            app = mock_http_component_app(hass)
            view.register(app.router)
            client = yield from test_client(app)
            hass.http.is_banned_ip.return_value = False

            with patch('homeassistant.components.notify.html5._save_config',
                       return_value=False):
                resp = yield from client.delete(REGISTER_URL, data=json.dumps({
                    'subscription': SUBSCRIPTION_1['subscription'],
                }))

            assert resp.status == 500, resp.response
            assert view.registrations == config
            handle = m()
            assert handle.write.call_count == 0
开发者ID:Khabi,项目名称:home-assistant,代码行数:46,代码来源:test_html5.py

示例9: test_unregistering_device_view

# 需要导入模块: from unittest.mock import MagicMock [as 别名]
# 或者: from unittest.mock.MagicMock import loop [as 别名]
    def test_unregistering_device_view(self, loop, test_client):
        """Test that the HTML unregister view works."""
        hass = MagicMock()

        config = {
            'some device': SUBSCRIPTION_1,
            'other device': SUBSCRIPTION_2,
        }

        m = mock_open(read_data=json.dumps(config))
        with patch(
            'homeassistant.util.json.open',
            m, create=True
        ):
            hass.config.path.return_value = CONFIG_FILE
            service = html5.get_service(hass, {})

            assert service is not None

            # assert hass.called
            assert len(hass.mock_calls) == 3

            view = hass.mock_calls[1][1][0]
            assert view.json_path == hass.config.path.return_value
            assert view.registrations == config

            hass.loop = loop
            app = mock_http_component_app(hass)
            view.register(app.router)
            client = yield from test_client(app)
            hass.http.is_banned_ip.return_value = False

            resp = yield from client.delete(REGISTER_URL, data=json.dumps({
                'subscription': SUBSCRIPTION_1['subscription'],
            }))

            config.pop('some device')

            assert resp.status == 200, resp.response
            assert view.registrations == config

            hass.async_add_job.assert_called_with(save_json, CONFIG_FILE,
                                                  config)
开发者ID:devanl,项目名称:home-assistant,代码行数:45,代码来源:test_html5.py


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