Esempio n. 1
0
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': {},
    }
Esempio n. 2
0
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': {},
    }
Esempio n. 3
0
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': {},
    }
Esempio n. 4
0
def state_reading():
    """Fixture to return a new V3Reading for a state device."""

    return api.V3Reading(
        id='ccc',
        timestamp='2019-04-22T13:30:00Z',
        type='state',
        deviceType='led',
        string_value='on',
    )
Esempio n. 5
0
def humidity_reading():
    """Fixture to return a new V3Reading for a humidity device."""

    return api.V3Reading(
        id='bbb',
        timestamp='2019-04-22T13:30:00Z',
        type='humidity',
        deviceType='humidity',
        unit=api.V3OutputUnit(
            name='percent',
            symbol='%',
        ),
        float64_value=42.0,
    )
Esempio n. 6
0
def temperature_reading():
    """Fixture to return a new V3Reading for a temperature device."""

    return api.V3Reading(
        id='aaa',
        timestamp='2019-04-22T13:30:00Z',
        type='temperature',
        deviceType='temperature',
        context={'zone': '1'},
        unit=api.V3OutputUnit(
            name='celsius',
            symbol='C',
        ),
        int64_value=30,
    )
Esempio n. 7
0
     api.V3WriteData(
         action='foo',
         data=b'bar',
     ),
     {
         'action': 'foo',
         'data': b'bar',
         'transaction': '',
     },
 ),
 (
     api.V3Reading(
         id='123',
         string_value='foo',
         type='test',
         unit=api.V3OutputUnit(
             name='test',
             symbol='T',
         ),
     ),
     {
         'id': '123',
         'timestamp': '',
         'type': 'test',
         'device_type': '',
         'device_info': '',
         'string_value': 'foo',
         'unit': {
             'name': 'test',
             'symbol': 'T',
         },