def test_reading_to_dict_byte_json(raw_value: bytes, expected: Any, const_uuid: str) -> None: """Convert a reading to a dictionary. Here, the reading value is typed as bytes which are not JSON serializable. The method should convert the bytes to a string and then load it as a valid JSON payload. Regression for: https://vaporio.atlassian.net/browse/VIO-1238 """ msg = api.V3Reading( id=const_uuid, timestamp='2019-04-22T13:30:00Z', type="example", deviceType="example", deviceInfo="An Example Device", bytes_value=raw_value, ) actual = reading_to_dict(msg) assert actual == { 'device': const_uuid, 'timestamp': '2019-04-22T13:30:00Z', 'type': 'example', 'device_type': 'example', 'device_info': 'An Example Device', 'value': expected, 'unit': None, 'context': {}, }
def test_reading_to_dict_5_float_not_nan(state_reading, reading_value: str) -> None: """Convert a reading to a dictionary. Here, the string value is castable to a float, and thus will go through the NaN check, but should fail the check since the float values do not resolve to NaN. Regression for: https://vaporio.atlassian.net/browse/VIO-1616 """ msg = api.V3Reading( id='ddd', timestamp='2019-04-22T13:30:00Z', type='color', deviceType='led', deviceInfo='Example LED Device', string_value=reading_value, ) actual = reading_to_dict(msg) assert actual == { 'device': 'ddd', 'timestamp': '2019-04-22T13:30:00Z', 'type': 'color', 'device_type': 'led', 'device_info': 'Example LED Device', 'value': reading_value, 'unit': None, 'context': {}, }
def test_reading_to_dict_4_nan(state_reading): """Handle NaN when converting the gRPC message to JSON, as NaN is not part of the JSON spec. NaN should be converted to None. Regression for: https://vaporio.atlassian.net/browse/VIO-1369 """ msg = api.V3Reading( id='ddd', timestamp='2019-04-22T13:30:00Z', type='temperature', deviceType='temperature', deviceInfo='Example Temperature Device', float64_value=float('nan'), ) actual = reading_to_dict(msg) assert actual == { 'device': 'ddd', 'timestamp': '2019-04-22T13:30:00Z', 'type': 'temperature', 'device_type': 'temperature', 'device_info': 'Example Temperature Device', 'value': None, 'unit': None, 'context': {}, }
def test_reading_to_dict_3(state_reading): actual = reading_to_dict(state_reading) assert actual == { # from state_reading fixture 'device': 'ccc', 'timestamp': '2019-04-22T13:30:00Z', 'type': 'state', 'device_type': 'led', 'value': 'on', 'unit': None, 'context': {}, }
def test_reading_to_dict_2(humidity_reading): actual = reading_to_dict(humidity_reading) assert actual == { # 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': {}, }
def test_reading_to_dict_1(temperature_reading): actual = reading_to_dict(temperature_reading) assert actual == { # from temperature_reading fixture 'device': 'aaa', 'timestamp': '2019-04-22T13:30:00Z', 'type': 'temperature', 'device_type': 'temperature', 'unit': { 'name': 'celsius', 'symbol': 'C', }, 'value': 30, 'context': { 'zone': '1', }, }