async def test_read_cached_no_plugins(monkeypatch):
    """Read the plugin cache(s) when there are no plugins configured."""

    assert len(plugin.Plugin.manager.plugins) == 0

    results = [i async for i in read_cached()]
    assert len(results) == 0
async def test_read_cached_command_no_device(monkeypatch, add_plugin):
    """Read a plugin cache for an existing plugin."""

    # monkeypatch the read_cached method so it yields some data
    def _mock_read(*args, **kwargs):
        yield api.DeviceReading(
            rack='rack',
            board='board',
            device='device',
            reading=api.Reading(
                timestamp='2018-10-18T16:43:18+00:00',
                type='temperature',
                int64_value=10,
            )
        )
    monkeypatch.setattr(PluginClient, 'read_cached', _mock_read)

    # monkeypatch get_device_info to raise a DeviceNotFoundError
    def _mock_device(*args, **kwargs):
        raise errors.DeviceNotFoundError('')
    mocked = asynctest.CoroutineMock(synse.cache.get_device_info, side_effect=_mock_device)
    monkeypatch.setattr(synse.cache, 'get_device_info', mocked)

    assert len(plugin.Plugin.manager.plugins) == 1
    results = [i async for i in read_cached()]
    assert len(results) == 0
async def test_read_cached_no_data(monkeypatch, add_plugin):
    """Read the plugin cache(s) when there is no data in the cache to get."""

    # monkeypatch the read_cached method so it yields no readings
    def _mock(*args, **kwargs):
        yield
    monkeypatch.setattr(PluginClient, 'read_cached', _mock)

    assert len(plugin.Plugin.manager.plugins) == 1
    results = [i async for i in read_cached()]
    assert len(results) == 0
async def test_read_cached_grpc_error(monkeypatch, add_plugin):
    """Read the plugin cache(s) but get back a grpc error."""

    # monkeypatch the read_cached method so it fails with a grpc error
    def _mock(*args, **kwargs):
        raise grpc.RpcError()
    monkeypatch.setattr(PluginClient, 'read_cached', _mock)

    assert len(plugin.Plugin.manager.plugins) == 1
    with pytest.raises(errors.FailedReadCachedCommandError):
        _ = [i async for i in read_cached()]
async def test_read_cached_command(monkeypatch, patch_get_device_info, add_plugin):
    """Read a plugin cache for an existing plugin."""

    # monkeypatch the read_cached method so it yields some data
    def _mock(*args, **kwargs):
        readings = [
            api.DeviceReading(
                rack='rack',
                board='board',
                device='device',
                reading=api.Reading(
                    timestamp='2018-10-18T16:43:18+00:00',
                    type='temperature',
                    int64_value=10,
                )
            ),
            api.DeviceReading(
                rack='rack',
                board='board',
                device='device',
                reading=api.Reading(
                    timestamp='2018-10-18T16:43:18+00:00',
                    type='humidity',
                    int64_value=30,
                )
            )
        ]
        for r in readings:
            yield r
    monkeypatch.setattr(PluginClient, 'read_cached', _mock)

    assert len(plugin.Plugin.manager.plugins) == 1
    results = [i async for i in read_cached()]
    assert len(results) == 2
    assert results[0].data['type'] == 'temperature'
    assert results[1].data['type'] == 'humidity'
async def test_read_cached_command_2(monkeypatch, patch_get_device_info, clear_manager):
    """Read the plugin cache for multiple plugins."""

    # Add plugins to the manager (this is done by the constructor)
    plugin.Plugin(
        metadata=api.Metadata(
            name='foo',
            tag='vaporio/foo',
        ),
        address='localhost:5001',
        plugin_client=PluginTCPClient(
            address='localhost:5001',
        ),
    )
    plugin.Plugin(
        metadata=api.Metadata(
            name='bar',
            tag='vaporio/bar',
        ),
        address='localhost:5002',
        plugin_client=PluginTCPClient(
            address='localhost:5002',
        ),
    )

    # monkeypatch the read_cached method so it yields some data
    def _mock(*args, **kwargs):
        readings = [
            api.DeviceReading(
                rack='rack',
                board='board',
                device='device',
                reading=api.Reading(
                    timestamp='2018-10-18T16:43:18+00:00',
                    type='temperature',
                    int64_value=10,
                )
            ),
            api.DeviceReading(
                rack='rack',
                board='board',
                device='device',
                reading=api.Reading(
                    timestamp='2018-10-18T16:43:18+00:00',
                    type='humidity',
                    int64_value=30,
                )
            )
        ]
        for r in readings:
            yield r

    monkeypatch.setattr(PluginClient, 'read_cached', _mock)

    assert len(plugin.Plugin.manager.plugins) == 2
    results = [i async for i in read_cached()]

    # two plugins with two patched readings each
    assert len(results) == 4
    assert results[0].data['type'] == 'temperature'
    assert results[1].data['type'] == 'humidity'
    assert results[2].data['type'] == 'temperature'
    assert results[3].data['type'] == 'humidity'