def test_delegate_parameter_get_and_snapshot_with_none_source():
    """
    Test that a delegate parameter returns None on get and snapshot if
    the source has a value of None and an offset or scale is used.
    And returns a value if the source is remapped to a real parameter.
    """
    none_param = Parameter("None")
    source_param = Parameter('source',
                             get_cmd=None,
                             set_cmd=None,
                             initial_value=2)
    delegate_param = DelegateParameter(name='delegate', source=none_param)
    delegate_param.offset = 4
    assert delegate_param.get() is None
    assert delegate_param.snapshot()['value'] is None

    delegate_param.offset = None
    delegate_param.scale = 2
    assert delegate_param.get() is None
    assert delegate_param.snapshot()['value'] is None

    assert delegate_param.cache._parameter.source.cache is none_param.cache
    delegate_param.source = source_param
    assert delegate_param.get() == 1
    assert delegate_param.snapshot()['value'] == 1
    assert delegate_param.cache._parameter.source.cache is source_param.cache
def test_delegate_parameter_with_changed_source_snapshot_matches_value(
        value, scale, offset):
    delegate_param = DelegateParameter(name="delegate",
                                       source=None,
                                       scale=scale,
                                       offset=offset)
    source_parameter = Parameter(name="source",
                                 get_cmd=None,
                                 set_cmd=None,
                                 initial_value=value)
    _assert_none_source_is_correct(delegate_param)
    delegate_param.source = source_parameter
    calc_value = (value - offset) / scale
    assert delegate_param.cache.get(get_if_invalid=False) == calc_value
    assert delegate_param.source.cache.get(get_if_invalid=False) == value
    snapshot = delegate_param.snapshot()
    # disregard timestamp that might be slightly different
    snapshot["source_parameter"].pop("ts")
    source_snapshot = source_parameter.snapshot()
    source_snapshot.pop("ts")
    assert snapshot["source_parameter"] == source_snapshot
    assert snapshot["value"] == calc_value
    assert delegate_param.get() == calc_value
    # now remove the source again
    delegate_param.source = None
    _assert_none_source_is_correct(delegate_param)
    _assert_delegate_cache_none_source(delegate_param)
def test_snapshot():
    p = Parameter('testparam',
                  set_cmd=None,
                  get_cmd=None,
                  offset=1,
                  scale=2,
                  initial_value=1)
    d = DelegateParameter('test_delegate_parameter',
                          p,
                          offset=3,
                          scale=5,
                          initial_value=2)

    delegate_snapshot = d.snapshot()
    source_snapshot = delegate_snapshot.pop('source_parameter')
    assert source_snapshot == p.snapshot()
    assert delegate_snapshot['value'] == 2
    assert source_snapshot['value'] == 13