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


Python common.mock_coro函数代码示例

本文整理汇总了Python中tests.common.mock_coro函数的典型用法代码示例。如果您正苦于以下问题:Python mock_coro函数的具体用法?Python mock_coro怎么用?Python mock_coro使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。


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

示例1: test_controller_no_mac

async def test_controller_no_mac(hass):
    """Test that configured options for a host are loaded via config entry."""
    entry = MockConfigEntry(domain=unifi.DOMAIN, data={
        'controller': {
            'host': '0.0.0.0',
            'username': 'user',
            'password': 'pass',
            'port': 80,
            'site': 'default',
            'verify_ssl': True
        },
        'poe_control': True
    })
    entry.add_to_hass(hass)
    mock_registry = Mock()
    with patch.object(unifi, 'UniFiController') as mock_controller, \
        patch('homeassistant.helpers.device_registry.async_get_registry',
              return_value=mock_coro(mock_registry)):
        mock_controller.return_value.async_setup.return_value = mock_coro(True)
        mock_controller.return_value.mac = None
        assert await unifi.async_setup_entry(hass, entry) is True

    assert len(mock_controller.mock_calls) == 2

    assert len(mock_registry.mock_calls) == 0
开发者ID:ManHammer,项目名称:home-assistant,代码行数:25,代码来源:test_init.py

示例2: test_flow_works

async def test_flow_works(hass, aioclient_mock):
    """Test config flow ."""
    aioclient_mock.get(hue.API_NUPNP, json=[
        {'internalipaddress': '1.2.3.4', 'id': 'bla'}
    ])

    flow = hue.HueFlowHandler()
    flow.hass = hass
    await flow.async_step_init()

    with patch('aiohue.Bridge') as mock_bridge:
        def mock_constructor(host, websession):
            mock_bridge.host = host
            return mock_bridge

        mock_bridge.side_effect = mock_constructor
        mock_bridge.username = 'username-abc'
        mock_bridge.config.name = 'Mock Bridge'
        mock_bridge.config.bridgeid = 'bridge-id-1234'
        mock_bridge.create_user.return_value = mock_coro()
        mock_bridge.initialize.return_value = mock_coro()

        result = await flow.async_step_link(user_input={})

    assert mock_bridge.host == '1.2.3.4'
    assert len(mock_bridge.create_user.mock_calls) == 1
    assert len(mock_bridge.initialize.mock_calls) == 1

    assert result['type'] == 'create_entry'
    assert result['title'] == 'Mock Bridge'
    assert result['data'] == {
        'host': '1.2.3.4',
        'bridge_id': 'bridge-id-1234',
        'username': 'username-abc'
    }
开发者ID:simpss,项目名称:home-assistant,代码行数:35,代码来源:test_hue.py

示例3: test_unload_entry

async def test_unload_entry(hass):
    """Test being able to unload an entry."""
    entry = MockConfigEntry(domain=unifi.DOMAIN, data={
        'controller': {
            'host': '0.0.0.0',
            'username': 'user',
            'password': 'pass',
            'port': 80,
            'site': 'default',
            'verify_ssl': True
        },
        'poe_control': True
    })
    entry.add_to_hass(hass)

    with patch.object(unifi, 'UniFiController') as mock_controller, \
        patch('homeassistant.helpers.device_registry.async_get_registry',
              return_value=mock_coro(Mock())):
        mock_controller.return_value.async_setup.return_value = mock_coro(True)
        mock_controller.return_value.mac = '00:11:22:33:44:55'
        assert await unifi.async_setup_entry(hass, entry) is True

    assert len(mock_controller.return_value.mock_calls) == 1

    mock_controller.return_value.async_reset.return_value = mock_coro(True)
    assert await unifi.async_unload_entry(hass, entry)
    assert len(mock_controller.return_value.async_reset.mock_calls) == 1
    assert hass.data[unifi.DOMAIN] == {}
开发者ID:ManHammer,项目名称:home-assistant,代码行数:28,代码来源:test_init.py

示例4: test_user_permissions_low

async def test_user_permissions_low(hass, aioclient_mock):
    """Test config flow."""
    flow = unifi.UnifiFlowHandler()
    flow.hass = hass

    with patch('aiounifi.Controller') as mock_controller:
        def mock_constructor(host, username, password, port, site, websession):
            """Fake the controller constructor."""
            mock_controller.host = host
            mock_controller.username = username
            mock_controller.password = password
            mock_controller.port = port
            mock_controller.site = site
            return mock_controller

        mock_controller.side_effect = mock_constructor
        mock_controller.login.return_value = mock_coro()
        mock_controller.sites.return_value = mock_coro({
            'site1': {'name': 'default', 'role': 'viewer', 'desc': 'site name'}
        })

        await flow.async_step_user(user_input={
            unifi.CONF_HOST: '1.2.3.4',
            unifi.CONF_USERNAME: 'username',
            unifi.CONF_PASSWORD: 'password',
            unifi.CONF_PORT: 1234,
            unifi.CONF_VERIFY_SSL: True
        })

        result = await flow.async_step_site(user_input={})

    assert result['type'] == 'abort'
开发者ID:ManHammer,项目名称:home-assistant,代码行数:32,代码来源:test_init.py

示例5: test_flow_entry_created_from_user_input

async def test_flow_entry_created_from_user_input() -> None:
    """Test that create data from user input.

    Test when the form should show when no configurations exists
    """
    hass = Mock()
    flow = config_flow.SmhiFlowHandler()
    flow.hass = hass

    test_data = {'name': 'home', CONF_LONGITUDE: '0', CONF_LATITUDE: '0'}

    # Test that entry created when user_input name not exists
    with \
        patch.object(flow, '_show_config_form',
                     return_value=mock_coro()) as config_form, \
        patch.object(flow, '_name_in_configuration_exists',
                     return_value=False), \
        patch.object(flow, '_homeassistant_location_exists',
                     return_value=mock_coro(False)), \
        patch.object(config_flow, 'smhi_locations',
                     return_value={
                         'test': 'something', 'name_exist': 'config'
                         }), \
        patch.object(flow, '_check_location',
                     return_value=mock_coro(True)):

        result = await flow.async_step_user(user_input=test_data)

        assert result['type'] == 'create_entry'
        assert result['data'] == test_data
        assert not config_form.mock_calls
开发者ID:ManHammer,项目名称:home-assistant,代码行数:31,代码来源:test_config_flow.py

示例6: test_flow_entry_created_user_input_faulty

async def test_flow_entry_created_user_input_faulty() -> None:
    """Test that create data from user input and are faulty.

    Test when the form should show when user puts faulty location
    in the config gui. Then the form should show with error
    """
    hass = Mock()
    flow = config_flow.SmhiFlowHandler()
    flow.hass = hass

    test_data = {'name': 'home', CONF_LONGITUDE: '0', CONF_LATITUDE: '0'}

    # Test that entry created when user_input name not exists
    with \
        patch.object(flow, '_check_location',
                     return_value=mock_coro(True)), \
        patch.object(flow, '_show_config_form',
                     return_value=mock_coro()) as config_form, \
        patch.object(flow, '_name_in_configuration_exists',
                     return_value=False), \
        patch.object(flow, '_homeassistant_location_exists',
                     return_value=mock_coro(False)), \
        patch.object(config_flow, 'smhi_locations',
                     return_value={
                         'test': 'something', 'name_exist': 'config'
                         }), \
        patch.object(flow, '_check_location',
                     return_value=mock_coro(False)):

        await flow.async_step_user(user_input=test_data)

        assert len(config_form.mock_calls) == 1
        assert len(flow._errors) == 1
开发者ID:ManHammer,项目名称:home-assistant,代码行数:33,代码来源:test_config_flow.py

示例7: test_flow_show_form_name_exists

async def test_flow_show_form_name_exists() -> None:
    """Test show form if name already exists.

    Test when the form should show when no configurations exists
    """
    hass = Mock()
    flow = config_flow.SmhiFlowHandler()
    flow.hass = hass
    test_data = {'name': 'home', CONF_LONGITUDE: '0', CONF_LATITUDE: '0'}
    # Test show form when home assistant config exists and
    # home is already configured, then new config is allowed
    with \
        patch.object(flow, '_show_config_form',
                     return_value=mock_coro()) as config_form, \
        patch.object(flow, '_name_in_configuration_exists',
                     return_value=True), \
        patch.object(config_flow, 'smhi_locations',
                     return_value={
                         'test': 'something', 'name_exist': 'config'
                         }), \
        patch.object(flow, '_check_location',
                     return_value=mock_coro(True)):

        await flow.async_step_user(user_input=test_data)

        assert len(config_form.mock_calls) == 1
        assert len(flow._errors) == 1
开发者ID:ManHammer,项目名称:home-assistant,代码行数:27,代码来源:test_config_flow.py

示例8: test_smartapp_update_syncs_subs

async def test_smartapp_update_syncs_subs(
        hass, smartthings_mock, config_entry, location, device_factory):
    """Test update synchronizes subscriptions."""
    # Arrange
    setattr(hass.config_entries, '_entries', [config_entry])
    app = Mock()
    app.app_id = config_entry.data['app_id']
    api = smartthings_mock.return_value
    api.delete_subscriptions = Mock()
    api.delete_subscriptions.return_value = mock_coro()
    api.create_subscription.return_value = mock_coro()
    request = Mock()
    request.installed_app_id = str(uuid4())
    request.auth_token = str(uuid4())
    request.location_id = location.location_id
    devices = [
        device_factory('', [Capability.battery, 'ping']),
        device_factory('', [Capability.switch, Capability.switch_level]),
        device_factory('', [Capability.switch])
    ]
    api.devices = Mock()
    api.devices.return_value = mock_coro(return_value=devices)
    # Act
    await smartapp.smartapp_update(hass, request, None, app)
    # Assert
    assert api.create_subscription.call_count == 3
    assert api.delete_subscriptions.call_count == 1
开发者ID:arsaboo,项目名称:home-assistant,代码行数:27,代码来源:test_smartapp.py

示例9: test_smartapp_install_creates_flow

async def test_smartapp_install_creates_flow(
        hass, smartthings_mock, config_entry, location, device_factory):
    """Test installation creates flow."""
    # Arrange
    setattr(hass.config_entries, '_entries', [config_entry])
    api = smartthings_mock.return_value
    api.create_subscription.return_value = mock_coro()
    app = Mock()
    app.app_id = config_entry.data['app_id']
    request = Mock()
    request.installed_app_id = str(uuid4())
    request.auth_token = str(uuid4())
    request.location_id = location.location_id
    devices = [
        device_factory('', [Capability.battery, 'ping']),
        device_factory('', [Capability.switch, Capability.switch_level]),
        device_factory('', [Capability.switch])
    ]
    api.devices = Mock()
    api.devices.return_value = mock_coro(return_value=devices)
    # Act
    await smartapp.smartapp_install(hass, request, None, app)
    # Assert
    await hass.async_block_till_done()
    entries = hass.config_entries.async_entries('smartthings')
    assert len(entries) == 2
    assert api.create_subscription.call_count == 3
    assert entries[1].data['app_id'] == app.app_id
    assert entries[1].data['installed_app_id'] == request.installed_app_id
    assert entries[1].data['location_id'] == request.location_id
    assert entries[1].data['access_token'] == \
        config_entry.data['access_token']
    assert entries[1].title == location.name
开发者ID:arsaboo,项目名称:home-assistant,代码行数:33,代码来源:test_smartapp.py

示例10: test_configurator_callback

async def test_configurator_callback(hass, mock_request):
    """."""
    hass.data[hue.DOMAIN] = {}
    with patch('aiohue.Bridge.create_user',
               side_effect=aiohue.LinkButtonNotPressed):
        await MockBridge(hass).async_setup()

    assert len(mock_request.mock_calls) == 1

    callback = mock_request.mock_calls[0][1][2]

    mock_init = Mock(return_value=mock_coro())
    mock_create = Mock(return_value=mock_coro())

    with patch('aiohue.Bridge') as mock_bridge, \
            patch('homeassistant.helpers.discovery.async_load_platform',
                  return_value=mock_coro()) as mock_load_platform, \
            patch('homeassistant.components.hue.save_json') as mock_save:
        inst = mock_bridge()
        inst.username = 'mock-user'
        inst.create_user = mock_create
        inst.initialize = mock_init
        await callback(None)

    assert len(mock_create.mock_calls) == 1
    assert len(mock_init.mock_calls) == 1
    assert len(mock_save.mock_calls) == 1
    assert mock_save.mock_calls[0][1][1] == {
        '1.2.3.4': {
            'username': 'mock-user'
        }
    }
    assert len(mock_load_platform.mock_calls) == 1
开发者ID:TheRealLink,项目名称:home-assistant,代码行数:33,代码来源:test_bridge.py

示例11: test_service_refresh_devices

async def test_service_refresh_devices(hass):
    """Test that service can refresh devices."""
    entry = MockConfigEntry(domain=deconz.DOMAIN, data={
        'host': '1.2.3.4', 'port': 80, 'api_key': '1234567890ABCDEF'
    })
    entry.add_to_hass(hass)
    mock_registry = Mock()

    with patch.object(deconz, 'DeconzGateway') as mock_gateway, \
        patch('homeassistant.helpers.device_registry.async_get_registry',
              return_value=mock_coro(mock_registry)):
        mock_gateway.return_value.async_setup.return_value = mock_coro(True)
        assert await deconz.async_setup_entry(hass, entry) is True

    with patch.object(hass.data[deconz.DOMAIN].api, 'async_load_parameters',
                      return_value=mock_coro(True)):
        await hass.services.async_call(
            'deconz', 'device_refresh', service_data={})
        await hass.async_block_till_done()

    with patch.object(hass.data[deconz.DOMAIN].api, 'async_load_parameters',
                      return_value=mock_coro(False)):
        await hass.services.async_call(
            'deconz', 'device_refresh', service_data={})
        await hass.async_block_till_done()
开发者ID:fbradyirl,项目名称:home-assistant,代码行数:25,代码来源:test_init.py

示例12: async_test_device_join

async def async_test_device_join(
        hass, zha_gateway, cluster_id, domain, device_type=None):
    """Test a newly joining device.

    This creates a new fake device and adds it to the network. It is meant to
    simulate pairing a new device to the network so that code pathways that
    only trigger during device joins can be tested.
    """
    from zigpy.zcl.foundation import Status
    from zigpy.zcl.clusters.general import Basic
    # create zigpy device mocking out the zigbee network operations
    with patch(
            'zigpy.zcl.Cluster.configure_reporting',
            return_value=mock_coro([Status.SUCCESS, Status.SUCCESS])):
        with patch(
                'zigpy.zcl.Cluster.bind',
                return_value=mock_coro([Status.SUCCESS, Status.SUCCESS])):
            zigpy_device = await async_init_zigpy_device(
                hass, [cluster_id, Basic.cluster_id], [], device_type,
                zha_gateway,
                ieee="00:0d:6f:00:0a:90:69:f7",
                manufacturer="FakeMan{}".format(cluster_id),
                model="FakeMod{}".format(cluster_id),
                is_new_join=True)
            cluster = zigpy_device.endpoints.get(1).in_clusters[cluster_id]
            entity_id = make_entity_id(
                domain, zigpy_device, cluster, use_suffix=device_type is None)
            assert hass.states.get(entity_id) is not None
开发者ID:arsaboo,项目名称:home-assistant,代码行数:28,代码来源:common.py

示例13: test_remove_entry_handles_callback_error

async def test_remove_entry_handles_callback_error(hass, manager):
    """Test that exceptions in the remove callback are handled."""
    mock_setup_entry = MagicMock(return_value=mock_coro(True))
    mock_unload_entry = MagicMock(return_value=mock_coro(True))
    mock_remove_entry = MagicMock(
        side_effect=lambda *args, **kwargs: mock_coro())
    loader.set_component(hass, 'test', MockModule(
        'test',
        async_setup_entry=mock_setup_entry,
        async_unload_entry=mock_unload_entry,
        async_remove_entry=mock_remove_entry
    ))
    entry = MockConfigEntry(
        domain='test',
        entry_id='test1',
    )
    entry.add_to_manager(manager)
    # Check all config entries exist
    assert [item.entry_id for item in manager.async_entries()] == \
        ['test1']
    # Setup entry
    await entry.async_setup(hass)
    await hass.async_block_till_done()

    # Remove entry
    result = await manager.async_remove('test1')
    await hass.async_block_till_done()
    # Check that unload went well and so no need to restart
    assert result == {
        'require_restart': False
    }
    # Check the remove callback was invoked.
    assert mock_remove_entry.call_count == 1
    # Check that config entry was removed.
    assert [item.entry_id for item in manager.async_entries()] == []
开发者ID:boced66,项目名称:home-assistant,代码行数:35,代码来源:test_config_entries.py

示例14: test_smartapp_sync_subscriptions_handles_exceptions

async def test_smartapp_sync_subscriptions_handles_exceptions(
        hass, smartthings_mock, device_factory, subscription_factory):
    """Test synchronization does nothing when current."""
    api = smartthings_mock.return_value
    api.delete_subscription.side_effect = \
        lambda loc_id, sub_id: mock_coro(exception=Exception)
    api.create_subscription.side_effect = \
        lambda sub: mock_coro(exception=Exception)
    subscriptions = [
        subscription_factory(Capability.battery),
        subscription_factory(Capability.switch),
        subscription_factory(Capability.switch_level)
    ]
    api.subscriptions.return_value = mock_coro(return_value=subscriptions)
    devices = [
        device_factory('', [Capability.thermostat, 'ping']),
        device_factory('', [Capability.switch, Capability.switch_level]),
        device_factory('', [Capability.switch])
    ]

    await smartapp.smartapp_sync_subscriptions(
        hass, str(uuid4()), str(uuid4()), str(uuid4()), devices)

    assert api.subscriptions.call_count == 1
    assert api.delete_subscription.call_count == 1
    assert api.create_subscription.call_count == 1
开发者ID:boced66,项目名称:home-assistant,代码行数:26,代码来源:test_smartapp.py

示例15: app_fixture

def app_fixture(hass, config_file):
    """Fixture for a single app."""
    app = AppEntity(Mock())
    app.apply_data({
        'appName': APP_NAME_PREFIX + str(uuid4()),
        'appId': str(uuid4()),
        'appType': 'WEBHOOK_SMART_APP',
        'classifications': [CLASSIFICATION_AUTOMATION],
        'displayName': 'Home Assistant',
        'description': "Home Assistant at " + hass.config.api.base_url,
        'singleInstance': True,
        'webhookSmartApp': {
            'targetUrl': webhook.async_generate_url(
                hass, hass.data[DOMAIN][CONF_WEBHOOK_ID]),
            'publicKey': ''}
    })
    app.refresh = Mock()
    app.refresh.return_value = mock_coro()
    app.save = Mock()
    app.save.return_value = mock_coro()
    settings = AppSettings(app.app_id)
    settings.settings[SETTINGS_INSTANCE_ID] = config_file[CONF_INSTANCE_ID]
    app.settings = Mock()
    app.settings.return_value = mock_coro(return_value=settings)
    return app
开发者ID:arsaboo,项目名称:home-assistant,代码行数:25,代码来源:conftest.py


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