async def test_read_cache_inactive_plugin(mocker, simple_plugin, humidity_reading): # Mock test data simple_plugin.active = False mocker.patch.dict('synse_server.plugin.PluginManager.plugins', { '123': simple_plugin, }) def patchreadcache(*args, **kwargs): yield humidity_reading mock_read = mocker.patch( 'synse_grpc.client.PluginClientV3.read_cache', side_effect=patchreadcache, ) # --- Test case ----------------------------- resp = [ r async for r in cmd.read_cache('2019-04-22T13:30:00Z', '2019-04-22T13:35:00Z') ] assert len(resp) == 0 # no readings since plugin not active assert simple_plugin.active is False mock_read.assert_not_called()
async def response_streamer(response): # Due to how streamed responses are handled, an exception here won't # end up surfacing to the user through Synse's custom error handler # and instead appear to create an opaque error relating to an invalid # character in the chunk header. Instead of surfacing that error, we # just log it and move on. try: async for reading in cmd.read_cache(start, end): await response.write(ujson.dumps(reading) + '\n') except Exception: logger.exception('failure when streaming cached readings')
async def test_read_cache_no_plugins(mocker): # Mock test data mocker.patch.dict('synse_server.plugin.PluginManager.plugins', {}) mock_read = mocker.patch('synse_grpc.client.PluginClientV3.read_cache', ) # --- Test case ----------------------------- res = [r async for r in cmd.read_cache()] assert len(res) == 0 mock_read.assert_not_called()
async def test_read_cache_ok(mocker, simple_plugin, humidity_reading): # Mock test data mocker.patch.dict('synse_server.plugin.PluginManager.plugins', { '123': simple_plugin, }) def patchreadcache(*args, **kwargs): data = [ humidity_reading, humidity_reading, humidity_reading, ] for d in data: yield d mock_read = mocker.patch( 'synse_grpc.client.PluginClientV3.read_cache', side_effect=patchreadcache, ) # --- Test case ----------------------------- resp = [ r async for r in cmd.read_cache('2019-04-22T13:30:00Z', '2019-04-22T13:35:00Z') ] assert len(resp) == 3 for reading in resp: assert reading == { # from humidity_reading fixture 'device': 'bbb', 'timestamp': '2019-04-22T13:30:00Z', 'type': 'humidity', 'device_type': 'humidity', 'unit': { 'name': 'percent', 'symbol': '%', }, 'value': 42.0, 'context': {}, } assert simple_plugin.active is True mock_read.assert_called_once() mock_read.assert_called_with(start='2019-04-22T13:30:00Z', end='2019-04-22T13:35:00Z')
async def test_read_cache_fails_read_cache(mocker, simple_plugin): # Mock test data mocker.patch.dict('synse_server.plugin.PluginManager.plugins', { '123': simple_plugin, }) mock_read = mocker.patch( 'synse_grpc.client.PluginClientV3.read_cache', side_effect=ValueError(), ) # --- Test case ----------------------------- with pytest.raises(errors.ServerError): _ = [r async for r in cmd.read_cache()] assert simple_plugin.active is False mock_read.assert_called_once() mock_read.assert_called_with(start=None, end=None)